import"../checkouts/events";import state,{on}from"./store";on("set",((t,e,i)=>{if("checkout"!==t)return;if(null==i?void 0:i.id)return;if(!(null==e?void 0:e.id))return;if(!state.isCheckoutPage)return;const n=new CustomEvent("scCheckoutInitiated",{detail:e,bubbles:!0});document.dispatchEvent(n)}));const COMPLETED_CHECKOUTS_KEY="scCompletedCheckouts",hasTrackedCompletion=t=>{try{return JSON.parse(localStorage.getItem("scCompletedCheckouts")||"[]").includes(t)}catch{return!1}},trackCompletion=t=>{try{const e=JSON.parse(localStorage.getItem("scCompletedCheckouts")||"[]");localStorage.setItem("scCompletedCheckouts",JSON.stringify([...e,t].slice(-20)))}catch{}};on("set",((t,e,i)=>{var n,o;if("checkout"!==t)return;if(!(null==e?void 0:e.status)||(null==i?void 0:i.status)===(null==e?void 0:e.status))return;if(!["paid","processing"].includes(e.status))return;if(hasTrackedCompletion(e.id))return;trackCompletion(e.id);const s=new CustomEvent("scOrderPaid",{detail:e,bubbles:!0});document.dispatchEvent(s);const c=new CustomEvent("scCheckoutCompleted",{detail:e,bubbles:!0});document.dispatchEvent(c);const u=((null===(n=null==e?void 0:e.line_items)||void 0===n?void 0:n.data)||[]).filter((t=>{var e;return(null===(e=null==t?void 0:t.price)||void 0===e?void 0:e.trial_duration_days)>0}));if(u.length>0){const t=new CustomEvent("scTrialStarted",{detail:u,bubbles:!0});document.dispatchEvent(t)}const r=((null===(o=null==e?void 0:e.line_items)||void 0===o?void 0:o.data)||[]).filter((t=>{var e;return(null===(e=null==t?void 0:t.price)||void 0===e?void 0:e.recurring_interval_count)>0}));if(r.length>0){const t=new CustomEvent("scSubscriptionStarted",{detail:r,bubbles:!0});document.dispatchEvent(t)}})),on("set",((t,e,i)=>{if("checkout"!==t)return;if(!state.isCheckoutPage)return;if(!(null==e?void 0:e.selected_shipping_choice))return;if((null==i?void 0:i.selected_shipping_choice)===(null==e?void 0:e.selected_shipping_choice))return;const n=new CustomEvent("scShippingInfoAdded",{detail:e,bubbles:!0});document.dispatchEvent(n)})),on("set",((t,e,i)=>{if("checkout"!==t)return;if(!state.isCheckoutPage)return;if(!(null==i?void 0:i.id))return;if(JSON.stringify(e)===JSON.stringify(i))return;const n=new CustomEvent("scCheckoutUpdated",{detail:{currentCheckout:e,previousCheckout:i},bubbles:!0});document.dispatchEvent(n)}));