1/** 2 * Service selection helpers for the Vue3 booking form. 3 */ 4 5export function selectedService(state) { 6 const fd = (state && state.appointment_step_form_data) || {}; 7 const id = parseInt(fd.selected_service || 0, 10); 8 if (!id || !Array.isArray(state && state.services)) return null; 9 return state.services.find((svc) => parseInt(svc.serviceId, 10) === id) || null; 10} 11 12export function selectedServiceDurationUnit(state) { 13 const fd = (state && state.appointment_step_form_data) || {}; 14 if ( 15 Array.isArray(fd.selected_services) && 16 fd.selected_services.length >= 2 && 17 Object.prototype.hasOwnProperty.call(fd, 'multi_service_duration_unit') 18 ) { 19 return String(fd.multi_service_duration_unit || ''); 20 } 21 const svc = selectedService(state); 22 if (svc && Object.prototype.hasOwnProperty.call(svc, 'serviceDurationUnit')) { 23 return String(svc.serviceDurationUnit || ''); 24 } 25 return String(fd.selected_service_duration_unit || ''); 26} 27 28export function selectedServiceDurationValue(state) { 29 const fd = (state && state.appointment_step_form_data) || {}; 30 if ( 31 Array.isArray(fd.selected_services) && 32 fd.selected_services.length >= 2 && 33 fd.multi_service_duration_unit === 'd' 34 ) { 35 return Math.max(0, parseInt(fd.multi_service_duration_days || 0, 10) || 0); 36 } 37 const svc = selectedService(state); 38 let duration = 0; 39 if (svc && Object.prototype.hasOwnProperty.call(svc, 'serviceDurationVal')) { 40 duration = Math.max(0, parseInt(svc.serviceDurationVal || 0, 10) || 0); 41 } else { 42 duration = Math.max(0, parseInt(fd.selected_service_duration || 0, 10) || 0); 43 } 44 45 if (selectedServiceDurationUnit(state) === 'd') { 46 const hooks = (typeof window !== 'undefined' && window.wp && window.wp.hooks) || null; 47 if (hooks && typeof hooks.applyFilters === 'function') { 48 duration = parseInt(hooks.applyFilters( 49 'bookingpress_form_v3_day_service_duration', 50 duration, 51 { state, service: svc } 52 ), 10) || duration; 53 } 54 } 55 56 return Math.max(0, duration); 57} 58 59export function isSelectedDayService(state) { 60 return selectedServiceDurationUnit(state) === 'd'; 61} 62 63function parseYmdLocal(ymd) { 64 const s = String(ymd || ''); 65 const m = s.match(/^(\d{4})-(\d{2})-(\d{2})$/); 66 if (!m) return null; 67 const d = new Date(parseInt(m[1], 10), parseInt(m[2], 10) - 1, parseInt(m[3], 10)); 68 return Number.isNaN(d.getTime()) ? null : d; 69} 70 71function formatYmdLocal(d) { 72 const y = d.getFullYear(); 73 const mo = String(d.getMonth() + 1).padStart(2, '0'); 74 const day = String(d.getDate()).padStart(2, '0'); 75 return `${y}-${mo}-${day}`; 76} 77 78export function selectedDayServiceDateRange(state) { 79 if (!isSelectedDayService(state)) return []; 80 const fd = (state && state.appointment_step_form_data) || {}; 81 const start = parseYmdLocal(fd.selected_date); 82 if (!start) return []; 83 84 const duration = Math.max(1, parseInt(selectedServiceDurationValue(state) || 1, 10) || 1); 85 const days = []; 86 for (let i = 0; i < duration; i++) { 87 const d = new Date(start.getFullYear(), start.getMonth(), start.getDate()); 88 d.setDate(d.getDate() + i); 89 days.push(formatYmdLocal(d)); 90 } 91 return days; 92} 93 94export function syncSelectedServiceDuration(state, svc) { 95 if (!state || !state.appointment_step_form_data) return; 96 const fd = state.appointment_step_form_data; 97 if (!svc) { 98 fd.selected_service_duration = ''; 99 fd.selected_service_duration_unit = ''; 100 return; 101 } 102 fd.selected_service_duration = String(parseInt(svc.serviceDurationVal || 0, 10) || 0); 103 fd.selected_service_duration_unit = String(svc.serviceDurationUnit || ''); 104} 105 106export function dayServiceEndDate(startYmd, durationDays) { 107 const s = String(startYmd || ''); 108 const start = parseYmdLocal(startYmd); 109 if (!start) return ''; 110 const duration = Math.max(1, parseInt(durationDays || 1, 10) || 1); 111 if (duration === 1) return s; 112 const d = new Date(start.getFullYear(), start.getMonth(), start.getDate()); 113 d.setDate(d.getDate() + duration - 1); 114 return formatYmdLocal(d); 115}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.