(function(){"use strict";function y(n,i){for(var o=0;o<i.length;o++){var t=i[o];t.enumerable=t.enumerable||!1,t.configurable=!0,"value"in t&&(t.writable=!0),Object.defineProperty(n,S(t.key),t)}}function b(n,i,o){return i&&y(n.prototype,i),o&&y(n,o),Object.defineProperty(n,"prototype",{writable:!1}),n}function _(n,i){n.prototype=Object.create(i.prototype),n.prototype.constructor=n,f(n,i)}function p(n){return p=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(o){return o.__proto__||Object.getPrototypeOf(o)},p(n)}function f(n,i){return f=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},f(n,i)}function I(){if(typeof Reflect>"u"||!Reflect.construct||Reflect.construct.sham)return!1;if(typeof Proxy=="function")return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch{return!1}}function v(n,i,o){return I()?v=Reflect.construct.bind():v=function(e,r,s){var a=[null];a.push.apply(a,r);var l=Function.bind.apply(e,a),c=new l;return s&&f(c,s.prototype),c},v.apply(null,arguments)}function w(n){return Function.toString.call(n).indexOf("[native code]")!==-1}function m(n){var i=typeof Map=="function"?new Map:void 0;return m=function(t){if(t===null||!w(t))return t;if(typeof t!="function")throw new TypeError("Super expression must either be null or a function");if(typeof i<"u"){if(i.has(t))return i.get(t);i.set(t,e)}function e(){return v(t,arguments,p(this).constructor)}return e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),f(e,t)},m(n)}function C(n,i){if(typeof n!="object"||n===null)return n;var o=n[Symbol.toPrimitive];if(o!==void 0){var t=o.call(n,i||"default");if(typeof t!="object")return t;throw new TypeError("@@toPrimitive must return a primitive value.")}return(i==="string"?String:Number)(n)}function S(n){var i=C(n,"string");return typeof i=="symbol"?i:String(i)}/** * @copyright (C) 2018 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */window.customElements.define("joomla-field-fancy-select",function(n){_(i,n);function i(){var t;if(t=n.call(this)||this,t.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 t.choicesCache={},t.activeXHR=null,t.choicesInstance=null,t.isDisconnected=!1,t}var o=i.prototype;return o.connectedCallback=function(){var e=this;if(window.Choices||document.readyState==="complete")this.doConnect();else{var r=function s(){e.doConnect(),window.removeEventListener("load",s)};window.addEventListener("load",r)}},o.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.choicesInstance.setChoiceByValue(this.disconnectValues),this.isDisconnected=!1);return}if(this.isDisconnected=!1,this.select.multiple&&this.placeholder){var r=document.createElement("option");r.setAttribute("placeholder",""),r.textContent=this.placeholder,this.select.appendChild(r)}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 s=this.choicesInstance._highlightChoice;this.choicesInstance._highlightChoice=function(c){c&&s.call(e.choicesInstance,c)},this.addEventListener("mouseleave",function(){if(e.choicesInstance.dropdown.isActive){var c=Array.from(e.choicesInstance.dropdown.element.querySelectorAll("."+e.choicesInstance.config.classNames.highlightedState));c.forEach(function(u){u.classList.remove(e.choicesInstance.config.classNames.highlightedState),u.setAttribute("aria-selected","false")}),e.choicesInstance._highlightPosition=0}}),this.addEventListener("keydown",function(c){if(!(c.keyCode!==e.keyCode.ENTER||c.target!==e.choicesInstance.input.element)&&(c.preventDefault(),!(e.choicesInstance._highlightPosition||!c.target.value))){var u=e.choicesInstance.dropdown.element.querySelector("."+e.choicesInstance.config.classNames.highlightedState);if(!u){var g=c.target.value.toLowerCase(),d=!1;if(e.choicesInstance.config.choices.some(function(h){return h.value.toLowerCase()===g||h.label.toLowerCase()===g?(d=h.value,!0):!1}),d===!1&&Object.keys(e.choicesCache).some(function(h){return h.toLowerCase()===g||e.choicesCache[h].toLowerCase()===g?(d=h,!0):!1}),d!==!1){e.choicesInstance.setChoiceByValue(d),c.target.value=null,e.choicesInstance.hideDropdown();return}e.choicesInstance.setChoices([{value:e.newItemPrefix+c.target.value,label:c.target.value,selected:!0,customProperties:{value:c.target.value}}],"value","label",!1),e.choicesCache[c.target.value]=c.target.value,c.target.value=null,e.choicesInstance.hideDropdown()}}})}if(this.remoteSearch&&this.url){this.choicesInstance.config.choices.forEach(function(c){e.choicesCache[c.value]=c.label});var a=300,l=null;this.select.addEventListener("search",function(){clearTimeout(l),l=setTimeout(e.requestLookup.bind(e),a)})}},o.disconnectedCallback=function(){this.choicesInstance&&(this.disconnectValues=this.choicesInstance.getValue(!0),this.choicesInstance.destroy(),this.isDisconnected=!0),this.activeXHR&&(this.activeXHR.abort(),this.activeXHR=null)},o.requestLookup=function(){var e=this,r=this.url;r+=r.indexOf("?")===-1?"?":"&",r+=encodeURIComponent(this.termKey)+"="+encodeURIComponent(this.choicesInstance.input.value),this.activeXHR&&this.activeXHR.abort(),this.activeXHR=Joomla.request({url:r,onSuccess:function(a){e.activeXHR=null;var l=a?JSON.parse(a):[];if(l.length){for(var c,u=l.length-1;u>=0;u--)c=l[u],c.value=""+c.value,e.choicesCache[c.value]?l.splice(u,1):e.choicesCache[c.value]=c.text;l.length&&e.choicesInstance.setChoices(l,"value","text",!1)}},onError:function(){e.activeXHR=null}})},o.disableAllOptions=function(){var e=this.choicesInstance._store.choices;e.forEach(function(r,s){e[s].disabled=!0,e[s].selected=!1}),this.choicesInstance.clearStore(),this.choicesInstance.setChoices(e,"value","label",!0)},o.enableAllOptions=function(){var e=this.choicesInstance._store.choices,r=this.choicesInstance.getValue(!0);e.forEach(function(s,a){e[a].disabled=!1}),this.choicesInstance.clearStore(),this.choicesInstance.setChoices(e,"value","label",!0),this.value=r},o.disableByValue=function(e){var r=this.choicesInstance._store.choices,s=this.choicesInstance.getValue(!0);r.forEach(function(l,c){l.value===e&&(r[c].disabled=!0,r[c].selected=!1)});var a=s.indexOf(e);a>-1&&s.slice(a,1),this.choicesInstance.clearStore(),this.choicesInstance.setChoices(r,"value","label",!0),this.value=s},o.enableByValue=function(e){var r=this.choicesInstance._store.choices,s=this.choicesInstance.getValue(!0);r.forEach(function(a,l){a.value===e&&(r[l].disabled=!1)}),this.choicesInstance.clearStore(),this.choicesInstance.setChoices(r,"value","label",!0),this.value=s},b(i,[{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)}}]),i}(m(HTMLElement)))})();