import { useCallback, useState } from 'react' import classNames from 'classnames' import { __ } from '@wordpress/i18n' import { ReactComponent as CheckIcon } from '../../../../public/images/icon-checkmark.svg' import arrowDownIcon from '../../../../public/images/arrow-down.svg' import { useBookingContext } from '../../providers/BookingFormProvider/BookingFormProvider' import { wbkFormatPrice } from '../../providers/BookingFormProvider/utils' import { Button } from '../Button/Button' import { useWording } from '../../hooks/useWording' import { getPlainTextFromHtml } from '../../lib/utils' import { IExtraProps } from './types' import cartReduceIcon from '../../../../public/images/icon-cart-reduce.svg' import cartIncreaseIcon from '../../../../public/images/icon-cart-increase.svg' import '../Services/Services.scss' import './Extras.scss' const DESCRIPTION_TRUNCATE_LENGTH = 190 export const ExtraItem = ({ id, label, description, image, price, min_quantity, max_quantity, selected, quantity, onUpdate, }: IExtraProps) => { const wording = useWording() const { priceFormat } = useBookingContext() const [descriptionExpanded, setDescriptionExpanded] = useState(false) const descriptionPlainText = description ? getPlainTextFromHtml(description) : '' const shouldTruncate = descriptionPlainText.length > DESCRIPTION_TRUNCATE_LENGTH const minQ = Math.max(1, Number(min_quantity) || 1) const maxQ = Math.max(minQ, Number(max_quantity) || 1) const hasQuantityRange = maxQ > minQ const currentQuantity = Math.min( maxQ, Math.max(minQ, Number(quantity) || minQ) ) const setQuantity = useCallback( (updatedQuantity: number) => { const num = Math.min(maxQ, Math.max(minQ, updatedQuantity)) onUpdate({ quantity: num }) }, [minQ, maxQ, onUpdate] ) const toggleSelectExtra = useCallback(() => { onUpdate({ selected: !selected, }) }, [selected, onUpdate]) return (