b23-lib 1.8.6 → 1.8.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/Classes/LineItem.ts"],"names":["LineItemModel","data","ImageInfoModel","item","tier","locale","sum","quantity","unitPrice","applicableTier","b","subItems","addQuantity","subItem","existingSubItem","sub"],"mappings":"qCAmCqBA,IAAAA,CAAAA,CAArB,KAAmC,CACvB,EACA,CAAA,UAAA,CACA,UACA,IACA,CAAA,UAAA,CAGA,cACA,CAAA,YAAA,CACA,QACA,CAAA,aAAA,CACA,UACA,WAIA,CAAA,UAAA,CAMV,WAAYC,CAAAA,CAAAA,CAA0B,CAClC,IAAA,CAAK,GAAKA,CAAK,CAAA,EAAA,CACf,IAAK,CAAA,UAAA,CAAaA,CAAK,CAAA,UAAA,CACvB,KAAK,SAAYA,CAAAA,CAAAA,CAAK,SAEtB,CAAA,IAAA,CAAK,IAAO,CAAA,CAAE,GAAGA,CAAK,CAAA,IAAK,CAC3B,CAAA,IAAA,CAAK,UAAa,CAAA,CAAE,GAAGA,CAAK,CAAA,UAAW,CACvC,CAAA,IAAA,CAAK,cAAiB,CAAA,CAAC,GAAGA,CAAAA,CAAK,cAAe,CAAA,CAC9C,IAAK,CAAA,YAAA,CAAe,IAAIC,CAAAA,CAAeD,EAAK,YAAY,CAAA,CACxD,IAAK,CAAA,QAAA,CAAWA,CAAK,CAAA,QAAA,CAAS,IAAIE,CAAS,GAAA,CAAE,GAAGA,CAAK,CAAE,CAAA,CAAA,CACvD,KAAK,SAAY,CAAA,CAAE,GAAGF,CAAAA,CAAK,SAAU,CAAA,CACrC,KAAK,UAAaA,CAAAA,CAAAA,CAAK,UAAW,CAAA,GAAA,CAAIG,CAAS,GAAA,CAAE,GAAGA,CAAK,CAAA,CAAE,CAE3D,CAAA,IAAA,CAAK,aAAgB,CAAA,CAAA,CACrB,KAAK,WAAc,CAAA,CACf,QAAU,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CAAAA,CAAqBC,CAA4B,CAAA,CAC3DD,EAAS,OAAQE,CAAAA,CAAAA,EAAW,CAC1B,IAAMC,CAAkB,CAAA,IAAA,CAAK,SAAS,IAAKX,CAAAA,CAAAA,EAAQA,CAAK,CAAA,IAAA,GAASU,CAAQ,CAAA,IAAI,EACzEC,CACAA,CAAAA,CAAAA,CAAgB,QAAWF,CAAAA,CAAAA,CAAcE,CAAgB,CAAA,QAAA,CAAWD,EAAQ,QAAWA,CAAAA,CAAAA,CAAQ,QAE/F,CAAA,IAAA,CAAK,QAAS,CAAA,IAAA,CAAKA,CAAO,EAEhC,CAAC,CACD,CAAA,IAAA,CAAK,QAAW,CAAA,IAAA,CAAK,QAAS,CAAA,MAAA,CAAOE,CAAOA,EAAAA,CAAAA,CAAI,QAAQ,CAAA,CAExD,IAAK,CAAA,wBAAA,GACL,IAAK,CAAA,qBAAA,GACP,CAOA,aAAsB,EAAA,CACpB,KAAK,EAAK,CAAA,EAAA,CACV,IAAK,CAAA,UAAA,CAAa,EAClB,CAAA,IAAA,CAAK,UAAY,EACjB,CAAA,IAAA,CAAK,IAAO,CAAA,CAAE,EAAI,CAAA,EAAG,EACrB,IAAK,CAAA,UAAA,CAAa,CAAE,KAAA,CAAO,CAAE,IAAA,CAAM,GAAI,GAAK,CAAA,EAAG,CAAE,CAAA,CACjD,IAAK,CAAA,YAAA,CAAe,IAAIb,CAAe,CAAA,CAAE,OAAS,CAAA,CAAE,QAAU,CAAA,EAAG,CAAE,CAAC,CAAA,CACpE,IAAK,CAAA,QAAA,CAAW,EAAC,CACjB,IAAK,CAAA,wBAAA,EACL,CAAA,IAAA,CAAK,qBAAsB,GAC7B,CACF","file":"chunk-STQZVQ7K.mjs","sourcesContent":["import { BasePrice, Color, LocaleCode, LocalizedString, PriceTier } from \"./Common\";\r\nimport ImageInfoModel, { ImageInfoData } from \"./ImageInfo\";\r\nimport { LocalizedProductSpecification, ProductSpecification } from \"./Product\";\r\n\r\nexport type SubItem = {\r\n size: string;\r\n quantity: number;\r\n};\r\n\r\nexport type LineItemAttributes = {\r\n id: string;\r\n productKey: string;\r\n variantId: string;\r\n name: LocalizedString;\r\n attributes: {\r\n color: Color;\r\n };\r\n specifications: LocalizedProductSpecification;\r\n primaryImage: ImageInfoData;\r\n subItems: SubItem[];\r\n basePrice: BasePrice;\r\n priceTiers: PriceTier[];\r\n}\r\n\r\nexport type LineItemData = LineItemAttributes & {\r\n totalQuantity: number;\r\n priceTotals: {\r\n subtotal: number;\r\n mrpTotal: number;\r\n };\r\n}\r\n\r\n/**\r\n * Represents a line item within a shopping cart.\r\n */\r\nexport default class LineItemModel {\r\n protected id: string;\r\n protected productKey: string;\r\n protected variantId: string;\r\n protected name: LocalizedString;\r\n protected attributes: {\r\n color: Color;\r\n };\r\n protected specifications: LocalizedProductSpecification;\r\n protected primaryImage: ImageInfoModel;\r\n protected subItems: SubItem[];\r\n protected totalQuantity: number;\r\n protected basePrice: BasePrice;\r\n protected priceTotals: {\r\n subtotal: number;\r\n mrpTotal: number;\r\n };\r\n protected priceTiers: PriceTier[];\r\n\r\n /**\r\n * Creates an instance of LineItemModel.\r\n * @param data - The initial line item attributes.\r\n */\r\n constructor(data: LineItemAttributes) {\r\n this.id = data.id;\r\n this.productKey = data.productKey;\r\n this.variantId = data.variantId;\r\n\r\n this.name = { ...data.name };\r\n this.attributes = { ...data.attributes };\r\n this.specifications = {...data.specifications };\r\n this.primaryImage = new ImageInfoModel(data.primaryImage);\r\n this.subItems = data.subItems.map(item => ({ ...item }));\r\n this.basePrice = { ...data.basePrice };\r\n this.priceTiers = data.priceTiers.map(tier => ({ ...tier }));\r\n\r\n this.totalQuantity = 0;\r\n this.priceTotals = {\r\n subtotal: 0,\r\n mrpTotal: 0\r\n };\r\n\r\n this.recalculateTotalQuantity();\r\n this.recalculatePriceTotal();\r\n }\r\n\r\n /** Gets the unique identifier of the line item. */\r\n getId(): string {\r\n return this.id;\r\n }\r\n\r\n /** Gets the product key associated with this line item. */\r\n getProductKey(): string {\r\n return this.productKey;\r\n }\r\n\r\n /** Gets the product variant ID associated with this line item. */\r\n getVariantId(): string {\r\n return this.variantId;\r\n }\r\n\r\n /**\r\n * Gets the full localized product name object.\r\n * @returns A copy of the LocalizedString object for the name.\r\n */\r\n getName(): LocalizedString\r\n /**\r\n * Gets the product name for a specific locale, falling back to English ('en').\r\n * @param locale - The desired locale code.\r\n * @returns The name string for the specified locale.\r\n */\r\n getName(locale: LocaleCode): string\r\n getName(locale?: LocaleCode): LocalizedString | string {\r\n if(locale) {\r\n return this.name[locale] ?? this.name.en;\r\n } else {\r\n return { ...this.name };\r\n }\r\n }\r\n\r\n\r\n /** Gets the variant attributes. */\r\n getAttributes(): { color: Color } {\r\n return { ...this.attributes };\r\n }\r\n\r\n /**\r\n * Gets the full localized product specifications object.\r\n * @returns Product Specifications\r\n */\r\n getSpecifications(): LocalizedProductSpecification\r\n /**\r\n * Gets the product specifications for a specific locale, falling back to English ('en').\r\n * @param locale - The desired locale code.\r\n * @returns The ProductSpecification object for the specified locale, or undefined if not found.\r\n */\r\n getSpecifications(locale: LocaleCode): ProductSpecification | undefined\r\n getSpecifications(locale?: LocaleCode): LocalizedProductSpecification | ProductSpecification | undefined {\r\n if(locale){\r\n return this.specifications[locale] ?? this.specifications.en;\r\n } else {\r\n return JSON.parse(JSON.stringify(this.specifications));\r\n }\r\n }\r\n\r\n /** Gets the image information model for this line item. */\r\n getImage(): ImageInfoModel {\r\n return this.primaryImage;\r\n }\r\n\r\n /** Gets the sub-items (size/quantity breakdown). */\r\n getSubItems(): SubItem[] {\r\n return this.subItems.map(item => ({ ...item }));\r\n }\r\n\r\n /** Gets the total quantity for this line item across all sub-items. */\r\n getTotalQuantity(): number {\r\n return this.totalQuantity;\r\n }\r\n\r\n /** Gets the base price for a single unit. */\r\n getBasePrice(): BasePrice {\r\n return { ...this.basePrice };\r\n }\r\n\r\n /** Gets the calculated price details (subtotal, mrpTotal) for this line item. */\r\n getPriceTotals(): { subtotal: number; mrpTotal: number } {\r\n return { ...this.priceTotals };\r\n }\r\n\r\n /** Gets the applicable price tiers for this product variant. */\r\n getPriceTiers(): PriceTier[] {\r\n return this.priceTiers.map(tier => ({ ...tier }));\r\n }\r\n\r\n /**\r\n * Recalculates the total quantity based on the quantities in subItems.\r\n */\r\n protected recalculateTotalQuantity(): void {\r\n this.totalQuantity = this.subItems.reduce((sum, item) => sum + item.quantity, 0);\r\n }\r\n\r\n /**\r\n * Recalculates the line item's price (subtotal and mrpTotal)\r\n * based on total quantity, base price, and applicable price tiers.\r\n */\r\n protected recalculatePriceTotal(): void {\r\n const quantity = this.totalQuantity;\r\n let unitPrice = this.basePrice.unitPrice;\r\n\r\n let applicableTier = null;\r\n for (const tier of this.priceTiers.sort((a, b) => b.minQuantity - a.minQuantity)) { \r\n if (quantity >= tier.minQuantity) {\r\n applicableTier = tier;\r\n break;\r\n }\r\n }\r\n\r\n if (applicableTier) {\r\n unitPrice = applicableTier.unitPrice;\r\n }\r\n\r\n // Calculate totals\r\n this.priceTotals.mrpTotal = this.basePrice.unitPrice * quantity;\r\n this.priceTotals.subtotal = unitPrice * quantity;\r\n }\r\n\r\n\r\n /**\r\n * Gets a plain data object representing the line item's details.\r\n * @returns LineItemData.\r\n */\r\n getDetails(): LineItemData {\r\n return {\r\n id: this.getId(),\r\n productKey: this.getProductKey(),\r\n variantId: this.getVariantId(),\r\n name: this.getName(),\r\n attributes: this.getAttributes(),\r\n specifications: this.getSpecifications(),\r\n primaryImage: this.getImage().getDetails(),\r\n subItems: this.getSubItems(),\r\n totalQuantity: this.getTotalQuantity(),\r\n basePrice: this.getBasePrice(),\r\n priceTotals: this.getPriceTotals(),\r\n priceTiers: this.getPriceTiers(),\r\n };\r\n }\r\n \r\n /**\r\n * Adds or updates sub-items (e.g., sizes with quantities) to the line item.\r\n * If a sub-item with the same size already exists, its quantity is either\r\n * updated by adding the new quantity or replaced entirely, based on the `addQuantity` flag.\r\n * Sub-items with a resulting quantity of 0 are removed.\r\n * Recalculates total quantity and price totals afterwards.\r\n *\r\n * @param subItems - An array of `SubItem` objects to add or update.\r\n * @param addQuantity - If true, adds the quantity from `subItem` to the existing quantity.\r\n * If false, replaces the existing quantity with the one from `subItem`.\r\n */\r\n addSubItems(subItems: SubItem[], addQuantity: boolean): void {\r\n subItems.forEach(subItem => {\r\n const existingSubItem = this.subItems.find(item => item.size === subItem.size);\r\n if (existingSubItem) {\r\n existingSubItem.quantity = addQuantity ? existingSubItem.quantity + subItem.quantity : subItem.quantity;\r\n } else {\r\n this.subItems.push(subItem);\r\n }\r\n })\r\n this.subItems = this.subItems.filter(sub => sub.quantity);\r\n\r\n this.recalculateTotalQuantity();\r\n this.recalculatePriceTotal();\r\n }\r\n\r\n /**\r\n * Resets the line item's properties to their default empty or initial state.\r\n * Useful for clearing out line item data without creating a new instance.\r\n * Recalculates total quantity and price totals afterwards (which will be zero).\r\n */\r\n clearLineItem(): void {\r\n this.id = '';\r\n this.productKey = '';\r\n this.variantId = '';\r\n this.name = { en: '' };\r\n this.attributes = { color: { name: '', hex: '' } };\r\n this.primaryImage = new ImageInfoModel({ sources: { original: '' } });\r\n this.subItems = [];\r\n this.recalculateTotalQuantity();\r\n this.recalculatePriceTotal();\r\n }\r\n}"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "b23-lib",
3
- "version": "1.8.6",
3
+ "version": "1.8.8",
4
4
  "description": "This repo hold common classes, type and util functiona",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -1,2 +0,0 @@
