{"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","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,wBAA2B,mDAE/CC,gBAAmB,2BACnBC,wBAA2B,oCAG3BC,kBAAqB,6DACrBC,oBAAuB,+BAEnBC,mBAGTC,cACI,GA0DIC,KAAAC,SAAoBC,OAAOC,eAAeC,0BAC1CJ,KAAAK,mBAAoDC,EACpDN,KAAAO,aAAe,IAAIb,YACnBM,KAAAQ,OAAS,IAAIb,oBACbK,KAAAS,iBAAmB,IAAIZ,gBA9DvBK,OAAOQ,aAOX,OALKZ,mBAAmBa,YACpBb,mBAAmBa,SAAWX,MACzBY,WACLZ,KAAKa,yBAEFf,mBAAmBa,SAPD,MAAMG,MAAM,iCAWlCD,wBACHpB,oBAAoBsB,iBAAiBf,KAAKgB,kBAAkBC,KAAKjB,OACjEP,oBAAoByB,mBAAmBlB,KAAKmB,oBAAoBF,KAAKjB,OACrEP,oBAAoB2B,cAAcpB,KAAKqB,eAAeJ,KAAKjB,OAC3DP,oBAAoB6B,sBAAsBtB,KAAKuB,uBAAuBN,KAAKjB,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KAAKK,cAAc6C,SAAWA,EAASO,OACnCC,IAAY1D,KAAK0C,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\";\nimport { CartItemsResultEventData, CartItemsAddedResultEventData, CartItemsRemovedResultEventData} from \"../../common/pubsub/models/cart-items-result-event-data.js\";\nimport { CartService } from \"./cart-service.js\";\nimport { CartControllerUtils } from \"./cart-controller-utils.js\";\nimport { IDetailedCartInfo, IDetailedCartInfoForUi } from \"./interfaces/detailed-cart-info.js\";\nimport { TempOrderLineProductContractUiHelper, TempOrderLineProductContract } from \"./interfaces/temp-order-line-product-contract.js\";\nimport { OrderLineType } from \"../../../components-shared/enums/order-line-type.js\";\nimport { FreeShippingApi } from \"./free-shipping-api.js\";\n\nexport class SiteCartController {\n private static instance: SiteCartController;\n\n constructor() {\n if(!window.cartSettings) throw Error(\"Cart settings are not defined\");\n\n if (!SiteCartController.instance) {\n SiteCartController.instance = this;\n this._getData();\n this.initEventSubscibtions();\n }\n return SiteCartController.instance;\n }\n\n /* #region Public */\n public initEventSubscibtions(){\n cartEventSubscriber.onCartItemsAdded(this._onCartItemsAdded.bind(this));\n cartEventSubscriber.onCartItemsRemoved(this._onCartItemsRemoved.bind(this));\n cartEventSubscriber.onCartEmptied(this._onCartEmptied.bind(this));\n cartEventSubscriber.onCartQuantityUpdated(this._onCartQuantityUpdated.bind(this));\n }\n\n public getSiteCartInfo(): IDetailedCartInfo | undefined{\n return this._siteCartInfo;\n }\n\n public async removeLine(line: TempOrderLineProductContract){\n try{\n await this._cartService.removeFromCart(line.tempOrderLineID);\n } catch(error) {\n console.log('Remove orderline error', error);\n }\n }\n\n public async clearCart(){\n try{\n await this._cartService.emptyCart();\n } catch(error) {\n console.log('Clear cart failed', error);\n }\n }\n /* #endregion */\n\n /* #region Event handlers */\n private async _onCartQuantityUpdated(topic: string, eventData: CartItemsResultEventData){\n await this._updateSiteCartInfo(eventData.result.detailedCartInfo);\n }\n\n private async _onCartItemsAdded(topic: string, eventData: CartItemsAddedResultEventData){\n await this._updateSiteCartInfo(eventData.result.detailedCartInfo);\n }\n\n private async _onCartItemsRemoved(topic: string, eventData: CartItemsRemovedResultEventData){\n await this._updateSiteCartInfo(eventData.result.detailedCartInfo);\n }\n\n private async _onCartEmptied(topic: string, eventData: CartItemsRemovedResultEventData){\n await this._updateSiteCartInfo(eventData.result.detailedCartInfo);\n }\n /* #endregion */\n\n /* #region Private */\n private _inclVat: boolean = window.globalSettings.displayPricesIncludingVat;\n private _siteCartInfo: IDetailedCartInfoForUi | undefined = undefined;\n private _cartService = new CartService();\n private _utils = new CartControllerUtils();\n private _freeShipipngApi = new FreeShippingApi();\n\n private _getData(): void{\n this._cartService.getSiteCartInfo()\n .then((detailedCartInfo) => {\n this._updateSiteCartInfo(detailedCartInfo);\n })\n }\n\n private _publishDetailedCartChangedEvent() {\n cartEventPublisher.siteCartChanged(this._siteCartInfo);\n }\n\n private _isOrderLineTypeText(lineType: OrderLineType): boolean { ///TODO: move to utils but not controller? Review after order-line refactoring\n return lineType === OrderLineType.ExtraField ||\n lineType === OrderLineType.InscriptionTextLine;\n }\n\n private async _updateSiteCartInfo(data: IDetailedCartInfo) {\n if (data) {\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).\n\n this._siteCartInfo = {...data} as IDetailedCartInfoForUi;\n this._siteCartInfo.products = [... this._excludeProductsFromSiteCart(this._siteCartInfo.products)];\n this._siteCartInfo.products = [... this._utils.extendDataForUi(this._siteCartInfo.products)];\n this._siteCartInfo.products = [... this._utils.setGroupingParams(this._siteCartInfo.products, true)];\n this._siteCartInfo.freeShippingInfo = freeShippingInfo;\n\n this._publishDetailedCartChangedEvent();\n }\n }\n\n private _orderSumAmount(data: IDetailedCartInfoForUi){\n return this._inclVat ? data.summary.totalPrice : data.summary.totalPriceExclVat;\n }\n\n private _excludeProductsFromSiteCart(products: TempOrderLineProductContractUiHelper[]): TempOrderLineProductContractUiHelper[]{\n this._siteCartInfo.products = products.filter(\n product => !this._isOrderLineTypeText(product.lineType) && product.parentID === 0 || (product.parentID > 0 && product.offerID && product.offerID > 0)\n );\n return this._siteCartInfo.products;\n }\n /* #endregion */\n}"]}