{"version":3,"file":"Shopify_ProductAddToCart.P2n6xT0d.mjs","names":["useState","useCallback","useMemo"],"sources":["https:/framerusercontent.com/modules/xEahqZEsyG5uoZky61jQ/4ANjnqbmpaLaLb8jiNAF/_useProduct.js","https:/framerusercontent.com/modules/sLbkDuXdCdpfHR6EbULm/hh6vclsjUEq55rFdXyJ0/Shopify_ProductAddToCart.js"],"sourcesContent":["import{useState,useEffect,useMemo,useCallback}from\"react\";import{useAtom}from\"jotai\";import{useShopify}from\"https://framerusercontent.com/modules/tdCHCV0pSkxlDaX5GkOX/tTqDmuuUuEjWv7zCFWmI/_useShopify.js\";import{priceCode}from\"https://framerusercontent.com/modules/7dEgbg7qo3lsQk361YtS/Gqkzce24K1Cg6rwNd2hA/_utils.js\";import{selectedProductsAtom}from\"https://framerusercontent.com/modules/yZBbIevRfN36MGDPpltx/urppTTwyyMIUHdUuGBV5/_atoms.js\";import{DefaultProduct}from\"https://framerusercontent.com/modules/fWwtzMT2tYXMW4IKB8On/TRxT523e6Q50khecgJTP/_test_data.js\";const DefaultSelectedOption={quantity:1,quantityAvailable:null,selectedOption:null,sellingPlan:null};export function useProduct(productID,config,addToSelected){const[selectedProducts,setSelectedProducts]=useAtom(selectedProductsAtom);const{products}=useShopify(config);const[product,setProduct]=useState(config?.testMode?DefaultProduct:null);const selected=product?.id?selectedProducts[product.id]:null;// get price and compare price\nconst getPrice=useCallback((camparePrice,price)=>{const comparePriceValue=camparePrice&&camparePrice.amount!==\"0.0\"?priceCode(camparePrice,config):null;const priceValue=priceCode(price,config);return{camparePrice:comparePriceValue!==priceValue?comparePriceValue:null,price:priceValue};},[config]);const changeQuantity=quantity=>{if(!product)return;setSelectedProducts(state=>({...state,[product.id]:{...DefaultSelectedOption,...state[product.id],quantity}}));};const selectVariant=(item,sellingPlan)=>{if(!product||!item)return;setSelectedProducts(state=>({...state,[product.id]:{...DefaultSelectedOption,...state[product.id],sellingPlan,quantityAvailable:item.quantityAvailable,selectedOption:item.id}}));};// set first variant as default selected when addToSelected is true\nuseEffect(()=>{if(product&&addToSelected){const defaultVariant=product.variants.nodes[0];setSelectedProducts(state=>({...state,[product.id]:{...DefaultSelectedOption,quantityAvailable:defaultVariant.quantityAvailable,selectedOption:defaultVariant.id}}));return()=>{setSelectedProducts(state=>({...state,[product.id]:null}));};}},[product,addToSelected,setSelectedProducts]);// set product when productID found\nuseEffect(()=>{const foundProduct=products.find(item=>item.id===productID);if(foundProduct){setProduct(config?.testMode?DefaultProduct:foundProduct);}},[products,productID,config?.testMode]);// return result with two mode: when variant selected and when not selected\nconst 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{compareAtPriceV2,availableForSale,priceV2,quantityAvailable}=selectedVariant;const price=selectedSellingPlan?.checkoutChargeAmount??priceV2;return{product,price:getPrice(compareAtPriceV2,price),multiplePrice:false,availableForSale,stock:quantityAvailable,selectedVariant,selectedProduct:selected};}const{compareAtPriceRange,priceRange,availableForSale,totalInventory}=product??{};const multiplePrice=priceRange.minVariantPrice.amount!==priceRange.maxVariantPrice.amount;return{product,price:getPrice(compareAtPriceRange.minVariantPrice,priceRange.minVariantPrice),multiplePrice,availableForSale,stock: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\"}}}","import{jsx as _jsx,jsxs as _jsxs}from\"react/jsx-runtime\";import{addPropertyControls,ControlType}from\"framer\";import{motion}from\"framer-motion\";import{cn,textProperties,ProductBaseControls,ConfigControls}from\"https://framerusercontent.com/modules/eAejHXy6apg0StNmpKvK/urqFMX8moevipl03sMG8/_framer_utils.js\";import{useProduct}from\"https://framerusercontent.com/modules/xEahqZEsyG5uoZky61jQ/4ANjnqbmpaLaLb8jiNAF/_useProduct.js\";import{useCart}from\"https://framerusercontent.com/modules/kEMMGb5hqGCVwEpqX70U/NZjnChqwTgzCh3I9W4a3/_useCart.js\";import ComponentWrapper from\"https://framerusercontent.com/modules/hQlZBgGeLDev5WuUJA4d/efplB9Cs5ZjhxVN6gafY/ComponentWrapper.js\";import Button,{ButtonProperties}from\"https://framerusercontent.com/modules/EoMllm11FAeAfrQZgkjQ/3Dz8a5Se5utjXemwL1BT/Button.js\";/**\n * @framerDisableUnlink\n * @framerSupportedLayoutWidth any-prefer-fixed\n * @framerSupportedLayoutHeight auto\n */export default function Shopify_ProductAddToCart(props){const{product,selectedProduct,availableForSale}=useProduct(props.productID??null,props.config);const{addToCart,actionLoading,cartError}=useCart(props.config);const handleAddToCart=async()=>{const merchandiseId=selectedProduct?.selectedOption||product?.variants.nodes[0].id;const quantity=selectedProduct?selectedProduct.quantity:1;const sellingPlanId=selectedProduct?.sellingPlan;// find & add attribute to cart\nconst attributes=[];const productElements=document.querySelectorAll('[name^=\"product-\"]');productElements.forEach(element=>{const key=element.getAttribute(\"name\")?.replace(\"product-\",\"\");const value=element.value;if(key&&value&&value!==\"\"){attributes.push({key,value});}});// finally add to cart\nawait addToCart({merchandiseId:merchandiseId??\"\",quantity,attributes,sellingPlanId:sellingPlanId??undefined});props.openCartEvent?.();};return /*#__PURE__*/_jsxs(ComponentWrapper,{style:cn(styles.wrapper),name:\"ProductAddToCart\",productID:props.productID||\"EMPTY\",config:props.config,children:[/*#__PURE__*/_jsx(Button,{onClick:handleAddToCart,loading:actionLoading,disabled:!availableForSale,...props}),cartError&&/*#__PURE__*/_jsx(motion.span,{style:cn(props.errorStyle),children:cartError})]});}Shopify_ProductAddToCart.displayName=\"Product Add To Cart\";addPropertyControls(Shopify_ProductAddToCart,{...ConfigControls,...ProductBaseControls,...ButtonProperties(\"Add to cart\"),errorStyle:{title:\"Error Style\",type:ControlType.Object,controls:{...textProperties(14,\"#bb0000\"),textTransform:{type:ControlType.Enum,defaultValue:\"none\",segmentedControlDirection:\"vertical\",options:[\"none\",\"lowercase\",\"uppercase\",\"capitalize\"],optionTitles:[\"Normal\",\"Lowercase\",\"Uppercase\",\"Capitalize\"]}}},openCartEvent:{type:ControlType.EventHandler}});const styles={wrapper:{display:\"flex\",flexDirection:\"column\",gap:8}};\nexport const __FramerMetadata__ = {\"exports\":{\"default\":{\"type\":\"reactComponent\",\"name\":\"Shopify_ProductAddToCart\",\"slots\":[],\"annotations\":{\"framerContractVersion\":\"1\",\"framerSupportedLayoutHeight\":\"auto\",\"framerDisableUnlink\":\"* @framerSupportedLayoutWidth any-prefer-fixed\"}},\"__FramerMetadata__\":{\"type\":\"variable\"}}}"],"mappings":"obAAwpB,SAAgB,EAAW,EAAU,EAAO,EAAc,CAAC,GAAK,CAAC,EAAiB,GAAqB,EAAQ,EAAqB,CAAM,CAAC,YAAU,EAAW,EAAO,CAAM,CAAC,EAAQ,GAAYA,EAAS,GAAQ,SAAS,EAAe,KAAK,CAAO,EAAS,GAAS,GAAG,EAAiB,EAAQ,IAAI,KAC37B,EAASC,GAAa,EAAa,IAAQ,CAAC,IAAM,EAAkB,GAAc,EAAa,SAAS,MAAM,EAAU,EAAa,EAAO,CAAC,KAAW,EAAW,EAAU,EAAM,EAAO,CAAC,MAAM,CAAC,aAAa,IAAoB,EAA6B,KAAlB,EAAuB,MAAM,EAAW,EAAG,CAAC,EAAO,CAAC,CAAO,EAAe,GAAU,CAAK,GAAe,EAAoB,IAAQ,CAAC,GAAG,GAAO,EAAQ,IAAI,CAAC,GAAG,EAAsB,GAAG,EAAM,EAAQ,IAAI,WAAS,CAAC,EAAE,EAAS,GAAe,EAAK,IAAc,CAAI,CAAC,GAAS,CAAC,GAAY,EAAoB,IAAQ,CAAC,GAAG,GAAO,EAAQ,IAAI,CAAC,GAAG,EAAsB,GAAG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