const UNSAFE_PASSWORD_SYMBOL_PATTERN = /[\s"'`\\]/
const REQUIRED_UPPERCASE_PATTERN = /[A-Z]/
const REQUIRED_LOWERCASE_PATTERN = /[a-z]/
const REQUIRED_NUMBER_PATTERN = /\d/
const REQUIRED_SYMBOL_PATTERN = /[!@#$%^&*()_\-+=[\]{}|;:,.<>/?~]/

export const PASSWORD_POLICY_HINT =
  '8+ chars; upper/lower, number, symbol. Avoid spaces, quotes, backticks, backslashes.'

export const PASSWORD_POLICY_PLACEHOLDER = '8+ chars; upper/lower, number, symbol'

export function validateStrongPassword(password: string) {
  const value = String(password || '')

  if (value.length < 8) {
    return 'Use at least 8 characters.'
  }
  if (!REQUIRED_UPPERCASE_PATTERN.test(value) || !REQUIRED_LOWERCASE_PATTERN.test(value)) {
    return 'Use uppercase and lowercase letters.'
  }
  if (!REQUIRED_NUMBER_PATTERN.test(value)) {
    return 'Add at least one number.'
  }
  if (!REQUIRED_SYMBOL_PATTERN.test(value)) {
    return 'Add at least one symbol.'
  }
  if (UNSAFE_PASSWORD_SYMBOL_PATTERN.test(value)) {
    return 'Avoid spaces, quotes, backticks, and backslashes.'
  }

  return undefined
}
