import{p as i}from"./_shopify-analytics.BSnzbwJg.min.js";const n="search_results_load";class u extends Event{constructor(t,r,e,s,a,o){super(n),this.query=t,this.attributionToken=r,this.products=e,this.totalResults=s,this.totalPages=a,this.currentPage=o}}class h extends HTMLElement{queryString=null;page=1;connectedCallback(){this.queryString=this.getAttribute("query"),this.page=Number(this.getAttribute("page"))||1,this.checkUrlForUpdates(),this.loadPageData()}listen(t,r){return this.addEventListener(t,r),()=>{this.removeEventListener(t,r)}}checkUrlForUpdates(){const t=new URL(window.location.href);t.searchParams.has("q")&&(this.queryString=t.searchParams.get("q")),t.searchParams.has("page")&&(this.page=Number.parseInt(t.searchParams.get("page")))}async loadPageData(){if(!this.queryString)return;const t=new URL("https://j9jkp5xi0m.execute-api.us-east-1.amazonaws.com/prod");t.searchParams.set("q",this.queryString);const r=await fetch(t.toString());if(!r.ok)throw new Error("Error fetching search results response");const e=await r.json(),s={query:this.queryString,attributionToken:e.attributionToken,products:e.results.map(o=>o.productJson),totalResults:e.totalResults,totalPages:e.totalPages,currentPage:this.page};if(s.products.length===0){console.log("No products from google, falling back to Shopify"),this.loadPageDataFromShopify();return}const a=new u(s.query,s.attributionToken,s.products,s.totalResults,s.totalPages,s.currentPage);this.dispatchEvent(a),i(n,s)}async loadPageDataFromShopify(){if(!this.queryString)return;const t=await fetch(`/search/suggest.json?q=${this.queryString}&resources[type]=product&resources[options][unavailable_products]=hide&resources[options][fields]=title,tag,product_type`);if(!t.ok)throw new Error("Error fetching search results response");const r=await t.json(),e={query:this.queryString,attributionToken:null,products:r.resources.results.products,totalResults:r.resources.results.products.length,totalPages:1,currentPage:1},s=new u(e.query,e.attributionToken,e.products,e.totalResults,e.totalPages,e.currentPage);this.dispatchEvent(s),i(n,e)}}customElements.get("search-context")||customElements.define("search-context",h);export{n as S,u as a};