import{newSpecPage}from"@stencil/core/testing";import{dispose as disposeCheckout}from"../../../../../../store/checkout/index";import{ScCheckoutStockAlert}from"../sc-checkout-stock-alert";import{buildStockAdjustedLineItems,buildStockAlertRows,getBundleComponentVariants,getBundleQuantityReductions}from"../../../../../../functions/stock";const bundleLineItems=(e,t,n)=>[{id:"parent",quantity:e,component_line_item:!1,image:{src:"kit.jpg"},price:{id:"price_parent",product:{name:"Camping Kit",bundle:!0}}},{id:"comp",quantity:t,purchasable_status:"out_of_stock",component_line_item:!0,bundle_line_item:"parent",variant_display_options:"2-Person",price:{id:"price_comp",product:{name:"Trail Tent",available_stock:n}}}];describe("stock",(()=>{describe("getBundleQuantityReductions",(()=>{it("caps the bundle at floor(stock / perBundleQty) instead of touching the component",(()=>{expect(getBundleQuantityReductions(bundleLineItems(2,2,1)).get("parent")).toBe(1)})),it("reduces the bundle to 0 when not even one bundle can be fulfilled",(()=>{expect(getBundleQuantityReductions(bundleLineItems(2,4,1)).get("parent")).toBe(0)}))})),describe("buildStockAdjustedLineItems",(()=>{it("posts the parent only (no component line items) with the reduced quantity",(()=>{expect(buildStockAdjustedLineItems(bundleLineItems(2,2,1))).toEqual([{id:"parent",price_id:"price_parent",quantity:1}])})),it("adjusts a standalone out-of-stock item to its available stock",(()=>{expect(buildStockAdjustedLineItems([{id:"a",quantity:5,purchasable_status:"out_of_stock",price:{id:"price_a",product:{name:"Mug",available_stock:2}}}])).toEqual([{id:"a",price_id:"price_a",quantity:2}])})),it("still caps the bundle by another short component when one component is swapped",(()=>{const e=new Map([["parent",{pA:"variant-blue"}]]);expect(buildStockAdjustedLineItems([{id:"parent",quantity:5,component_line_item:!1,price:{id:"price_parent",product:{name:"Kit",bundle:!0}}},{id:"a",quantity:5,purchasable_status:"out_of_stock",component_line_item:!0,bundle_line_item:"parent",price:{product:{id:"pA",available_stock:0}}},{id:"b",quantity:5,purchasable_status:"out_of_stock",component_line_item:!0,bundle_line_item:"parent",price:{product:{id:"pB",available_stock:3}}}],e)).toEqual([{id:"parent",price_id:"price_parent",quantity:3,bundle_component_variants:{pA:"variant-blue"}}])}))})),describe("getBundleComponentVariants",(()=>{it("rebuilds the selection map from component line items, ignoring the empty parent field",(()=>{expect(getBundleComponentVariants("parent",[{id:"parent",component_line_item:!1,bundle_component_variants:[],price:{product:{name:"Kit",bundle:!0}}},{id:"c1",component_line_item:!0,bundle_line_item:"parent",variant:{id:"v-tent"},price:{product:{id:"p-tent"}}},{id:"c2",component_line_item:!0,bundle_line_item:"parent",variant:{id:"v-bag"},price:{product:{id:"p-bag"}}},{id:"c3",component_line_item:!0,bundle_line_item:"parent",price:{product:{id:"p-lantern"}}}])).toEqual({"p-tent":"v-tent","p-bag":"v-bag"})})),it("returns an empty map for a parent with no components",(()=>{expect(getBundleComponentVariants("parent",[])).toEqual({})}))})),describe("buildStockAlertRows",(()=>{it("shows the bundle row (parent quantity change), not the component",(()=>{expect(buildStockAlertRows(bundleLineItems(2,2,1))).toEqual([{name:"Camping Kit",variant:void 0,image:{src:"kit.jpg"},from:2,to:1}])}))})),describe("sc-checkout-stock-alert",(()=>{beforeEach((()=>disposeCheckout())),it("renders with no dialog open when there are no stock issues",(async()=>{var e,t,n;const i=await newSpecPage({components:[ScCheckoutStockAlert],html:"<sc-checkout-stock-alert></sc-checkout-stock-alert>"});expect(null===(n=null===(t=null===(e=i.root)||void 0===e?void 0:e.shadowRoot)||void 0===t?void 0:t.querySelector("sc-dialog"))||void 0===n?void 0:n.getAttribute("open")).toBeFalsy()}))}))}));