import{on}from"./store";import{on as onUIChange}from"../ui";import{state as checkoutState}from"../checkout/index";on("set",((t,n,e)=>Object.keys(n||{}).forEach((t=>handleCheckoutLineItemChange(n[t],null==e?void 0:e[t])))));export const handleCheckoutLineItemChange=(t,n)=>{var e,o;const i=(null===(e=null==t?void 0:t.line_items)||void 0===e?void 0:e.data)||[],u=(null===(o=null==n?void 0:n.line_items)||void 0===o?void 0:o.data)||[];if(i.forEach((t=>{const n=u.find((n=>n.id===t.id));if(!n||(null==n?void 0:n.quantity)<(null==t?void 0:t.quantity)){const e=new CustomEvent("scAddedToCart",{detail:{...t,quantity:t.quantity-((null==n?void 0:n.quantity)||0)},bubbles:!0});document.dispatchEvent(e)}})),u.forEach((t=>{const n=i.find((n=>n.id===t.id));if(!n||(null==t?void 0:t.quantity)>(null==n?void 0:n.quantity)){const e=new CustomEvent("scRemovedFromCart",{detail:{...t,quantity:t.quantity-((null==n?void 0:n.quantity)||0)},bubbles:!0});document.dispatchEvent(e)}})),!(null==checkoutState?void 0:checkoutState.isCheckoutPage)&&JSON.stringify(i)!==JSON.stringify(u)){const e=new CustomEvent("scCartUpdated",{detail:{currentCart:t,previousCart:n},bubbles:!0});document.dispatchEvent(e)}};onUIChange("set",((t,n,e)=>{if("cart"===t&&(null==n?void 0:n.open)!==(null==e?void 0:e.open)&&(null==n?void 0:n.open)){const t=new CustomEvent("scViewedCart",{detail:checkoutState.checkout,bubbles:!0});document.dispatchEvent(t)}}));