import{getContext as e,getElement as t,store as o}from"@wordpress/interactivity";function s(e){const[t,o]=e.split(":").map(Number),s=t>=12?"pm":"am";return`${String(t%12||12).padStart(2,"0")}:${String(o).padStart(2,"0")} ${s}`}const{state:r}=o("woocommerce-bookings/booking-form",{}),{state:n,actions:a}=o("woocommerce-bookings/booking-time-slots",{state:{get isVisible(){return!!e("woocommerce-bookings/booking-form")?.requiresTimeSelection},get shouldShowPlaceholder(){const e=r.slotsForSelectedDate;return!e&&!r.isBusy||0===e.length},get totalPages(){const t=e().slotsPerPage,o=r.slotsForSelectedDate;return Math.ceil(o.length/t)},get pages(){return Array.from({length:n.totalPages},(e,t)=>({pageNumber:t+1,ariaLabel:`Go to page ${t+1}`,isSelected:t+1===r.slotsCurrentPage}))},get shouldShowPagination(){return n.totalPages>1},get slotsForPage(){const t=r.slotsCurrentPage,o=r.slotsForSelectedDate;if(!o)return[];const s=e();return o.slice((t-1)*s.slotsPerPage,t*s.slotsPerPage)},get isPreviousPageDisabled(){return 1===r.slotsCurrentPage},get isNextPageDisabled(){return r.slotsCurrentPage===n.totalPages},get slotIsSelected(){const t=e(),o=e("woocommerce-bookings/booking-form").selectedSlotKey;return o&&o===t.slot.time},get slotAriaLabel(){return`Select ${s(e().slot.time)}`},get slotTimeString(){return s(e().slot.time)}},actions:{nextPage(){const t=e("woocommerce-bookings/booking-form");t.slotsCurrentPage=t.slotsCurrentPage+1},prevPage(){const t=e("woocommerce-bookings/booking-form");t.slotsCurrentPage=t.slotsCurrentPage-1},handleGoToPage(){const o=t()?.ref;if(!o)return;const s=parseInt(o.getAttribute("data-pageNumber"),10);s<1||s>n.totalPages||(e("woocommerce-bookings/booking-form").slotsCurrentPage=s)},handleSelectTime(){const o=t()?.ref;if(!o)return;const s=o.dataset.time,r=e("woocommerce-bookings/booking-form");r.selectedSlotKey!==s&&(r.selectedSlotKey=s,setTimeout(()=>{const e=document.querySelector(".wc-bookings-modal[open] .wc-bookings-modal-buttons .wp-element-button");e&&e?.focus()},0))},onTouchStart(t){const o=e(),{clientX:s}=t.touches[0];o.touchStartX=s,o.touchCurrentX=s,o.isDragging=!0},onTouchMove(t){const o=e();if(!o.isDragging)return;const{clientX:s}=t.touches[0];o.touchCurrentX=s;const r=s-o.touchStartX;Math.abs(r)>10&&t.preventDefault()},onTouchEnd:()=>{const o=e();if(!o.isDragging)return;const s=o.touchCurrentX-o.touchStartX,r=t()?.ref,n=r?.offsetWidth||0;Math.abs(s)>.3*n&&(s>0?a.prevPage():s<0&&a.nextPage()),o.isDragging=!1,o.touchStartX=0,o.touchCurrentX=0}},callbacks:{selectFirstSlotWhenDateSelected(){const t=e("woocommerce-bookings/booking-form"),o=r.slotsForSelectedDate;o&&o.length>0?(t.selectedSlotKey=o[0].time,t.slotsCurrentPage=1):(t.selectedSlotKey=null,t.slotsCurrentPage=1)}}});