1var u=Object.defineProperty;var d=(a,h,e)=>h in a?u(a,h,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[h]=e;var n=(a,h,e)=>d(a,typeof h!="symbol"?h+"":h,e);import{B as p}from"./BaseElement.CsZ3Pn0g.min.js";import{L as m}from"./LiveRegionUtility.44lKd9Yx.min.js";import"./lit-element.D1QXZDXi.min.js";import"./aria-live-region-context.BZEtRCSs.min.js";import"./create-context.89xeped_.min.js";class g extends p{constructor(){super(...arguments);n(this,"$liveRegionUtility",new m(this));n(this,"resultsMaxHeight");n(this,"searchTerm","");n(this,"input");n(this,"predictiveSearchResults");n(this,"cachedResults",{});n(this,"abortController",new AbortController);n(this,"allPredictiveSearchInstances",[])}connectedCallback(){super.connectedCallback(),this.input=this.querySelector('input[type="search"]'),this.predictiveSearchResults=this.querySelector("[data-predictive-search]"),this.allPredictiveSearchInstances=document.querySelectorAll("predictive-search"),this.setupEventListeners()}setupEventListeners(){this.input.form.addEventListener("submit",this.onFormSubmit.bind(this)),this.input.addEventListener("focus",this.onFocus.bind(this)),this.input.addEventListener("input",this.onChange.bind(this)),this.addEventListener("focusout",this.onFocusOut.bind(this)),this.addEventListener("keyup",this.onKeyup.bind(this)),this.addEventListener("keydown",this.onKeydown.bind(this)),this.onViewportResize=this.onViewportResize.bind(this),window.addEventListener("resize",this.onViewportResize),window.addEventListener("orientationchange",this.onViewportResize)}disconnectedCallback(){var e;(e=super.disconnectedCallback)==null||e.call(this),window.removeEventListener("resize",this.onViewportResize),window.removeEventListener("orientationchange",this.onViewportResize)}onViewportResize(){this.hasAttribute("open")&&(this.resultsMaxHeight=!1,this.predictiveSearchResults.style.maxHeight=`${this.getResultsMaxHeight()}px`)}get query(){return this.input.value.trim()}onChange(){var i;const e=this.query;if((!this.searchTerm||!e.startsWith(this.searchTerm))&&((i=this.querySelector("#predictive-search-results-groups-wrapper"))==null||i.remove()),this.updateSearchForTerm(this.searchTerm,e),this.searchTerm=e,!this.searchTerm.length){this.close(!0);return}this.getSearchResults(this.searchTerm)}onFormSubmit(e){(!this.query.length||this.querySelector('[aria-selected="true"] a'))&&e.preventDefault()}onFocus(){const e=this.query;e.length&&(this.searchTerm!==e?this.onChange():this.getAttribute("results")==="true"?this.open():this.getSearchResults(this.searchTerm))}onFocusOut(){setTimeout(()=>{this.contains(document.activeElement)||this.close()})}onKeyup(e){switch(this.query.length||this.close(!0),e.preventDefault(),e.code){case"ArrowUp":this.switchOption("up");break;case"ArrowDown":this.switchOption("down");break;case"Enter":this.selectOption();break}}onKeydown(e){(e.code==="ArrowUp"||e.code==="ArrowDown")&&e.preventDefault()}updateSearchForTerm(e,i){var t;const r=(t=this.searchForTextElement)==null?void 0:t.innerText;if(r){if(r.match(new RegExp(e,"g")).length>1)return;const s=r.replace(e,i);this.searchForTextElement.innerText=s}}switchOption(e){if(!this.getAttribute("open"))return;const i=e==="up";this.selectedElement=this.querySelector('[aria-selected="true"]');const r=Array.from(this.querySelectorAll("li, [data-predictive-search-item]")).filter(l=>l.offsetParent!==null);let t=0;if(i&&!this.selectedElement)return;let s=-1,c=0;for(;s===-1&&c<r.length;)r[c]===this.selectedElement&&(s=c),c++;if(!i&&this.selectedElement?t=s===r.length-1?0:s+1:i&&(t=s===0?r.length-1:s-1),t===s)return;const o=r[t];o.setAttribute("aria-selected",!0),this.selectedElement&&this.selectedElement.setAttribute("aria-selected",!1),this.input.setAttribute("aria-activedescendant",o.id)}selectOption(){const e=this.querySelector('[aria-selected="true"] a, button[aria-selected="true"]');e&&e.click()}getSearchResults(e){const i=e.replace(" ","-").toLowerCase();if(this.setLiveRegionLoadingState(),this.cachedResults[i]){this.renderSearchResults(this.cachedResults[i]);return}const r=new URLSearchParams({q:e,section_id:"predictive-search","resources[type]":"query,product,collection,page,article"});fetch(`${window.routes.predictive_search_url}?${r}`,{signal:this.abortController.signal}).then(t=>{if(!t.ok){var s=new Error(t.status);throw this.close(),s}return t.text()}).then(t=>{const s=new DOMParser().parseFromString(t,"text/html").querySelector("#shopify-section-predictive-search").innerHTML;
1this.allPredictiveSearchInstances.forEach(c=>{c.cachedResults[i]=s}),this.renderSearchResults(s)}).catch(t=>{if((t==null?void 0:t.code)!==20)throw this.close(),t})}setLiveRegionLoadingState(){this.loadingText=this.loadingText||this.getAttribute("data-loading-text"),this.$liveRegionUtility.announce({message:this.loadingText}),this.setAttribute("loading",!0)}renderSearchResults(e){this.predictiveSearchResults.innerHTML=e,this.setAttribute("results",!0),this.setLiveRegionResults(),this.open()}setLiveRegionResults(){this.removeAttribute("loading"),this.$liveRegionUtility.announce({message:this.querySelector("[data-predictive-search-live-region-count-value]").textContent})}getResultsMaxHeight(){const e=document.querySelector(".section-header");return e?this.resultsMaxHeight=window.innerHeight-e.getBoundingClientRect().bottom:this.resultsMaxHeight=Math.min(window.innerHeight*.6,400)}open(){this.predictiveSearchResults.style.maxHeight=this.resultsMaxHeight||`${this.getResultsMaxHeight()}px`,this.setAttribute("open",!0),this.input.setAttribute("aria-expanded",!0)}close(e=!1){this.closeResults(e)}closeResults(e=!1){e&&(this.input.value="",this.removeAttribute("results"));const i=this.querySelector('[aria-selected="true"]');i&&i.setAttribute("aria-selected",!1),this.input.setAttribute("aria-activedescendant",""),this.removeAttribute("loading"),this.removeAttribute("open"),this.input.setAttribute("aria-expanded",!1),this.resultsMaxHeight=!1,this.predictiveSearchResults.removeAttribute("style")}}window.customElements.define("predictive-search",g);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.