import{b as c}from"./chunk-W5RPWA2M.js";import{a as m}from"./chunk-ULT7T2WM.js";import{b as f}from"./chunk-JC2BH436.js";var d=f(m());var l=class extends c{constructor(a){super(a);this.getValue=()=>[];this.filterByValues=new Map;this.touch=()=>{this.fieldGroup.classList.remove("dj-untouched","dj-validated"),this.fieldGroup.classList.add("dj-touched")};this.isIncomplete=a.hasAttribute("incomplete")}async setupFilters(a){var s;(((s=a.getAttribute("filter-by"))==null?void 0:s.split(","))??[]).forEach(t=>{var i;let e=(i=a.form)==null?void 0:i.elements.namedItem(t);e instanceof HTMLInputElement?(this.filterByValues.set(t,e.value),e.addEventListener("change",async r=>{let o=r.currentTarget;o instanceof HTMLInputElement&&(this.filterByValues.set(t,o.value),await this.reloadOptions(!0))})):e instanceof HTMLSelectElement&&(this.filterByValues.set(t,Array.from(e.selectedOptions).map(r=>r.value)),e.addEventListener("change",async r=>{r.currentTarget instanceof HTMLSelectElement&&(this.filterByValues.set(t,Array.from(e.selectedOptions).map(p=>p.value)),await this.reloadOptions(!0))}))}),Array.from(this.filterByValues.values()).some(t=>t.some(e=>e))&&await this.reloadOptions()}buildFetchQuery(a,n){let s=new URLSearchParams;s.set("offset",String(a)),n!=null&&n.pk?s.set("pk",n.pk):n!=null&&n.search&&s.set("search",encodeURIComponent(n.search));for(let[t,e]of this.filterByValues)(0,d.default)(e)?s.set(`filter-${t}`,encodeURIComponent(e)):Array.isArray(e)&&(e.length===0?s.append(`filter-${t}`,""):e.forEach(i=>s.append(`filter-${t}`,encodeURIComponent(i))));return s}async loadOptions(a,n){let s=new Headers;s.append("Accept","application/json"),a.set("field",this.fieldName);let t=await fetch(`${this.endpoint}?${a.toString()}`,{method:"GET",headers:s});if(t.status===200){let e=await t.json();typeof e.incomplete=="boolean"&&(this.isIncomplete=e.incomplete),n(e.options)}else console.error(`Failed to fetch from ${this.endpoint} (status=${t.status})`)}};export{l as a};
