if(!customElements.get("remex-select")){class e extends HTMLElement{static get observedAttributes(){return["placeholder","icon","name","required","language","search-threshold","modal-threshold","dark","input-background","input-border","input-focused-background","input-focused-border","input-color","input-focused-color"]}static formAssociated=!0;constructor(){super(),this.uniqueSuffix="_"+Math.random().toString(36).substr(2,9),this.attachShadow({mode:"open"}),this._items=[],this._selectedItem=null,this._isOpen=!1,this.language="es",this.container=document.createElement("div"),this.container.classList.add("custom-select-container"),this.searchThreshold=5,this.modalThreshold=10,this.overlay=document.createElement("div"),this.overlay.classList.add(`${this.uniqueSuffix}modal-overlay`),this.overlay.addEventListener("click",(()=>this._closeDropdown())),this._transMap={},this.i18n={en:{Buscar:"Search","Por favor seleccione un valor.":"Please select a value.",Otros:"Others"}}, this._updateInputBackground(this.inputBackground),this._updateInputBorder(this.inputBorder),this._updateInputColor(this.inputColor),this._focusedBackground=this.inputFocusedBackground,this._focusedBorder=this.inputFocusedBorder,this._focusedColor=this.inputFocusedColor, this.shadowRoot.innerHTML=`\n \n `, this._observer=new MutationObserver(this._handleMutations.bind(this)),this._observer.observe(this,{childList:!0,subtree:!0,characterData:!0}),this.selectHeader=document.createElement("div"),this.selectHeader.classList.add(`${this.uniqueSuffix}select-header`),this.selectHeader.innerHTML=``+(this.placeholder||"Selecciona..."),this.selectHeader.addEventListener("click",(()=>this._toggleDropdown())),this.selectDropdown=document.createElement("div"),this.selectDropdown.classList.add(`${this.uniqueSuffix}select-dropdown`),this.dark?(this.selectHeader.classList.add("dark"),this.selectDropdown.classList.add("dark")):(this.selectHeader.classList.remove("dark"),this.selectDropdown.classList.remove("dark")),this.searchInputContainer=document.createElement("div"),this.searchInputContainer.classList.add(`${this.uniqueSuffix}select-search-container`),this.searchInput=document.createElement("input"),this.searchInput.type="text", this.searchInput.placeholder=this.trans("Buscar",null,!0)+"...",this.searchInput.setAttribute='{"placeholder":"Buscar"}',this.searchInput.classList.add(`${this.uniqueSuffix}select-search`),this.searchInput.addEventListener("input",(()=>this._filterOptions())),this.optionsContainer=document.createElement("div"),this.optionsContainer.classList.add(`${this.uniqueSuffix}select-dropdown-content`),this.selectDropdown.appendChild(this.searchInputContainer),this.searchInputContainer.appendChild(this.searchInput),this.selectDropdown.appendChild(this.optionsContainer),this.container.appendChild(this.selectHeader),this.container.appendChild(this.selectDropdown),this.shadowRoot.appendChild(this.container),document.body.appendChild(this.overlay),this._mouseLeaveTimeout=null,this.container.addEventListener("mouseenter",(()=>{this._mouseLeaveTimeout&&(clearTimeout(this._mouseLeaveTimeout),this._mouseLeaveTimeout=null)})),this.container.addEventListener("mouseleave",(()=>{ !window.matchMedia("(max-width: 768px)").matches&&this._isOpen&&(this._mouseLeaveTimeout=setTimeout((()=>{this._closeDropdown()}),500))})),document.addEventListener("click",(e=>{if(this._isOpen){const t=this._getSelectArea(),s=e.clientX,i=e.clientY;(st.right||it.bottom)&&this._closeDropdown()}})),this._internals=this.attachInternals()}isInsideShadowRoot(){const e=new Event("test",{bubbles:!0,composed:!0});return this.dispatchEvent(e),e.composedPath().some((e=>e instanceof ShadowRoot))}_getSelectArea(){const e=this.selectHeader.getBoundingClientRect(),t=this.selectDropdown.getBoundingClientRect();return{top:Math.min(e.top,t.top),bottom:Math.max(e.bottom,t.bottom),left:Math.min(e.left,t.left),right:Math.max(e.right,t.right)}}_parseBorderValue(e){const t=e.split(" ");let s="1px",i="solid";3===t.length?(s=t[0],i=t[1]):2===t.length&&(i=t[0]);let n=e.replace(s,"").replace(i).trim();return 0==n.length&&(n=null),{width:s,style:i,color:n}}_handleFocus(){ if(this.selectHeader){this.selectHeader.style.backgroundColor=this._focusedBackground,this.selectHeader.style.color=this._focusedColor;const{width:e,style:t,color:s}=this._parseBorderValue(this._focusedBorder);this.selectHeader.style.borderWidth=e,this.selectHeader.style.borderStyle=t,this.selectHeader.style.borderColor=s??this.selectHeader.style.borderColor}}_handleBlur(){if(this.selectHeader){this.selectHeader.style.backgroundColor=this.inputBackground,this.selectHeader.style.color=this.inputColor;const{width:e,style:t,color:s}=this._parseBorderValue(this.inputBorder);this.selectHeader.style.borderWidth=e,this.selectHeader.style.borderStyle=t,this.selectHeader.style.borderColor=s??this.selectHeader.style.borderColor}}translateUI(){this.querySelectorAll("i18n").forEach((e=>e.innerHTML=this.trans(this._transMap[e.getAttribute("key")],null,!0)||e.innerHTML)),this.querySelectorAll("[i18n]").forEach((e=>{const t=JSON.parse(e.getAttribute("i18n").replace(/_cm_/g,'"'));for(let s in t){ const i=t[s];void 0!==this._transMap[i]&&e.setAttribute(s,this.trans(this._transMap[i],null,!0)||i)}}))}transKey(e){let t=0;if(!e||0===(e??"").length)return t;for(let s=0;s<(e??"").length;s++)t=(t<<5)-t+e.charCodeAt(s),t&=t;return t=t.toString(),t=/-/.test(t)?t.replace("-","N"):"P"+t,this._transMap[t]=e,t}trans(e,t,s=!1){const i=this.i18n[this.language]||{};let n=void 0!==i[e]?(i[e]??e??"").trim():(e??"").trim();if(void 0!==t)for(var e in t)n=(n?.replace(e,t[e])??"").trim();if(!s){const t=this.transKey(e);return this._transMap[t]=e,`${(n??"").trim()}`}return n.trim()}connectedCallback(){this.selectHeader.innerHTML=``+this.placeholder,this.searchInput.placeholder=this.trans("Buscar",null,!0)+"...",this.hasAttribute("search-threshold")&&(this.searchThreshold=parseInt(this.getAttribute("search-threshold"),10)||5),this.hasAttribute("modal-threshold")&&(this.modalThreshold=parseInt(this.getAttribute("modal-threshold"),10)||10), this._loadHTMLOptions(),window.addEventListener("popstate",(e=>{this._isOpen&&this._closeDropdown()}))}_handleMutations(e){this._loadHTMLOptions()}_loadHTMLOptions(){const e=Array.from(this.getElementsByTagName("option"));if(0===e.length)return;const t=e.map((e=>{const t={};return Array.from(e.attributes).forEach((e=>{["value","disabled"].includes(e.name)||(t[e.name]=e.value)})),{value:e.value||e.textContent,label:e.textContent,disabled:e.disabled,group:e.getAttribute("data-group")||"OPTGROUP"===e.parentElement.tagName?e.parentElement.label:null,attributes:t}}));Array.from(this.getElementsByTagName("optgroup")).forEach((e=>{Array.from(e.getElementsByTagName("option")).forEach((s=>{const i=t.find((e=>e.value===(s.value||s.textContent)));i&&(i.group=e.label)}))})),this._items=t;const s=e.find((e=>e.selected));if(s){const e=t.find((e=>e.value===(s.value||s.textContent)));e&&(this._selectedItem=e,this.selectHeader.innerHTML=``+e.label)} this._cleanupHTMLOptions(),this._renderOptions(this._items)}_cleanupHTMLOptions(){this._observer.disconnect();const e=this.getElementsByTagName("optgroup");for(;e.length>0;)e[0].remove();const t=this.getElementsByTagName("option");for(;t.length>0;)t[0].remove();this._observer.observe(this,{childList:!0,subtree:!0,characterData:!0})}attributeChangedCallback(e,t,s){"placeholder"!==e&&"icon"!==e||(this.selectHeader.innerHTML=``+this.placeholder),"language"===e&&(this.searchInput.placeholder=this.trans("Buscar",null,!0)+"..."),"dark"==e&&("yes"==s||"true"==s?(this.classList.add("dark"),this.selectHeader&&this.selectHeader.classList.add("dark"),this.selectDropdown&&this.selectDropdown.classList.add("dark")):(this.classList.remove("dark"),this.selectHeader&&this.selectHeader.classList.remove("dark"),this.selectDropdown&&this.selectDropdown.classList.remove("dark"))),"required"===e&&this._validate(),"name"===e&&this._internals.setFormValue(this.value,s), "search-threshold"===e&&(this.searchThreshold=parseInt(s||10,10)||5),"modal-threshold"===e&&(this.modalThreshold=parseInt(s||10,10)||10),"modal-threshold"===e&&(this.modalThreshold=parseInt(s||10,10)||10),"input-background"===e&&this._updateInputBackground(s),"input-color"===e&&this._updateInputColor(s),"input-border"===e&&this._updateInputBorder(s),"input-focused-background"===e&&this._updateInputFocusedBackground(s),"input-focused-border"===e&&this._updateInputFocusedBorder(s),"input-focused-color"===e&&this._updateInputFocusedColor(s)}_updateInputBackground(e){this.selectHeader&&(this.selectHeader.style.color=e)}_updateInputBorder(e){this.selectHeader&&(this.selectHeader.style.border=e)}_updateInputFocusedBackground(e){this._focusedBackground=e}_updateInputFocusedBorder(e){this._focusedBorder=e}_updateInputColor(e){this.selectHeader&&(this.selectHeader.style.color=e),this.selectHeader&&(this.selectHeader.style.backgroundColor=e)}_updateInputFocusedColor(e){this._focusedColor=e} _updateInputBackground(e){this.selectHeader&&(this.selectHeader.style.backgroundColor=e)}get inputBackground(){return this.getAttribute("input-background")||(this.dark?"#2b5159":"#fff")}set inputBackground(e){this.setAttribute("input-background",e)}get inputBorder(){return this.getAttribute("input-border")||"none"}set inputBorder(e){this.setAttribute("input-border",e)}get inputFocusedBackground(){return this.getAttribute("input-focused-background")||this.inputBackground}set inputFocusedBackground(e){this.setAttribute("input-focused-background",e)}get inputFocusedBorder(){return this.getAttribute("input-focused-border")||this.inputBorder}set inputFocusedBorder(e){this.setAttribute("input-focused-border",e)}get inputColor(){return this.getAttribute("input-color")||(this.dark?"#fff":"#005f75")}set inputColor(e){this.setAttribute("input-color",e)}get inputFocusedColor(){return this.getAttribute("input-focused-color")||this.inputColor}set inputFocusedColor(e){ this.setAttribute("input-focused-color",e)}set value(e){const t=this._items.find((t=>t.value===e))||null;this._selectedItem=t,this.selectHeader&&(this.selectHeader.innerHTML=t?.label||this.placeholder||""),this._updateFormValue()}get value(){return this._selectedItem?this._selectedItem.value:""}get text(){return this._selectedItem?this._selectedItem.label:""}set items(e){if(!Array.isArray(e))throw new Error("items debe ser un array");this._items=e.map((e=>({...e,value:e.value,label:e.label,img:e.img||null,disabled:e.disabled||!1,group:e.group||null,attributes:e.attributes||{},getAttribute:t=>e.attributes?.[t]||null,hasAttribute:t=>e.attributes?.hasOwnProperty(t)||!1}))),this._selectedItem=null,this.selectHeader.innerHTML=``+(this.placeholder||"Selecciona..."),this._renderOptions(this._items)}get name(){return this.getAttribute("name")||""}get validity(){const e=!this.hasAttribute("required")||""!==this.value&&null!==this.value&&void 0!==this.value ;return{valid:e,valueMissing:!e}}get form(){return this._internals.form}get type(){return"rmselect"}get willValidate(){return!0}get validationMessage(){return this.validity.valueMissing?this.trans("Por favor seleccione un valor.",null,!0):""}get items(){return this._items}get language(){return this.getAttribute("language")??"es"}set language(e){this.setAttribute("language",e)}get placeholder(){return this.getAttribute("placeholder")}get dark(){return/yes|true/.test(this.getAttribute("dark")??"false")}set placeholder(e){this.setAttribute("placeholder",e)}get icon(){return this.getAttribute("icon")??""}set icon(e){this.setAttribute("icon",e)}get options(){return{length:this._items.length,selectedIndex:this.selectedIndex,item:e=>{if(e<0||e>=this._items.length)return null;const t=this._items[e];return{value:t.value,text:t.label,disabled:t.disabled||!1,selected:this._selectedItem?.value===t.value,getAttribute:e=>t.attributes?.[e]||null,hasAttribute:e=>t.attributes?.hasOwnProperty(e)||!1, _originalItem:t}},...Object.fromEntries(this._items.map(((e,t)=>[t,{value:e.value,text:e.label,disabled:e.disabled||!1,selected:this._selectedItem?.value===e.value,getAttribute:t=>e.attributes?.[t]||null,hasAttribute:t=>e.attributes?.hasOwnProperty(t)||!1,_originalItem:e}])))}}get selectedIndex(){return this._items.findIndex((e=>e.value===this.value))}set selectedIndex(e){this.value=this._items[e]?.value??null}get selectedItem(){return this._selectedItem}set selectedItem(e){this._selectedItem=e,this.selectHeader.innerHTML=``+(e?.label||this.placeholder||"Selecciona..."),this._highlightSelectedOption()}get value(){return this._selectedItem?this._selectedItem.value:null}checkValidity(){return this._validate(),this._internals.checkValidity()}reportValidity(){return this._internals.reportValidity()}formResetCallback(){this.value=""}formDisabledCallback(e){this.selectHeader.disabled=e}_updateFormValue(){ this._internals.setFormValue(this.value,this.name),this._validate()}_validate(){this.validity.valid?this._internals.setValidity({}):this._internals.setValidity({valueMissing:!0},this.validationMessage)}_calculateDynamicZIndex(){let e=0;return document.querySelectorAll("*").forEach((t=>{const s=window.getComputedStyle(t).getPropertyValue("z-index");s&&!isNaN(s)&&(e=Math.max(e,parseInt(s,10)))})),e+1}_renderOptions(e){if(this.optionsContainer.innerHTML="",e.length>=this.searchThreshold||this.searchInput.value.length>0?this.searchInput.style.display="block":this.searchInput.style.display="none",!e.some((e=>e.hasOwnProperty("group")&&e.group)))return void e.forEach((e=>{const t=document.createElement("div");if(t.classList.add(`${this.uniqueSuffix}select-option`),"function"==typeof e.render){const s=e.render(e) ;"string"==typeof s?t.innerHTML=s:s instanceof Node?t.appendChild(s):t.innerHTML=e.img?`
${e.label}
`:e.label}else t.innerHTML=e.img?`
${e.label}
`:e.label;e.disabled||!(e.enabled??1)?t.classList.add("disabled"):t.addEventListener("click",(()=>{this._selectedItem=e,this.selectHeader.innerHTML=e.label,this._closeDropdown(),this._updateFormValue(),this.dispatchEvent(new CustomEvent("change",{detail:e})),this._highlightSelectedOption()})),this._selectedItem&&this._selectedItem.value===e.value&&t.classList.add("selected"),this.optionsContainer.appendChild(t)}));const t={},s=[] ;e.forEach((e=>{e.group?(t[e.group]||(t[e.group]=[]),t[e.group].push(e)):s.push(e)})),e.length>=this.searchThreshold?this.searchInput.style.display="block":this.searchInput.style.display="none";const i=(e,t)=>{const s=document.createElement("div");s.classList.add(`${this.uniqueSuffix}select-group-header`),s.style.fontWeight="bold",s.style.padding=e?"8px":0,s.style.fontSize=e?"0.6rem":0,s.textContent=e,this.optionsContainer.appendChild(s),t.forEach((e=>{const t=document.createElement("div");t.classList.add(`${this.uniqueSuffix}select-option`),t.innerHTML=e.img?`
${e.label}
`:e.label,e.disabled?t.classList.add("disabled"):t.addEventListener("click",(()=>{this._selectedItem=e,this.selectHeader.innerHTML=e.label,this._closeDropdown(),this._updateFormValue(), this.dispatchEvent(new CustomEvent("change",{detail:e})),this._highlightSelectedOption()})),this._selectedItem&&this._selectedItem.value===e.value&&t.classList.add("selected"),this.optionsContainer.appendChild(t)}))};s.length&&i(null,s),Object.keys(t).sort().forEach((e=>i(e,t[e])))}_filterOptions(){const e=this.searchInput.value.toLowerCase().trim(),t=this._items.filter((t=>t.label.toLowerCase().includes(e)));this._renderOptions(t)}_highlightSelectedOption(){const e=this.optionsContainer.querySelectorAll(`.${this.uniqueSuffix}select-option`);e.forEach((e=>e.classList.remove("selected")));const t=Array.from(e).find((e=>e.innerHTML===this._selectedItem?.label));t&&t.classList.add("selected")}_toggleDropdown(){this._isOpen=!this._isOpen,this._updateDropdownVisibility(),this._handleFocus()}_closeDropdown(){this._isOpen=!1,this._updateDropdownVisibility(),this._handleBlur()}_updateDropdownVisibility(){if(this._isOpen){history.pushState({selectOpen:!0},"") ;const e=this._calculateDynamicZIndex();this.selectDropdown.style.display="block",this.selectDropdown.style.zIndex=e,window.matchMedia("(max-width: 768px)").matches&&this._items.length>=this.modalThreshold?(this.selectDropdown.classList.add("modal-mode"),this.overlay.style.display="block",this.overlay.style.zIndex=e-1):(this.selectDropdown.classList.remove("modal-mode"),this.overlay.style.display="none"),this.selectHeader.classList.add("expanded"),this.selectDropdown.animate([{opacity:0,transform:"translateY(-5px)"},{opacity:1,transform:"translateY(0)"}],{duration:200,fill:"forwards",easing:"ease-out"}).onfinish=()=>{document.querySelector("body").classList.add("remex-select-open"),this.dispatchEvent(new CustomEvent("open"))},this.searchInput.value="","none"!==this.searchInput.style.display&&this.searchInput.focus(),this._renderOptions(this._items)}else this.dispatchEvent(new CustomEvent("close")),this.overlay.style.display="none",this.selectDropdown.animate([{opacity:1, transform:"translateY(0)"},{opacity:0,transform:"translateY(-5px)"}],{duration:150,fill:"forwards",easing:"ease-in"}).onfinish=()=>{this.selectDropdown.style.display="none",document.querySelector("body").classList.remove("remex-select-open"),this.selectHeader.classList.remove("expanded")}}}customElements.define("remex-select",e),window.RemexSelectClass=e}