{"version":3,"sources":["estore/cart/cart-service.ts"],"names":["cartEventPublisher","ImageMode","EventActionResult","Notify","CartService","constructor","this","_isPrivateAction","_includeVat","window","globalSettings","displayPricesIncludingVat","_notify","instance","_detailedCartData","couponCodes","products","summary","freeShippingInfo","_siteCartData","_showErrorMessage","errorMessage","error","emptyCart","clearCartEventData","PublicEStore","then","data","status","Ok","cartEmptied","result","detailedCartInfo","imageWidth","_getDetailedCartInfo","Promise","resolve","reject","getDetailedCartInfo","Pad","getSiteCartInfo","_getSiteCartInfo","removeFromCart","tempOrderLineID","removeFromCartContract","removeFromCartEventData","cartItemsRemoved","addToCart","productId","quantity","updateCartQuantity","productID","updateCartEventData","cartQuantityUpdated","resetDetailedCartInfoCache","resetSiteCartInfoCache","createOffer","response","fetch","method","mode","cache","credentials","headers","Content-Type","json","ok","success","offerId","location","href","message","Message"],"mappings":"OAESA,uBAA0B,mDAC1BC,cAAiB,+CACjBC,sBAAyB,uDAGzBC,WAAc,mDAEVC,YAGTC,cAkBI,OAKIC,KAAAC,kBAA2B,EAC3BD,KAAAE,YAAsBC,OAAOC,eAAeC,0BAG5CL,KAAAM,QAAU,IAAIT,OA1BbC,YAAYS,WACbT,YAAYS,SAAWP,MAG3BA,KAAKQ,kBAAoB,CACrBC,YAAa,GACbC,SAAU,GACVC,QAAS,KACTC,iBAAkB,MAGtBZ,KAAKa,cAAgB,CACjBJ,YAAa,GACbC,SAAU,GACVC,QAAS,KACTC,iBAAkB,MAEfd,YAAYS,SAWfO,kBAAkBC,GACtBf,KAAKM,QAAQU,MAAMD,GAIhBE,kBACH,IAAMC,QAA2Bf,OAAOgB,aAAaF,UAAUjB,KAAKC,kBAAkBmB,KAAK,GAAUC,GAMrG,OAJIH,EAAmBI,SAAW1B,kBAAkB2B,IAChD7B,mBAAmB8B,YAAYN,GAG5BA,EAAmBO,OAAOC,iBAG9BA,uBAAuBxB,GAAsB,EAAMD,GAA2B,EAAM0B,EAAoB,IAwB3G,OAvBK3B,KAAK4B,u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F,EAAmBC,EAAkBE,GACjE,IAMMC,QAA4B3C,OAAOgB,aAAayB,mBANnB,CAC/BP,gBAAiBK,EACjBC,SAAUA,EACVE,UAAWA,GAGsF7C,KAAKC,kBAM1G,OAJI6C,EAAoBrB,QACpB/B,mBAAmBqD,oBAAoBD,GAGpCA,EAGJE,6BACHhD,KAAK4B,qBAAuB,KAC5B5B,KAAKQ,kBAAkBG,QAAU,KAG9BsC,yBACHjD,KAAKmC,iBAAmB,KACxBnC,KAAKa,cAAcF,QAAU,KAG1BuC,oBAWH,MAAMC,QAAiBC,MAAM,kCAVT,CAChBC,OAAQ,OACRC,KAAM,OACNC,MAAO,WACPC,YAAa,cACbC,QAAS,CACLC,eAAgB,sBAKxB,IAAMjC,QAAe0B,EAASQ,OAEzBR,EAASS,GAMVnC,EAAOoC,SAAWpC,EAAOqC,UACzB3D,OAAO4D,SAASC,KAAO,wCAAwCvC,EAAOqC,UANhE/C,EAAeU,EAAOwC,SAA2BxC,EAAOyC,SAA2B,6CACzFlE,KAAKc,kBAAkBC,KA/KhBjB,YAAAS,SAA+B,YADrCT","file":"cart-service.js","sourcesContent":["import { CartItemsAddedResultEventData, CartItemsRemovedResultEventData, CartItemsResultEventData } from \"../../common/pubsub/models/cart-items-result-event-data.js\";\nimport { IDetailedCartInfo } from \"./interfaces/detailed-cart-info.js\";\nimport { cartEventPublisher } from \"../../common/pubsub/cart-event-pub-sub.js\";\nimport { ImageMode } from \"../../common/interfaces/image-mode.js\";\nimport { EventActionResult } from \"../../common/pubsub/models/event-data-base.js\";\nimport { RemoveFromCartItemContract } from \"../../common/public-estore/remove-from-cart-contract.js\";\nimport { UpdateCartQuantityItemContract } from \"../../common/public-estore/update-cart-quantity-contract.js\";\nimport { Notify } from \"../../../components-shared/utils/notify.js\";\n\nexport class CartService {\n private static instance: CartService | null = null;\n\n constructor() {\n if (!CartService.instance) {\n CartService.instance = this;\n } \n\n this._detailedCartData = {\n couponCodes: [],\n products: [],\n summary: null,\n freeShippingInfo: null,\n }\n\n this._siteCartData = {\n couponCodes: [],\n products: [],\n summary: null,\n freeShippingInfo: null,\n }\n return CartService.instance as CartService;\n }\n\n private _getDetailedCartInfo: Promise;\n private _getSiteCartInfo: Promise;\n private _isPrivateAction:boolean = true; // What is it? Developers told that it requried to be checked, could be useless.\n private _includeVat:boolean = window.globalSettings.displayPricesIncludingVat;\n private _detailedCartData: IDetailedCartInfo;\n private _siteCartData: IDetailedCartInfo;\n private _notify = new Notify(); \n\n private _showErrorMessage(errorMessage: string){\n this._notify.error(errorMessage);\n } \n \n /* #region Public */\n public async emptyCart() : Promise{\n const clearCartEventData = await window.PublicEStore.emptyCart(this._isPrivateAction).then((data) => data);\n\n if (clearCartEventData.status === EventActionResult.Ok) {\n cartEventPublisher.cartEmptied(clearCartEventData);\n }\n\n return clearCartEventData.result.detailedCartInfo;\n }\n\n public async detailedCartInfo(_includeVat:boolean = true, _isPrivateAction:boolean = true, imageWidth:number = 64) : Promise{\n if (!this._getDetailedCartInfo) {\n this._getDetailedCartInfo = new Promise((resolve, reject)=>{\n // The line below returns cache result is one more request fired. \n // Resolves the issue when webcomponent based on CartController included in the component based on CartController, ex: free-shipping-progress-bar in cart-sidebar-v1\n // Creates an issue if timed out request needs to be fired again, then resetDetailedCartInfoCache() should be called before forced request.\n // Probably can be done better.\n this._detailedCartData.summary ? resolve(this._detailedCartData) : \n window.PublicEStore\n .getDetailedCartInfo(_includeVat, _isPrivateAction, imageWidth, ImageMode.Pad)\n .then((data) => {\n if(data.status === EventActionResult.Ok){\n this._detailedCartData = data.result.detailedCartInfo;\n resolve(this._detailedCartData);\n } else {\n reject(data);\n }\n this._getDetailedCartInfo = null;\n }, (error) => {\n reject(error)\n this._getDetailedCartInfo = null;\n })\n });\n }\n return this._getDetailedCartInfo;\n }\n\n public async getSiteCartInfo(_includeVat:boolean = true, _isPrivateAction:boolean = true, imageWidth:number = 60) : Promise{\n if (!this._getSiteCartInfo) {\n this._getSiteCartInfo = new Promise((resolve, reject)=>{\n // The line below returns cache result is one more request fired. \n // Resolves the issue when webcomponent based on CartController included in the component based on CartController, ex: free-shipping-progress-bar in cart-sidebar-v1\n // Creates an issue if timed out request needs to be fired again, then resetDetailedCartInfoCache() should be called before forced request.\n // Probably can be done better.\n this._siteCartData.summary ? resolve(this._siteCartData) : \n window.PublicEStore\n .getSiteCartInfo(_includeVat, _isPrivateAction, imageWidth, ImageMode.Pad)\n .then((data) => {\n if(data.status === EventActionResult.Ok){\n this._siteCartData = data.result.detailedCartInfo;\n resolve(this._siteCartData);\n } else {\n reject(data);\n }\n this._getSiteCartInfo = null;\n }, (error) => {\n reject(error)\n this._getSiteCartInfo = null;\n })\n });\n }\n return this._getSiteCartInfo;\n }\n\n public async removeFromCart(tempOrderLineID: number) : Promise {\n try{\n const removeFromCartContract = {\n tempOrderLineID: tempOrderLineID,\n } as RemoveFromCartItemContract;\n\n const removeFromCartEventData = await window.PublicEStore.removeFromCart(removeFromCartContract, this._isPrivateAction).then((data) => data);\n\n if (removeFromCartEventData.result) {\n cartEventPublisher.cartItemsRemoved(removeFromCartEventData);\n }\n\n return removeFromCartEventData;\n\n } catch (error){\n throw error;\n }\n }\n\n public async addToCart(productId: number, quantity: number) : Promise{\n const addToCartContract = {\n productId: productId,\n //imageId: $prodContainer.data(dataNames.imageId),\n quantity: quantity,\n //fraction: $prodContainer.find('.' + classes.fractionInput).val(),\n //customFields: customFields\n };\n\n const addToCartEventData = await window.PublicEStore.addToCart(addToCartContract, this._includeVat, this._isPrivateAction);\n\n return addToCartEventData;\n }\n\n public async updateCartQuantity(productId: number, quantity: number, productID: number) : Promise{\n const udpateCartQuantityContract = {\n tempOrderLineID: productId,\n quantity: quantity,\n productID: productID,\n } as UpdateCartQuantityItemContract;\n\n const updateCartEventData = await window.PublicEStore.updateCartQuantity(udpateCartQuantityContract, this._isPrivateAction);\n\n if (updateCartEventData.result) {\n cartEventPublisher.cartQuantityUpdated(updateCartEventData);\n }\n\n return updateCartEventData;\n }\n\n public resetDetailedCartInfoCache() {\n this._getDetailedCartInfo = null;\n this._detailedCartData.summary = null;\n }\n\n public resetSiteCartInfoCache() {\n this._getSiteCartInfo = null;\n this._siteCartData.summary = null;\n }\n\n public async createOffer() {\n const requestInit = {\n method: \"POST\",\n mode: \"cors\",\n cache: \"no-cache\",\n credentials: \"same-origin\",\n headers: {\n \"Content-Type\": \"application/json\"\n }\n } as RequestInit;\n\n const response = await fetch(`/api/shopping-cart/create-offer`, requestInit);\n const result = await response.json();\n\n if (!response.ok) {\n const errorMessage = result.message ? result.message : result.Message ? result.Message : \"An error occurred while creating the offer\";\n this._showErrorMessage(errorMessage);\n return;\n }\n\n if (result.success && result.offerId) {\n window.location.href = \"/Startup/Common/Default.aspx?offerId=\"+result.offerId;\n }\n }\n\n /* #endregion */\n}"]}