{"version":3,"file":"Shopify_ProductAddToCart.CbUIDA7J.mjs","names":["useState","useMemo"],"sources":["https:/framerusercontent.com/modules/xEahqZEsyG5uoZky61jQ/yMYjCncXBsxOaKoj8nUI/_useProduct.js","https:/framerusercontent.com/modules/sLbkDuXdCdpfHR6EbULm/s7fMRimeStvBe5mHY38U/Shopify_ProductAddToCart.js"],"sourcesContent":["import{useState,useEffect,useMemo}from\"react\";import{useAtom}from\"jotai\";import{useShopify}from\"https://framerusercontent.com/modules/tdCHCV0pSkxlDaX5GkOX/SxZI1Y5OuGrQCk85U8Xt/_useShopify.js\";import{priceCode}from\"https://framerusercontent.com/modules/7dEgbg7qo3lsQk361YtS/NrF60tPHZRRgADqpoRFr/_utils.js\";import{selectedProductsAtom}from\"https://framerusercontent.com/modules/yZBbIevRfN36MGDPpltx/GNI9VoSkUDyJw3EMOM7K/_atoms.js\";import{DefaultProduct}from\"https://framerusercontent.com/modules/fWwtzMT2tYXMW4IKB8On/1dFHOQ0gPjMC0e1WUStY/_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=(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};};const changeQuantity=quantity=>{if(!product)return;setSelectedProducts(state=>({...state,[product.id]:{...DefaultSelectedOption,...state[product.id],quantity}}));};const selectVariant=(item,sellingPlan)=>{if(!product)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]);// 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,selected,config?.languageIsoCode,config?.countryIsoCode]);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/GXn1lJ5V4nUwhRO9yQKC/_framer_utils.js\";import{useProduct}from\"https://framerusercontent.com/modules/xEahqZEsyG5uoZky61jQ/yMYjCncXBsxOaKoj8nUI/_useProduct.js\";import{useCart}from\"https://framerusercontent.com/modules/kEMMGb5hqGCVwEpqX70U/xtbUYyPMDwLhvH64M4SK/_useCart.js\";import ComponentWrapper from\"https://framerusercontent.com/modules/hQlZBgGeLDev5WuUJA4d/6ncqGCEKZWsfLWpBe7Jn/ComponentWrapper.js\";import Button,{ButtonProperties}from\"https://framerusercontent.com/modules/EoMllm11FAeAfrQZgkjQ/LMhGmi204cugd6mksqKj/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(value&&value!==\"\"){attributes.push({key,value});}});// finally add to cart\nawait addToCart({merchandiseId,quantity,attributes,sellingPlanId});props.openCartEvent?.();};return /*#__PURE__*/_jsxs(ComponentWrapper,{style:cn(styles.wrapper),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\":{\"framerDisableUnlink\":\"* @framerSupportedLayoutWidth any-prefer-fixed\",\"framerSupportedLayoutHeight\":\"auto\",\"framerContractVersion\":\"1\"}},\"__FramerMetadata__\":{\"type\":\"variable\"}}}"],"mappings":"2aAA4oB,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,KAC/6B,GAAU,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,EAAS,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,CAAK,GAAe,EAAoB,IAAQ,CAAC,GAAG,GAAO,EAAQ,IAAI,CAAC,GAAG,EAAsB,GAAG,EAAM,EAAQ,IAAI,cAAY,kBAAkB,EAAK,kBAAkB,eAAe,EAAK,GAAG,CAAC,EAAE,EAGmd,OAFvnC,MAAc,CAA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