import { REGEX } from "./commonVariables";

/**
 * Evaluates the strength of a given password based on specific criteria.
 * Strength Levels:
 * - **1** → Weak (Minimum criteria met)
 * - **2** → Medium (Moderate security with numbers or special characters)
 * - **3** → Ultimate (Strong password with all security factors)
 * @param {string} password - The password to evaluate.
 * @returns {number} - The password strength score (1 = Weak, 2 = Medium, 3 = Ultimate).
 * Criteria:
 * - At least 8 characters in length.
 * - Contains alphabetical characters.
 * - Contains at least one numeric character.
 * - Contains at least one special character.
 * Note: If the password meets none or minimal criteria, it defaults to Weak (score = 1).
 */
export const getPasswordStrength = (password: string): number => {
  let strength = 0;

  if (password === undefined || password.length === 0) {
    return strength;
  }
  const hasLength = password.length >= 8;
  const hasUppercase = REGEX.UPPERCASE.test(password);
  const hasLowercase = REGEX.LOWERCASE.test(password);
  const hasNumber = REGEX.NUMBERS.test(password);
  const hasSpecialChar = REGEX.PASSWORD_SPECIAL_CHARACTERS.test(password);

  /* Ensure password isn't just numbers */
  if (hasLength && hasLowercase && hasNumber) strength++; // At least 8 chars & has letters
  if (hasLength && hasNumber && hasLowercase && (hasUppercase || hasSpecialChar)) strength++; // Contains a number or has letters
  if (hasLength && hasSpecialChar && hasUppercase && hasNumber && hasLowercase) strength++; // Contains a special character

  /* Determine strength level */
  switch (strength) {
    case 1:
      return 1; // Weak
    case 2:
      return 2; // Medium
    case 3:
      return 3; // Ultimate
    default:
      return 1; // Default to Weak
  }
};
