"use strict";!function(e,t){"function"==typeof define&&define.amd?define(t):"object"==typeof exports?module.exports=t():e.CheckboxFilter=t()}(window,function(){return function(e,l){l=l||{};const r=e.querySelector(l.filterfield||".checkbox-filter__filter"),o=e.querySelectorAll(l.checkboxes||"div.checkbox")||[],i=e.querySelector(l.selectedContainer||".checkbox-filter__selected"),t=e.querySelectorAll(l.checkboxContainers||".checkbox-filter__checkboxes"),c=e.querySelector(l.openBtn||".checkbox-filter__open"),n=e.querySelector(l.submitBtn||".checkbox-filter__submit"),d=e.querySelectorAll(l.closeBtns||".checkbox-filter__close"),a=e.querySelector(l.resultSpan||".checkbox-filter__result"),u=e.querySelector(l.countSpanWrapper||".checkbox-filter__count-wrapper"),h=e.querySelector(l.countSpan||".checkbox-filter__count");let s=[];const b=!1!==l.makeTags||l.makeTags,k=e=>{if(r){e&&(r.value="");let n=0;[].slice.call(t).forEach(e=>{e.style.display="none"}),x(e=>{var{checkboxWrapper:e,checkbox:t,label:c}=e;c&&-1!==c.textContent.toUpperCase().indexOf(r.value.toUpperCase())?(e.removeAttribute("hidden"),t.removeAttribute("hidden"),n++):(e.setAttribute("hidden","true"),t.setAttribute("hidden","true"))}),[].slice.call(t).forEach(e=>{e.querySelectorAll(`${l.checkboxes||"div.checkbox"}:not([hidden])`).length&&(e.style.display="")}),v(n)}},p=(e,t)=>{let c=document.createElement("span");c.className="tag filter",c.textContent=t.textContent,c.setAttribute("data-value",e.value);t=document.createElement("button");return t.type="button",t.innerHTML=`<span class="visually-hidden">${l.hiddenTagText||"Remove tag"}</span>`,t.addEventListener("click",()=>{e.checked=!1,i.removeChild(c),"function"==typeof l.onRemoveTag&&l.onRemoveTag(e,c)}),c.appendChild(t),c},f=()=>{var e=i.children.length;h&&(h.textContent=e),u&&(0<e?u.classList.remove("hidden"):u.classList.add("hidden"))},v=e=>{a&&(a.textContent=e)},x=t=>{for(let e=o.length;e--;){var c=o[e],n=c.querySelector("input[type=checkbox]"),l=c.querySelector("label");n&&l&&t({checkboxWrapper:c,checkbox:n,label:l})}},y=()=>{b&&(i.innerHTML=""),x(e=>{var{checkbox:e,label:t}=e;-1!==s.indexOf(e)?e.checked=!0:e.checked=!1,e.checked&&b&&i.appendChild(p(e,t))})};const m=e=>{27===(e.keyCode||e.which)&&(e.preventDefault(),y(),f())};if(s=[],x(e=>{var{checkbox:e,label:t}=e;e.checked&&!e.indeterminate&&b&&i.appendChild(p(e,t))}),f(),k(!0),r){let e=null;r.addEventListener("input",()=>{e&&clearTimeout(e),e=setTimeout(k,200)})}if(x(e=>{let{checkbox:n,label:l}=e;n.addEventListener("change",()=>{if(n.checked)b&&i.appendChild(p(n,l));else if(b){var t=n,c=i.querySelectorAll(".filter");for(let e=c.length;e--;)c[e].getAttribute("data-value")===t.value&&i.removeChild(c[e])}})}),c&&c.addEventListener("click",e=>{s=[];let t=0;x(e=>{e=e.checkbox;e.checked&&s.push(e),t++}),v(t),document.addEventListener("keydown",m)}),d)for(let e=d.length;e--;)d[e].addEventListener("click",()=>{y(),f(),document.removeEventListener("keydown",m)});return n&&n.addEventListener("click",()=>{f(),document.removeEventListener("keydown",m)}),{}}});