1import{j as H}from"./CheveronLeft-BLchON5X.js";import{r as a}from"./useCartUpsells-oN76HI6E.js";import{f as O,u as K,c as V,a as q,b as X,d as Y}from"./ShopifyCart-LdCOHsRZ.js";import{r as Z,f as U}from"./tenderCoupons-ClMQo-uT.js";import{h as P}from"./tenderCoupon-B4fpH8v8.js";import{C as m}from"./events-Dm1k7AS7.js";import"./index-kA4PVysc.js";const A=a.createContext(),ct=({children:d})=>{const[o,u]=a.useState([]),[_,s]=a.useState(!1),[f,C]=a.useState(!1),[l,w]=a.useState([]),[D,c]=a.useState(""),[S,h]=a.useState(0),[T,y]=a.useState(0),[I,E]=a.useState(()=>Z().lines);a.useEffect(()=>{k()},[]);const k=async()=>{try{s(!0);const t=await O(),e=t.items||[];u(e),c(t.note||""),h(t.total_price||0),y(t.original_total_price||0),E((await U()).lines),await x(e)}catch(t){console.error("Error bootstrapping cart:",t),u([]),c("")}finally{s(!1)}},x=async(t=[])=>{try{if(t&&t.length>0){const r=t[t.length-1];if(r&&r.product_id)try{const n=await fetch(`/recommendations/products.json?product_id=${r.product_id}&intent=complementary&limit=6`);if(n.ok){const g=await n.json();if(g.products&&g.products.length>0){w(await P(g.products));return}else{const L=await fetch(`/recommendations/products.json?product_id=${r.product_id}&intent=related&limit=6`);if(L.ok){const v=await L.json();if(v.products&&v.products.length>0){w(await P(v.products));return}}}}}catch{}}const e=document.querySelector("#upsell-products-json");if(e){const r=JSON.parse(e.textContent).upsellProducts;w(await P(r))}}catch(e){console.error("Error fetching upsell products:",e)}},i=async()=>{try{const t=await O(),e=t.items||[];u(e),c(t.note||""),h(t.total_price||0),y(t.original_total_price||0),E((await U()).lines),await x(e)}catch(t){console.error("Error fetching cart data:",t)}},F=async(t,e=1)=>{try{return s(!0),await Y({items:[{id:t,quantity:e}]}),await i(),s(!1),Promise.resolve()}catch(r){return console.error("Error adding item:",r),window.dispatchEvent(new CustomEvent("cart:error",{detail:{message:"Failed to add item to cart. Please try again.",title:"Cart Error"}})),s(!1),Promise.reject(r)}},N=async(t,e)=>{try{const r={updates:{[t]:e}};await X(r),await i()}catch(r){console.error("Error updating quantity:",r),window.dispatchEvent(new CustomEvent("cart:error",{detail:"Failed to update quantity. Please try again."}))}},R=async t=>{try{const e=o.find(r=>r.key===t);if(e?.properties?.Contents!==void 0){const r=o.filter(n=>n.properties?._bundle===e.product_title);for(const n of r)await q({id:String(n.key),quantity:0})}await q({id:String(t),quantity:0}),await i()}catch(e){console.error("Error removing item:",e)}},M=()=>{C(!f)},b=()=>{C(!0)},$=()=>{C(!1)},J=async()=>{try{await V(),u([]),c(""),h(0),y(0),E({})}catch(t){console.error("Error clearing cart:",t),window.dispatchEvent(new CustomEvent("cart:error",{detail:"Failed to clear cart. Please try again."}))}},Q=async t=>{try{s(!0);const e=await K(t);return c(e.note||t),e}catch(e){throw console.error("Error updating cart note:",e),window.dispatchEvent(new CustomEvent("cart:error",{detail:"Failed to update note. Please try again."})),e}finally{s(!1)}},W=o.filter(t=>t.properties?._is_bundle_component!=="true"),z=S,B=T,p=W.reduce((t,e)=>t+e.quantity,0),j=a.useMemo(()=>{if(!l||!o)return l;const t=o.map(e=>e.product_id);return l.filter(e=>!t.includes(e.id))},[l,o]);a.useEffect(()=>{document.dispatchEvent(new CustomEvent(m.COUNT_UPDATE,{detail:{count:p}}))},[p]),a.useEffect(()=>{const t=async()=>{await i(),document.dispatchEvent(new CustomEvent(m.ADDED,{detail:{items:o}})),b()};return document.addEventListener(m.ADD,t),()=>{document.removeEventListener(m.ADD,t)}},[]);const G=a.useMemo(()=>({lineItems:o,isOpen:f,isLoading:_,toggleOpen:M,openCart:b,closeCart:$,addItem:F,updateQuantity:N,removeItem:R,clearCart:J,refreshCart:i,upsellProducts:j,tenderCouponLines:I,subtotal:z,originalSubtotal:B,itemCount:p,note:D,updateNote:Q}),[o,f,_,j,p,D,S,T,I]);return H.jsx(A.Provider,{value:G,children:d})},it=()=>{const d=a.useContext(A);if(!d)throw new Error("useCart must be used within a CartProvider");return d};export{ct as C,it as u};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.