{"version":3,"file":"_useProduct.DUXZnvyc.mjs","names":["useState","useAtomValue","useMemo","useAtom","useCallback","useEffect","useLayoutEffect"],"sources":["https:/framerusercontent.com/modules/xEahqZEsyG5uoZky61jQ/Jq5xpVB2EtHRhNbukrHt/_useProduct.js"],"sourcesContent":["import{useCallback,useEffect,useLayoutEffect,useMemo,useState}from\"react\";import{useAtom,useAtomValue}from\"https://esm.sh/jotai@2.15.0?external=react\";import{parseProductPayload,priceCode}from\"https://framerusercontent.com/modules/7dEgbg7qo3lsQk361YtS/ThUoOgwiB0dnN03sGTvb/_utils.js\";import{configAtom,productsLoadableAtom,selectedProductsAtom}from\"https://framerusercontent.com/modules/yZBbIevRfN36MGDPpltx/89RrH2Io0vUgcxyspY67/_atoms.js\";import{DefaultProduct}from\"https://framerusercontent.com/modules/fWwtzMT2tYXMW4IKB8On/qkWzXbhYbATozMlhaVV8/_test_data.js\";import{useIsStaticWebsite}from\"https://framerusercontent.com/modules/eAejHXy6apg0StNmpKvK/GfiUlYgjnAB0Az7cClGa/_framer_utils.js\";import{useInitializeShopify}from\"https://framerusercontent.com/modules/tdCHCV0pSkxlDaX5GkOX/WKo1sACwoziPHO6aRe0i/_useShopify.js\";const DefaultSelectedOption={quantity:1,quantityAvailable:null,selectedOption:null,sellingPlan:null};const normalizeAvailableQuantity=quantity=>typeof quantity===\"number\"?Math.max(0,quantity):null;// Persist the initial selection before the browser paints, while keeping SSR\n// safe by falling back to useEffect when window is unavailable.\nconst useSelectionEffect=typeof window===\"undefined\"?useEffect:useLayoutEffect;const resolveSelectedProduct=(product,storedSelection)=>{const selectedVariant=product.variants.nodes.find(item=>item.id===storedSelection?.selectedOption);if(storedSelection&&selectedVariant){return{...storedSelection,quantityAvailable:normalizeAvailableQuantity(selectedVariant.quantityAvailable)};}const defaultVariant=product.variants.nodes[0];if(!defaultVariant)return null;return{...DefaultSelectedOption,quantityAvailable:normalizeAvailableQuantity(defaultVariant.quantityAvailable),selectedOption:defaultVariant.id};};/** Resolve a CMS payload on static output and Storefront data interactively. */export function useProduct(productID,addToSelected){const isStatic=useIsStaticWebsite();// Other Shopiframe boundaries can finish loading products or selections while\n// this boundary is hydrating. Preserve its server snapshot until it commits.\nconst[hasMounted,setHasMounted]=useState(false);useInitializeShopify(isStatic);useEffect(()=>{setHasMounted(true);},[]);const config=useAtomValue(configAtom);const productsState=useAtomValue(productsLoadableAtom);const cmsProduct=useMemo(()=>parseProductPayload(productID),[productID]);const[selectedProducts,setSelectedProducts]=useAtom(selectedProductsAtom);const product=useMemo(()=>{if(!cmsProduct)return DefaultProduct;if(!hasMounted||isStatic)return cmsProduct;if(productsState.state!==\"hasData\")return cmsProduct;return productsState.data.find(item=>item.id===cmsProduct.id)??cmsProduct;},[cmsProduct,hasMounted,isStatic,productsState]);const selected=useMemo(()=>{const storedSelection=hasMounted&&product?.id?selectedProducts[product.id]:null;if(!addToSelected||!product)return storedSelection;return resolveSelectedProduct(product,storedSelection);},[addToSelected,hasMounted,product,selectedProducts]);const activeConfig=hasMounted?config:null;const getPrice=useCallback((camparePrice,price)=>{const comparePriceValue=camparePrice&&camparePrice.amount!==\"0.0\"?priceCode(camparePrice,activeConfig??undefined):null;const priceValue=priceCode(price,activeConfig??undefined);return{camparePrice:comparePriceValue!==priceValue?comparePriceValue:null,price:priceValue};},[activeConfig]);const changeQuantity=quantity=>{if(!product)return;setSelectedProducts(state=>({...state,[product.id]:{...DefaultSelectedOption,...resolveSelectedProduct(product,state[product.id]),quantity}}));};const selectVariant=(item,sellingPlan)=>{if(!product||!item)return;setSelectedProducts(state=>({...state,[product.id]:{...DefaultSelectedOption,...resolveSelectedProduct(product,state[product.id]),sellingPlan,quantityAvailable:normalizeAvailableQuantity(item.quantityAvailable),selectedOption:item.id}}));};useSelectionEffect(()=>{if(!product||!addToSelected)return;setSelectedProducts(state=>{const currentSelection=state[product.id];const resolvedSelection=resolveSelectedProduct(product,currentSelection);if(!resolvedSelection)return state;if(currentSelection?.selectedOption===resolvedSelection.selectedOption&&currentSelection?.quantity===resolvedSelection.quantity&&currentSelection?.quantityAvailable===resolvedSelection.quantityAvailable&&currentSelection?.sellingPlan===resolvedSelection.sellingPlan){return state;}return{...state,[product.id]:resolvedSelection};});},[product,addToSelected,setSelectedProducts]);const result=useMemo(()=>{if(!product)return{product:null,availableForSale:null,multiplePrice:null,price:null,stock:null,selectedVariant:null,selectedProduct:null};const selectedVariant=product.variants.nodes.find(item=>item.id===selected?.selectedOption);if(selectedVariant){const selectedSellingPlan=selectedVariant.sellingPlanAllocations?.nodes.find(item=>item.sellingPlan.id===selected?.sellingPlan);const price=selectedSellingPlan?.checkoutChargeAmount??selectedVariant.priceV2;return{product,price:getPrice(selectedVariant.compareAtPriceV2,price),multiplePrice:false,availableForSale:selectedVariant.availableForSale,stock:normalizeAvailableQuantity(selectedVariant.quantityAvailable),selectedVariant,selectedProduct:selected};}const multiplePrice=product.priceRange.minVariantPrice.amount!==product.priceRange.maxVariantPrice.amount;return{product,price:getPrice(product.compareAtPriceRange.minVariantPrice,product.priceRange.minVariantPrice),multiplePrice,availableForSale:product.availableForSale,stock:normalizeAvailableQuantity(product.totalInventory),selectedVariant:null,selectedProduct:selected};},[product,getPrice,selected]);return{...result,changeQuantity,selectVariant};}\nexport const __FramerMetadata__ = {\"exports\":{\"useProduct\":{\"type\":\"function\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"__FramerMetadata__\":{\"type\":\"variable\"}}}\n//# sourceMappingURL=./_useProduct.map"],"mappings":"qTAE6qB,SAAgB,EAAW,EAAU,EAAc,CAAC,IAAM,EAAS,EAAmB,EAE9vB,CAAC,EAAW,GAAeA,EAAS,EAAK,EAAE,EAAqB,CAAQ,EAAE,MAAc,CAAC,EAAc,EAAI,CAAE,EAAE,CAAC,CAAC,EAAE,IAAM,EAAOC,EAAa,CAAU,EAAQ,EAAcA,EAAa,CAAoB,EAAQ,EAAWC,MAAY,EAAoB,CAAS,EAAE,CAAC,CAAS,CAAC,EAAO,CAAC,EAAiB,GAAqB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