import{i as n}from"./_scheduler.DOZJZeMb.min.js";import{p as l}from"./_product-card-batch-loader.Btc9rgsa.min.js";const c="collection_load";class d extends Event{constructor(t,s,e,i,a,r){super(c),this.handle=t,this.products=s,this.facets=e,this.totalResults=i,this.totalPages=a,this.currentPage=r}}class h extends HTMLElement{collectionHandle=null;page=1;limit=100;sortCode=null;accessToken=null;filters=[];facets=[];pageDataLoaded=!1;pageLoadEvent=null;handlePopState;constructor(){super(),this.initializeAttributes(),this.checkUrlForUpdates(),this.loadPageData(),this.handlePopState=()=>{this.pageDataLoaded=!1,this.checkUrlForUpdates(),this.loadPageData()}}connectedCallback(){window.addEventListener("popstate",this.handlePopState.bind(this)),this.checkUrlForUpdates()}disconnectedCallback(){window.removeEventListener("popstate",this.handlePopState.bind(this))}initializeAttributes(){this.collectionHandle=this.getAttribute("collection-handle"),this.page=Number(this.getAttribute("page"))||1,this.limit=Number(this.getAttribute("limit"))||100,this.sortCode=this.getAttribute("sort-code"),this.accessToken=this.getAttribute("access-token"),this.facets=this.getAttribute("facets")?.split(",").map(t=>t.trim()).filter(t=>t!="")??[]}listen(t,s){return this.addEventListener(t,s),()=>{this.removeEventListener(t,s)}}checkUrlForUpdates(){const t=new URL(window.location.href),s=[];for(const[e,i]of t.searchParams.entries())e.startsWith("sort.")?this.sortCode=e.replace("sort.",""):e.startsWith("filter.")?s.push({property:e.replace("filter.",""),operator:"in",values:[i]}):e==="page"&&(this.page=parseInt(i));this.filters=s}loadPageData(){if(!this.collectionHandle||!this.accessToken||!this.sortCode||this.pageDataLoaded)return;const t=new XMLHttpRequest;t.open("POST",`https://dsoptwrvacv84.cloudfront.net/api/storefront/v1/browse/${this.collectionHandle}`),t.setRequestHeader("Content-Type","application/json"),t.setRequestHeader("Accept","application/json"),t.setRequestHeader("X-Storefront-Access-Token",this.accessToken);const s=JSON.stringify({attributes:["handle","title","featured_media"],facets:this.facets.length?this.facets:void 0,retrieveFacetCount:this.facets.length?!0:void 0,pagination:{page:this.page,limit:this.limit},filter_group:this.filters.length?{conditional:"AND",expressions:this.filters}:void 0,sort_order_code:this.sortCode});try{t.send(s),t.onload=()=>{if(t.status!==200)throw new Error("Error fetching collection response");const e=JSON.parse(t.responseText),i=n()?4:8;e.results.slice(0,i).forEach(a=>l.addRequest(a.handle)),this.pageDataLoaded=!0,this.pageLoadEvent=new d(this.collectionHandle,e.results,e.facets,e.totalResults,e.totalPages,this.page),this.dispatchEvent(this.pageLoadEvent)}}catch(e){console.error("Failed to load page data:",e)}}}customElements.get("collection-context")||customElements.define("collection-context",h);export{c as C,d as a};