// async-validator rules
// to view at https://github.com/yiminghe/async-validator

const username = [
  {
    required: true,
    pattern: /^[a-zA-Z0-9][a-zA-Z0-9_]{2,19}$/,
    message: "用户名必须以字母或数字开头，长度为3-20位，允许字母数字下划线",
    trigger: "change",
  },
];

const commonCode = ({ prop, min = 3, max = 20 }) => {
  return [
    {
      required: true,
      pattern: new RegExp(`^[a-zA-Z0-9_]{${min},${max}}$`),
      message: `${prop}只能由数字、字母和下划线组成，长度${min}-${max}个字符`,
      trigger: "change",
    },
  ];
};

const email = [
  {
    required: true,
    type: "email",
    message: "邮箱格式不正确",
    trigger: "change",
  },
];

const password = [
  {
    required: true,
    pattern: /^[a-zA-Z0-9_]{6,20}$/,
    message: "密码只能由数字、字母和下划线组成，长度6-20个字符",
    trigger: "change",
  },
];
const strictPassword = [
  {
    required: true,
    validator: (rule, value, callback) => {
      if (!value) {
        return callback(new Error(`请输入密码`));
      }
      // // 禁止使用相同的数字或字符作为密码
      // const reg2 = /^[a-zA-Z0-9].+$/;
      // const vals = new Set(value.split(""));
      // if (reg2.test(value) && vals.size === 1) {
      //   return callback(new Error(`禁止使用相同的数字或字符作为密码`));
      // }
      // // 禁止使用连续升序或降序的数字或字母作为密码
      // const valCharCodes = value.split("").map((item) => item.charCodeAt());
      // let intervals = [];
      // for (let i = 0; i < valCharCodes.length; i++) {
      //   const element = valCharCodes[i];
      //   if (i === 0) continue;
      //   intervals.push(element - valCharCodes[i - 1]);
      // }
      // const interVals = Array.from(new Set(intervals));
      // if (
      //   reg2.test(value) &&
      //   interVals.length === 1 &&
      //   Math.abs(interVals[0]) === 1
      // ) {
      //   return callback(
      //     new Error(`禁止使用连续升序或降序的数字或字母作为密码`)
      //   );
      // }
      // 密码应至少包含数字、大小写字母及特殊宇符中的两种；
      const reg1 =
        /^(?![\d]+$)(?![a-z]+$)(?![A-Z]+$)(?![!@#$%^&*]+$)[\da-zA-Z!@#$%^&*]{8,20}$/g;
      if (!reg1.test(value)) {
        return callback(
          new Error(
            `密码应至少包含数字、大小写字母及特殊宇符(!@#$%^&*)中的两种，长度8-20位`
          )
        );
      }

      return callback();
    },
    trigger: "change",
  },
];

const phone = [
  {
    required: true,
    pattern: /^1\d{10}$/,
    message: "请输入合适的手机号码",
    trigger: "change",
  },
];

const smscode = [
  {
    required: true,
    pattern: /^[a-zA-Z0-9]{4}$/,
    message: "请输入4位短信验证码",
    trigger: "change",
  },
];

const numberValidator = ({ prop, min = 1, max = 100 }) => {
  return [
    {
      required: true,
      validator: (rule, value, callback) => {
        if (!value && value !== 0) {
          return callback(new Error(`请输入${prop}`));
        }

        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (value < min || value > max) {
            callback(new Error(`${prop}的大小只能介于${min}-${max}之间。`));
          } else {
            callback();
          }
        }
      },
      trigger: "change",
    },
  ];
};

export {
  username,
  commonCode,
  email,
  password,
  strictPassword,
  phone,
  smscode,
  numberValidator,
};
