function c(a){return String(a??"").trim().toLocaleLowerCase()}function n(a,e){return e===""?!0:String(a.dataset.search??"").includes(e)}class d{constructor(e){this.root=e,this.groups=[...e.querySelectorAll("[data-perm-group]")],this.bind(),this.updateCounts()}bind(){this.root.querySelector("[data-perm-search]")?.addEventListener("input",e=>{this.filter(c(e.target.value))}),this.root.querySelector("[data-perm-expand-all]")?.addEventListener("click",()=>this.expandAll(!0)),this.root.querySelector("[data-perm-collapse-all]")?.addEventListener("click",()=>this.expandAll(!1)),this.root.querySelector("[data-perm-select-visible]")?.addEventListener("click",()=>this.setVisible(!0)),this.root.querySelector("[data-perm-clear-visible]")?.addEventListener("click",()=>this.setVisible(!1)),this.root.addEventListener("change",e=>{e.target.matches("[data-perm-checkbox]")&&this.updateCounts()}),this.groups.forEach(e=>this.bindGroup(e))}bindGroup(e){e.querySelector("[data-perm-group-select]")?.addEventListener("click",()=>{this.setGroup(e,!0)}),e.querySelector("[data-perm-group-clear]")?.addEventListener("click",()=>{this.setGroup(e,!1)})}filter(e){this.groups.forEach(t=>{const s=[...t.querySelectorAll("[data-perm-row]")];s.forEach(i=>{i.hidden=!n(i,e)});const r=s.some(i=>!i.hidden);t.hidden=!r,e!==""&&r&&this.setExpanded(t,!0)})}expandAll(e){this.groups.filter(t=>!t.hidden).forEach(t=>{this.setExpanded(t,e)})}setExpanded(e,t){e.querySelector("[data-perm-collapse]")?.classList.toggle("show",t),e.querySelector('[data-bs-toggle="collapse"]')?.setAttribute("aria-expanded",String(t))}setVisible(e){this.groups.filter(t=>!t.hidden).forEach(t=>{this.setGroup(t,e,!0)}),this.updateCounts()}setGroup(e,t,s=!1){e.querySelectorAll("[data-perm-row]").forEach(r=>{(!s||!r.hidden)&&(r.querySelector("[data-perm-checkbox]").checked=t)}),this.updateCounts()}updateCounts(){const e=[...this.root.querySelectorAll("[data-perm-checkbox]")],t=e.filter(r=>r.checked).length,s=this.root.querySelector("[data-perm-total-count]");s&&(s.textContent=`${t} / ${e.length}`),this.groups.forEach(r=>{const i=[...r.querySelectorAll("[data-perm-checkbox]")],o=r.querySelector("[data-perm-group-count]");o&&(o.textContent=`${i.filter(l=>l.checked).length} / ${i.length}`)})}}document.querySelectorAll("[data-permission-matrix]").forEach(a=>{new d(a)});
