"use strict";
/**
* Name: HaziJs Select2
* Desc: A Simple and Lightweight JavScript Framework.
* version: 1.0.0
* Package: @HaziJs
* Author: https://github.com/golaphazi
* Developer: Hazi
*/

var $hJsSelect2={init:function(e=".hjs-select2"){let t,a,l=document.querySelectorAll(e);for(let i of l){if(i.classList.contains("has-hjs-select2"))continue;let s=document.createElement("div"),n=document.createElement("div"),r=document.createElement("datalist");i.querySelectorAll("optgroup");let d=document.createElement("span");d.setAttribute("class","hjs-select2-display");let o=document.createElement("span");o.setAttribute("class","hjs-select2-input"),o.setAttribute("contenteditable",!0),d.appendChild(o);let u=i.options,c=i.parentElement,b=i.hasAttribute("multiple");i.classList.add("has-hjs-select2");let h=function(e){let t=this.hasAttribute("data-disabled"),l=s.querySelectorAll("select option"),i=this.dataset.value;this.dataset.label;let n=this.dataset.index;if(!t){if(b?l.forEach(e=>{let t=e.value?e.value:e.innerText;if(e.hasAttribute("tabindex")&&(t=e.getAttribute("tabindex"),i=n),t==i){let a=e.hasAttribute("selected");a?e.removeAttribute("selected"):e.setAttribute("selected","")}}):l.forEach(e=>{let t=e.value?e.value:e.innerText;e.hasAttribute("tabindex")&&(t=e.getAttribute("tabindex"),i=n),t==i?e.setAttribute("selected",""):e.removeAttribute("selected")}),b){if(e.shiftKey){let r=this.hasAttribute("data-checked");r?this.removeAttribute("data-checked"):this.setAttribute("data-checked","")}else{let d=this.hasAttribute("data-checked");d?this.removeAttribute("data-checked"):this.setAttribute("data-checked","")}}else{let u=s.querySelectorAll(".option");for(a=0;a<u.length;a++){let c=u[a];c.removeAttribute("data-checked")}this.setAttribute("data-checked","")}o.innerHTML="",f(),A()}},p=function(e){e.preventDefault(),e.stopPropagation(),13===e.keyCode&&this.click()},A=function(){let e=i.querySelectorAll("option[selected]");if(d.innerHTML="",e.length>0){let t=0;e.forEach(e=>{let a=e.hasAttribute("selected");if(a){let l=e.innerText.trim();if(""==l&&(l=e.value?e.value:""),e.hasAttribute("tabindex")){let n=e.getAttribute("tabindex"),r=s.querySelector('datalist div.option[data-index="'+n+'"]');if(r&&r.hasAttribute("data-label")){let o=r.getAttribute("data-label");""!=o.trim()&&(l=o)}}let u=document.createElement("span");u.innerText=l.trim(),u.tabIndex=e.getAttribute("tabindex"),u.setAttribute("data-value",e.value),u.addEventListener("click",function(e){e.preventDefault(),this.getAttribute("data-value");let t=this.getAttribute("tabindex"),a=s.querySelector('.option[data-index="'+t+'"]');a&&a.removeAttribute("data-checked");let l=i.querySelector('option[tabindex="'+t+'"]');l&&l.removeAttribute("selected"),this.remove(),s.setAttribute("data-open","");0==c.querySelectorAll("select option[selected]").length&&(d.innerText=i.title?i.title:"Select Item",f())}),d.append(u)}t++})}else d.innerText=i.title?i.title:"Select Item",f();d.appendChild(o)},f=function(){let e=r.querySelectorAll("div.option");if(e)for(let t=0;t<e.length;t++)e[t].style.display=""};for(t of(s.classList.add("hjs-select2-wrap"),n.classList.add("nextheader-wrap"),s.tabIndex=1,i.tabIndex=-1,d.innerText=i.title?i.title:"Select Item",n.appendChild(d),i.attributes));let v=c.querySelectorAll("select > *");if(v.length>0){let x=0;v.forEach(e=>{let a=e.hasAttribute("disabled"),l=e.hasAttribute("selected"),i=e.querySelectorAll("option");if(i.length>0){let s=document.createElement("div"),n=document.createElement("div");n.classList.add("label"),n.innerText=e.label,s.appendChild(n),i.forEach(e=>{let a=document.createElement("div"),l=document.createElement("div"),i=e,n=e.hasAttribute("disabled"),r=e.hasAttribute("selected");for(t of(n&&a.setAttribute("data-disabled",""),r&&a.setAttribute("data-checked",""),i.attributes));if(a.classList.add("option"),a.classList.add("optgroup-item"),l.classList.add("label"),l.innerText=i.label,a.dataset.value=i.value,a.dataset.label=i.label,a.dataset.index=x,!i.hasAttribute("value")){let d=i.value?i.value:i.innerText;i.setAttribute("value",d)}e.setAttribute("tabindex",x),a.onclick=h,a.onkeyup=p,a.appendChild(l),s.appendChild(a),x++}),s.classList.add("optgroup"),r.appendChild(s)}else{let d=document.createElement("div"),o=document.createElement("div"),u=e;for(t of(a&&d.setAttribute("data-disabled",""),l&&d.setAttribute("data-checked",""),u.attributes));if(d.classList.add("option"),o.classList.add("label"),o.innerText=u.label,d.dataset.value=u.value,d.dataset.label=u.label,d.dataset.index=x,!u.hasAttribute("value")){let c=u.value?u.value:u.innerText;u.setAttribute("value",c)}e.setAttribute("tabindex",x),d.onclick=h,d.onkeyup=p,d.appendChild(o),r.appendChild(d)}x++})}s.appendChild(n),s.onclick=function(e){e.preventDefault()},c.insertBefore(s,i),n.appendChild(i),s.appendChild(r),r.style.top="auto",s.onclick=function(e){if(b){let t=this.hasAttribute("data-open");e.stopPropagation(),t?this.removeAttribute("data-open"):this.setAttribute("data-open","")}else{let a=this.hasAttribute("data-open");e.stopPropagation(),a?this.removeAttribute("data-open"):this.setAttribute("data-open","")}},s.onkeyup=function(e){e.preventDefault(),13===e.keyCode&&this.click()},o.onkeyup=function(e){e.preventDefault(),s.setAttribute("data-open","");let t=this.innerText.toUpperCase(),a=r.querySelectorAll("div.option");if(a)for(let l=0;l<a.length;l++){let i=a[l].querySelectorAll("*");if(i){let n="";i.forEach(function(e){n+=e.textContent||e.innerText+" "}),n.toUpperCase().indexOf(t)>-1?a[l].style.display="":a[l].style.display="none"}}},document.addEventListener("click",function(e){s.hasAttribute("data-open")&&s.removeAttribute("data-open")});let m=Math.max(...Array.from(u).map(function(e){return s.offsetWidth}));s.style.minWidth=m+"px";c.querySelectorAll("select option[selected]").length>0&&A()}}};
