var y="is-active",u=["is-active","bg-primary","text-white"],p="data-value",b="form-control-focus",v=new WeakMap,m=class{constructor(t,s={}){v.set(t,this),this.t=t;let i=a=>["true","false","1","0",!0,!1].includes(a)&&!!JSON.parse(a),l={...s,...t.dataset};for(this.allowNew=l.allowNew?i(l.allowNew):!1,this.showAllSuggestions=l.showAllSuggestions?i(l.showAllSuggestions):!1,this.badgeStyle=l.badgeStyle||"primary",this.allowClear=l.allowClear?i(l.allowClear):!1,this.clearEnd=l.clearEnd?i(l.clearEnd):!1,this.server=l.server||!1,this.liveServer=l.liveServer?i(l.liveServer):!1,this.serverParams=l.serverParams||{},typeof this.serverParams=="string"&&(this.serverParams=JSON.parse(this.serverParams)),this.selected=l.selected?l.selected.split(","):[],this.suggestionsThreshold=typeof l.suggestionsThreshold<"u"?parseInt(l.suggestionsThreshold):1,this.validationRegex=l.regex||"",this.separator=l.separator?l.separator.split("|"):[],this.max=l.max?parseInt(l.max):null,this.clearLabel=l.clearLabel||"Clear",this.searchLabel=l.searchLabel||"Type a value",this.valueField=l.valueField||"value",this.labelField=l.labelField||"label",this.keepOpen=l.keepOpen?i(l.keepOpen):!1,this.fullWidth=l.fullWidth?i(l.fullWidth):!1,this.debounceTime=l.debounceTime?parseInt(l.debounceTime):300,this.baseClass=l.baseClass||"",this.placeholder=l.placeholder||this.b(),this.addOnBlur=l.addOnBlur?i(l.addOnBlur):!1,this.h=!1,this.n=!0,this.g=m.debounce(()=>{this.f(!0)},this.debounceTime),this.o=[],this.overflowParent=null,this.parentForm=t.parentElement;this.parentForm&&(this.parentForm.style.overflow==="hidden"&&(this.overflowParent=this.parentForm),this.parentForm=this.parentForm.parentElement,!(this.parentForm&&this.parentForm.nodeName=="FORM")););this.reset=this.reset.bind(this),this.parentForm&&this.parentForm.addEventListener("reset",this.reset),this.i=document.createElement("div"),this.a=document.createElement("div"),this.s=document.createElement("ul"),this.e=document.createElement("input"),this.i.appendChild(this.a),this.a.appendChild(this.e),this.i.appendChild(this.s),this.t.parentNode.insertBefore(this.i,this.t.nextSibling),this.S(),this.w(),this.L(),this.A(),this.C(),this.resetState(),this.server&&!this.liveServer?this.f():this.resetSuggestions()}static init(t="select[multiple]",s={}){let i=document.querySelectorAll(t);for(let l=0;l<i.length;l++)m.getInstance(i[l])||new m(i[l],s)}static getInstance(t){if(v.has(t))return v.get(t)}static debounce(t,s=300){let i;return(...l)=>{clearTimeout(i),i=setTimeout(()=>{t.apply(this,l)},s)}}dispose(){v.delete(this.t),this.t.style.display="block",this.i.parentNode.removeChild(this.i),this.parentForm&&this.parentForm.removeEventListener("reset",this.reset)}resetState(){this.isDisabled()?(this.i.setAttribute("readonly",""),this.e.setAttribute("disabled","")):(this.i.hasAttribute("readonly")&&this.i.removeAttribute("readonly"),this.e.hasAttribute("disabled")&&this.e.removeAttribute("disabled"))}resetSuggestions(){let t=Array.from(this.t.querySelectorAll("option")).filter(s=>!s.disabled).map(s=>({value:s.getAttribute("value"),label:s.textContent}));this.p(t)}f(t=!1){this.d&&this.d.abort(),this.d=new AbortController,this.serverParams.query=this.e.value;let s=new URLSearchParams(this.serverParams).toString();fetch(this.server+"?"+s,{signal:this.d.signal}).then(i=>i.json()).then(i=>{let l=i.data||i;this.p(l),this.d=null,t?this.c():this.l()}).catch(i=>{i.name!=="AbortError"&&console.error(i)})}b(){if(this.t.hasAttribute("placeholder"))return this.t.getAttribute("placeholder");if(this.t.dataset.placeholder)return this.t.dataset.placeholder;let t=this.t.querySelector("option");return t?(t.hasAttribute("selected")&&t.removeAttribute("selected"),t.value?"":t.textContent):""}S(){this.t.style.position="absolute",this.t.style.left="-9999px",this.t.addEventListener("focus",t=>{this.e.focus()})}L(){this.s.classList.add("dropdown-menu","p-0"),this.s.style.maxHeight="280px",this.fullWidth||(this.s.style.maxWidth="360px"),this.s.style.overflowY="auto",this.s.addEventListener("mouseenter",t=>{this.h=!1})}w(){this.i.classList.add("form-control","dropdown"),this.t.classList.contains("form-select-lg")&&this.i.classList.add("form-control-lg"),this.t.classList.contains("form-select-sm")&&this.i.classList.add("form-control-sm"),this.overflowParent&&(this.i.style.position="inherit"),this.m()===4&&(this.i.style.height="auto")}A(){this.a.addEventListener("click",s=>{this.isDisabled()||this.e.style.visibility!="hidden"&&this.e.focus()});let t=this.t.selectedOptions;for(let s=0;s<t.length;s++){let i=t[s];!i.value||(this.o.push(i),this.addItem(i.textContent,i.value))}}C(){this.e.type="text",this.e.autocomplete="off",this.e.spellcheck=!1,this.e.style.backgroundColor="transparent",this.e.style.border=0,this.e.style.outline=0,this.e.style.maxWidth="100%",this.e.ariaLabel=this.searchLabel,this.resetSearchInput(!0),this.e.addEventListener("input",t=>{let s=t.target.value.replace(t.target.dataset.tmp??"","");if(s){let i=s.slice(-1);if(this.separator.length&&this.e.value&&this.separator.includes(i)){this.e.value=this.e.value.slice(0,-1);let l=this.e.value;this.u(l,null);return}}this.r(),this.e.value.length>=this.suggestionsThreshold?this.v():this.l()}),this.e.addEventListener("beforeinput",t=>{t.target.dataset.tmp=t.target.value}),this.e.addEventListener("focus",t=>{this.i.classList.add(b),this.e.value.length>=this.suggestionsThreshold&&this.v()}),this.e.addEventListener("focusout",t=>{let s=this.getActiveSelection(),i={selection:s?s.dataset.value:null,input:this.e.value};this.i.classList.remove(b),this.l(),this.keepOpen&&this.resetSearchInput(),this.addOnBlur&&this.allowNew&&this.canAdd(i.input)&&(this.addItem(i.input),this.resetSearchInput()),this.n&&this.t.dispatchEvent(new CustomEvent("tags.blur",{bubbles:!0,detail:i}))}),this.e.addEventListener("keydown",t=>{let s=t.keyCode||t.key;switch(t.keyCode==229&&(s=e.target.value.charAt(e.target.selectionStart-1).charCodeAt()),s){case 13:case"Enter":t.preventDefault();let i=this.getActiveSelection();if(i)i.click();else if(this.allowNew&&this.e.value){let a=this.e.value;this.u(a,null)}break;case 38:case"ArrowUp":t.preventDefault(),this.h=!0;let l=this.E();this.e.value.length==0&&this.s.classList.contains("show")&&!l&&this.l();break;case 40:case"ArrowDown":t.preventDefault(),this.h=!0,this.x(),this.e.value.length==0&&!this.s.classList.contains("show")&&this.c();break;case 8:case"Backspace":this.e.value.length==0&&(this.removeLastItem(),this.r(),this.l());break;case 27:case"Escape":this.l();break}})}u(t,s=null,i={}){!this.canAdd(t,s)||(this.addItem(t,s,i),this.keepOpen?this.c():this.resetSearchInput())}E(){let t=this.getActiveSelection();if(t){let s=t.parentNode;do s=s.previousSibling;while(s&&s.style.display=="none");return s?(t.classList.remove(...u),s.querySelector("a").classList.add(...u),s.parentNode.scrollTop=s.offsetTop-s.parentNode.offsetTop,s):null}return null}x(){let t=this.getActiveSelection(),s=null;if(t){s=t.parentNode;do s=s.nextSibling;while(s&&s.style.display=="none");return s?(t.classList.remove(...u),s.querySelector("a").classList.add(...u),s.offsetTop>s.parentNode.offsetHeight-s.offsetHeight&&(s.parentNode.scrollTop+=s.offsetHeight),s):null}return s}I(t,s=null){var i=document.createElement("span");document.body.appendChild(i),i.style.fontSize=s||"inherit",i.style.height="auto",i.style.width="auto",i.style.position="absolute",i.style.whiteSpace="no-wrap",i.innerHTML=t;let l=Math.ceil(i.clientWidth)+8;return document.body.removeChild(i),l}r(){this.e.value?this.e.size=this.e.value.length:this.getSelectedValues().length?(this.e.placeholder="",this.e.size=1):(this.e.size=this.placeholder.length>0?this.placeholder.length:1,this.e.placeholder=this.placeholder);let t=this.e.value||this.e.placeholder,s=window.getComputedStyle(this.i).fontSize,i=this.I(t,s);this.e.style.minWidth=i+"px"}p(t){for(;this.s.lastChild;)this.s.removeChild(this.s.lastChild);for(let s=0;s<t.length;s++){let i=t[s];if(!i[this.valueField])continue;if(!this.liveServer&&(i.selected||this.selected.includes(i[this.valueField]))){this.o.push({value:i[this.valueField],textContent:i[this.labelField],dataset:i.data}),this.u(i[this.labelField],i[this.valueField],i.data);continue}let l=document.createElement("li"),a=document.createElement("a");if(l.append(a),a.classList.add("dropdown-item","text-truncate"),a.setAttribute(p,i[this.valueField]),a.setAttribute("href","#"),a.textContent=i[this.labelField],i.data)for(let[n,h]of Object.entries(i.data))a.dataset[n]=h;this.s.appendChild(l),a.addEventListener("mouseenter",n=>{this.h||(this.removeActiveSelection(),l.querySelector("a").classList.add(...u))}),a.addEventListener("mousemove",n=>{this.h=!1}),a.addEventListener("mousedown",n=>{n.preventDefault()}),a.addEventListener("click",n=>{n.preventDefault(),this.u(a.textContent,a.getAttribute(p),a.dataset)})}}reset(){this.removeAll(),this.n=!1;for(let t=0;t<this.o.length;t++){let s=this.o[t];this.addItem(s.textContent,s.value,s.dataset)}this.r(),this.n=!0}resetSearchInput(t=!1){this.e.value="",this.r(),t||(this.l(),this.e===document.activeElement&&this.e.dispatchEvent(new Event("input"))),this.max&&this.getSelectedValues().length>=this.max?this.e.style.visibility="hidden":this.e.style.visibility=="hidden"&&(this.e.style.visibility="visible"),this.isSingle()&&!t&&document.activeElement.blur()}getSelectedValues(){let t=this.t.querySelectorAll("option[selected]");return Array.from(t).map(s=>s.value)}v(){this.liveServer?this.g():this.c()}c(){if(this.e.style.visibility=="hidden")return;let t=this.e.value.toLocaleLowerCase(),s=this.getSelectedValues(),i=this.s.querySelectorAll("li"),l=!1,a=null,n=!1;for(let h=0;h<i.length;h++){let r=i[h],c=r.textContent.toLocaleLowerCase(),o=r.querySelector("a");if(o.classList.remove(...u),s.indexOf(o.getAttribute(p))!=-1){r.style.display="none";continue}n=!0;let d=t.length===0||c.indexOf(t)!==-1;this.showAllSuggestions||this.suggestionsThreshold===0||d?(r.style.display="list-item",l=!0,!a&&d&&(a=r)):r.style.display="none"}if(a||this.showAllSuggestions?(this.i.classList.remove("is-invalid"),a&&(a.querySelector("a").classList.add(...u),a.parentNode.scrollTop=a.offsetTop)):!this.allowNew&&!(t.length===0&&!n)?this.i.classList.add("is-invalid"):this.validationRegex&&this.isInvalid()&&this.i.classList.remove("is-invalid"),!l||this.isInvalid())this.s.classList.remove("show");else if(this.s.classList.add("show"),this.fullWidth)this.s.style.left=-1+"px",this.s.style.width=this.i.offsetWidth+"px";else{let h=this.e.offsetLeft,r=document.body.offsetWidth-1,c=30,o=r-(h+this.s.offsetWidth)-c;o<0&&(h=h+o),this.s.style.left=h+"px";let d=document.body.offsetHeight,f=this.e.getBoundingClientRect().y+window.pageYOffset+this.s.offsetHeight;d-f<0?this.s.style.transform="translateY(calc(-100% - "+c+"px))":this.s.style.transform="none"}}l(){this.s.classList.remove("show"),this.i.classList.remove("is-invalid"),this.removeActiveSelection()}m(){let t=5;return window.jQuery&&$.fn.tooltip!=null&&$.fn.tooltip.Constructor!=null&&(t=parseInt($.fn.tooltip.Constructor.VERSION.charAt(0))),t}k(t){let s=Array.from(this.t.querySelectorAll("option")).find(i=>i.textContent==t);return!!(s&&s.getAttribute("selected"))}F(t){return new RegExp(this.validationRegex.trim()).test(t)}getActiveSelection(){return this.s.querySelector("a."+y)}removeActiveSelection(){let t=this.getActiveSelection();t&&t.classList.remove(...u)}removeAll(){this.getSelectedValues().forEach(s=>{this.removeItem(s,!0)}),this.r()}removeLastItem(t){let s=this.a.querySelectorAll("span");if(!s.length)return;let i=s[s.length-1];this.removeItem(i.getAttribute(p),t)}isDisabled(){return this.t.hasAttribute("disabled")||this.t.disabled||this.t.hasAttribute("readonly")}isInvalid(){return this.i.classList.contains("is-invalid")}isSingle(){return!this.t.hasAttribute("multiple")}canAdd(t,s=null){return s||(s=t),!t||this.isDisabled()||!this.isSingle()&&this.k(t)||this.max&&this.getSelectedValues().length>=this.max?!1:this.validationRegex&&!this.F(t)?(this.i.classList.add("is-invalid"),!1):!0}addItem(t,s=null,i={}){s||(s=t),this.isSingle()&&this.getSelectedValues().length&&this.removeLastItem(!0);let l=this.m(),a=this.t.querySelector('option[value="'+s+'"]');a&&(i=a.dataset);let n=t,h=document.createElement("span"),r=["badge"],c=this.badgeStyle;if(i.badgeStyle&&(c=i.badgeStyle),i.badgeClass&&r.push(...i.badgeClass.split(" ")),this.baseClass?(l===5?r.push("me-2"):r.push("mr-2"),r.push(...this.baseClass.split(" "))):l===5?r=[...r,"me-2","bg-"+c,"mw-100"]:r=[...r,"mr-2","badge-"+c],h.classList.add(...r),h.setAttribute(p,s),this.allowClear){let o=r.includes("text-dark")?"btn-close":"btn-close-white",d,f;this.clearEnd?(d=l===5?"ms-2":"ml-2",f=l===5?"float-end":"float:right;"):(d=l===5?"me-2":"mr-2",f=l===5?"float-start":"float:left;"),n=(l===5?'<button type="button" style="font-size:0.65em" class="'+d+" "+f+" btn-close "+o+'" aria-label="'+this.clearLabel+'"></button>':'<button type="button" style="font-size:1em;'+f+'text-shadow:none;color:currentColor;transform:scale(1.2)" class="'+d+' close" aria-label="'+this.clearLabel+'"><span aria-hidden="true">×</span></button>')+n}if(h.innerHTML=n,this.a.insertBefore(h,this.e),this.allowClear&&h.querySelector("button").addEventListener("click",o=>{o.preventDefault(),o.stopPropagation(),this.isDisabled()||(this.removeItem(s),document.activeElement.blur(),this.r())}),!a){a=document.createElement("option"),a.value=s,a.textContent=t;for(let[o,d]of Object.entries(i))a.dataset[o]=d;this.t.appendChild(a)}a.setAttribute("selected","selected"),a.selected=!0,this.n&&this.t.dispatchEvent(new Event("change",{bubbles:!0}))}removeItem(t,s=!1){let i=this.a.querySelector("span["+p+'="'+t+'"]');if(!i)return;i.remove();let l=this.t.querySelector('option[value="'+t+'"]');l&&(l.removeAttribute("selected"),l.selected=!1,this.n&&!s&&this.t.dispatchEvent(new Event("change",{bubbles:!0}))),this.e.style.visibility=="hidden"&&this.max&&this.getSelectedValues().length<this.max&&(this.e.style.visibility="visible")}},S=m;export{S as default};