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

export interface SignupRequest {
  registerInviteCode: string;
  username: string,
  password: string,
  confirmPassword: string,
  code:string
  company:string
  referCase?:string
  mobile?:string
  from_type?:number
}

export interface updatePassRequest {
  oldPassword: string,
  newPassword: string,
}

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

export interface ForgetPasswordReq {
  username: 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()
}

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" }
  ],

  confirmPassword: [
    { required: true, message: 'Confirm password is required', trigger: 'blur' },
    {
      validator: function (rule: any, value: any, callback: any) {
        if (!value)
          return callback(new Error("Confirm password is required."))
        return callback()
      }
    }
  ],

  oldPassword: [
    { required: true, message: 'Old Password is required', trigger: 'blur' },
    { validator: passwordValidator, trigger: "blur" }
  ],

  newPassword: [
    { required: true, message: 'New Password is required', trigger: 'blur' },
    { validator: passwordValidator, trigger: "blur" }
  ],

  confirmNewPassword: [
    { required: true, message: 'Confirm new password is required', trigger: 'blur' },
    {
      validator: function (rule: any, value: any, callback: any) {
        if (!value)
          return callback(new Error("Confirm password is required."))
        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
  })
}
