import{K as t}from"./p-c55c67b2.js";import{K as e}from"./p-c2e91ee8.js";import{i as s}from"./p-97d1323d.js";class i{get options(){return this.filters.filter((t=>t.type===e.Options))}get sliders(){return this.filters.filter((t=>t.type===e.Slider))}constructor(t){var e;this.filters=[];this.filters=(e=t===null||t===void 0?void 0:t.filters)!==null&&e!==void 0?e:[]}initFromListFilters(e){this.filters=e;if(s()){document.dispatchEvent(new CustomEvent(t.FiltersApplied,{detail:{filters:this.filters}}))}}clear(){this.filters=[]}getCurrentState(){return{filters:this.filters}}setState(t){this.filters=t.filters}toggleOption(e,i){const n=this.getOptionByKeyCreateIfNotExists(e,i);const o=n.selected;n.selected=!n.selected;if(s()){document.dispatchEvent(new CustomEvent(t.FilterSelectionUpdate,{detail:{key:e,name:i,selected:!o}}))}}selectOption(e,i){const n=this.getOptionByKeyCreateIfNotExists(e,i);n.selected=true;if(s()){document.dispatchEvent(new CustomEvent(t.FilterSelectionUpdate,{detail:{key:e,name:i,selected:true}}))}}deselectOption(e,i){const n=this.getOptionByKeyCreateIfNotExists(e,i);n.selected=false;if(s()){document.dispatchEvent(new CustomEvent(t.FilterSelectionUpdate,{detail:{key:e,name:i,selected:false}}))}}clearOptionSelections(e){if(e){const t=this.filters.find((t=>t.key===e));if(t&&i.isKlevuFilterResultOptions(t)){t.options.forEach((t=>t.selected=false))}return}this.filters.forEach((t=>{if(t&&i.isKlevuFilterResultOptions(t)){t.options.forEach((t=>t.selected=false))}}));if(s()){document.dispatchEvent(new CustomEvent(t.FilterSelectionUpdate))}}updateSlide(n,o,r){const l=this.filters.findIndex((t=>t.key===n));let c;if(l===-1){c={key:n,end:r.toString(),start:o.toString(),min:o.toString(),max:r.toString(),label:n,type:e.Slider};this.filters.push(c)}else if(!i.isKlevuFilterResultSlider(this.filters[l])){throw new Error(`Filter ${n} is not a slider filter.`)}else{c=this.filters[l];c.start=o.toString();c.end=r.toString()}if(s()){document.dispatchEvent(new CustomEvent(t.FilterSelectionUpdate,{detail:{key:n,name:c.label,start:o.toString(),end:r.toString()}}))}}toApplyFilters(){const t=[];for(const e of this.filters){if(i.isKlevuFilterResultOptions(e)||i.isKlevuFilterResultRating(e)){const s=e.options.filter((t=>t.selected===true));if(s.length===0){continue}t.push({key:e.key,values:s.map((t=>t.value)),settings:{singleSelect:false}})}else if(i.isKlevuFilterResultSlider(e)){if(!e.start||!e.end){continue}t.push({key:e.key,values:[e.start,e.end],settings:{singleSelect:false}})}}return t}currentSelection(t){const e=this.filters.find((e=>e.key===t));if(e&&(i.isKlevuFilterResultOptions(e)||i.isKlevuFilterResultRating(e))){const t=e.options.filter((t=>t.selected===true));return t.map((t=>t.value))}if(e&&i.isKlevuFilterResultSlider(e)&&e.start&&e.end){return[e.start,e.end]}return undefined}toURLParams(){const t=new URLSearchParams;for(const e of this.filters){if(i.isKlevuFilterResultOptions(e)||i.isKlevuFilterResultRating(e)){const s=e.options.filter((t=>t.selected===true));if(s.length===0){continue}t.append(`o_${e.key}`,s.map((t=>t.value)).join(","))}else if(i.isKlevuFilterResultSlider(e)){if(!e.start||!e.end){continue}t.append(`s_${e.key}`,`${e.start}-${e.end}`)}}return t.toString()}readFromURLParams(t){this.clearOptionSelections();for(const[e,s]of t.entries()){if(e.startsWith("o")){const t=e.substring(2);s.split(",").forEach((e=>this.selectOption(t,e)))}else if(e.startsWith("s")){const t=e.substring(2);this.updateSlide(t,parseFloat(s.split("-")[0]),parseFloat(s.split("-")[1]))}}}selectedFilters(){const t=[];for(const e of this.filters){if(i.isKlevuFilterResultOptions(e)||i.isKlevuFilterResultRating(e)){const s=e.options.filter((t=>t.selected===true));if(s.length===0){continue}for(const i of s){t.push({key:e.key,label:e.label,type:e.type,value:i.value})}}else if(i.isKlevuFilterResultSlider(e)){if(!e.start||!e.end){continue}t.push({key:e.key,label:e.label,type:e.type,value:`${e.start}-${e.end}`})}}return t}getOptionByKeyCreateIfNotExists(t,s){const n=this.filters.findIndex((e=>e.key===t));let o;if(n===-1){o={key:t,label:t,options:[],type:e.Options};this.filters.push(o)}else if(!i.isKlevuFilterResultOptions(this.filters[n])&&!i.isKlevuFilterResultRating(this.filters[n])){throw new Error(`Filter ${t} is not an option filter.`)}else{o=this.filters[n]}const r=o.options.findIndex((t=>t.value===s));if(r===-1){o.options.push({count:1,name:s,selected:false,value:s});return o.options[o.options.length-1]}return o.options[r]}static isKlevuFilterResultSlider(t){return t.type===e.Slider}static isKlevuFilterResultOptions(t){return t.type===e.Options}static isKlevuFilterResultRating(t){return t.type===e.Rating}}export{i as F};
//# sourceMappingURL=p-e40a1f91.js.map