import { FormData, StepValidation } from './types'; import { EMAIL_REGEX, PHONE_REGEX } from './constants'; import { getAppContentLocation } from '../../utils'; export const trackEnquiryEvent = ( name: string, method: string, errorMessage?: string ): void => { if (typeof window === 'undefined' || !window.utag) { return; } const payload: Record = { event_navigation_action: 'navigation', event_navigation_name: name, event_navigation_browsing_method: method, app_content_location: getAppContentLocation('nuk_preferred_edition') }; if (errorMessage) { payload.article_parent_name = errorMessage; } window.utag.link(payload); }; export const formatCurrency = (value: number) => `£${value.toLocaleString('en-GB')}`; type FieldRule = { field: keyof FormData; message: string; pattern?: RegExp; patternMessage?: string; }; const STEP_RULES: Record = { 1: [ { field: 'firstName', message: 'Please enter your first name' }, { field: 'lastName', message: 'Please enter your last name' }, { field: 'telephone', message: 'Please enter your telephone number', pattern: PHONE_REGEX, patternMessage: 'Please enter a valid telephone number' }, { field: 'email', message: 'Please enter your email address', pattern: EMAIL_REGEX, patternMessage: 'Please enter a valid email address' } ], 2: [ { field: 'departurePort', message: 'Please enter your departure' }, { field: 'destination', message: 'Please enter your destination' }, { field: 'month', message: 'Please select the month' }, { field: 'year', message: 'Please select the year' }, { field: 'duration', message: 'Please select the duration' }, { field: 'adults', message: 'Please select the number of adults' }, { field: 'children', message: 'Please select the number of children' } ] }; export const validateStep = ( step: number, formData: FormData ): StepValidation => { const stepErrors: Record = {}; const rules = STEP_RULES[step] || []; for (const rule of rules) { const value = String(formData[rule.field]).trim(); if (!value) { stepErrors[rule.field] = rule.message; } else if (rule.pattern && !rule.pattern.test(value)) { stepErrors[rule.field] = rule.patternMessage!; } } return { isValid: Object.keys(stepErrors).length === 0, errors: stepErrors }; };