(()=>{"use strict";function e(e){return`s_${e}`}function t(e){document.dispatchEvent(new window.CustomEvent("flexa-post-grid:filter",{detail:e}))}function n(e){const t=e.querySelector(".flexa-filter-field__menu-value");if(!t)return;const n=Array.from(e.querySelectorAll(".flexa-filter-field__option-input:checked")).map(e=>{const t=e.parentElement?.querySelector(".flexa-filter-field__option-text");return(t?.firstChild?.textContent||"").trim()}).filter(Boolean);t.textContent=n.length?n.join(", "):t.getAttribute("data-empty")||""}function r(t,n){const r=new window.URL(window.location.href),o=e(n),a=`tx_${n}_`;Array.from(r.searchParams.keys()).forEach(e=>{e!==o&&e!==function(e){return e?`pg_${e}`:"pg"}(n)&&0!==e.indexOf(a)||r.searchParams.delete(e)});const i={};return Array.from(t.elements).forEach(e=>{const t=e,n=t.name||"";if(!n)return;const r=n.replace(/\[\]$/,"");r!==o&&0!==r.indexOf(a)||("checkbox"!==t.type&&"radio"!==t.type||t.checked)&&(t instanceof window.HTMLSelectElement&&t.multiple?Array.from(t.selectedOptions).map(e=>e.value):[t.value]).filter(Boolean).forEach(e=>{i[r]=(i[r]||[]).concat(e.trim())})}),Object.keys(i).forEach(e=>{i[e].length&&r.searchParams.set(e,i[e].join(","))}),r}function o(e){const t=e.querySelector(".flexa-filter-field__menu-search");if(!t)return;t.hidden=!1;const n=Array.from(e.querySelectorAll(".flexa-filter-field__option"));t.addEventListener("input",e=>{e.stopPropagation();const r=t.value.trim().toLowerCase();n.forEach(e=>{const t=e.querySelector(".flexa-filter-field__option-input"),n=(e.textContent||"").toLowerCase();e.hidden=""!==r&&!n.includes(r)&&!t?.checked})})}function a(t,n){const o=r(t,n),a=e(n),i=`tx_${n}_`,l=Array.from(o.searchParams.keys()).some(e=>e===a||0===e.indexOf(i));t.classList.toggle("flexa-post-filter--filtered",l);const c=t.querySelector(".flexa-filter-field--reset");c&&c.classList.toggle("flexa-filter-field--empty",!l)}function i(e){const i=e.getAttribute("data-flexa-post-filter")||"";if(!i)return;e.classList.add("is-js");const l=()=>t({target:i,url:r(e,i).toString()});e.addEventListener("input",()=>a(e,i)),e.addEventListener("change",()=>a(e,i)),e.addEventListener("reset",()=>window.setTimeout(()=>a(e,i),0)),a(e,i);const c=Array.from(e.querySelectorAll(".flexa-filter-field__menu"));c.length&&(c.forEach(o),e.addEventListener("change",()=>c.forEach(n)),e.addEventListener("reset",()=>window.setTimeout(()=>c.forEach(n),0)),document.addEventListener("click",e=>{const t=e.target;c.forEach(e=>{e.open&&t&&!e.contains(t)&&(e.open=!1)})}),document.addEventListener("keydown",e=>{"Escape"===e.key&&c.forEach(e=>{e.open&&(e.open=!1,e.querySelector(".flexa-filter-field__menu-toggle")?.focus())})})),e.addEventListener("submit",e=>{e.preventDefault(),l()});const s=e=>{const t=e?.type;return"search"===t||"text"===t};e.addEventListener("change",e=>{s(e.target)||l()});let f=0;e.addEventListener("input",e=>{s(e.target)&&(window.clearTimeout(f),f=window.setTimeout(l,300))}),e.addEventListener("click",n=>{if(n.defaultPrevented||0!==n.button||n.metaKey||n.ctrlKey||n.shiftKey||n.altKey)return;const r=n.target,o=r?r.closest(".flexa-filter-reset__control"):null;o&&o.href&&(n.preventDefault(),e.reset(),t({target:i,url:o.href}))})}function l(){document.querySelectorAll("[data-flexa-post-filter]").forEach(i)}"loading"===document.readyState?document.addEventListener("DOMContentLoaded",l):l()})();