import type { FormItemRule } from 'element-plus';
import { password_RegEx, tel_RegEx } from '@/utils/verify';

/**
 * 得到密码验证规则
 */
export function getPasswordRules() {
  const passwordRules = (text: '原密码' | '密码' | '新密码' | '确认密码'): FormItemRule[] => {
    //  const pssdErrText = `${text}至少包含字母、数字、特殊字符中的两种，且不能出现4个大小连续或相同的数字`;
    return [
      { required: true, type: 'string', message: `请输入${text}`, trigger: ['blur', 'change'] },
      // { pattern: password_RegEx, type: 'string', message: pssdErrText, trigger: ['blur', 'change'] },
    ];
  };
  return { passwordRules };
}

/**
 * 得到密码验证规则
 */
export function getNewPasswordRules() {
  const newPasswordMin = 8;
  const newPasswordMax = 24;

  const newPasswordRules = (text: '原密码' | '密码' | '新密码' | '确认密码'): FormItemRule[] => {
    const pssdErrText = `${text}至少包含字母、数字、特殊字符中的两种，且不能出现4个大小连续或相同的数字`;
    return [
      { required: true, type: 'string', message: `请输入${text}`, trigger: ['blur', 'change'] },
      {
        min: newPasswordMin,
        max: newPasswordMax,
        type: 'string',
        message: `${text}长度在 ${newPasswordMin} 到 ${newPasswordMax} 之间`,
        trigger: ['blur', 'change'],
      },
      { pattern: password_RegEx, type: 'string', message: pssdErrText, trigger: ['blur', 'change'] },
    ];
  };
  return { newPasswordMin, newPasswordMax, newPasswordRules };
}

/** 得到电话验证规则 */
export function getPhoneRules() {
  const phoneRules: FormItemRule[] = [
    { required: true, type: 'string', message: '请输入手机号', trigger: ['blur', 'change'] },
    { pattern: tel_RegEx, type: 'string', message: '手机号不合法', trigger: ['blur', 'change'] },
  ];
  return { phoneRules };
}

/** 得到账号验证规则 */
export function getUsernameRules() {
  const usernameMin = 4;
  const usernameMax = 24;
  const usernameRules: FormItemRule[] = [
    { required: true, type: 'string', message: '请输入账号', trigger: ['blur', 'change'] },
    { min: usernameMin, max: usernameMax, type: 'string', message: `账号长度在 ${usernameMin} 到 ${usernameMax} 之间`, trigger: ['blur', 'change'] },
    { pattern: /^[a-z0-9A-Z]+$/, type: 'string', message: '账号只能由字母或数字组成', trigger: ['blur', 'change'] },
  ];
  return { usernameRules, usernameMin, usernameMax };
}

/** 得到验证码验证规则 */
export function getSmsCodeRules() {
  const smsCodeLen = 6;
  const smsCodeRules: FormItemRule[] = [
    { required: true, type: 'string', message: '请输入验证码', trigger: ['blur', 'change'] },
    { len: smsCodeLen, type: 'string', message: `验证码长度只能是${smsCodeLen}位`, trigger: ['blur', 'change'] },
    { pattern: /^[0-9]+$/, type: 'string', message: '验证码应全部为数字', trigger: ['blur', 'change'] },
  ];
  return { smsCodeLen, smsCodeRules };
}
