import{Component}from"@theme/component";import{isClickedOutside,normalizeString,onAnimationEnd}from"@theme/utilities";class LocalizationFormComponent extends Component{connectedCallback(){super.connectedCallback(),this.refs.search&&this.refs.search.addEventListener("keydown",this.#onSearchKeyDown),this.refs.countryList&&this.refs.countryList.addEventListener("keydown",this.#onContainerKeyDown),this.refs.countryList&&this.refs.countryList.addEventListener("scroll",this.#onCountryListScroll),this.resizeLanguageInput()}disconnectedCallback(){super.disconnectedCallback(),this.refs.search&&this.refs.search.removeEventListener("keydown",this.#onSearchKeyDown),this.refs.countryList&&this.refs.countryList.removeEventListener("keydown",this.#onContainerKeyDown),this.refs.countryList&&this.refs.countryList.removeEventListener("scroll",this.#onCountryListScroll)}#onContainerKeyDown=event=>{const{countryInput,countryListItems,form}=this.refs;switch(event.key){case"ArrowUp":event.preventDefault(),event.stopPropagation(),this.#changeCountryFocus("UP");break;case"ArrowDown":event.preventDefault(),event.stopPropagation(),this.#changeCountryFocus("DOWN");break;case"Enter":{event.preventDefault(),event.stopPropagation();const focusedItem=countryListItems.find(item=>item.getAttribute("aria-selected")==="true");focusedItem&&(countryInput.value=focusedItem.dataset.value??"",form.submit());break}}this.refs.search&&setTimeout(()=>{const focusableItems=this.refs.countryListItems.filter(item=>!item.hasAttribute("hidden")),focusedItemIndex=focusableItems.findIndex(item=>item===document.activeElement),focusedItem=focusableItems[focusedItemIndex];focusedItem?this.refs.search.setAttribute("aria-activedescendant",focusedItem.id):this.refs.search.setAttribute("aria-activedescendant","")})};selectCountry=(countryName,event)=>{event.preventDefault();const{countryInput,form}=this.refs;countryInput.value=countryName,form?.submit()};changeLanguage(event){const{form,languageInput}=this.refs,value=event.target instanceof HTMLSelectElement?event.target.value:null;value&&(languageInput.value=value,this.resizeLanguageInput(),form.submit())}resizeLanguageInput(){const{languageInput}=this.refs;if(!languageInput)return;for(const option of languageInput.options)option.selected||(option.dataset.optionLabel=option.textContent||"",option.innerText="");languageInput.style.width="fit-content";const originalElementWidth=`${Math.ceil(languageInput.offsetWidth)+1}px`;languageInput.offsetWidth>0&&(languageInput.style.width=originalElementWidth);for(const option of languageInput.options)option.dataset.optionLabel&&(option.textContent=option.dataset.optionLabel,delete option.dataset.optionLabel)}#findMatches(searchValue,countryEl,options={matchLabel:!0,matchAlias:!0,matchIso:!0,matchCurrency:!0,labelMatchStart:!1,aliasExactMatch:!1}){let matchTypes={};const{aliases,value:iso}=countryEl.dataset;if(options.matchLabel){const countryName=normalizeString(countryEl.querySelector(".country")?.textContent??"");if(!countryName)return matchTypes;matchTypes.label=options.labelMatchStart?countryName.startsWith(searchValue):countryName.includes(searchValue)}if(options.matchCurrency){const currency=normalizeString(countryEl.querySelector(".localization-form__currency")?.textContent??"");matchTypes.currency=currency.includes(searchValue)}if(options.matchIso&&(matchTypes.iso=normalizeString(iso??"")==searchValue),options.matchAlias){const countryAliases=aliases?.split(",").map(alias=>normalizeString(alias));if(!countryAliases)return matchTypes;matchTypes.alias=countryAliases.length>0&&countryAliases.find(alias=>options.aliasExactMatch?alias===searchValue:alias.startsWith(searchValue))!==void 0}return matchTypes}#highlightMatches(text,searchValue){if(!text||!searchValue)return text??"";const normalizedText=normalizeString(text),normalizedSearch=normalizeString(searchValue),startIndex=normalizedText.indexOf(normalizedSearch);if(startIndex===-1)return text;const endIndex=startIndex+normalizedSearch.length,before=text.slice(0,startIndex),match=text.slice(startIndex,endIndex),after=text.slice(endIndex);let result="";return before&&(result+=`${before}`),result+=match,after&&(result+=`${after}`),result}filterCountries(){const{countryList,countryListItems,liveRegion,noResultsMessage,popularCountries,resetButton,search}=this.refs,{labelResultsCount}=this.dataset,searchValue=normalizeString(search.value);let countVisibleCountries=0;resetButton.toggleAttribute("hidden",!searchValue),popularCountries&&popularCountries.toggleAttribute("hidden",!!searchValue);const wrapper=this.querySelector(".country-selector-form__wrapper");wrapper&&wrapper.classList.toggle("is-searching",!!searchValue);for(const countryEl of countryListItems)if(searchValue===""){countryEl.removeAttribute("hidden");const countrySpan=countryEl.querySelector(".country");countrySpan&&(countrySpan.textContent=countrySpan.textContent),countVisibleCountries++}else{const matches=this.#findMatches(searchValue,countryEl);if(matches.label||matches.alias||matches.iso||matches.currency){countryEl.removeAttribute("hidden");const countrySpan=countryEl.querySelector(".country");countrySpan&&(countrySpan.innerHTML=this.#highlightMatches(countrySpan.textContent,searchValue)),countVisibleCountries++}else countryEl.setAttribute("hidden","")}liveRegion&&labelResultsCount&&(liveRegion.innerText=labelResultsCount.replace("[count]",`${countVisibleCountries}`)),noResultsMessage.hidden=countVisibleCountries>0,countryList.scrollTop=0}#changeCountryFocus(direction){const{countryListItems}=this.refs,focusableItems=countryListItems.filter(item=>!item.hasAttribute("hidden")),focusedItemIndex=focusableItems.findIndex(item=>item===document.activeElement),focusedItem=focusableItems[focusedItemIndex];let itemToFocus;direction==="UP"?itemToFocus=focusedItemIndex>0?focusableItems[focusedItemIndex-1]:focusableItems[focusableItems.length-1]:itemToFocus=focusedItemIndex{if(event.key==="Enter"){event.preventDefault(),event.stopPropagation();return}this.#onContainerKeyDown(event)};resetForm(){const{search}=this.refs;search&&search.value!=""&&(search.value="",this.filterCountries(),search.setAttribute("aria-activedescendant",""))}focusSearchInput=()=>{const{search}=this.refs;search?.focus()};#onCountryListScroll=event=>{const countryFilter=this.querySelector(".country-filter"),countryList=event.target instanceof HTMLElement?event.target:null;if(countryFilter&&countryList){const shouldShowBorder=countryList.scrollTop>0;countryFilter.classList.toggle("is-scrolled",shouldShowBorder)}}}class DropdownLocalizationComponent extends Component{get isHidden(){return this.refs.panel.hasAttribute("hidden")}toggleSelector(){return this.isHidden?this.showPanel():this.hidePanel()}showPanel(){this.isHidden&&(this.addEventListener("keyup",this.#handleKeyUp),document.addEventListener("click",this.#handleClickOutside),this.refs.panel.removeAttribute("hidden"),this.refs.button.setAttribute("aria-expanded","true"),onAnimationEnd(this.refs.panel,()=>{this.#updateWidth(),this.refs.localizationForm?.focusSearchInput()}))}hidePanel=()=>{this.isHidden||(this.removeEventListener("keyup",this.#handleKeyUp),document.removeEventListener("click",this.#handleClickOutside),this.refs.button?.setAttribute("aria-expanded","false"),this.refs.panel.setAttribute("hidden",""),this.refs.localizationForm?.resetForm())};#handleClickOutside=event=>{isClickedOutside(event,this)&&this.hidePanel()};#updateWidth(){this.style.setProperty("--width",`${this.refs.localizationForm.offsetWidth}px`)}#handleKeyUp=event=>{switch(event.key){case"Escape":this.hidePanel(),event.stopPropagation(),this.refs.button?.focus();break}}}class DrawerLocalizationComponent extends Component{toggle(event){const{target}=event,{localizationForm}=this.refs;if(!localizationForm||!(target instanceof HTMLDetailsElement))return;const countryList=localizationForm.querySelector(".country-selector-form__wrapper");target.open?(countryList&&countryList.addEventListener("scroll",this.#onCountryListScroll),onAnimationEnd(target,localizationForm.focusSearchInput)):(countryList?.removeEventListener("scroll",this.#onCountryListScroll),localizationForm.resetForm())}#onCountryListScroll=event=>{const countryFilter=this.querySelector(".country-filter"),countryList=event.target instanceof HTMLElement?event.target:null;if(countryFilter&&countryList){const shouldShowBorder=countryList.scrollTop>0;countryFilter.classList.toggle("is-scrolled",shouldShowBorder)}}}customElements.get("localization-form-component")||customElements.define("localization-form-component",LocalizationFormComponent),customElements.get("dropdown-localization-component")||customElements.define("dropdown-localization-component",DropdownLocalizationComponent),customElements.get("drawer-localization-component")||customElements.define("drawer-localization-component",DrawerLocalizationComponent); //# sourceMappingURL=/cdn/shop/t/47/assets/localization.js.map?v=87759161307859666401757451905