function handleAllSelection(n,t){return t===""?[""]:n.filter(n=>n!=="")}function designsSearch(){return{results:[],totalResults:0,totalPages:0,currentPage:1,loading:!0,error:null,showClientFilters:!1,query:{Types:[{Value:defaultType,Selected:!0}],Brands:[],Colours:[],Categories:[],Keywords:"",NewOnly:{Selected:!1,Value:"NewOnly"},Availability:{Start:null,End:null},Price:{Start:null,End:null},PageIndex:0},selectedBrands:[""],selectedColours:[""],selectedCategories:[""],selectedTypes:[defaultType],priceFrom:null,priceTo:null,availabilityFrom:null,availabilityTo:null,mobileSearchOpen:!1,inMoodboard(n){return Moodboard.isProductInMoodboard(n)},get selectedBrandsString(){return this.getSelectedText(this.query.Brands,this.selectedBrands)},get selectedColoursString(){return this.getSelectedText(this.query.Colours,this.selectedColours)},get selectedCategoriesString(){return this.getSelectedText(this.query.Categories,this.selectedCategories)},get selectedTypesString(){return this.getSelectedText(this.query.Types,this.selectedTypes)},getSelectedText(n,t){return n.filter(n=>t.includes(n.Value)).map(n=>n.Text).join(", ")||"All"},newOnlySelected:!1,syncSelectionsToQuery(){const n=(n,t)=>n?.length?n.filter(n=>n.Value!="").map(n=>({...n,Selected:t.includes(n.Value)})):t.filter(n=>n!="").map(n=>({Value:n,Selected:!0}));this.query.Brands=n(this.query.Brands,this.selectedBrands);this.query.Colours=n(this.query.Colours,this.selectedColours);this.query.Categories=n(this.query.Categories,this.selectedCategories);this.query.Types=n(this.query.Types,this.selectedTypes);this.query.NewOnly.Selected=this.newOnlySelected;this.query.PageIndex=this.currentPage-1;this.showClientFilters?(this.query.Price={Start:this.priceFrom||null,End:this.priceTo||null},this.query.Availability={Start:this.availabilityFrom||null,End:this.availabilityTo||null}):(delete this.query.Price,delete this.query.Availability)},async fetchData(n=false){this.syncSelectionsToQuery();this.loading=!0;this.error=null;this.controller&&this.controller.abort();this.controller=new AbortController;try{const i=await fetch("/api/ProductSearch",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(this.query),signal:this.controller.signal});i.ok||(this.error=await i.text());const t=await i.json();this.query=t.Query;this.showClientFilters=t.ShowClientFilters;n?this.results.push(...t.Result.Products):this.results=t.Result.Products;this.totalResults=t.Result.TotalResults;this.totalPages=t.Result.TotalPages;await Alpine.nextTick();window.routing.updatePageLinks();document.title=this.getPageTitle()}catch(t){t.name!=="AbortError"&&(this.error=t.message)}finally{this.loading=!1}},onRouteChanged(n){this.applyRouteParams(n);this.fetchData()},submitQuery(n=false){n&&(this.selectedTypes=handleAllSelection(this.selectedTypes,""));this.loading=!0;this.mobileSearchOpen=!1;this.query.PageIndex=0;this.currentPage=1;this.results=[];this.totalResults=0;this.totalPages=0;this.updateRoute();this.fetchData()},loadMore(){this.query.PageIndex+1{this.onRouteChanged(n.detail?.url)});window.addEventListener("search:initialise-for-design",async()=>{await this.fetchData()});window.addEventListener("search:mobile-search-toggle",()=>{this.mobileSearchOpen=!this.mobileSearchOpen});const n=()=>{i||(clearTimeout(t),t=setTimeout(()=>{this.updateRoute(),this.fetchData()},150))};this.$watch("selectedBrands",n);this.$watch("selectedColours",n);this.$watch("selectedCategories",n);this.$watch("selectedTypes",n);this.$watch("newOnlySelected",n)},clearFilters(){this.selectedBrands=[];this.selectedColours=[];this.selectedCategories=[];this.typesSelected=[defaultType];this.newOnlySelected=!1;this.query.Keywords="";this.priceFrom=null;this.priceTo=null;this.availabilityFrom=null;this.availabilityTo=null;this.query.PageIndex=0;this.currentPage=1;this.results=[];this.totalResults=0;this.totalPages=0;this.mobileSearchOpen=!1;this.fetchData()},updateRoute(){const u=["/Search",this.selectedTypes.join(",")||"All","Brand",this.selectedBrands.join(",")||"All","Colour",this.selectedColours.join(",")||"All","Category",this.selectedCategories.join(",")||"All","New",this.newOnlySelected?"1":"0","Page",this.currentPage];let n=u.join("/");const t=new URLSearchParams;this.query.Keywords&&this.query.Keywords.trim()&&t.set("Keywords",this.query.Keywords.trim());const i=t.toString();i&&(n+=`?${i}`);const r=(window.routing.lastResolved()||[])[0];r&&`/${r.url}`===n||window.routing.navigate(n,{callHandler:!1})},applyRouteParams(n){const t=n?n.split("/").filter(Boolean):[],i=n=>{const i=t.indexOf(n);return i>-1&&t[i+1]?t[i+1].split(","):[]};this.selectedTypes=t[1]?t[1]==="All"?[""]:t[1].split(","):[defaultType];this.selectedBrands=i("Brand");(this.selectedBrands.length===0||this.selectedBrands.some(n=>n==="All"))&&(this.selectedBrands=[""]);this.selectedColours=i("Colour");(this.selectedColours.length===0||this.selectedColours.some(n=>n==="All"))&&(this.selectedColours=[""]);this.selectedCategories=i("Category");(this.selectedCategories.length===0||this.selectedCategories.some(n=>n==="All"))&&(this.selectedCategories=[""]);this.newOnlySelected=i("New")[0]==="1";const r=parseInt(i("Page")[0]);this.query.PageIndex=isNaN(r)?0:r-1;this.currentPage=this.query.PageIndex+1;const u=new URLSearchParams(window.location.search);this.query.Keywords=u.get("Keywords")||""},getPageTitle(){const t=n=>{if(!n.includes(","))return n.trim();const t=n.split(",").map(n=>n.trim()).filter(Boolean);return t.length===1?t[0]:t.slice(0,-1).join(", ")+" & "+t.slice(-1)},n=[];this.newOnlySelected&&n.push("New");this.selectedColoursString!=="All"&&n.push(t(this.selectedColoursString));this.selectedCategoriesString!=="All"&&n.push(t(this.selectedCategoriesString));this.selectedTypesString==="All"?n.push(t(this.query.Types.map(n=>n.Text).join(", "))):n.push(t(this.selectedTypesString));this.selectedBrandsString==="All"?n.push(`by ${t(this.query.Brands.map(n=>n.Text).join(", "))}`):n.push(`by ${t(this.selectedBrandsString)}`);this.query.Keywords?.trim()&&n.push(`matching “${this.query.Keywords.trim()}”`);let i=n.join(" ");return i=i.replace(/\s+/g," ").trim(),i||(i="All designs"),i.charAt(0).toUpperCase()+i.slice(1)}}}const defaultType="F";$(function(){$(document).on("click",function(){$("aside.filters .selection").removeClass("open")});$("aside.filters .selection").on("click",function(n){n.stopPropagation();var t=$(this);t.hasClass("open")?$("aside.filters .selection").removeClass("open"):($("aside.filters .selection").removeClass("open"),t.addClass("open"))});$("#scroll-to-top").click(()=>{window.scrollTo({top:0,left:0,behavior:"smooth"})})})