{"version":3,"file":"Errors.js","names":["Errors","a","setters","Event","on","off","Component","default","deepMerge","execute","constructor","element","options","arguments","length","classNames","errorWrapper","errorListClass","errorItem","pid","key","initState","state","isError","bindEvents","onAddToCartFailed","onProductUpdated","bind","clear","e","data","errorList","showErrorMessages","detail","previousPid","classList","remove","split","errorElement","newErrorElement","document","createElement","errorWrapperClasses","filter","item","add","errorListClasses","forEach","error","errorItemElement","itemClasses","errorMessage","innerText","push","type","concat","append","replaceChild","appendChild","destroy","removeListener"],"sources":["components/product/Errors.js"],"sourcesContent":["import { Event } from 'services/EventEmitter';\nimport { on, off } from 'toolbox/event';\nimport Component from 'core/Component';\nimport { deepMerge } from 'toolbox/deepMerge';\n\n/**\n * This is a description of the Errors constructor function.\n * @class\n * @classdesc This is a description of the Errors class.\n * @extends Component\n */\nexport default class Errors extends Component {\n /**\n * Constructor of the class that mainly merge the options of the components\n *\n * @param {HTMLElement} element HTMLElement of the component\n * @param {Object} options That belongs to the component\n */\n constructor(element, options = {}) {\n super(element, deepMerge({\n classNames: {\n errorWrapper: 'c-field__error-wrapper',\n errorListClass: 'c-field__error-list',\n errorItem: 'c-field__error-message',\n },\n pid: null,\n key: null,\n }, options));\n }\n\n /**\n * Init the different state of the component\n * It helps to avoid heavy DOM manipulation\n */\n initState() {\n this.state.isError = false;\n }\n\n /**\n * Should contain only event listeners and nothing else\n * All the event handlers should be into a separated function. No usage of anonymous function\n */\n bindEvents() {\n Event.on('cart.product.add.failed', this.onAddToCartFailed, this);\n on('product.updated', this.element, this.onProductUpdated.bind(this));\n Event.on('cart.product.added', this.clear, this);\n Event.on('cart.updated', this.clear, this);\n }\n\n /**\n * Custom event handler that is triggered on add product error.\n *\n * @param {Event} e Event object\n */\n onAddToCartFailed(e) {\n if (e.data.pid === this.options.pid && e.data.key === this.options.key) {\n const errorList = e.data.errorList || [];\n\n this.showErrorMessages(errorList);\n }\n }\n\n /**\n * Update product id\n *\n * @param {Object} e - event data\n */\n onProductUpdated(e) {\n if (e.detail.previousPid === this.options.pid) {\n this.options.pid = e.detail.pid;\n }\n this.clear();\n }\n\n /**\n * Clear state isError\n *\n * @returns {Boolean} true or false\n */\n clear() {\n if (this.state.isError) {\n this.element.classList.remove(...this.options.classNames.errorWrapper.split(' '));\n this.errorElement.remove();\n this.state.isError = false;\n\n return true;\n }\n return false;\n }\n\n /**\n * Show error messages\n * @param {String} errorList list of error messages\n */\n showErrorMessages(errorList) {\n const newErrorElement = document.createElement('ul');\n\n const errorWrapperClasses = this.options.classNames.errorWrapper.split(' ').filter(item => item);\n if (errorWrapperClasses) {\n this.element.classList.add(...errorWrapperClasses);\n }\n const errorListClasses = this.options.classNames.errorListClass.split(' ').filter(item => item);\n if (errorListClasses) {\n newErrorElement.classList.add(...errorListClasses);\n }\n\n errorList.forEach((error) => {\n const errorItemElement = document.createElement('li');\n const itemClasses = this.options.classNames.errorItem.split(' ').filter(item => item);\n if (typeof error === 'object' && error.errorMessage) {\n errorItemElement.innerText = error.errorMessage;\n itemClasses.push(error.type ? `m-${error.type}` : 'm-error');\n } else {\n errorItemElement.innerText = error;\n itemClasses.push('m-error');\n }\n if (itemClasses) {\n errorItemElement.classList.add(...itemClasses);\n }\n newErrorElement.append(errorItemElement);\n }, this);\n\n if (this.state.isError) {\n this.element.replaceChild(newErrorElement, this.errorElement);\n } else {\n this.element.appendChild(newErrorElement);\n }\n\n this.errorElement = newErrorElement;\n this.state.isError = true;\n }\n\n /**\n * Destroy is called automatically after the component is being removed from the DOM\n * You must always destroy the listeners attached to an element to avoid any memory leaks\n */\n destroy() {\n Event.removeListener('cart.product.add.failed', this.onAddToCartFailed, this);\n off('product.updated', this.element);\n Event.removeListener('cart.product.added', this.clear, this);\n Event.removeListener('cart.updated', this.clear, this);\n }\n}\n"],"mappings":"mJAWqBA,CAAM,QAAAC,CAAA,oBAAAC,OAAA,WAAAD,CAAA,EAXlBE,CAAK,CAAAF,CAAA,CAALE,KAAK,WAAAF,CAAA,EACLG,CAAE,CAAAH,CAAA,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