;
(self.AMP=self.AMP||[]).push({m:1,v:"2601162341000",n:"amp-selector",ev:"0.1",l:!0,f:function(t,s){(()=>{var s;function e(){return s||(s=Promise.resolve(void 0))}function i(t){return t?Array.prototype.slice.call(t):[]}var{isArray:n}=Array;function r(t,s){if(t.length!==s.length)return!1;for(let e=0;e<t.length;e++)if(t[e]!==s[e])return!1;return!0}var{hasOwnProperty:o,toString:h}=Object.prototype,c="Enter",l="ArrowLeft",a="ArrowUp",u="ArrowRight",d="ArrowDown",p="Home",_="End";function f(t){return"rtl"==(t.body.getAttribute("dir")||t.documentElement.getAttribute("dir")||"ltr")}self.__AMP_LOG=self.__AMP_LOG||{user:null,dev:null,userForEmbed:null};var A=self.__AMP_LOG;function m(t){return function(t,s){throw new Error("failed to call initLogConstructor")}()}function b(t,s,e,i,n,r,o,h,c,l,a){return t}function P(t,s,e,i,n,r,o,h,c,l,a){return(A.user||(A.user=m()),void A.user.win?A.userForEmbed||(A.userForEmbed=m()):A.user).assert(t,s,e,i,n,r,o,h,c,l,a)}function E(t){return t.nodeType?(e=t,s=(e.ownerDocument||e).defaultView,function(t,s){return g(t=function(t){return t.__AMP_TOP||(t.__AMP_TOP=t)}(t),"ampdoc")}(s)).getAmpDoc(t):t;var s,e}function g(t,s){b(v(t,s));const e=function(t){let s=t.__AMP_SERVICES;return s||(s=t.__AMP_SERVICES={}),s}(t)[s];return e.obj||(b(e.ctor),b(e.context),e.obj=new e.ctor(e.context),b(e.obj),e.context=null,e.resolve&&e.resolve(e.obj)),e.obj}function v(t,s){const e=t.__AMP_SERVICES&&t.__AMP_SERVICES[s];return!(!e||!e.ctor)}var w="amp-selector",R="none",T="select",k=class extends t.BaseElement{static prerenderAllowed(){return!0}constructor(t){super(t),this.UH=!1,this.FH=[],this.St=[],this.GH=[],this.$c=null,this.JH=0,this.QH=R}isLayoutSupported(){return!0}buildCallback(){this.$c=function(t,s){const e=function(t){const s=E(t);return s.isSingleDoc()?s.win:s}(E(t));return v(e,s)?g(e,s):null}(this.element,"action"),this.UH=this.element.hasAttribute("multiple"),this.element.hasAttribute("role")||this.element.setAttribute("role","listbox"),this.UH&&this.element.setAttribute("aria-multiselectable","true"),this.element.hasAttribute("disabled")&&this.element.setAttribute("aria-disabled","true");let t=this.element.getAttribute("keyboard-select-mode");var s;t?(t=t.toLowerCase(),P("none"===(s=t)||"focus"===s||"select"===s,`Unknown keyboard-select-mode: ${t}`),P(!(this.UH&&t==T),"[keyboard-select-mode=select] not supported for multiple selection amp-selector")):t=R,this.QH=t,this.registerAction("clear",this.WH.bind(this)),this.sl(),this.element.addEventListener("click",this.AH.bind(this)),this.element.addEventListener("keydown",this.xH.bind(this)),this.registerAction("selectUp",(t=>{const{args:s,trust:e}=t,i=s&&void 0!==s.delta?-s.delta:-1;this.XH(i,e)}),1),this.registerAction("selectDown",(t=>{const{args:s,trust:e}=t,i=s&&void 0!==s.delta?s.delta:1;this.XH(i,e)}),1),this.registerAction("toggle",(t=>{const{args:s,trust:e}=t;return P(s.index>=0,"'index' must be greater than 0"),P(s.index<this.St.length,"'index' must be less than the length of options in the <amp-selector>"),s&&void 0!==s.index?this.S$(s.index,s.value,e):Promise.reject("'index' must be specified")}),1),this.$c.addToAllowlist(w,["clear","selectDown","selectUp","toggle"],["email"]),this.element.addEventListener("amp:dom-update",this.YH.bind(this))}mutatedAttributesCallback(t){const s=t.selected;void 0!==s&&this.ZH(s);const e=t.disabled;void 0!==e&&(e?this.element.setAttribute("aria-disabled","true"):this.element.removeAttribute("aria-disabled"))}ZH(t){let s=Array.isArray(t)?t:[t];if(null===t||0==s.length)return void this.WH();if(this.UH||(s=s.slice(0,1)),r(this.tK().sort(),s.sort()))return;const e=s.reduce(((t,s)=>(t[s]=!0,t)),Object.create(null));for(let t=0;t<this.St.length;t++){const s=this.St[t];e[s.getAttribute("option")]?this.sK(s):this.eK(s)}this.iK(),this.nK()}iK(t){if(this.QH==R)return;this.St.forEach((t=>{t.tabIndex=-1}));let s=t;s||(s=this.UH?this.St[0]:this.FH[0]||this.St[0]),s&&(this.JH=this.St.indexOf(s),s.tabIndex=0)}YH(t){const s=i(this.element.querySelectorAll("[option]"));r(this.St,s)||this.sl(s)}sl(t){this.FH.length=0;const s=t||i(this.element.querySelectorAll("[option]"));s.forEach((t=>{t.hasAttribute("role")||t.setAttribute("role","option"),t.hasAttribute("disabled")&&t.setAttribute("aria-disabled","true"),t.hasAttribute("selected")?this.sK(t):this.eK(t),t.tabIndex=0})),this.St=s,this.iK(),this.nK()}nK(){const t=[],s=this.element.getAttribute("name");if(!s||this.element.hasAttribute("disabled"))return t;const e=this.element.getAttribute("form");this.GH.forEach((t=>{this.element.removeChild(t)})),this.GH=[];const i=this.win.document,n=i.createDocumentFragment();return this.FH.forEach((r=>{if(!r.hasAttribute("disabled")){const o=i.createElement("input"),h=r.getAttribute("option");o.setAttribute("type","hidden"),o.setAttribute("name",s),o.setAttribute("value",h),e&&o.setAttribute("form",e),this.GH.push(o),n.appendChild(o),t.push(h)}})),this.element.appendChild(n),t}rK(t){t.hasAttribute("disabled")||this.mutateElement((()=>{t.hasAttribute("selected")?this.UH&&(this.eK(t),this.nK()):(this.sK(t),this.nK()),this.iK(t),this.oK(t,3)}))}tK(){return this.FH.map((t=>t.getAttribute("option")))}AH(t){if(this.element.hasAttribute("disabled"))return;let s=t.target;s&&(s.hasAttribute("option")||(s=s.closest("[option]")),s&&this.rK(s))}S$(t,s,i){const n=this.St[t],r=n.hasAttribute("selected"),o=void 0!==s?s:!r,h=this.St.indexOf(this.FH[0]);return o===r?e():this.mutateElement((()=>{if(h!==t){this.sK(n);const t=this.St[h];t&&this.eK(t)}else this.eK(n);this.oK(n,i)}))}oK(t,s){const e=function(t,s,e,i){const n={detail:e};return Object.assign(n,void 0),new t.CustomEvent("amp-selector.select",n)}(this.win,0,{"targetOption":t.getAttribute("option"),"selectedOptions":this.tK()});this.$c.trigger(this.element,"select",e,s)}XH(t,s){const e=this.St.indexOf(this.FH[0]),i=(n=-1===e&&t<0?t:e+t,r=this.St.length,n>0&&r>0?n%r:(n%r+r)%r);var n,r;const o=this.St[i];this.sK(o);const h=this.St[e];h&&this.eK(h),this.nK(),this.oK(o,s)}xH(t){if(this.element.hasAttribute("disabled"))return e();const{key:s}=t;switch(s){case l:case a:case u:case d:case p:case _:return this.QH!=R?this.wH(t):e();case c:case" ":return this.hK(t),e()}return e()}wH(t){const s=this.win.document;let i=0;switch(t.key){case l:i=f(s)?1:-1;break;case a:i=-1;break;case u:i=f(s)?-1:1;break;case d:case p:i=1;break;case _:i=-1;break;default:return e()}return t.preventDefault(),this.St[this.JH].tabIndex=-1,this.cK().then((s=>{const e=this.JH;switch(t.key){case p:this.JH=this.St.length-1;break;case _:this.JH=0}do{this.JH=(this.JH+i)%this.St.length,this.JH<0&&(this.JH=this.JH+this.St.length)}while(I(this.St[this.JH],s[this.JH])&&this.JH!=e);const n=this.St[this.JH];n.tabIndex=0,function(t){try{t.focus()}catch(t){}}(n);const r=this.St[this.JH];this.QH==T&&this.rK(r)}))}hK(t){const{key:s}=t;if((" "==s||s==c)&&this.St.includes(t.target)){t.preventDefault();const s=t.target;this.rK(s)}}eK(t){t.removeAttribute("selected"),t.setAttribute("aria-selected","false");const s=this.FH.indexOf(t);-1!==s&&this.FH.splice(s,1)}WH(){for(;this.FH.length>0;){const t=this.FH.pop();this.eK(t)}this.nK()}sK(t){this.FH.includes(t)||(this.UH||this.WH(),t.setAttribute("selected",""),t.setAttribute("aria-selected","true"),this.FH.push(t))}getElementsForTesting(){return this.St}getSelectedElementsForTesting(){return this.FH}cK(){return this.measureElement((()=>this.St.map((t=>t.getBoundingClientRect()))))}};function I(t,s){const{height:e,width:i}=s;return t.hidden||0==i||0==e}t.registerElement(w,k,"amp-selector [option]{cursor:pointer}amp-selector [option][selected]{cursor:auto;outline:1px solid rgba(0,0,0,.7)}amp-selector[multiple] [option][selected]{cursor:pointer;outline:1px solid rgba(0,0,0,.7)}amp-selector [disabled][option],amp-selector [selected][disabled],amp-selector[disabled] [option],amp-selector[disabled] [selected]{cursor:auto;outline:none}\n/*# sourceURL=/extensions/amp-selector/0.1/amp-selector.css*/")})();
/*! https://mths.be/cssescape v1.5.1 by @mathias | MIT license */}});
//# sourceMappingURL=amp-selector-0.1.mjs.map