import { computed, type Ref } from 'vue' import { useValidation } from '@/composables/unifyValidation/useValidation' import type { ValidationRule as SyValidationRule, VuetifyValidationRule } from '@/composables/unifyValidation/useValidation' import { mdiAlertOutline, mdiCheck, mdiAlertCircle } from '@mdi/js' export function useSyTextFieldValidation(params: { modelValue: Ref readonly: Ref disabled: Ref required: Ref isValidateOnBlur: Ref showSuccessMessages: Ref disableErrorHandling: Ref useVuetifyValidation: Ref label: Ref focused: Ref customRules: Ref customWarningRules: Ref customSuccessRules: Ref rules: Ref errorMessages: Ref warningMessages: Ref successMessages: Ref hasErrorProp: Ref hasWarningProp: Ref hasSuccessProp: Ref maxErrors: Ref }) { const defaultRules = computed(() => params.required.value ? [{ type: 'required', options: { message: `Le champ ${params.label.value || 'ce champ'} est requis.`, fieldIdentifier: params.label.value, }, }] : [], ) const { validate, errors, warnings, successes, hasError, hasWarning, hasSuccess, clearValidation } = useValidation({ modelValue: params.modelValue, 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: computed(() => [ ...defaultRules.value, ...(params.customRules.value ?? []), ]), customWarningRules: params.customWarningRules as Ref, customSuccessRules: params.customSuccessRules as Ref, errorMessages: params.errorMessages, warningMessages: params.warningMessages, successMessages: params.successMessages, hasErrorProp: params.hasErrorProp, hasWarningProp: params.hasWarningProp, hasSuccessProp: params.hasSuccessProp, maxErrors: params.maxErrors, focused: params.focused, }) const iconColor = computed(() => { if (hasError.value) return 'error' if (hasWarning.value) return 'warning' if (hasSuccess.value) return 'success' return 'rgba(0, 0, 0, 1)' }) // Le bouton clear garde toujours une couleur neutre, quel que soit l'état de validation const clearButtonColorClass = computed(() => 'text-iconBase') const validationIcon = computed(() => { if (hasError.value) return mdiAlertCircle if (hasWarning.value) return mdiAlertOutline if (hasSuccess.value) return mdiCheck return null }) const hasMessages = computed(() => { if (params.disableErrorHandling.value) return false return (params.errorMessages.value?.length ?? 0) > 0 || hasError.value || hasWarning.value || (hasSuccess.value && params.showSuccessMessages.value) }) return { errors, warnings, successes, hasError, hasWarning, hasSuccess, iconColor, clearButtonColorClass, validationIcon, hasMessages, validate, clearValidation, } }