"use strict";!function(e,t){"function"==typeof define&&define.amd?define(t):"object"==typeof exports?module.exports=t():e.CheckboxFilterDynamic=t()}(window,function(){return function(t,i){(i=i||{}).filterField=i.filterField||".checkbox-filter__filter",i.modalPreview=i.modalPreview||".modal-preview",i.accordionPreview=i.accordionPreview||".accordion-preview",i.accordionBtn=i.accordionBtn||"button.accordion--button",i.previewCheckboxes=i.previewCheckboxes||"div.checkbox.preview",i.checkboxes=i.checkboxes||"div.checkbox:not(.preview)",i.selectedContainer=i.selectedContainer||".checkbox-filter__selected",i.checkboxContainers=i.checkboxContainers||".checkbox-filter__checkboxes",i.openBtn=i.openBtn||".checkbox-filter__open",i.submitBtn=i.submitBtn||".checkbox-filter__submit",i.closeBtns=i.closeBtns||".checkbox-filter__close",i.resultSpan=i.resultSpan||".checkbox-filter__result",i.makeTags=!1!==i.makeTags,i.hiddenTagText=i.hiddenTagText||"remove tag",i.onRemoveTag=i.onRemoveTag||function(){};const r=t.querySelector(i.filterField),a=t.querySelector(i.modalPreview),c=t.querySelector(i.accordionPreview),e=t.querySelector(i.accordionBtn),n=t.querySelectorAll(i.previewCheckboxes)||[],o=t.querySelectorAll(i.checkboxes)||[],d=t.querySelector(i.selectedContainer),l=t.querySelectorAll(i.checkboxContainers),s=t.querySelector(i.openBtn),u=t.querySelector(i.submitBtn),h=t.querySelectorAll(i.closeBtns),k=t.querySelector(i.resultSpan);let p=[];const v=e=>{if(r){e&&(r.value="");let n=0;[].slice.call(l).forEach(e=>{e.style.display="none"}),m(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(l).forEach(e=>{e.querySelectorAll(i.checkboxes+":not([hidden])").length&&(e.style.display="")}),k&&(k.textContent=""+n)}},b=(e,t)=>{var c=document.createElement("li"),n=document.createElement("span"),r=(n.className="tag filter",n.textContent=t.textContent,n.setAttribute("data-value",e.value),document.createElement("button"));return r.type="button",r.innerHTML=`<span class="visually-hidden">${i.hiddenTagText} ${t.textContent}</span>`,r.addEventListener("click",()=>e.click()),n.appendChild(r),c.appendChild(n),c},x=(e,t)=>{i.makeTags&&d&&d.appendChild(b(e,t))},m=t=>{for(let e=0;e<o.length;e++){var c=o[e],n=c.querySelector("input[type=checkbox]"),r=c.querySelector("label");n&&r&&t({checkboxWrapper:c,checkbox:n,label:r})}},f=()=>{m(e=>{var e=e["checkbox"],t=-1!==p.indexOf(e);(t&&!e.checked||!t&&e.checked)&&e.click()})};const y=(e,t)=>{e.checked!==t.checked&&e.click()};const g=e=>{27===(e.keyCode||e.which)&&(e.preventDefault(),f())};if(m(e=>{var{checkbox:e,label:t}=e;e.checked&&!e.indeterminate&&x(e,t)}),v(!0),s&&s.addEventListener("click",()=>{p=[],m(e=>{e=e.checkbox;e.checked&&p.push(e)}),document.addEventListener("keydown",g)}),h)for(let e=h.length;e--;)h[e].addEventListener("click",()=>{f(),document.removeEventListener("keydown",g)});if(u&&u.addEventListener("click",()=>{document.removeEventListener("keydown",g)}),r){let e=null;r.addEventListener("input",()=>{e&&clearTimeout(e),e=setTimeout(v,200)})}if(n.length)for(let e=n.length;e--;){const C=n[e].querySelector("input[type=checkbox]"),S=t.querySelector("#"+C.getAttribute("data-original"));C&&(C.addEventListener("change",()=>y(S,C)),S.addEventListener("change",()=>y(C,S)))}return e&&c&&e.addEventListener("click",()=>m(e=>{var{checkboxWrapper:e,checkbox:t}=e;t.checked&&!c.contains(e)&&c.appendChild(e)})),m(e=>{let{checkboxWrapper:r,checkbox:o,label:l}=e;o.addEventListener("change",()=>{if(o.checked){x(o,l);var e=r,t=o;if(a&&!a.querySelector("#"+t.id+"-preview")){e=e.cloneNode(!0);const n=e.querySelector("input");var c=e.querySelector("label");n.name=null,n.id+="-preview",c.for+="-preview",t.addEventListener("change",()=>y(n,t)),n.addEventListener("change",()=>y(t,n)),a.appendChild(e)}}else c=o,i.makeTags&&d&&(e=d.querySelector('.filter[data-value="'+c.value+'"]'))&&(d.removeChild(e.parentElement),i.onRemoveTag(c,e))})}),{}}});