{"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","instance","_getData","initEventSubscibtions","Error","onCartItemsAdded","_onCartItemsAdded","bind","onCartItemsRemoved","_onCartItemsRemoved","onCartEmptied","_onCartEmptied","onCartQuantityUpdated","_onCartQuantityUpdated","getSiteCartInfo","removeLine","line","await","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,mBAAsE,KAA3C,mDAE/CC,WAAsC,KAAnB,2BACnBC,mBAAuD,KAA5B,oCAG3BC,aAA0E,KAArD,6DACrBC,eAA+C,KAAxB,+BAEnBC,mBAGTC,cACI,GA0DIC,KAAAC,SAAoBC,OAAOC,eAAeC,0BAC1CJ,KAAAK,cAAoDC,KAAAA,EACpDN,KAAAO,aAAe,IAAIb,YACnBM,KAAAQ,OAAS,IAAIb,oBACbK,KAAAS,iBAAmB,IAAIZ,gBA9DvBK,OAAOQ,aAOX,OALKZ,mBAAmBa,YACpBb,mBAAmBa,SAAWX,MACzBY,SAAQ,EACbZ,KAAKa,sBAAqB,GAEvBf,mBAAmBa,SAPD,MAAMG,MAAM,+BAA+B,CAQxE,CAGOD,wBACHpB,oBAAoBsB,iBAAiBf,KAAKgB,kBAAkBC,KAAKjB,IAAI,CAAC,EACtEP,oBAAoByB,mBAAmBlB,KAAKmB,oBAAoBF,KAAKjB,IAAI,CAAC,EAC1EP,oBAAoB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cAAc8C,QAAQ,GAChGnD,KAAKK,cAAc8C,SAAW,CAAC,GAAInD,KAAKQ,OAAO6C,gBAAgBrD,KAAKK,cAAc8C,QAAQ,GAC1FnD,KAAKK,cAAc8C,SAAW,CAAC,GAAInD,KAAKQ,OAAO8C,kBAAkBtD,KAAKK,cAAc8C,SAAU,CAAA,CAAI,GAClGnD,KAAKK,cAAc2C,iBAAmBA,EAEtChD,KAAKyC,iCAAgC,EAE7C,CAEQS,gBAAgBH,GACpB,OAAO/C,KAAKC,SAAW8C,EAAKQ,QAAQC,WAAaT,EAAKQ,QAAQE,iBAClE,CAEQL,6BAA6BD,GAIjC,OAHAnD,KAAKK,cAAc8C,SAAWA,EAASO,OACnCC,GAAW,CAAC3D,KAAK2C,qBAAqBgB,EAAQf,QAAQ,GAA0B,IAArBe,EAAQC,UAAsC,EAAnBD,EAAQC,UAAgBD,EAAQE,SAA6B,EAAlBF,EAAQE,OAAY,EAElJ7D,KAAKK,cAAc8C,QAC9B,C,QA3GSrD,kB","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}"]}