import type { FormRules } from "element-plus";
import commonValidators from "~/utils/commonValidators";

export interface LoginRequest {
  mobile: string,
  password?: string,
  code?:string
}

export interface CompleteRegistrationRequest {
  session: string,
  email: string,
  token: string
}

export interface ForgetPasswordReq {
  email: string
}

const passwordValidator = function (rule: any, value: any, callback: any) {
  if (!value)
    return callback(new Error("Password is required."))
  if (value.length < 8 || value.length > 16)
    return callback(new Error("Password length should between 8 and 16"))

  // todo valid charactor check
  return callback()
}

export const rules = {
  // email: [
  //   { required: true, message: 'Email is required', trigger: 'blur' },
  //   { validator: commonValidators.email, trigger: "blur" }
  // ],
  // password: [
  //   { required: true, message: 'Password is required', trigger: 'blur' },
  //   { validator: passwordValidator, trigger: "blur" }
  // ],
  username: [
    { required: true, message: "手机号码 必填", trigger: "blur" },
  ],
  password: [
    { required: true, message: "密码 必填", trigger: "blur" },
  ],
  code: [
    { required: true, message: "验证码 必填", trigger: "blur" },
  ],
  confirmPassword: [
    { required: true, message: '确认密码 必填', trigger: 'blur' },
    {
      validator: function (rule: any, value: any, callback: any) {
        if (!value)
          return callback(new Error("确认密码 必填"))
        return callback()
      }
    }
  ],

  oldPassword: [
    { required: true, message: '旧密码 必填', trigger: 'blur' },
    { validator: passwordValidator, trigger: "blur" }
  ],

  newPassword: [
    { required: true, message: '新密码 必填', trigger: 'blur' },
    { validator: passwordValidator, trigger: "blur" }
  ],

  confirmNewPassword: [
    { required: true, message: '确认新密码 必填', trigger: 'blur' },
    {
      validator: function (rule: any, value: any, callback: any) {
        if (!value)
          return callback(new Error("确认新密码 必填"))
        return callback()
      }
    }
  ],

  token: [
    { required: true, message: 'Token is required', trigger: 'blur' }
  ]
}

export const registerRules = reactive<FormRules>(rules)
export const extendRegisterRules = function (moreRules: object) {
  return reactive<FormRules>({
    ...rules,
    ...moreRules
  })
}
