import{isStringEmpty as t}from"./lib/utils.js";class e{constructor(t){this._host=t,this._productsByCategories=[],this._categoriesFilters=[],this._currentCategoryNameFilter=null,this._currentTextFilter=""}getFilteredProductsByCategories(){const e=this._getProductsByCurrentCategory(),r=this._currentTextFilter?.toLowerCase().trim();if(t(r))return e;const s=r.split(" ").filter((t=>""!==t));return e.map((t=>({...t,products:this._filterProducts(t.products,s)}))).filter((t=>t.products.length>0))}set productsByCategories(t){this._productsByCategories=t,this._categoriesFilters=this._productsByCategories.map((({categoryName:t})=>({categoryName:t,toggled:!1}))),this._currentCategoryNameFilter="all"}getCategories(){return this._categoriesFilters}getCurrentCategory(){return this._currentCategoryNameFilter}set textFilter(t){this._currentTextFilter=t??"",this._host.requestUpdate()}toggleCategoryFilter(t){const e=null!=this._categoriesFilters.find((e=>e.categoryName===t));this._currentCategoryNameFilter=null!=t&&""!==t&&e&&this._currentCategoryNameFilter!==t?t:"all",this._categoriesFilters=this._categoriesFilters.map((t=>({categoryName:t.categoryName,toggled:"all"!==this._currentCategoryNameFilter&&this._currentCategoryNameFilter===t.categoryName}))),this._host.requestUpdate()}_filterProducts(t,e){return t.filter((t=>{const r=t?.searchTerms??[];return[t.name,t.description??"",...r].some((t=>e.some((e=>t.toLowerCase().includes(e)))))}))}_getProductsByCurrentCategory(){return"all"!==this._currentCategoryNameFilter?this._productsByCategories.filter((({categoryName:t})=>t===this._currentCategoryNameFilter)):this._productsByCategories}}export{e as ProductsController};
//# sourceMappingURL=products-controller.js.map
