(()=>{"use strict";!function(){const e="ador_country_selection";function t(e){const t=e.querySelector(".ador-routing-data");if(!t)return[];try{return JSON.parse(t.textContent||"[]")}catch{return[]}}function n(t){try{return sessionStorage.getItem(e+"_"+t)||null}catch{return null}}function r(t,n){try{sessionStorage.setItem(e+"_"+t,n)}catch{}}const o=["a[href]","button:not([disabled])","input:not([disabled])","select:not([disabled])","textarea:not([disabled])",'[tabindex]:not([tabindex="-1"])'].join(", ");function a(e){e.modal.hidden=!1,e.modal.setAttribute("aria-hidden","false"),e.trigger&&e.trigger.setAttribute("aria-expanded","true");const t=e.modal.querySelector('[role="option"]');t&&t.focus(),document.addEventListener("keydown",e.trapHandler)}function i(e){e.modal.hidden=!0,e.modal.setAttribute("aria-hidden","true"),e.trigger&&(e.trigger.setAttribute("aria-expanded","false"),e.trigger.focus()),document.removeEventListener("keydown",e.trapHandler)}function c(e,t){const n=e.wrap.querySelectorAll(".givewp-country-selector-form");let o=null;n.forEach(e=>{e.dataset.routingCountry===t?(o=e,e.style.display="",e.removeAttribute("aria-hidden"),function(e,t){const n=e.querySelector("form");n&&(d(n,"_ador_donor_country",t),d(n,"_ador_account_id",e.dataset.accountId||""))}(e,t)):(e.style.display="none",e.setAttribute("aria-hidden","true"))}),o&&(r(e.instanceId,t),function(e,t,n){const r=new CustomEvent("givewp:country-selected",{bubbles:!0,composed:!0,detail:{instanceId:e.instanceId,countryCode:t,formId:parseInt(n.dataset.formId||"0",10)}});e.wrap.dispatchEvent(r)}(e,t,o))}function d(e,t,n){let r=e.querySelector('input[name="'+t+'"]');r||(r=document.createElement("input"),r.type="hidden",r.name=t,e.appendChild(r)),r.value=n}function u(e,t){const n=t.dataset.country;n&&(e.modal.querySelectorAll('[role="option"]').forEach(e=>{e.setAttribute("aria-selected","false")}),t.setAttribute("aria-selected","true"),i(e),c(e,n))}function l(e){const r=e.dataset.instance||"",d=e.dataset.trigger||"auto",l=e.dataset.defaultCountry||"",s=t(e),f=document.getElementById(r+"-modal");if(!f||!s.length)return;const y=e.querySelector("[data-givewp-country-selector-trigger]"),p={wrap:e,modal:f,trigger:y,routings:s,instanceId:r,trapHandler:null};var m;p.trapHandler=(m=f,function(e){if("Tab"!==e.key)return;const t=function(e){return Array.from(e.querySelectorAll(o)).filter(e=>!e.closest("[hidden]")&&!e.closest('[aria-hidden="true"]'))}(m);if(!t.length)return void e.preventDefault();const n=t[0],r=t[t.length-1];e.shiftKey?document.activeElement===n&&(e.preventDefault(),r.focus()):document.activeElement===r&&(e.preventDefault(),n.focus())}),f.addEventListener("keydown",function(e){"Escape"===e.key&&i(p)});const g=f.querySelector('[data-ador-close="backdrop"]');g&&g.addEventListener("click",function(){i(p)}),f.querySelectorAll('[role="option"]').forEach(function(e){e.addEventListener("click",function(){u(p,e)}),e.addEventListener("keydown",function(e){!function(e,t){if("Enter"===t.key||" "===t.key)t.preventDefault(),u(e,t.currentTarget);else if("Escape"===t.key)i(e);else if("ArrowDown"===t.key){t.preventDefault();const n=Array.from(e.modal.querySelectorAll('[role="option"]')),r=n.indexOf(t.currentTarget);r<n.length-1&&n[r+1].focus()}else if("ArrowUp"===t.key){t.preventDefault();const n=Array.from(e.modal.querySelectorAll('[role="option"]')),r=n.indexOf(t.currentTarget);r>0&&n[r-1].focus()}}(p,e)})}),y&&y.addEventListener("click",function(){a(p)});const v=n(r);if(v)c(p,v);else{if(l&&s.find(e=>e.countryCode===l))return void c(p,l);"auto"===d&&a(p)}}document.addEventListener("DOMContentLoaded",function(){document.querySelectorAll(".givewp-country-selector-wrap").forEach(l)}),window.adorCountrySelector={parseRoutingData:t,getSessionSelection:n,saveSessionSelection:r}}()})();