import{Segment}from"@dna/analytics";class DNACountrySelector extends HTMLElement{connectedCallback(){this.$countries=this.querySelectorAll(".js-country-selector__item"),this.boundKeyPress=this.handleKeyPress.bind(this),document.addEventListener("dna:country-selector:show",event=>{event.detail.selectorMenu===this?(this.setAttribute("visible",""),this.addEventListener("keydown",this.boundKeyPress),event.detail.selectorButton.setAttribute("aria-expanded",!0)):this.removeAttribute("visible","")}),document.addEventListener("dna:country-selector:hide",()=>{this.removeAttribute("visible"),this.removeEventListener("keydown",this.boundKeyPress);const elements=document.querySelectorAll(".js-toggle-country-selector, .js-toggle-language-selector, .js-mobile-menu__country-selector-icon, .js-footer-country-selector-icon");for(const element of elements)element&&element.setAttribute("aria-expanded","false")}),document.addEventListener("click",event=>{event.target.closest(".js-toggle-country-selector, .js-toggle-language-selector, .js-mobile-menu__country-selector-icon, .js-footer-country-selector-icon")||document.dispatchEvent(new CustomEvent("dna:country-selector:hide"))});for(const $country of this.$countries)$country.addEventListener("click",event=>{const countryCode=$country.dataset.countryCode,localeCode=$country.dataset.localeCode;this.updateCountry(countryCode,localeCode),this.trackCountrySwitcher(event,"switched",countryCode)})}disconnectedCallback(){document.removeEventListener("dna:country-selector:show"),document.removeEventListener("dna:country-selector:hide"),document.removeEventListener("keydown",this.boundKeyPress)}updateCountry(country,locale){const form=document.getElementById("localization_form");if(form){const countrySelect=form.elements.country_code,localeSelect=form.elements.locale_code;country&&(countrySelect.value=country),locale&&(localeSelect.value=locale);const returnToInput=form.querySelector('[name="return_to"]');if(returnToInput){const url=new URL(window.location.href),params=new URLSearchParams(url.search);params.has("country")&¶ms.delete("country"),url.search=params.toString();const updatedSearchParams=url.search?`${url.search}`:"";returnToInput.value=url.pathname+updatedSearchParams,window.history.replaceState({},"",url.toString())}form.submit()}}handleKeyPress(event){const tabPressed=event.key==="Tab"||event.keyCode===9,escapePressed=event.key==="Escape"||event.keyCode===27,enterPressed=event.key==="Enter"||event.keyCode===13;if(escapePressed){document.dispatchEvent(new CustomEvent("dna:country-selector:hide"));return}if(enterPressed){document.activeElement.click();return}if(!tabPressed)return;const firstItem=this.$countries[0],lastItem=this.$countries[this.$countries.length-1];event.shiftKey?document.activeElement===firstItem&&(lastItem.focus(),event.preventDefault()):document.activeElement===lastItem&&(firstItem.focus(),event.preventDefault())}trackCountrySwitcher(eventType,countryCode){Segment.protect(()=>{countryCode&&eventType==="switched"&&Segment.track(Segment.CONSTANTS.EVENTS.COUNTRY_SWITCHED,{selected_country:countryCode})})}}customElements.define("dna-country-selector",DNACountrySelector); //# sourceMappingURL=/cdn/shop/t/1163/assets/component-country-selector.js.map?v=64988471744888602111760566292