import{r as i,j}from"./index.1ec44e23.js";import{f as m,P as k}from"./3c10424c.js";function x(n,r){var e,s;if(!(!n.length||((s=(e=n==null?void 0:n[0])==null?void 0:e.selectedOptions)==null?void 0:s.length)!==Object.keys(r).length))return n==null?void 0:n.find(c=>Object.entries(r).every(([l,P])=>{var f;return(f=c==null?void 0:c.selectedOptions)==null?void 0:f.some(u=>(u==null?void 0:u.name)===l&&(u==null?void 0:u.value)===P)}))}function E(n){const r=n.reduce((e,s)=>{var c;if(!s.selectedOptions)throw new Error("'getOptions' requires 'variant.selectedOptions'");return(c=s==null?void 0:s.selectedOptions)==null||c.forEach(l=>{var P,f,u,g;e[(P=l==null?void 0:l.name)!=null?P:""]=e[(f=l==null?void 0:l.name)!=null?f:""]||new Set,e[(u=l==null?void 0:l.name)!=null?u:""].add((g=l==null?void 0:l.value)!=null?g:"")}),e},{});return Object.keys(r).map(e=>({name:e,values:Array.from(r[e])}))}function B({children:n,data:r,initialVariantId:e}){const s=i.exports.useMemo(()=>{var t;return m((t=r.variants)!=null?t:{})},[r.variants]);if(!M(s))throw new Error("<ProductOptionsProvider/> requires 'product.variants.nodes' or 'product.variants.edges'");const c=i.exports.useMemo(()=>E(s),[s]),[l,P]=i.exports.useState(()=>S(e,s)),[f,u]=i.exports.useState(()=>b(l));i.exports.useEffect(()=>{const t=S(e,s);P(t),u(b(t))},[e,s]);const g=i.exports.useCallback((t,d)=>{u(o=>{const a={...o,[t]:d};return P(x(s,a)),a})},[u,s]),w=i.exports.useCallback((t,d)=>{var a;const o=x(s,{...f,[t]:d});return(a=o==null?void 0:o.availableForSale)!=null?a:!0},[f,s]),A=i.exports.useMemo(()=>{var t;return m((t=r.sellingPlanGroups)!=null?t:{}).map(d=>{var o;return{...d,sellingPlans:m((o=d==null?void 0:d.sellingPlans)!=null?o:{})}})},[r.sellingPlanGroups]),[O,y]=i.exports.useState(void 0),h=i.exports.useMemo(()=>{var t,d;if(!(!l||!O)){if(!((t=l.sellingPlanAllocations)!=null&&t.nodes)&&!((d=l.sellingPlanAllocations)!=null&&d.edges))throw new Error("<ProductOptionsProvider/>: You must include 'sellingPlanAllocations.nodes' or 'sellingPlanAllocations.edges' in your variants in order to calculate selectedSellingPlanAllocation");return m(l.sellingPlanAllocations).find(o=>{var a;return((a=o==null?void 0:o.sellingPlan)==null?void 0:a.id)===O.id})}},[l,O]),C=i.exports.useMemo(()=>({variants:s,variantsConnection:r.variants,options:c,selectedVariant:l,setSelectedVariant:P,selectedOptions:f,setSelectedOption:g,setSelectedOptions:u,isOptionInStock:w,selectedSellingPlan:O,setSelectedSellingPlan:y,selectedSellingPlanAllocation:h,sellingPlanGroups:A,sellingPlanGroupsConnection:r.sellingPlanGroups}),[w,c,r.sellingPlanGroups,r.variants,f,O,h,l,A,g,s]);return j(k.Provider,{value:C,children:n})}function S(n,r){if(n){const e=r.find(s=>(s==null?void 0:s.id)===n);return e||console.warn("<ProductOptionsProvider/> received a 'initialVariantId' prop, but could not actually find a variant with that ID"),e}if(n===null)return null;if(n===void 0)return r.find(e=>e==null?void 0:e.availableForSale)||r[0]}function b(n){return n!=null&&n.selectedOptions?n.selectedOptions.reduce((r,e)=>{var s,c;return r[(s=e==null?void 0:e.name)!=null?s:""]=(c=e==null?void 0:e.value)!=null?c:"",r},{}):{}}function M(n){return!(!n||!Array.isArray(n))}export{B as ProductOptionsProvider}; //# sourceMappingURL=cae1308a.js.map