{"version":3,"names":["ActionBatchStore","Instance","this","_instance","allProducts","store","state","products","selectedProducts","selectedProduts","productId","storedProduct","selected","constructor","createStore","initialStore","onChange","selectedCount","selectionState","productCount","register","product","quantity","id","ec_product_id","rowIndex","Object","keys","length","updateStoreProducts","reset","select","changeSelectedStateForOne","deselect","selectAll","changeSelectedStateForAll","deselectAll","updateQuantity","console","warn","count","actionBatchStore"],"sources":["src/components/result-actions-batch/action-batch-store.ts"],"sourcesContent":["import { createStore } from \"@stencil/store\";\r\nimport { Product } from '@coveo/headless/commerce';\r\n\r\ninterface StoredProduct {\r\n product: Product;\r\n selected: boolean,\r\n rowIndex: number,\r\n quantity: number\r\n}\r\n\r\ninterface StoredProducts { [productId: string]: StoredProduct }\r\n\r\nexport type SelectionState = 'none' | 'some' | 'all';\r\n\r\ninterface StoreModel {\r\n products: StoredProducts,\r\n selectionState: SelectionState,\r\n productCount: number,\r\n selectedCount: number\r\n}\r\n\r\n/*ActionBatchStore: singleton*/\r\nclass ActionBatchStore {\r\n private static _instance: ActionBatchStore;\r\n private static initialStore: StoreModel = {\r\n products: {},\r\n selectionState: 'none',\r\n productCount: 0,\r\n selectedCount: 0\r\n }\r\n\r\n public static get Instance() {\r\n return this._instance || (this._instance = new this());\r\n }\r\n\r\n public store = createStore(ActionBatchStore.initialStore);\r\n\r\n public get allProducts() : StoredProducts {\r\n return this.store.state.products;\r\n }\r\n \r\n public get selectedProducts() : StoredProducts {\r\n const selectedProduts: StoredProducts = {};\r\n for(const productId in this.store.state.products){\r\n const storedProduct = this.store.state.products[productId];\r\n if(storedProduct.selected){\r\n selectedProduts[productId] = storedProduct\r\n }\r\n }\r\n return selectedProduts;\r\n }\r\n\r\n constructor() {\r\n this.store.onChange('selectedCount', (selectedCount) => {\r\n const selectionState: SelectionState = selectedCount == 0 ? 'none' : selectedCount == this.store.state.productCount ? 'all' : 'some';\r\n this.store.state.selectionState = selectionState;\r\n })\r\n\r\n }\r\n\r\n public register(product: Product, quantity: number) {\r\n const id = product.ec_product_id!;\r\n const { products } = this.store.state;\r\n if (!products[id]) {\r\n products[id] = {\r\n product,\r\n selected: false,\r\n rowIndex: Object.keys(products).length,\r\n quantity /*can be updated when qtyselector change, via updateQuantity */\r\n };\r\n this.updateStoreProducts(products);\r\n }\r\n }\r\n\r\n public reset(){\r\n this.updateStoreProducts({});\r\n this.store.state.selectedCount = 0;\r\n }\r\n\r\n public select(product: Product) {\r\n this.changeSelectedStateForOne(product, true);\r\n }\r\n\r\n public deselect(product: Product) {\r\n this.changeSelectedStateForOne(product, false);\r\n }\r\n\r\n public selectAll() {\r\n this.changeSelectedStateForAll(true);\r\n }\r\n\r\n public deselectAll() {\r\n this.changeSelectedStateForAll(false);\r\n }\r\n\r\n public updateQuantity(productId: string, quantity: number){\r\n const product = this.store.state.products[productId];\r\n if(!product){\r\n console.warn(`Cannot updateQuantity: product ${productId} not found in action-batch-store`);\r\n return\r\n }\r\n product.quantity = quantity;\r\n //NOTE: we dont' need to trigger a store onchange: no one listens to that info.\r\n }\r\n\r\n private changeSelectedStateForAll(selected: boolean) {\r\n const { products } = this.store.state;\r\n let count = 0;\r\n for (const id in products) {\r\n products[id].selected = selected;\r\n count++;\r\n }\r\n this.updateStoreProducts(products);\r\n\r\n actionBatchStore.store.state.selectedCount = selected ? count : 0;\r\n }\r\n\r\n private changeSelectedStateForOne(product: Product, selected: boolean) {\r\n const id = product.ec_product_id!;\r\n const { products } = this.store.state;\r\n if (products[id]) {\r\n products[id].selected = selected;\r\n this.updateStoreProducts(products);\r\n\r\n const { state } = actionBatchStore.store\r\n selected ? state.selectedCount++ : state.selectedCount--;\r\n }\r\n }\r\n\r\n private updateStoreProducts(products: StoredProducts) {\r\n this.store.state.products = { ...products }; //triggers store's onChange\r\n this.store.state.productCount = Object.keys(products).length;\r\n }\r\n}\r\n\r\nconst actionBatchStore = ActionBatchStore.Instance;\r\nexport default actionBatchStore;\r\n"],"mappings":"oCAsBA,MAAMA,EASG,mBAAWC,GAChB,OAAOC,K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