const REG_STRONG_PWD =
  /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?])(?!.*(123|321|abc|cba))[a-zA-Z\d!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]{8,16}$/;

// 常见密码列表（可以根据需要扩展）
const COMMON_PASSWORDS = [
  "password",
  "password123",
  "123456",
  "12345678",
  "qwerty",
  "abc123",
  "Password1",
  "password1",
  "123456789",
  "welcome",
  "admin",
  "letmein",
  "monkey",
  "dragon",
  "master",
  "superman",
  "qwerty123",
  "admin123",
  "root",
  "pass",
  "test",
  "guest",
  "user",
  "000000",
  "111111",
  "666666",
  "888888",
  "999999",
  "iloveyou",
  "welcome123",
  "password!",
  "Passw0rd!",
  "Passw0rd",
  "Password!",
  "passw0rd",
  "P@ssw0rd",
  "P@ssword",
  "Password@123",
];

// 检查连续数字（3个或以上）
const hasConsecutiveNumbers = (password) => {
  for (let i = 0; i <= password.length - 3; i++) {
    const substr = password.substring(i, i + 3);
    if (/^\d{3,}$/.test(substr)) {
      // 检查是否为连续数字
      const digits = substr.split("").map(Number);
      let isConsecutive = true;
      for (let j = 1; j < digits.length; j++) {
        if (digits[j] !== digits[j - 1] + 1) {
          isConsecutive = false;
          break;
        }
      }
      if (isConsecutive) return true;
    }
  }
  return false;
};

// 检查连续字母（3个或以上）
const hasConsecutiveLetters = (password) => {
  const lowerPassword = password.toLowerCase();
  for (let i = 0; i <= lowerPassword.length - 3; i++) {
    const substr = lowerPassword.substring(i, i + 3);
    if (/^[a-z]{3,}$/.test(substr)) {
      // 检查是否为连续字母
      let isConsecutive = true;
      for (let j = 1; j < substr.length; j++) {
        if (substr.charCodeAt(j) !== substr.charCodeAt(j - 1) + 1) {
          isConsecutive = false;
          break;
        }
      }
      if (isConsecutive) return true;
    }
  }
  return false;
};

// 检查重复字符（3个或以上相同字符）
const hasRepeatedChars = (password) => {
  return /(.)\1{2,}/.test(password);
};

// 检查键盘连续按键（如 qwerty, asdf 等）
const hasKeyboardPattern = (password) => {
  const keyboardRows = ["qwertyuiop", "asdfghjkl", "zxcvbnm", "1234567890"];
  const lowerPassword = password.toLowerCase();
  for (const row of keyboardRows) {
    for (let i = 0; i <= row.length - 3; i++) {
      const pattern = row.substring(i, i + 3);
      if (lowerPassword.includes(pattern)) {
        return true;
      }
    }
  }
  return false;
};

/**
 * 密码校验结果对象
 * @typedef {Object} PasswordValidationResult
 * @property {boolean} isValid
 * @property {string[]} errors
 */

/**
 * 增强的密码校验函数
 * @param {string} password
 * @returns {PasswordValidationResult}
 */
export const validateStrongPasswordPhone = (password) => {
  const result = {
    isValid: true,
    errors: [],
  };

  // 1. 基本格式校验
  if (!REG_STRONG_PWD.test(password)) {
    result.isValid = false;
    result.errors.push(
      "密码长度必须为8-16位，且需同时包含大写字母、小写字母、数字和特殊字符"
    );
    return result;
  }

  // 2. 检查是否为常见密码
  if (
    COMMON_PASSWORDS.includes(password) ||
    COMMON_PASSWORDS.includes(password.toLowerCase())
  ) {
    result.isValid = false;
    result.errors.push("不能使用常见密码");
    return result;
  }

  // 3. 检查连续数字
  if (hasConsecutiveNumbers(password)) {
    result.isValid = false;
    result.errors.push("不能包含连续数字");
    return result;
  }

  // 4. 检查连续字母
  if (hasConsecutiveLetters(password)) {
    result.isValid = false;
    result.errors.push("不能包含连续字母");
    return result;
  }

  // 5. 检查重复字符
  if (hasRepeatedChars(password)) {
    result.isValid = false;
    result.errors.push("不能包含3个或以上相同字符");
    return result;
  }

  // 6. 检查键盘模式
  if (hasKeyboardPattern(password)) {
    result.isValid = false;
    result.errors.push("不能包含键盘连续按键模式");
    return result;
  }

  return result;
};
