{"version":3,"sources":["estore/cart/site-cart-controller.ts"],"names":["cartEventPublisher","cartEventSubscriber","CartService","CartControllerUtils","OrderLineType","FreeShippingApi","SiteCartController","constructor","this","_inclVat","window","globalSettings","displayPricesIncludingVat","_siteCartInfo","undefined","_cartService","_utils","_freeShipipngApi","cartSettings","Error","instance","_getData","initEventSubscibtions","onCartItemsAdded","_onCartItemsAdded","bind","onCartItemsRemoved","_onCartItemsRemoved","onCartEmptied","_onCartEmptied","onCartQuantityUpdated","_onCartQuantityUpdated","getSiteCartInfo","removeLine","line","removeFromCart","tempOrderLineID","error","console","log","clearCart","emptyCart","topic","eventData","_updateSiteCartInfo","result","detailedCartInfo","then","_publishDetailedCartChangedEvent","siteCartChanged","_isOrderLineTypeText","lineType","ExtraField","InscriptionTextLine","data","freeShippingInfo","getFreeShippingData","_orderSumAmount","products","_excludeProductsFromSiteCart","extendDataForUi","setGroupingParams","summary","totalPrice","totalPriceExclVat","filter","product","parentID","offerID"],"mappings":"OAASA,mBAAoBC,2EAEpBC,2CACAC,4DAGAC,+EACAC,mDAEIC,mBAGTC,cACI,GA0DIC,KAAAC,SAAoBC,OAAOC,eAAeC,0BAC1CJ,KAAAK,mBAAoDC,EACpDN,KAAAO,aAAe,IAAIb,YACnBM,KAAAQ,OAAS,IAAIb,oBACbK,KAAAS,iBAAmB,IAAIZ,iBA9DvBK,OAAOQ,aAAc,MAAMC,MAAM,iCAOrC,OALKb,mBAAmBc,YACpBd,mBAAmBc,SAAWZ,MACzBa,WACLb,KAAKc,yBAEFhB,mBAAmBc,SAIvBE,wBACHrB,oBAAoBsB,iBAAiBf,KAAKgB,kBAAkBC,KAAKjB,OACjEP,oBAAoByB,mBAAmBlB,KAAKmB,oBAAoBF,KAAKjB,OACrEP,oBAAoB2B,cAAcpB,KAAKqB,eAAeJ,KAAKjB,OAC3DP,oBAAoB6B,sBAAsBtB,KAAKuB,uBAAuBN,KAAKjB,OAGxEwB,kBACH,OAAOxB,KAAKK,cAGToB,iBAAiB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AK0C,qBAAqBgB,EAAQf,WAAkC,IAArBe,EAAQC,UAAsC,EAAnBD,EAAQC,UAAgBD,EAAQE,SAA6B,EAAlBF,EAAQE,SAEtI5D,KAAKK,cAAc6C,iBA1GrBpD","file":"site-cart-controller.js","sourcesContent":["import { cartEventPublisher, cartEventSubscriber } from \"../../common/pubsub/cart-event-pub-sub.js\";\r\nimport { CartItemsResultEventData, CartItemsAddedResultEventData, CartItemsRemovedResultEventData} from \"../../common/pubsub/models/cart-items-result-event-data.js\";\r\nimport { CartService } from \"./cart-service.js\";\r\nimport { CartControllerUtils } from \"./cart-controller-utils.js\";\r\nimport { IDetailedCartInfo, IDetailedCartInfoForUi } from \"./interfaces/detailed-cart-info.js\";\r\nimport { TempOrderLineProductContractUiHelper, TempOrderLineProductContract } from \"./interfaces/temp-order-line-product-contract.js\";\r\nimport { OrderLineType } from \"../../../components-shared/enums/order-line-type.js\";\r\nimport { FreeShippingApi } from \"./free-shipping-api.js\";\r\n\r\nexport class SiteCartController {\r\n private static instance: SiteCartController;\r\n\r\n constructor() {\r\n if(!window.cartSettings) throw Error(\"Cart settings are not defined\");\r\n\r\n if (!SiteCartController.instance) {\r\n SiteCartController.instance = this;\r\n this._getData();\r\n this.initEventSubscibtions();\r\n }\r\n return SiteCartController.instance;\r\n }\r\n\r\n /* #region Public */\r\n public initEventSubscibtions(){\r\n cartEventSubscriber.onCartItemsAdded(this._onCartItemsAdded.bind(this));\r\n cartEventSubscriber.onCartItemsRemoved(this._onCartItemsRemoved.bind(this));\r\n cartEventSubscriber.onCartEmptied(this._onCartEmptied.bind(this));\r\n cartEventSubscriber.onCartQuantityUpdated(this._onCartQuantityUpdated.bind(this));\r\n }\r\n\r\n public getSiteCartInfo(): IDetailedCartInfo | undefined{\r\n return this._siteCartInfo;\r\n }\r\n\r\n public async removeLine(line: TempOrderLineProductContract){\r\n try{\r\n await this._cartService.removeFromCart(line.tempOrderLineID);\r\n } catch(error) {\r\n console.log('Remove orderline error', error);\r\n }\r\n }\r\n\r\n public async clearCart(){\r\n try{\r\n await this._cartService.emptyCart();\r\n } catch(error) {\r\n console.log('Clear cart failed', error);\r\n }\r\n }\r\n /* #endregion */\r\n\r\n /* #region Event handlers */\r\n private async _onCartQuantityUpdated(topic: string, eventData: CartItemsResultEventData){\r\n await this._updateSiteCartInfo(eventData.result.detailedCartInfo);\r\n }\r\n\r\n private async _onCartItemsAdded(topic: string, eventData: CartItemsAddedResultEventData){\r\n await this._updateSiteCartInfo(eventData.result.detailedCartInfo);\r\n }\r\n\r\n private async _onCartItemsRemoved(topic: string, eventData: CartItemsRemovedResultEventData){\r\n await this._updateSiteCartInfo(eventData.result.detailedCartInfo);\r\n }\r\n\r\n private async _onCartEmptied(topic: string, eventData: CartItemsRemovedResultEventData){\r\n await this._updateSiteCartInfo(eventData.result.detailedCartInfo);\r\n }\r\n /* #endregion */\r\n\r\n /* #region Private */\r\n private _inclVat: boolean = window.globalSettings.displayPricesIncludingVat;\r\n private _siteCartInfo: IDetailedCartInfoForUi | undefined = undefined;\r\n private _cartService = new CartService();\r\n private _utils = new CartControllerUtils();\r\n private _freeShipipngApi = new FreeShippingApi();\r\n\r\n private _getData(): void{\r\n this._cartService.getSiteCartInfo()\r\n .then((detailedCartInfo) => {\r\n this._updateSiteCartInfo(detailedCartInfo);\r\n })\r\n }\r\n\r\n private _publishDetailedCartChangedEvent() {\r\n cartEventPublisher.siteCartChanged(this._siteCartInfo);\r\n }\r\n\r\n private _isOrderLineTypeText(lineType: OrderLineType): boolean { ///TODO: move to utils but not controller? Review after order-line refactoring\r\n return lineType === OrderLineType.ExtraField ||\r\n lineType === OrderLineType.InscriptionTextLine;\r\n }\r\n\r\n private async _updateSiteCartInfo(data: IDetailedCartInfo) {\r\n if (data) {\r\n const freeShippingInfo = await this._freeShipipngApi.getFreeShippingData(this._orderSumAmount(data)); //TODO: to use this record and avoid an extra request to FreeShippingApi - we need get propper prices from the server at freeshippinginfo contract (inclVat or/and exclVat).\r\n\r\n this._siteCartInfo = {...data} as IDetailedCartInfoForUi;\r\n this._siteCartInfo.products = [... this._excludeProductsFromSiteCart(this._siteCartInfo.products)];\r\n this._siteCartInfo.products = [... this._utils.extendDataForUi(this._siteCartInfo.products)];\r\n this._siteCartInfo.products = [... this._utils.setGroupingParams(this._siteCartInfo.products, true)];\r\n this._siteCartInfo.freeShippingInfo = freeShippingInfo;\r\n\r\n this._publishDetailedCartChangedEvent();\r\n }\r\n }\r\n\r\n private _orderSumAmount(data: IDetailedCartInfoForUi){\r\n return this._inclVat ? data.summary.totalPrice : data.summary.totalPriceExclVat;\r\n }\r\n\r\n private _excludeProductsFromSiteCart(products: TempOrderLineProductContractUiHelper[]): TempOrderLineProductContractUiHelper[]{\r\n this._siteCartInfo.products = products.filter(\r\n product => !this._isOrderLineTypeText(product.lineType) && product.parentID === 0 || (product.parentID > 0 && product.offerID && product.offerID > 0)\r\n );\r\n return this._siteCartInfo.products;\r\n }\r\n /* #endregion */\r\n}"]}