import {useI18n} from './useI18n'
import {FormItemRule} from 'element-plus'

const {t} = useI18n()

interface LengthRange {
    min: number
    max: number
    message?: string
}

export const useValidator = () => {
    const required = (message?: string): FormItemRule => {
        return {
            required: true,
            message: message || t('common.required')
        }
    }

    const lengthRange = (options: LengthRange): FormItemRule => {
        const {min, max, message} = options

        return {
            min,
            max,
            message: message || t('common.lengthRange', {min, max})
        }
    }

    const notSpace = (message?: string): FormItemRule => {
        return {
            validator: (_, val, callback) => {
                if (val?.indexOf(' ') !== -1) {
                    callback(new Error(message || t('common.notSpace')))
                } else {
                    callback()
                }
            }
        }
    }

    const notSpecialCharacters = (message?: string): FormItemRule => {
        return {
            validator: (_, val, callback) => {
                if (/[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/gi.test(val)) {
                    callback(new Error(message || t('common.notSpecialCharacters')))
                } else {
                    callback()
                }
            }
        }
    }

    const phone = (message?: string): FormItemRule => {
        return {
            validator: (_, val, callback) => {
                if (!val) return callback()
                if (!/^1[3456789]\d{9}$/.test(val)) {
                    callback(new Error(message || '请输入正确的手机号码'))
                } else {
                    callback()
                }
            },
            trigger: 'blur'
        }
    }

    // 以字母开头 字母数字下划线结尾
    const field = (message?: string): FormItemRule => {
        return {
            validator: (_, val, callback) => {
                if (!val) return callback()
                if (!/^[a-zA-Z][a-zA-Z0-9_]*$/.test(val)) {
                    callback(new Error(message || '请输入以字母开头，以字母数字下划线结尾的字段'))
                } else {
                    callback()
                }
            }
        }
    }

    const email = (message?: string): FormItemRule => {
        return {
            validator: (_, val, callback) => {
                if (!val) return callback()
                if (!/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(val)) {
                    callback(new Error(message || '请输入正确的邮箱'))
                } else {
                    callback()
                }
            },
            trigger: 'blur'
        }
    }

    const maxlength = (max: number): FormItemRule => {
        return {
            max,
            message: '长度不能超过' + max + '个字符'
        }
    }

    const number = (message?: string): FormItemRule => {
        return {
            validator: (_, val, callback) => {
                if (!val) return callback()
                if (!/^\d+$/.test(val)) {
                    callback(new Error(message || '请输入数字'))
                } else {
                    callback()
                }
            }
        }
    }

    const check = (message?: string): FormItemRule => {
        return {
            validator: (_, val, callback) => {
                if (!val) {
                    callback(new Error(message || t('common.required')))
                } else {
                    callback()
                }
            }
        }
    }

    const idCard = (message?: string): FormItemRule => {
        return {
            validator: (_, val, callback) => {
                if (!val) return callback()
                if (
                    !/^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(
                        val
                    )
                ) {
                    callback(new Error(message || '请输入正确的身份证号码'))
                } else {
                    callback()
                }
            },
            trigger: 'blur'
        }
    }

    return {
        required,
        lengthRange,
        notSpace,
        notSpecialCharacters,
        phone,
        email,
        maxlength,
        check,
        field,
        number,
        idCard
    }
}

