if(!customElements.get("predictive-search")){class PredictiveSearch extends HTMLElement{constructor(){super(),this.cachedResults={},this.input=this.querySelector(".js-search-input"),this.productTypeSelect=this.querySelector(".js-search-product-types"),this.productTypeInput=document.getElementById("product_type_input"),this.resetBtn=this.querySelector(".js-search-reset"),this.results=this.querySelector(".js-search-results"),this.overlay=this.querySelector(".js-search-overlay"),this.statusEl=this.querySelector(".js-search-status"),this.loadingText=this.getAttribute("data-loading-text"),this.addListeners()}disconnectedCallback(){this.close()}addListeners(){this.input.addEventListener("focus",this.handleFocus.bind(this)),this.input.addEventListener("input",debounce(this.handleInput.bind(this))),this.productTypeSelect&&this.productTypeSelect.addEventListener("change",this.handleProductTypeChange.bind(this)),this.querySelector(".search").addEventListener("submit",this.handleSubmit.bind(this))}getQuery(){return this.input.value.trim()}handleSubmit(evt){(!this.getQuery().length||this.querySelector('.predictive-search__item[aria-selected="true"]:not(.js-submit)'))&&evt.preventDefault()}handleInput(){this.input.setAttribute("value",this.input.value);const searchTerm=this.getQuery();if(!searchTerm.length){this.close();return}this.getResults(searchTerm)}handleProductTypeChange(evt){this.productTypeInput.value=evt.detail.selectedValue;const query=this.getQuery();query.length>0&&this.getResults(query)}handleFocus(){const searchTerm=this.getQuery();searchTerm.length&&(this.getAttribute("results")==="true"?this.open():this.getResults(searchTerm))}handleKeydown(evt){if(!evt.target.matches(".tablist__tab"))switch(evt.key){case"ArrowUp":case"ArrowDown":evt.preventDefault(),this.hasAttribute("open")&&this.handleResultsNav(evt.key);break;case"Enter":this.selectOption();break;case"Escape":this.close();break}}handleKeyup(){this.getQuery().length||this.close()}handleResultsNav(key){const selectedResult=this.querySelector('[role="tabpanel"]:not([hidden]) [aria-selected="true"]'),allResults=this.querySelectorAll('[role="tabpanel"]:not([hidden]) .predictive-search__item');let resultToSelect=allResults[0];if(key==="ArrowUp"){if(!selectedResult)return;resultToSelect=selectedResult.previousElementSibling||allResults[allResults.length-1]}else key==="ArrowDown"&&selectedResult&&(resultToSelect=selectedResult.nextElementSibling||allResults[0]);if(this.statusEl.textContent="",resultToSelect){if(resultToSelect===selectedResult)return;resultToSelect.setAttribute("aria-selected","true"),selectedResult&&selectedResult.setAttribute("aria-selected","false"),this.setLiveRegionText(resultToSelect.textContent),this.input.setAttribute("aria-activedescendant",resultToSelect.id)}}selectOption(){const selectedResult=this.querySelector('[aria-selected="true"] > .js-search-link');selectedResult&&selectedResult.click()}async getResults(searchTerm){this.setLiveRegionLoadingState();let searchFields="title,product_type,variants.title,vendor";theme.settings.pSearchIncludeSkus&&(searchFields+=",variants.sku"),theme.settings.pSearchIncludeTags&&(searchFields+=",tag");let searchParams="";this.productTypeInput&&this.productTypeInput.value!==""?searchParams=`q=product_type:${encodeURIComponent(this.productTypeInput.value)} AND ${encodeURIComponent(searchTerm)}`:searchParams=`q=${encodeURIComponent(searchTerm)}`,searchParams+=`&${encodeURIComponent("resources[limit]")}=${theme.settings.pSearchLimit}`,searchParams+=`&${encodeURIComponent("resources[limit_scope]")}=${theme.settings.pSearchLimitScope}`,searchParams+=`&${encodeURIComponent("resources[options][fields]")}=${searchFields}`,searchParams+="§ion_id=predictive-search";const queryKey=searchParams.replace(" ","-").toLowerCase();if(this.cachedResults[queryKey]){this.renderResults(this.cachedResults[queryKey]);return}try{const response=await fetch(`${theme.routes.predictiveSearch}?${searchParams}`);if(!response.ok)throw new Error(response.status);const tmpl=document.createElement("template");tmpl.innerHTML=await response.text();const resultsMarkup=tmpl.content.querySelector("#shopify-section-predictive-search").innerHTML.replace(/psearch/g,this.input.id);this.cachedResults[queryKey]=resultsMarkup,this.renderResults(resultsMarkup)}catch(error){throw this.close(),error}}setLiveRegionLoadingState(){this.setLiveRegionText(this.loadingText)}setLiveRegionText(statusText){this.statusEl.setAttribute("aria-hidden","false"),this.statusEl.textContent=statusText,setTimeout(()=>{this.statusEl.setAttribute("aria-hidden","true")},1e3)}renderResults(resultsMarkup){this.results.innerHTML=resultsMarkup,this.setAttribute("results",!0),this.open()}open(){this.overlay.classList.add("is-visible"),this.getQuery().length&&(this.resetBtn.hidden=!1),this.input.setAttribute("aria-expanded","true"),this.setAttribute("open",""),document.body.classList.add("overlay-predictive-search"),this.keydownHandler=this.keydownHandler||this.handleKeydown.bind(this),this.keyupHandler=this.keyupHandler||this.handleKeyup.bind(this),this.resetBtnClickHandler=this.resetBtnClickHandler||this.close.bind(this),this.overlayClickHandler=this.overlayClickHandler||this.close.bind(this),this.addEventListener("keydown",this.keydownHandler),this.addEventListener("keyup",this.keyupHandler),this.resetBtn.addEventListener("click",this.resetBtnClickHandler),this.overlay.addEventListener("click",this.overlayClickHandler)}close(evt){evt&&evt.target===this.resetBtn&&(this.input.value="",this.removeAttribute("results"),this.input.focus());const selected=this.querySelector('.predictive-search__item[aria-selected="true"]');selected&&selected.setAttribute("aria-selected","false"),this.removeAttribute("open"),this.resetBtn.hidden=!0,this.overlay.classList.remove("is-visible"),this.input.setAttribute("aria-activedescendant",""),this.input.setAttribute("aria-expanded","false"),document.body.classList.remove("overlay-predictive-search"),this.removeEventListener("keydown",this.keydownHandler),this.resetBtn.removeEventListener("click",this.resetBtnClickHandler),this.overlay.removeEventListener("click",this.overlayClickHandler)}}customElements.define("predictive-search",PredictiveSearch)} //# sourceMappingURL=/cdn/shop/t/10/assets/predictive-search.js.map?v=158424367886238494141716915740