import{K as t,a as e}from"./p-06444484.js";import{i as s}from"./p-e66456dc.js";class i{constructor(t){var e;this.filters=[];this.filters=(e=t===null||t===void 0?void 0:t.filters)!==null&&e!==void 0?e:[]}get options(){return this.filters.filter((e=>e.type===t.Options))}get sliders(){return this.filters.filter((e=>e.type===t.Slider))}initFromListFilters(t){this.filters=t;if(s()){document.dispatchEvent(new CustomEvent(e.FiltersApplied,{detail:{filters:this.filters}}))}}clear(){this.filters=[]}getCurrentState(){return{filters:this.filters}}setState(t){this.filters=t.filters}toggleOption(t,i){const n=this.getOptionByKeyCreateIfNotExists(t,i);const o=n.selected;n.selected=!n.selected;if(s()){document.dispatchEvent(new CustomEvent(e.FilterSelectionUpdate,{detail:{key:t,name:i,selected:!o}}))}}selectOption(t,i){const n=this.getOptionByKeyCreateIfNotExists(t,i);n.selected=true;if(s()){document.dispatchEvent(new CustomEvent(e.FilterSelectionUpdate,{detail:{key:t,name:i,selected:true}}))}}deselectOption(t,i){const n=this.getOptionByKeyCreateIfNotExists(t,i);n.selected=false;if(s()){document.dispatchEvent(new CustomEvent(e.FilterSelectionUpdate,{detail:{key:t,name:i,selected:false}}))}}clearOptionSelections(t){if(t){const e=this.filters.find((e=>e.key===t));if(e&&i.isKlevuFilterResultOptions(e)){e.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(e.FilterSelectionUpdate))}}updateSlide(n,o,r){const l=this.filters.findIndex((t=>t.key===n));let a;if(l===-1){a={key:n,end:r.toString(),start:o.toString(),min:o.toString(),max:r.toString(),label:n,type:t.Slider};this.filters.push(a)}else if(!i.isKlevuFilterResultSlider(this.filters[l])){throw new Error(`Filter ${n} is not a slider filter.`)}else{a=this.filters[l];a.start=o.toString();a.end=r.toString()}if(s()){document.dispatchEvent(new CustomEvent(e.FilterSelectionUpdate,{detail:{key:n,name:a.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(e,s){const n=this.filters.findIndex((t=>t.key===e));let o;if(n===-1){o={key:e,label:e,options:[],type:t.Options};this.filters.push(o)}else if(!i.isKlevuFilterResultOptions(this.filters[n])&&!i.isKlevuFilterResultRating(this.filters[n])){throw new Error(`Filter ${e} 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(e){return e.type===t.Slider}static isKlevuFilterResultOptions(e){return e.type===t.Options}static isKlevuFilterResultRating(e){return e.type===t.Rating}}export{i as F};
//# sourceMappingURL=p-e88ac86e.js.map