{"version":3,"file":"AbstractComparison.js","names":["AbstractComparison","a","setters","Component","default","deepMerge","comparisonStore","execute","constructor","element","options","arguments","length","selectedProducts","initStorageData","instanceId","initState","comparisonIdElement","closest","getAttribute","state","afterInit","dispatch","products","map","pid","syncProducts","get","storeUpdated","replaceProduct","previousPid","removeProduct","currentState","isSelectedProductsEqual","reload","getReloadData","params","comparisonId","Object","keys","forEach","index","some","item","includes"],"sources":["components/search/AbstractComparison.js"],"sourcesContent":["import Component from 'core/Component';\nimport { deepMerge } from 'toolbox/deepMerge';\nimport comparisonStore from 'stores/ComparisonStore';\n\n/**\n * This is a description of the AbstractComparison constructor function.\n * @class\n * @classdesc This is a description of the AbstractComparison class. (must be edited)\n * @extends Component\n */\nexport default class AbstractComparison extends Component {\n /**\n * Constructor of the class that mainly merge the options of the components\n * @param {HTMLElement} element HTMLElement of the component\n * @param {object} options options that belongs to the component\n */\n constructor(element, options = {}) {\n super(element, deepMerge({\n selectedProducts: [],\n initStorageData: false, // if true, init storage with selected products\n instanceId: null, // comparison id, to differentiate multiple comparison tables present\n // on the page, if not passed can be taken from closest DOM element, \"default\" by default\n }, options), comparisonStore);\n }\n\n /**\n * Init the different state of the component\n * It helps to avoid heavy DOM manipulation\n */\n initState() {\n let { instanceId } = this.options;\n if (!instanceId) {\n const comparisonIdElement = this.element.closest('[data-comparison-id]');\n if (comparisonIdElement) {\n instanceId = comparisonIdElement.getAttribute('data-comparison-id');\n }\n }\n\n this.state.instanceId = instanceId || 'default';\n }\n\n /**\n * After init\n * Set Initial data for the store\n */\n afterInit() {\n if (this.options.initStorageData) {\n comparisonStore.dispatch('init', {\n products: this.options.selectedProducts.map(pid => ({ pid })),\n instanceId: this.state.instanceId,\n });\n } else {\n this.syncProducts(comparisonStore.get());\n }\n }\n\n /**\n * StoreUpdated Hook\n * @param {Object} state new state\n */\n storeUpdated(state) {\n this.syncProducts(state);\n }\n\n /**\n * Replace product from previous to the new pid in the list of selected products\n * @param {String} previousPid previous product id\n * @param {String} pid new product id\n */\n replaceProduct(previousPid, pid) {\n comparisonStore.dispatch('replaceProduct', {\n previousPid,\n pid,\n instanceId: this.state.instanceId,\n });\n }\n\n /**\n * Remove product from list of selected products\n * @param {String} pid selected product id\n */\n removeProduct(pid) {\n comparisonStore.dispatch('removeProduct', {\n pid,\n instanceId: this.state.instanceId,\n });\n }\n\n /**\n * Synchronize products with store\n * @param {Object} state new state\n */\n syncProducts(state) {\n // check if selected products were changed\n const currentState = state[this.state.instanceId];\n if (currentState && !this.isSelectedProductsEqual(currentState.products)) {\n this.reload(this.getReloadData(currentState.products));\n }\n }\n\n /**\n * Returns object with product ids\n * @param {Array} products array of selected pids\n * @returns {Object} object with selected pid\n */\n getReloadData(products) {\n const params = {\n comparisonId: this.state.instanceId,\n };\n\n Object.keys(products).forEach((index) => {\n params[`pid_${index}`] = products[index] && products[index].pid;\n });\n\n return params;\n }\n\n /**\n * Compare products from store with current state of component\n * @param {Array} products array of products\n * @returns {Boolean} true if products are same\n */\n isSelectedProductsEqual(products) {\n if (this.options.selectedProducts.length !== products.length) {\n return false;\n }\n\n return !products.some(item => !this.options.selectedProducts.includes(item.pid));\n }\n}\n"],"mappings":"gIAUqBA,CAAkB,QAAAC,CAAA,oBAAAC,OAAA,WAAAD,CAAA,EAVhCE,CAAS,CAAAF,CAAA,CAAAG,OAAA,WAAAH,CAAA,EACPI,CAAS,CAAAJ,CAAA,CAATI,SAAS,WAAAJ,CAAA,EACXK,CAAe,CAAAL,CAAA,CAAAG,OAAA,GAAAG,OAAA,SAAAA,CAAA,EAAAN,CAAA,WAQDD,CAAkB,CAAxB,aAAiC,CAAAG,CAAU,CAMtDK,WAAWA,CAACC,CAAO,CAAgB,IAAd,CAAAC,CAAO,GAAAC,SAAA,CAAAC,MAAA,WAAAD,SAAA,IAAAA,SAAA,IAAG,CAAC,CAAC,CAC7B,KAAK,CAACF,CAAO,CAAEJ,CAAS,CAAC,CACrBQ,gBAAgB,CAAE,EAAE,CACpBC,eAAe,GAAO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