import type { Ref } from 'vue'
import type { FormError } from '@nuxt/ui'
import { settings } from '@/router/settings'
import { i18n } from '@/plugins/02_i18n'

export const useValidation = () => {
    const errors = [] as FormError[]

    function reset() {
        errors.length = 0
    }

    function validateFirstName(first_name_ref: Ref<string>, name: string, message: string) {
        if (!first_name_ref.value || !/^[A-Za-z]{2,}$/.test(first_name_ref.value)) {
            errors.push({ name: name, message: message })
        }
    }

    function validateLastName(last_name_ref: Ref<string>, name: string, message: string) {
        if (!last_name_ref.value || !/^[A-Za-z]{2,}$/.test(last_name_ref.value)) {
            errors.push({ name: name, message: message })
        }
    }

    function validateEmail(email_ref: Ref<string>, name: string, message: string) {
        if (!email_ref.value || !/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/.test(email_ref.value)) {
            errors.push({ name: name, message: message })
        }
    }

    // function validatePasswords(
    //     password_ref: Ref<string>,
    //     password_name: string,
    //     confirm_password_ref: Ref<string>,
    //     confirm_name: string,
    //     message_password: string,
    //     message_confirm_password: string,
    // ) {
    //     const regex = new RegExp(settings.app?.password_regex ?? '^.{8,}$')

    //     if (!password_ref.value) {
    //         errors.push({ name: password_name, message: message_password })
    //     } else if (!regex.test(password_ref.value)) {
    //         errors.push({
    //             name: password_name,
    //             message: 'general.registration_validation_password_requirements'
    //         })
    //     }

    //     if (!confirm_password_ref.value) {
    //         errors.push({ name: confirm_name, message: message_confirm_password })
    //     } else if (password_ref.value !== confirm_password_ref.value) {
    //         errors.push({
    //             name: confirm_name,
    //             message: 'registration_validation_password_not_same'
    //         })
    //     }
    // }

    function validatePasswords(
        password_ref: Ref<string>,
        password_name: string,
        confirm_password_ref: Ref<string>,
        confirm_name: string,
        message_confirm_password: string,
    ) {
        const regexPass = new RegExp(
            '^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@#$%^&*()_+\\-=[\\]{};:\'",.<>/?]).{8,}$'
        )

        if (!password_ref.value) {
            errors.push({ name: password_name, message: i18n.t('validate_error.password_required') })
        } else if (!regexPass.test(password_ref.value)) {
            errors.push({
                name: password_name,
                message: i18n.t('validate_error.registration_validation_password_requirements')
            })
        }

        if (!confirm_password_ref.value) {
            errors.push({ name: confirm_name, message: message_confirm_password })
        } else if (password_ref.value !== confirm_password_ref.value) {
            errors.push({
                name: confirm_name,
                message: i18n.t('validate_error.registration_validation_password_not_same')
            })
        }
    }

    return {
        errors,
        reset,
        validateFirstName,
        validateLastName,
        validateEmail,
        validatePasswords,
    }
}
