import{d as S}from"./_index.DsovQ8IZ.min.js";import{y as L}from"./_scheduler.DOZJZeMb.min.js";import{e as b}from"./_favorites-service.BaRMmbSg.min.js";import{r as C}from"./_abra-promotions.D4UKp64M.min.js";const v=navigator?.connection?.saveData;function _(a){if(a instanceof URLSearchParams)return a;{const t=new URLSearchParams;for(const[e,s]of Object.entries(a))s!==null&&t.set(e,s.toString());return t}}class x{pendingRequests=new Map;prefetchRequests=new Set;prefetchCache=new Map;ongoingFetches=new Set;fetchSectionsTimeout=null;prefetchSectionsTimeout=null;cacheMaxSize=100;buildCacheKey(t,e,s){return JSON.stringify({path:e||window.location.pathname,sectionId:t,queryParams:s?_(s).toString():""})}buildRequestGroups(t){const e=t.reduce((s,n)=>{const{path:c,sectionId:r,queryParams:i}=JSON.parse(n),o=JSON.stringify({path:c,queryParams:i}),h=i?new URLSearchParams(i):void 0;return s[o]||(s[o]={path:c,queryParams:h,keys:[],sectionIds:[]}),s[o].keys.push(n),s[o].sectionIds.push(r),s},{});return Object.values(e)}scheduleFetchSections(){this.fetchSectionsTimeout&&clearTimeout(this.fetchSectionsTimeout),this.fetchSectionsTimeout=setTimeout(()=>this.fetchSections(),50)}schedulePrefetchSections(){this.prefetchSectionsTimeout&&clearTimeout(this.prefetchSectionsTimeout),this.prefetchSectionsTimeout=setTimeout(()=>this.prefetchSections(),50)}async fetchSections(){if(this.pendingRequests.size===0)return;const t=Array.from(this.pendingRequests.keys()),e=this.buildRequestGroups(t);await Promise.allSettled(e.map(async({path:s,queryParams:n,keys:c,sectionIds:r})=>{await L();try{const i=new URL(window.location.href);i.pathname=s||window.location.pathname,n&&(i.search=n.toString()),i.searchParams.set("sections",r.join(","));const o=i.toString(),h=await fetch(o);if(!h.ok)throw new Error(`Failed to fetch: ${o}`);const u=await h.json();c.forEach((d,E)=>{const f=this.pendingRequests.get(d)||[];this.pendingRequests.delete(d);const l=r[E],g=u[l];g===void 0?f.forEach(({reject:m})=>m(new Error(`No data for section ${l}`))):(f.forEach(({resolve:m})=>m(g)),this.addToCache(d,g)),this.ongoingFetches.delete(d)})}catch(i){console.error(i),c.forEach(o=>{const h=this.pendingRequests.get(o)||[];this.pendingRequests.delete(o),h.forEach(({reject:u})=>u(i)),this.ongoingFetches.delete(o)})}}))}async prefetchSections(){if(this.prefetchRequests.size===0)return;const t=Array.from(this.prefetchRequests),e=this.buildRequestGroups(t);await Promise.allSettled(e.map(async({path:s,queryParams:n,keys:c,sectionIds:r})=>{try{const i=new URL(window.location.href);i.pathname=s||window.location.pathname,n&&(i.search=n.toString()),i.searchParams.set("sections",r.join(","));const o=i.toString(),h=await fetch(o,{priority:"low"});if(!h.ok)throw new Error(`Failed to prefetch: ${o}`);const u=await h.json();c.forEach((d,E)=>{const f=r[E],l=u[f];l!==void 0&&this.addToCache(d,l),this.prefetchRequests.delete(d),this.ongoingFetches.delete(d);const g=this.pendingRequests.get(d)||[];this.pendingRequests.delete(d),g.forEach(({resolve:m,reject:q})=>{l!==void 0?m(l):q(new Error(`No data for section ${f}`))})})}catch(i){console.error(i),c.forEach(o=>{this.prefetchRequests.delete(o),this.ongoingFetches.delete(o);const h=this.pendingRequests.get(o)||[];this.pendingRequests.delete(o),h.forEach(({reject:u})=>u(i))})}}))}addToCache(t,e){this.prefetchCache.size>this.cacheMaxSize&&this.prefetchCache.delete(this.prefetchCache.keys().next().value),this.prefetchCache.set(t,e)}loadSection(t,e="",s){const n=this.buildCacheKey(t,e,s);return this.prefetchCache.has(n)?Promise.resolve(this.prefetchCache.get(n)):new Promise((c,r)=>{this.pendingRequests.has(n)||this.pendingRequests.set(n,[{resolve:c,reject:r}]),!this.ongoingFetches.has(n)&&!this.prefetchRequests.has(n)&&(this.ongoingFetches.add(n),this.scheduleFetchSections())})}prefetchSection(t,e="",s){if(v)return;const n=this.buildCacheKey(t,e,s);this.pendingRequests.has(n)||this.ongoingFetches.has(n)||this.prefetchCache.has(n)||this.prefetchRequests.has(n)||(this.prefetchRequests.add(n),this.ongoingFetches.add(n),this.schedulePrefetchSections())}cancelPrefetchSection(t,e="",s){this.prefetchRequests.delete(this.buildCacheKey(t,e,s)),this.prefetchRequests.size&&this.schedulePrefetchSections()}}const p=new x,H=p.loadSection.bind(p),y=p.prefetchSection.bind(p);p.cancelPrefetchSection.bind(p);const P="product_change",R="product_may_change",U="product_selected_variant_change",A="product_selected_variant_may_change",M="product_unavailable_variant_selected";class T extends Event{constructor(t,e,s){super(P),this.oldHandle=t,this.newHandle=e,this.variantId=s}}class w extends Event{constructor(t,e){super(R),this.currentHandle=t,this.newHandle=e}}class N extends Event{constructor(t,e){super(U),this.handle=t,this.variantId=e}}class I extends Event{constructor(){super(M)}}class F extends Event{constructor(t,e){super(A),this.handle=t,this.variantId=e}}class V extends HTMLElement{handle=null;originalHandle=null;variantId=null;swatchHandles=new Set;isProductPage=!0;connectedCallback(){this.isProductPage=!this.closest("custom-dialog"),this.handle=this.getAttribute("handle"),this.originalHandle=this.handle,this.isProductPage&&(document.readyState==="complete"?this.onWindowLoad():window.addEventListener("load",this.onWindowLoad.bind(this)))}onWindowLoad(){v||this.runPreloads()}setHandle(t){if(t!==this.handle){const e=this.handle;this.handle=t,this.variantId=null,this.setAttribute("handle",this.handle),this.removeAttribute("variant-id"),e&&!this.originalHandle&&(this.originalHandle=e);const s=this.querySelector("button[type=submit]");if(s&&(s.dataset.state="submitting"),this.isProductPage){const n=new URL(`${window.location.origin}/products/${t}${window.location.search}`);n.searchParams.delete("variant"),window.history.replaceState(null,"",n.toString())}e&&this.dispatchEvent(new T(e,t,this.variantId))}}updateUrl(){if(!this.isProductPage)return;const t=new URL(window.location.href),e=t.pathname.split("/");e.pop(),e.push(this.handle),t.pathname=e.join("/"),this.variantId?t.searchParams.set("variant",this.variantId):t.searchParams.delete("variant"),window.history.replaceState(null,"",t.toString())}setVariantId(t){if(t===null){this.variantId=null,this.dispatchEvent(new I),this.updateUrl();return}if(t!==this.variantId){const e=this.querySelector("button[type=submit]");e&&(e.dataset.state="submitting"),this.updateUrl(),this.setAttribute("variant-id",t),this.variantId=t,this.dispatchEvent(new N(this.handle,t))}}signalHandleMayChange=S(t=>{this.dispatchEvent(new w(this.handle,t))},10);signalVariantIdMayChange=S(t=>{t!==this.variantId&&this.dispatchEvent(new F(this.handle,t))},10);preloadHandle(t){this.swatchHandles.has(t)||this.swatchHandles.add(t)}runPreloads(){this.swatchHandles.forEach(t=>this.dispatchEvent(new w(this.handle,t)))}listen(t,e){return this.addEventListener(t,e),()=>{this.removeEventListener(t,e)}}checkUrlForUpdates(){if(this.isProductPage){const{pathname:t}=window.location,e=t.split("/").pop();e&&this.setHandle(e)}else{const t=this.getAttribute("handle");t&&this.setHandle(t)}}}class D extends HTMLElement{sectionId=null;productContext;unbindCallbacks=[];bindProductContext(){const t=this.closest("product-context");if(!t)throw new Error(" must be a child of ");this.unbindCallbacks.length&&(this.unbindCallbacks.forEach(e=>e()),this.unbindCallbacks=[]),this.productContext=t,this.unbindCallbacks.push(this.productContext.listen(P,this.handleProductChange.bind(this))),this.unbindCallbacks.push(this.productContext.listen(R,this.handleProductMayChange.bind(this)))}connectedCallback(){if(this.hasAttribute("section-id"))this.sectionId=this.getAttribute("section-id");else throw new Error(`\`section-id\` must be specified with <${this.tagName.toLowerCase()}> or it will not refresh when color is changed.`);this.bindProductContext()}disconnectedCallback(){this.unbindCallbacks.forEach(t=>t()),this.unbindCallbacks=[]}handleProductMayChange(t){if(t instanceof w)return y(this.sectionId,`/products/${t.newHandle}`)}async handleProductChange(t){if(t instanceof T)return this.loadSection(t.newHandle,t.variantId)}loadSection(t,e=null){return H(this.sectionId,`/products/${t}`,this.getUrlParams(e)).then(s=>{if(!s)return;const r=document.createRange().createContextualFragment(s).querySelector(this.tagName);r&&b(async()=>{this.replaceSection(r.cloneNode(!0))})})}getUrlParams(t=null){const e=t?{variant:t}:{};return this.productContext?.isProductPage||(e.view="quick-view"),e}replaceSection(t){t.productContext=this.productContext,this.beforeSectionMount(this,t),this.productContext.isProductPage?b(async()=>{this.replaceWith(t),C()}):(this.replaceWith(t),C())}beforeSectionMount(t,e){}}customElements.get("product-context")||customElements.define("product-context",V);customElements.get("product-context-prefetchable-child-section")||customElements.define("product-context-prefetchable-child-section",D);const k=200,K=(a,t,e=k,s=!0,n)=>{if(s&&v)return;let c;const r=()=>{c=setTimeout(()=>t(a),e)},i=()=>{c!==void 0&&(clearTimeout(c),c=void 0),n&&n(a)},o=()=>{t(a)};return a.addEventListener("mouseenter",r,{passive:!0}),a.addEventListener("mouseleave",i,{passive:!0}),a.addEventListener("touchstart",o,{passive:!0}),a.addEventListener("touchend",i,{passive:!0}),a.addEventListener("touchcancel",i,{passive:!0}),()=>{a.removeEventListener("mouseenter",r),a.removeEventListener("mouseleave",i),a.removeEventListener("touchstart",o),a.removeEventListener("touchend",i),a.removeEventListener("touchcancel",i)}};export{k as H,D as P,A as a,U as b,M as c,F as d,N as e,P as f,V as g,K as h,v as i,y as p};