import{getConfig as t,getContext as e,getElement as o,store as n,withSyncEvent as r}from"@wordpress/interactivity";function s(t){return String(t).padStart(2,"0")}function i(t){return`${t.getFullYear()}-${s(t.getMonth()+1)}-${s(t.getDate())}`}function a(t){return`${t.getFullYear()}-${s(t.getMonth()+1)}`}function c(t,e){const o=new Date(t);return o.setDate(o.getDate()+e),o}function u(t){if(!t)return null;const[e,o]=t.split("-").map(Number);return e&&o?new Date(e,o-1,1):null}const g=(t,e)=>({previousMonth:1===t?12:t-1,previousYear:1===t?e-1:e}),m=(t,e)=>({nextMonth:12===t?1:t+1,nextYear:12===t?e+1:e}),{state:l,actions:h}=n("woocommerce-bookings/booking-form",{}),{state:d,actions:b}=n("woocommerce-bookings/booking-form-calendar",{state:{get visibleDates(){const t=e("woocommerce-bookings/booking-form"),o=l.gridStart,n=l.gridEnd,r=[],s=Math.floor((n.getTime()-o.getTime())/864e5)+1;for(let e=0;e<s;e++){const n=c(o,e),s=u(t.viewMonth);if(!s)continue;const g=a(n)===a(s);let m;m=g?"current":a(n)<a(s)?"prev":"next",r.push({key:`${m}-${n.getDate()}-${e}`,ymd:i(n),day:n.getDate(),isInViewMonth:g})}return r},get viewMonthName(){const o=e("woocommerce-bookings/booking-form").viewMonth||"",n=parseInt(o.split("-")[1]||"1",10),r=t()?.monthNames;return r[n-1]},get viewYear(){const t=e("woocommerce-bookings/booking-form").viewMonth||"";return parseInt(t.split("-")[0]||"2024",10)},get prevMonthLabel(){const o=(e("woocommerce-bookings/booking-form").viewMonth||a(new Date)).split("-"),n=parseInt(o[1],10),r=parseInt(o[0],10),{previousMonth:s,previousYear:i}=g(n,r),c=t()?.monthNames;return`Go to ${c[s-1]} ${i}`},get nextMonthLabel(){const o=(e("woocommerce-bookings/booking-form").viewMonth||a(new Date)).split("-"),n=parseInt(o[1],10),r=parseInt(o[0],10),{nextMonth:s,nextYear:i}=m(n,r),c=t()?.monthNames;return`Go to ${c[s-1]} ${i}`},get dayAriaLabel(){const o=e().day.ymd.split("-"),n=parseInt(o[0],10),r=parseInt(o[1],10),s=parseInt(o[2],10),i=t()?.monthNames;return`${i[r-1]} ${s}, ${n}`},get dayIsToday(){const t=e(),o=i(new Date);return t.day.ymd===o},get dayIsSelected(){const t=e();return e("woocommerce-bookings/booking-form").selectedDate===t.day.ymd},get dayIsDisabled(){if(d.dayIsToday)return!1;const t=e(),o=new Date(t.day.ymd);o.setHours(0,0,0,0);const n=l.bookingWindow;if(n){const t=new Date(n.start),e=new Date(n.end);if(t.setHours(0,0,0,0),e.setHours(0,0,0,0),o<t||o>e)return!0}const r=e("woocommerce-bookings/booking-form"),s=a(o),i=function(t,e){return`avail:${t?`team=${t}`:"team=none"}:month=${e}`}(r.selectedTeamId,s),c=r.availabilityCache?.[i],u=c?.data?.[t.day.ymd];return!(u&&"object"==typeof u?Object.keys(u).length>0:Boolean(u))},get dayTabIndex(){return d.dayIsDisabled?-1:0},get calendarKey(){return e("woocommerce-bookings/booking-form").viewMonth||""},get isPreviousMonthDisabled(){const t=e("woocommerce-bookings/booking-form");if(t.isBusy)return!0;const o=l.bookingWindow;if(!o)return!1;const n=(t.viewMonth||"").split("-"),r=parseInt(n[1],10),s=parseInt(n[0],10),{previousMonth:i,previousYear:a}=g(r,s),c=new Date(o.start);return i<c.getMonth()+1&&a===c.getFullYear()||a<c.getFullYear()},get isNextMonthDisabled(){const t=e("woocommerce-bookings/booking-form");if(t.isBusy)return!0;const o=l.bookingWindow;if(!o)return!1;const n=(t.viewMonth||"").split("-"),r=parseInt(n[1],10),s=parseInt(n[0],10),{nextMonth:i,nextYear:a}=m(r,s),c=new Date(o.end);return i>c.getMonth()+1&&a===c.getFullYear()||a>c.getFullYear()}},actions:{async navigateToPreviousMonth(){const t=(e("woocommerce-bookings/booking-form").viewMonth||"").split("-"),o=parseInt(t[1],10),n=parseInt(t[0],10),{previousMonth:r,previousYear:s}=g(o,n),i=`${s}-${String(r).padStart(2,"0")}`;h.setViewMonth(i)},async navigateToNextMonth(){const t=(e("woocommerce-bookings/booking-form").viewMonth||"").split("-"),o=parseInt(t[1],10),n=parseInt(t[0],10),{nextMonth:r,nextYear:s}=m(o,n),i=`${s}-${String(r).padStart(2,"0")}`;h.setViewMonth(i)},handleSelectDate:r(t=>{const n=o()?.ref;if(!n)return;if("true"===n.getAttribute("aria-disabled"))return void t.preventDefault();const r=n.dataset.date;if(!r)return;if(e("woocommerce-bookings/booking-form").selectedDate===r)return;h.selectDate(r);const s=e("woocommerce-bookings/booking-form");if(s?.requiresTimeSelection){const t=n.closest("form"),e=t?.querySelector(".wc-bookings-time-slots__grid");e&&setTimeout(()=>{const t=e?.querySelector("button");t&&t?.focus()},0)}}),onTouchStart(t){const o=e(),{clientX:n}=t.touches[0];o.touchStartX=n,o.touchCurrentX=n,o.isDragging=!0},onTouchMove(t){const o=e();if(!o.isDragging)return;const{clientX:n}=t.touches[0];o.touchCurrentX=n;const r=n-o.touchStartX;Math.abs(r)>10&&t.preventDefault()},onTouchEnd:()=>{const t=e();if(!t.isDragging)return;const n=t.touchCurrentX-t.touchStartX,r=o()?.ref,s=r?.offsetWidth||0;Math.abs(n)>.4*s&&(n>0&&!d.isPreviousMonthDisabled?b.navigateToPreviousMonth():n<0&&!d.isNextMonthDisabled&&b.navigateToNextMonth()),t.isDragging=!1,t.touchStartX=0,t.touchCurrentX=0}}});