1
- import {b}from'./chunk-DKI6BDWK.mjs';var o=class{id;productKey;variantId;name;attributes;primaryImage;subItems;totalQuantity;basePrice;priceTotals;priceTiers;constructor(t){this.id=t.id,this.productKey=t.productKey,this.variantId=t.variantId,this.name={...t.name},this.attributes={...t.attributes},this.primaryImage=new b(t.primaryImage),this.subItems=t.subItems.map(e=>({...e})),this.basePrice={...t.basePrice},this.priceTiers=t.priceTiers.map(e=>({...e})),this.totalQuantity=0,this.priceTotals={subtotal:0,mrpTotal:0},this.recalculateTotalQuantity(),this.recalculatePriceTotal();}getId(){return this.id}getProductKey(){return this.productKey}getVariantId(){return this.variantId}getName(t){return t?this.name[t]??this.name.en:{...this.name}}getAttributes(){return {...this.attributes}}getImage(){return this.primaryImage}getSubItems(){return this.subItems.map(t=>({...t}))}getTotalQuantity(){return this.totalQuantity}getBasePrice(){return {...this.basePrice}}getPriceTotals(){return {...this.priceTotals}}getPriceTiers(){return this.priceTiers.map(t=>({...t}))}recalculateTotalQuantity(){this.totalQuantity=this.subItems.reduce((t,e)=>t+e.quantity,0);}recalculatePriceTotal(){let t=this.totalQuantity,e=this.basePrice.unitPrice,i=null;for(let r of this.priceTiers.sort((a,n)=>n.minQuantity-a.minQuantity))if(t>=r.minQuantity){i=r;break}i&&(e=i.unitPrice),this.priceTotals.mrpTotal=this.basePrice.unitPrice*t,this.priceTotals.subtotal=e*t;}getDetails(){return {id:this.getId(),productKey:this.getProductKey(),variantId:this.getVariantId(),name:this.getName(),attributes:this.getAttributes(),primaryImage:this.getImage().getDetails(),subItems:this.getSubItems(),totalQuantity:this.getTotalQuantity(),basePrice:this.getBasePrice(),priceTotals:this.getPriceTotals(),priceTiers:this.getPriceTiers()}}addSubItems(t,e){t.forEach(i=>{let r=this.subItems.find(a=>a.size===i.size);r?r.quantity=e?r.quantity+i.quantity:i.quantity:this.subItems.push(i);}),this.subItems=this.subItems.filter(i=>i.quantity),this.recalculateTotalQuantity(),this.recalculatePriceTotal();}clearLineItem(){this.id="",this.productKey="",this.variantId="",this.name={en:""},this.attributes={color:{name:"",hex:""}},this.primaryImage=new b({sources:{original:""}}),this.subItems=[],this.recalculateTotalQuantity(),this.recalculatePriceTotal();}};export{o as a};//# sourceMappingURL=chunk-WU6FFDBS.mjs.map
2
- //# sourceMappingURL=chunk-WU6FFDBS.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/Classes/LineItem.ts"],"names":["LineItemModel","data","ImageInfoModel","item","tier","locale","sum","quantity","unitPrice","applicableTier","b","subItems","addQuantity","subItem","existingSubItem","sub"],"mappings":"qCAiCqBA,IAAAA,CAAAA,CAArB,KAAmC,CACvB,EAAA,CACA,WACA,SACA,CAAA,IAAA,CACA,UAGA,CAAA,YAAA,CACA,QACA,CAAA,aAAA,CACA,UACA,WAIA,CAAA,UAAA,CAMV,YAAYC,CAA0B,CAAA,CAClC,KAAK,EAAKA,CAAAA,CAAAA,CAAK,EACf,CAAA,IAAA,CAAK,UAAaA,CAAAA,CAAAA,CAAK,WACvB,IAAK,CAAA,SAAA,CAAYA,EAAK,SAEtB,CAAA,IAAA,CAAK,KAAO,CAAE,GAAGA,CAAK,CAAA,IAAK,CAC3B,CAAA,IAAA,CAAK,WAAa,CAAE,GAAGA,EAAK,UAAW,CAAA,CACvC,KAAK,YAAe,CAAA,IAAIC,CAAeD,CAAAA,CAAAA,CAAK,YAAY,CAAA,CACxD,KAAK,QAAWA,CAAAA,CAAAA,CAAK,SAAS,GAAIE,CAAAA,CAAAA,GAAS,CAAE,GAAGA,CAAK,CAAE,CAAA,CAAA,CACvD,IAAK,CAAA,SAAA,CAAY,CAAE,GAAGF,CAAAA,CAAK,SAAU,CACrC,CAAA,IAAA,CAAK,WAAaA,CAAK,CAAA,UAAA,CAAW,GAAIG,CAAAA,CAAAA,GAAS,CAAE,GAAGA,CAAK,CAAE,CAAA,CAAA,CAE3D,KAAK,aAAgB,CAAA,CAAA,CACrB,KAAK,WAAc,CAAA,CACf,QAAU,CAAA,CAAA,CACV,QAAU,CAAA,CACd,EAEA,IAAK,CAAA,wBAAA,EACL,CAAA,IAAA,CAAK,qBAAsB,GAC/B,CAGA,KAAgB,EAAA,CACZ,OAAO,IAAA,CAAK,EAChB,CAGA,eAAwB,CACpB,OAAO,KAAK,UAChB,CAGA,cAAuB,CACnB,OAAO,IAAK,CAAA,SAChB,CAaA,OAAA,CAAQC,EAA+C,CACrD,OAAGA,EACM,IAAK,CAAA,IAAA,CAAKA,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AE,EAAA,CAAI,EAAG,CACrB,CAAA,IAAA,CAAK,WAAa,CAAE,KAAA,CAAO,CAAE,IAAA,CAAM,EAAI,CAAA,GAAA,CAAK,EAAG,CAAE,CAAA,CACjD,KAAK,YAAe,CAAA,IAAIb,EAAe,CAAE,OAAA,CAAS,CAAE,QAAA,CAAU,EAAG,CAAE,CAAC,CACpE,CAAA,IAAA,CAAK,QAAW,CAAA,EAChB,CAAA,IAAA,CAAK,0BACL,CAAA,IAAA,CAAK,qBAAsB,GAC7B,CACF","file":"chunk-WU6FFDBS.mjs","sourcesContent":["import { BasePrice, Color, LocaleCode, LocalizedString, PriceTier } from \"./Common\";\r\nimport ImageInfoModel, { ImageInfoData } from \"./ImageInfo\";\r\n\r\nexport type SubItem = {\r\n size: string;\r\n quantity: number;\r\n};\r\n\r\nexport type LineItemAttributes = {\r\n id: string;\r\n productKey: string;\r\n variantId: string;\r\n name: LocalizedString;\r\n attributes: {\r\n color: Color;\r\n };\r\n primaryImage: ImageInfoData;\r\n subItems: SubItem[];\r\n basePrice: BasePrice;\r\n priceTiers: PriceTier[];\r\n}\r\n\r\nexport type LineItemData = LineItemAttributes & {\r\n totalQuantity: number;\r\n priceTotals: {\r\n subtotal: number;\r\n mrpTotal: number;\r\n };\r\n}\r\n\r\n/**\r\n * Represents a line item within a shopping cart.\r\n */\r\nexport default class LineItemModel {\r\n protected id: string;\r\n protected productKey: string;\r\n protected variantId: string;\r\n protected name: LocalizedString;\r\n protected attributes: {\r\n color: Color;\r\n };\r\n protected primaryImage: ImageInfoModel;\r\n protected subItems: SubItem[];\r\n protected totalQuantity: number;\r\n protected basePrice: BasePrice;\r\n protected priceTotals: {\r\n subtotal: number;\r\n mrpTotal: number;\r\n };\r\n protected priceTiers: PriceTier[];\r\n\r\n /**\r\n * Creates an instance of LineItemModel.\r\n * @param data - The initial line item attributes.\r\n */\r\n constructor(data: LineItemAttributes) {\r\n this.id = data.id;\r\n this.productKey = data.productKey;\r\n this.variantId = data.variantId;\r\n\r\n this.name = { ...data.name };\r\n this.attributes = { ...data.attributes };\r\n this.primaryImage = new ImageInfoModel(data.primaryImage);\r\n this.subItems = data.subItems.map(item => ({ ...item }));\r\n this.basePrice = { ...data.basePrice };\r\n this.priceTiers = data.priceTiers.map(tier => ({ ...tier }));\r\n\r\n this.totalQuantity = 0;\r\n this.priceTotals = {\r\n subtotal: 0,\r\n mrpTotal: 0\r\n };\r\n\r\n this.recalculateTotalQuantity();\r\n this.recalculatePriceTotal();\r\n }\r\n\r\n /** Gets the unique identifier of the line item. */\r\n getId(): string {\r\n return this.id;\r\n }\r\n\r\n /** Gets the product key associated with this line item. */\r\n getProductKey(): string {\r\n return this.productKey;\r\n }\r\n\r\n /** Gets the product variant ID associated with this line item. */\r\n getVariantId(): string {\r\n return this.variantId;\r\n }\r\n\r\n /**\r\n * Gets the full localized product name object.\r\n * @returns A copy of the LocalizedString object for the name.\r\n */\r\n getName(): LocalizedString\r\n /**\r\n * Gets the product name for a specific locale, falling back to English ('en').\r\n * @param locale - The desired locale code.\r\n * @returns The name string for the specified locale.\r\n */\r\n getName(locale: LocaleCode): string\r\n getName(locale?: LocaleCode): LocalizedString | string {\r\n if(locale) {\r\n return this.name[locale] ?? this.name.en;\r\n } else {\r\n return { ...this.name };\r\n }\r\n }\r\n\r\n\r\n /** Gets the variant attributes. */\r\n getAttributes(): { color: Color } {\r\n return { ...this.attributes };\r\n }\r\n\r\n /** Gets the image information model for this line item. */\r\n getImage(): ImageInfoModel {\r\n return this.primaryImage;\r\n }\r\n\r\n /** Gets the sub-items (size/quantity breakdown). */\r\n getSubItems(): SubItem[] {\r\n return this.subItems.map(item => ({ ...item }));\r\n }\r\n\r\n /** Gets the total quantity for this line item across all sub-items. */\r\n getTotalQuantity(): number {\r\n return this.totalQuantity;\r\n }\r\n\r\n /** Gets the base price for a single unit. */\r\n getBasePrice(): BasePrice {\r\n return { ...this.basePrice };\r\n }\r\n\r\n /** Gets the calculated price details (subtotal, mrpTotal) for this line item. */\r\n getPriceTotals(): { subtotal: number; mrpTotal: number } {\r\n return { ...this.priceTotals };\r\n }\r\n\r\n /** Gets the applicable price tiers for this product variant. */\r\n getPriceTiers(): PriceTier[] {\r\n return this.priceTiers.map(tier => ({ ...tier }));\r\n }\r\n\r\n /**\r\n * Recalculates the total quantity based on the quantities in subItems.\r\n */\r\n protected recalculateTotalQuantity(): void {\r\n this.totalQuantity = this.subItems.reduce((sum, item) => sum + item.quantity, 0);\r\n }\r\n\r\n /**\r\n * Recalculates the line item's price (subtotal and mrpTotal)\r\n * based on total quantity, base price, and applicable price tiers.\r\n */\r\n protected recalculatePriceTotal(): void {\r\n const quantity = this.totalQuantity;\r\n let unitPrice = this.basePrice.unitPrice;\r\n\r\n let applicableTier = null;\r\n for (const tier of this.priceTiers.sort((a, b) => b.minQuantity - a.minQuantity)) { \r\n if (quantity >= tier.minQuantity) {\r\n applicableTier = tier;\r\n break;\r\n }\r\n }\r\n\r\n if (applicableTier) {\r\n unitPrice = applicableTier.unitPrice;\r\n }\r\n\r\n // Calculate totals\r\n this.priceTotals.mrpTotal = this.basePrice.unitPrice * quantity;\r\n this.priceTotals.subtotal = unitPrice * quantity;\r\n }\r\n\r\n\r\n /**\r\n * Gets a plain data object representing the line item's details.\r\n * @returns LineItemData.\r\n */\r\n getDetails(): LineItemData {\r\n return {\r\n id: this.getId(),\r\n productKey: this.getProductKey(),\r\n variantId: this.getVariantId(),\r\n name: this.getName(),\r\n attributes: this.getAttributes(),\r\n primaryImage: this.getImage().getDetails(),\r\n subItems: this.getSubItems(),\r\n totalQuantity: this.getTotalQuantity(),\r\n basePrice: this.getBasePrice(),\r\n priceTotals: this.getPriceTotals(),\r\n priceTiers: this.getPriceTiers(),\r\n };\r\n }\r\n \r\n /**\r\n * Adds or updates sub-items (e.g., sizes with quantities) to the line item.\r\n * If a sub-item with the same size already exists, its quantity is either\r\n * updated by adding the new quantity or replaced entirely, based on the `addQuantity` flag.\r\n * Sub-items with a resulting quantity of 0 are removed.\r\n * Recalculates total quantity and price totals afterwards.\r\n *\r\n * @param subItems - An array of `SubItem` objects to add or update.\r\n * @param addQuantity - If true, adds the quantity from `subItem` to the existing quantity.\r\n * If false, replaces the existing quantity with the one from `subItem`.\r\n */\r\n addSubItems(subItems: SubItem[], addQuantity: boolean): void {\r\n subItems.forEach(subItem => {\r\n const existingSubItem = this.subItems.find(item => item.size === subItem.size);\r\n if (existingSubItem) {\r\n existingSubItem.quantity = addQuantity ? existingSubItem.quantity + subItem.quantity : subItem.quantity;\r\n } else {\r\n this.subItems.push(subItem);\r\n }\r\n })\r\n this.subItems = this.subItems.filter(sub => sub.quantity);\r\n\r\n this.recalculateTotalQuantity();\r\n this.recalculatePriceTotal();\r\n }\r\n\r\n /**\r\n * Resets the line item's properties to their default empty or initial state.\r\n * Useful for clearing out line item data without creating a new instance.\r\n * Recalculates total quantity and price totals afterwards (which will be zero).\r\n */\r\n clearLineItem(): void {\r\n this.id = '';\r\n this.productKey = '';\r\n this.variantId = '';\r\n this.name = { en: '' };\r\n this.attributes = { color: { name: '', hex: '' } };\r\n this.primaryImage = new ImageInfoModel({ sources: { original: '' } });\r\n this.subItems = [];\r\n this.recalculateTotalQuantity();\r\n this.recalculatePriceTotal();\r\n }\r\n}"]}