import { __ } from '@wordpress/i18n' import { useBookingContext } from '../../../providers/BookingFormProvider/BookingFormProvider' import { wbkFormatPrice } from '../../../providers/BookingFormProvider/utils' import { isHidePriceEnabled } from '../../../lib/hidePrice/areAllSelectedPricesHidden' import { minutesToText } from '../../../../admin/components/WebbaDataTable/utils' import { useWording } from '../../../hooks/useWording' import { useWebba5Booking } from './useWebba5Booking' import { Webba5ServiceChoice } from './Webba5ServiceChoice' import { Webba5Select } from './Webba5Select' import { Webba5ActionButton } from './Webba5ActionButton' import { StaffSelector } from '../../../components/StaffSelector/StaffSelector' import { IWebba5BookableItem } from './types' type BookingState = ReturnType const readPositivePrices = (value: unknown): number[] => { const numeric = Number(value) if (Number.isFinite(numeric) && numeric > 0) { return [numeric] } if (typeof value === 'string') { const trimmed = value.trim() if (!trimmed) { return [] } try { return readPositivePrices(JSON.parse(trimmed)) } catch { return [] } } if (Array.isArray(value)) { return value.flatMap((item) => readPositivePrices(item)) } if (value && typeof value === 'object') { return Object.values(value as Record).flatMap((item) => readPositivePrices(item) ) } return [] } const formatPriceLabel = ( item: IWebba5BookableItem, priceFormat: string, freeLabel: string ) => { if (isHidePriceEnabled(item.hide_price)) { return '' } const prices = readPositivePrices(item.price) if (prices.length === 0) { return freeLabel } const minPrice = Math.min(...prices) const maxPrice = Math.max(...prices) if (minPrice !== maxPrice) { return `${wbkFormatPrice(minPrice, priceFormat)} - ${wbkFormatPrice(maxPrice, priceFormat)}` } return wbkFormatPrice(minPrice, priceFormat) } export const Webba5ServiceStep = ({ booking }: { booking: BookingState }) => { const { priceFormat, bookingMode } = useBookingContext() const wording = useWording() const freeLabel = wording.free || __('Free', 'webba-booking-lite') return (

{__("Let's get started", 'webba-booking-lite')}

{bookingMode === 'units' ? __('Choose your unit.', 'webba-booking-lite') : __('Choose your service.', 'webba-booking-lite')}

{(booking.showLocation || booking.showCategory) && (
{booking.showLocation && ( ({ id: String(location.id), label: location.label, }))} value={booking.locationId} onChange={(id) => booking.handleLocationChange(id)} /> )} {booking.showCategory && ( )}
)} {(booking.showServiceList || booking.hasPresetService) && (
{booking.visibleItems.map((item) => ( booking.handleServiceChange(String(item.id)) } /> ))} {booking.visibleItems.length === 0 && (

{wording.no_services_found || __('No services found!', 'webba-booking-lite')}

)}
)} {booking.showStaffDropdown && (
{__('Preferred team member', 'webba-booking-lite')} { if (staffId != null) { booking.handleStaffChange(String(staffId)) } }} />
)}
booking.openStep('schedule')} />
) }