import { mdiAlertCircle, mdiAlert, mdiCheck } from '@mdi/js' import { computed, ref, type Ref } from 'vue' import type { ValidationRule as VuetifyValidationRule } from 'vuetify' import { locales } from './locales' import type { ValidationRule } from '@/composables/validation/useValidation' import { useValidation } from '@/composables/unifyValidation/useValidation' export function usePasswordField(params: { password: Ref focused: Ref readonly: Ref disabled: Ref label: Ref required: Ref isValidateOnBlur: Ref showSuccessMessages: Ref disableErrorHandling: Ref useVuetifyValidation: Ref rules: Ref customRules: Ref customWarningRules: Ref customSuccessRules: Ref errorMessages: Ref warningMessages: Ref successMessages: Ref hasErrorProp: Ref hasWarningProp: Ref hasSuccessProp: Ref maxErrors: Ref }) { const alertMessage = ref('') const allCustomRules = computed(() => { const rules: ValidationRule[] = [] if (params.required.value) { rules.push({ type: 'required', options: { message: locales.required, fieldIdentifier: params.label.value || 'password', }, }) } return [...rules, ...(params.customRules.value || [])] }) const { errors, warnings, successes, hasError, hasWarning, hasSuccess, validate, clearValidation } = useValidation({ modelValue: params.password, readonly: params.readonly, disabled: params.disabled, required: params.required, isValidateOnBlur: params.isValidateOnBlur, showSuccessMessages: params.showSuccessMessages, disableErrorHandling: params.disableErrorHandling, useVuetifyValidation: params.useVuetifyValidation, label: params.label, rules: params.rules, customRules: allCustomRules, customWarningRules: computed(() => params.customWarningRules.value || []), customSuccessRules: computed(() => params.customSuccessRules.value || []), errorMessages: params.errorMessages, warningMessages: params.warningMessages, successMessages: params.successMessages, hasErrorProp: computed(() => params.hasErrorProp.value ?? false), hasWarningProp: computed(() => params.hasWarningProp.value ?? false), hasSuccessProp: computed(() => params.hasSuccessProp.value ?? false), maxErrors: computed(() => params.maxErrors.value ?? 1), focused: params.focused, }) const validationIcon = computed(() => { if (hasError.value) return mdiAlertCircle if (hasWarning.value) return mdiAlert if (hasSuccess.value) return mdiCheck return '' }) const validationColor = computed(() => { if (hasError.value) return 'error' if (hasWarning.value) return 'onWarningVariant' if (hasSuccess.value) return 'onSuccessVariant' return 'rgb(var(--v-theme-onSurface))' }) return { alertMessage, errors, warnings, successes, hasError, hasWarning, hasSuccess, validationIcon, validationColor, validate, clearValidation, } }