"use strict";(()=>{var p=Object.defineProperty;var b=(n,t,e)=>t in n?p(n,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):n[t]=e;var l=(n,t,e)=>b(n,typeof t!="symbol"?t+"":t,e);var f="/apps/scheduling";function d(n){let t=Math.floor(n/60),e=n%60,i=t<12?"AM":"PM";return`${t%12===0?12:t%12}:${e.toString().padStart(2,"0")} ${i}`}function c(n){let[t,e,i]=n.split("-").map(Number);return new Date(Date.UTC(t,e-1,i)).toLocaleDateString(void 0,{weekday:"short",month:"short",day:"numeric",timeZone:"UTC"})}function w(n){let t=n.dataset,e=[];return t.showShipping==="true"&&e.push({value:"SHIPPING",label:t.labelShipping||"Shipping",attrLabel:t.attrLabelShipping||"Shipping date"}),t.showLocalDelivery==="true"&&e.push({value:"LOCAL_DELIVERY",label:t.labelLocalDelivery||"Local delivery",attrLabel:t.attrLabelLocalDelivery||"Delivery date"}),t.showPickup==="true"&&e.push({value:"PICKUP",label:t.labelPickup||"Pickup",attrLabel:t.attrLabelPickup||"Pickup date"}),{root:n,heading:t.heading||"",locationId:t.locationId||null,methods:e,labels:{chooseDate:t.labelChooseDate||"Choose a date",chooseTime:t.labelChooseTime||"Choose a time",noDates:t.labelNoDates||"No dates are available right now.",confirmed:t.labelConfirmed||"Confirmed for",change:t.labelChange||"Change",loading:t.labelLoading||"Loading available dates\u2026",error:t.labelError||"Couldn't load available dates. Please try again."}}}async function y(n,t){let e=new URLSearchParams({method:n,days:"14"});t&&e.set("locationId",t);let i=await fetch(`${f}/availability?${e.toString()}`,{headers:{Accept:"application/json"}}),o=await i.json();if(!i.ok)throw new Error(o.error||`Request failed (${i.status})`);return o}async function v(){return(await(await fetch("/cart.js",{headers:{Accept:"application/json"}})).json()).token}async function g(n){let t=await fetch(`${f}/hold`,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify(n)}),e=await t.json();return!t.ok&&n.intent==="create"?{success:!1,error:e.error||"Slot unavailable"}:e}async function C(n,t,e){await fetch("/cart/update.js",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({attributes:{[n]:e,...t}})})}var h=class{constructor(t){l(this,"config");l(this,"el",{heading:document.createElement("h3"),methodRow:document.createElement("div"),dateRow:document.createElement("div"),timeRow:document.createElement("div"),status:document.createElement("p"),confirmation:document.createElement("div")});l(this,"selectedMethod",null);l(this,"selectedDate",null);l(this,"availability",null);l(this,"heldSlot",null);this.config=t}mount(){let{root:t,heading:e,methods:i}=this.config;t.classList.add("dd-widget--ready"),t.innerHTML="",i.length!==0&&(e&&(this.el.heading.className="dd-widget__heading",this.el.heading.textContent=e,t.appendChild(this.el.heading)),this.el.methodRow.className="dd-widget__row dd-widget__methods",this.el.dateRow.className="dd-widget__row dd-widget__dates",this.el.timeRow.className="dd-widget__row dd-widget__times",this.el.status.className="dd-widget__status",this.el.confirmation.className="dd-widget__confirmation",this.el.confirmation.hidden=!0,t.append(this.el.confirmation,this.el.methodRow,this.el.dateRow,this.el.timeRow,this.el.status),i.length===1?this.selectMethod(i[0]):this.renderMethods())}renderMethods(){var t;this.el.methodRow.innerHTML="";for(let e of this.config.methods){let i=document.createElement("button");i.type="button",i.className="dd-widget__pill",i.textContent=e.label,i.setAttribute("aria-pressed",String(((t=this.selectedMethod)==null?void 0:t.value)===e.value)),i.addEventListener("click",()=>this.selectMethod(e)),this.el.methodRow.appendChild(i)}}async selectMethod(t){this.selectedMethod=t,this.selectedDate=null,this.el.timeRow.innerHTML="",this.el.confirmation.hidden=!0,this.config.methods.length>1&&this.renderMethods(),this.el.status.textContent=this.config.labels.loading,this.el.dateRow.innerHTML="";try{this.availability=await y(t.value,this.config.locationId),this.renderDates()}catch{this.el.status.textContent=this.config.labels.error}}renderDates(){var e,i;let t=Object.keys((i=(e=this.availability)==null?void 0:e.dates)!=null?i:{}).sort();if(this.el.dateRow.innerHTML="",t.length===0){this.el.status.textContent=this.config.labels.noDates;return}this.el.status.textContent=this.config.labels.chooseDate;for(let o of t){let a=document.createElement("button");a.type="button",a.className="dd-widget__pill",a.textContent=c(o),a.setAttribute("aria-pressed",String(this.selectedDate===o)),a.addEventListener("click",()=>this.selectDate(o)),this.el.dateRow.appendChild(a)}}selectDate(t){var i,o;this.selectedDate=t;for(let a of Array.from(this.el.dateRow.children))a.setAttribute("aria-pressed",String(a.textContent===c(t)));let e=(o=(i=this.availability)==null?void 0:i.dates[t])!=null?o:[];this.el.timeRow.innerHTML="",this.el.status.textContent=this.config.labels.chooseTime;for(let a of e){let s=document.createElement("button");s.type="button",s.className="dd-widget__pill",s.textContent=`${d(a.startMin)}\u2013${d(a.endMin)}`,s.addEventListener("click",()=>this.selectSlot(t,a)),this.el.timeRow.appendChild(s)}}async selectSlot(t,e){let i=this.selectedMethod,o=this.availability,a=`${c(t)}, ${d(e.startMin)}\u2013${d(e.endMin)}`;this.el.status.textContent=this.config.labels.loading;try{let s=await v();if(!(await g({intent:"create",locationId:o.locationId,method:i.value,date:t,startMin:e.startMin,cartToken:s})).success){this.el.status.textContent=this.config.labels.error,await this.selectMethod(i);return}this.heldSlot={locationId:o.locationId,method:i.value,date:t,startMin:e.startMin,cartToken:s},await C(i.attrLabel,{dd_method:i.value,dd_date:t,dd_start_min:String(e.startMin),dd_end_min:String(e.endMin),dd_location_id:o.locationId},a),this.el.status.textContent="",this.el.methodRow.hidden=!0,this.el.dateRow.hidden=!0,this.el.timeRow.hidden=!0,this.el.confirmation.hidden=!1,this.el.confirmation.innerHTML="";let u=document.createElement("p");u.textContent=`${this.config.labels.confirmed} ${a}`;let r=document.createElement("button");r.type="button",r.className="dd-widget__link",r.textContent=this.config.labels.change,r.addEventListener("click",()=>{this.heldSlot&&(g({intent:"release",...this.heldSlot}),this.heldSlot=null),this.el.methodRow.hidden=!1,this.el.dateRow.hidden=!1,this.el.timeRow.hidden=!1,this.el.confirmation.hidden=!0}),this.el.confirmation.append(u,r)}catch{this.el.status.textContent=this.config.labels.error}}};function m(){document.querySelectorAll("[data-dd-widget]").forEach(t=>{new h(w(t)).mount()})}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",m):m();})();