!function(){"use strict";function e(e,t){for(var n=0;n<t.length;n++){var c=t[n];c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(e,(o=c.key,i=void 0,"symbol"==typeof(i=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var c=n.call(e,t||"default");if("object"!=typeof c)return c;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(o,"string"))?i:String(i)),c)}var o,i}function t(e){return t=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},t(e)}function n(e,t){return n=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},n(e,t)}function c(e,t,o){return c=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(e){return!1}}()?Reflect.construct.bind():function(e,t,c){var o=[null];o.push.apply(o,t);var i=new(Function.bind.apply(e,o));return c&&n(i,c.prototype),i},c.apply(null,arguments)}function o(e){var i="function"==typeof Map?new Map:void 0;return o=function(e){if(null===e||(o=e,-1===Function.toString.call(o).indexOf("[native code]")))return e;var o;if("function"!=typeof e)throw new TypeError("Super expression must either be null or a function");if(void 0!==i){if(i.has(e))return i.get(e);i.set(e,s)}function s(){return c(e,arguments,t(this).constructor)}return s.prototype=Object.create(e.prototype,{constructor:{value:s,enumerable:!1,writable:!0,configurable:!0}}),n(s,e)},o(e)}window.customElements.define("joomla-field-fancy-select",function(t){var c,o;function i(){var e;if((e=t.call(this)||this).keyCode={ENTER:13},!Joomla)throw new Error("Joomla API is not properly initiated");if(!window.Choices)throw new Error("JoomlaFieldFancySelect requires Choices.js to work");return e.choicesCache={},e.activeXHR=null,e.choicesInstance=null,e.isDisconnected=!1,e}o=t,(c=i).prototype=Object.create(o.prototype),c.prototype.constructor=c,n(c,o);var s,r,a,l=i.prototype;return l.connectedCallback=function(){var e=this;if(window.Choices||"complete"===document.readyState)this.doConnect();else{window.addEventListener("load",(function t(){e.doConnect(),window.removeEventListener("load",t)}))}},l.doConnect=function(){var e=this;if(this.select=this.querySelector("select"),!this.select)throw new Error("JoomlaFieldFancySelect requires <select> element to work");if(this.choicesInstance)this.isDisconnected&&(this.choicesInstance.init(),this.isDisconnected=!1);else{if(this.isDisconnected=!1,this.select.multiple&&this.placeholder){var t=document.createElement("option");t.setAttribute("placeholder",""),t.textContent=this.placeholder,this.select.appendChild(t)}if(this.choicesInstance=new Choices(this.select,{placeholderValue:this.placeholder,searchPlaceholderValue:this.searchPlaceholder,removeItemButton:!0,searchFloor:this.minTermLength,searchResultLimit:parseInt(this.select.dataset.maxResults,10)||10,renderChoiceLimit:parseInt(this.select.dataset.maxRender,10)||-1,shouldSort:!1,fuseOptions:{threshold:.3},noResultsText:Joomla.Text._("JGLOBAL_SELECT_NO_RESULTS_MATCH","No results found"),noChoicesText:Joomla.Text._("JGLOBAL_SELECT_NO_RESULTS_MATCH","No results found"),itemSelectText:Joomla.Text._("JGLOBAL_SELECT_PRESS_TO_SELECT","Press to select"),classNames:{button:"choices__button_joomla"}}),this.allowCustom){var n=this.choicesInstance._highlightChoice;this.choicesInstance._highlightChoice=function(t){t&&n.call(e.choicesInstance,t)},this.addEventListener("mouseleave",(function(){e.choicesInstance.dropdown.isActive&&(Array.from(e.choicesInstance.dropdown.element.querySelectorAll("."+e.choicesInstance.config.classNames.highlightedState)).forEach((function(t){t.classList.remove(e.choicesInstance.config.classNames.highlightedState),t.setAttribute("aria-selected","false")})),e.choicesInstance._highlightPosition=0)})),this.addEventListener("keydown",(function(t){if(t.keyCode===e.keyCode.ENTER&&t.target===e.choicesInstance.input.element&&(t.preventDefault(),!e.choicesInstance._highlightPosition&&t.target.value&&!e.choicesInstance.dropdown.element.querySelector("."+e.choicesInstance.config.classNames.highlightedState))){var n=t.target.value.toLowerCase(),c=!1;if(e.choicesInstance.config.choices.some((function(e){return(e.value.toLowerCase()===n||e.label.toLowerCase()===n)&&(c=e.value,!0)})),!1===c&&Object.keys(e.choicesCache).some((function(t){return(t.toLowerCase()===n||e.choicesCache[t].toLowerCase()===n)&&(c=t,!0)})),!1!==c)return e.choicesInstance.setChoiceByValue(c),t.target.value=null,void e.choicesInstance.hideDropdown();e.choicesInstance.setChoices([{value:e.newItemPrefix+t.target.value,label:t.target.value,selected:!0,customProperties:{value:t.target.value}}],"value","label",!1),e.choicesCache[t.target.value]=t.target.value,t.target.value=null,e.choicesInstance.hideDropdown()}}))}if(this.remoteSearch&&this.url){this.choicesInstance.config.choices.forEach((function(t){e.choicesCache[t.value]=t.label}));var c=null;this.select.addEventListener("search",(function(){clearTimeout(c),c=setTimeout(e.requestLookup.bind(e),300)}))}}},l.disconnectedCallback=function(){this.choicesInstance&&(this.choicesInstance.destroy(),this.isDisconnected=!0),this.activeXHR&&(this.activeXHR.abort(),this.activeXHR=null)},l.requestLookup=function(){var e=this,t=this.url;t+=-1===t.indexOf("?")?"?":"&",t+=encodeURIComponent(this.termKey)+"="+encodeURIComponent(this.choicesInstance.input.value),this.activeXHR&&this.activeXHR.abort(),this.activeXHR=Joomla.request({url:t,onSuccess:function(t){e.activeXHR=null;var n=t?JSON.parse(t):[];if(n.length){for(var c,o=n.length-1;o>=0;o--)(c=n[o]).value=""+c.value,e.choicesCache[c.value]?n.splice(o,1):e.choicesCache[c.value]=c.text;n.length&&e.choicesInstance.setChoices(n,"value","text",!1)}},onError:function(){e.activeXHR=null}})},l.disableAllOptions=function(){var e=this.choicesInstance._store.choices;e.forEach((function(t,n){e[n].disabled=!0,e[n].selected=!1})),this.choicesInstance.clearStore(),this.choicesInstance.setChoices(e,"value","label",!0)},l.enableAllOptions=function(){var e=this.choicesInstance._store.choices,t=this.choicesInstance.getValue(!0);e.forEach((function(t,n){e[n].disabled=!1})),this.choicesInstance.clearStore(),this.choicesInstance.setChoices(e,"value","label",!0),this.value=t},l.disableByValue=function(e){var t=this.choicesInstance._store.choices,n=this.choicesInstance.getValue(!0);t.forEach((function(n,c){n.value===e&&(t[c].disabled=!0,t[c].selected=!1)}));var c=n.indexOf(e);c>-1&&n.slice(c,1),this.choicesInstance.clearStore(),this.choicesInstance.setChoices(t,"value","label",!0),this.value=n},l.enableByValue=function(e){var t=this.choicesInstance._store.choices,n=this.choicesInstance.getValue(!0);t.forEach((function(n,c){n.value===e&&(t[c].disabled=!1)})),this.choicesInstance.clearStore(),this.choicesInstance.setChoices(t,"value","label",!0),this.value=n},s=i,(r=[{key:"allowCustom",get:function(){return this.hasAttribute("allow-custom")}},{key:"remoteSearch",get:function(){return this.hasAttribute("remote-search")}},{key:"url",get:function(){return this.getAttribute("url")}},{key:"termKey",get:function(){return this.getAttribute("term-key")||"term"}},{key:"minTermLength",get:function(){return parseInt(this.getAttribute("min-term-length"),10)||1}},{key:"newItemPrefix",get:function(){return this.getAttribute("new-item-prefix")||""}},{key:"placeholder",get:function(){return this.getAttribute("placeholder")}},{key:"searchPlaceholder",get:function(){return this.getAttribute("search-placeholder")}},{key:"value",get:function(){return this.choicesInstance.getValue(!0)},set:function(e){this.choicesInstance.setChoiceByValue(e)}}])&&e(s.prototype,r),a&&e(s,a),Object.defineProperty(s,"prototype",{writable:!1}),i}(o(HTMLElement)))}();