import { __, sprintf } from '@wordpress/i18n'; export interface FormError { type: string; message?: string; [key: string]: any; } class FormService { getErrorMessage(formError: FormError | undefined): string { if (!formError) { return ''; } // If a custom message is provided, use it if (formError.message) { return formError.message; } // Otherwise, use default messages based on error type switch (formError.type) { case 'required': return __('Field is required', 'timetailor-salon-booking'); case 'maxLength': return formError.maxLength ? sprintf(__('Max %s characters', 'timetailor-salon-booking'), formError.maxLength) : __('Maximum length exceeded', 'timetailor-salon-booking'); case 'minLength': return formError.minLength ? sprintf(__('Min %s characters', 'timetailor-salon-booking'), formError.minLength) : __('Minimum length not met', 'timetailor-salon-booking'); case 'pattern': return __('Invalid format', 'timetailor-salon-booking'); case 'validate': return formError.message || __('Validation failed', 'timetailor-salon-booking'); case 'min': return formError.min ? sprintf(__('Min %s', 'timetailor-salon-booking'), formError.min) : __('Value too small', 'timetailor-salon-booking'); case 'max': return formError.max ? sprintf(__('Max %s', 'timetailor-salon-booking'), formError.max) : __('Value too large', 'timetailor-salon-booking'); case 'email': return __('Must be a valid email address', 'timetailor-salon-booking'); case 'url': return __('Must be a valid URL', 'timetailor-salon-booking'); case 'number': return __('Must be a valid number', 'timetailor-salon-booking'); case 'date': return __('Must be a valid date', 'timetailor-salon-booking'); case 'phone': return __('Must be a valid phone number', 'timetailor-salon-booking'); default: return __('Invalid value', 'timetailor-salon-booking'); } } } export const formService = new FormService();