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