text: "use strict";function typeaheadTagbox(e){function t(t){var n=l("."+e.dropdownItemClass+":nth-child("+t+")");l("."+e.dropdownItemClass+"."+e.dropdownSelectedClass)&&l("."+e.dropdownItemClass+"."+e.dropdownSelectedClass).classList.remove(e.dropdownSelectedClass),y=e.data.find(function(e){return e.id===+n.dataset.id}),n.classList.add(e.dropdownSelectedClass)}function n(t){g.value="",o(t),i(),g.focus(),e.onClickDropdownOption&&"function"==typeof e.onClickDropdownOption&&e.onClickDropdownOption(t)}function a(t){v.splice(v.findIndex(function(n){return n[e.idKey]===t[e.idKey]}),1),p.value=JSON.stringify(v),e.onRemoveTag&&"function"==typeof e.onRemoveTag&&e.onRemoveTag(t,v),l("."+e.tagClass+'[data-id="'+t[e.idKey]+'"]').remove()}function o(t){var n=document.createElement("span"),o=document.createElement("button"),d=r("."+e.tagClass);n.innerHTML=s(e.tagTemplate,t),o.dataset.id=t.id,o.innerHTML=e.tagDeleteButtonContent,n.dataset.id=t[e.idKey],n.setAttribute("class",e.tagClass),o.addEventListener("click",function(){a(t)}),v.push(t),e.onAddTag&&"function"==typeof e.onAddTag&&e.onAddTag(t,v),p.value=JSON.stringify(v),d.length>0?d[r("."+e.tagClass).length-1].after(n):c.prepend(n),n.append(o)}function d(t,a){var o=void 0;u.innerHTML="",t.forEach(function(t){(o=document.createElement("li")).classList.add(e.dropdownItemClass),o.dataset.id=t.id,o.innerHTML=s(e.dropdownItemTemplate,Object.assign({},t,{text:a.replace(new RegExp(a,e.caseSensitive?"g":"gi"),'<span class="'+e.highlightClass+'">$&</span>')})),o.addEventListener("click",function(){n(t)}),h++,u.append(o)}),f=!0,m=0,u.style.display="block",e.onOpenDropdown&&"function"==typeof e.onOpenDropdown&&e.onOpenDropdown(a,t)}function s(t,n){return t.replace(/{{text}}/g,n[e.textKey]).replace(/{{id}}/g,n[e.idKey]).replace(/{{(\w+)}}/g,function(e,t){return n[t]||""})}function i(){u.innerHTML="",f=!1,h=0,y=void 0,u.style.display="none"}function l(e){return document.querySelector(e)}function r(e){return document.querySelectorAll(e)}var p=l((e=Object.assign({caseSensitive:!1,data:[],dropdownClass:"typeahead-dropdown",dropdownItemClass:"typeahead-dropdown-item",dropdownItemTemplate:"{{text}}",dropdownSelectedClass:"selected",highlightClass:"typeahead-highlight",idKey:"id",inputClass:"typeahead-input",inputPlaceholder:"add...",minLength:2,onAddTag:void 0,onClickDropdownOption:void 0,onOpenDropdown:void 0,onRemoveTag:void 0,selector:".typeahead-tagbox",tagClass:"typeahead-tag",tagDeleteButtonContent:"x",tagRemovingClass:"removing",tagTemplate:"{{text}}",textKey:"text",typeaheadClass:"typeahead"},e)).selector),c=document.createElement("div"),u=document.createElement("ul"),g=document.createElement("input"),v=[],f=!1,m=0,y=void 0,h=0,C=void 0;if(!p)throw"Tagbox element not found";c.classList.add(e.typeaheadClass),u.classList.add(e.dropdownClass),g.classList.add(e.inputClass),g.type="text",g.placeholder=e.inputPlaceholder,p.after(c),p.type="hidden",p.remove(),c.append(p),c.append(g),c.append(u),c.addEventListener("click",function(){g.focus()}),c.addEventListener("keydown",function(o){var d=o.target.value;if(f&&("ArrowDown"===o.key&&m<h&&(o.preventDefault(),t(++m)),"ArrowUp"===o.key&&m>0&&(o.preventDefault(),t(--m)),"Enter"===o.key&&y&&(o.preventDefault(),n(y))),!d.length&&"Backspace"===o.key&&v.length>0){var s=r("."+e.tagClass);o.preventDefault(),C?(a(C),C=void 0):(C=v[v.length-1],s[s.length-1].classList.add(e.tagRemovingClass))}else l("."+e.tagClass+"."+e.tagRemovingClass)&&(C=void 0,l("."+e.tagClass+"."+e.tagRemovingClass).classList.remove(e.tagRemovingClass))}),c.addEventListener("input",function(t){var n=t.target.value;if(n.length>=e.minLength){var a=e.data.filter(function(t){var a=t[e.textKey];return e.caseSensitive||(a=a.toLowerCase(),n=n.toLowerCase()),a.includes(n)&&!v.find(function(n){return n[e.idKey]===t[e.idKey]})});a.length>0&&d(a,n)}else 0===n.length&&i()})}Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=typeaheadTagbox;