{"version":3,"file":"AddCart.B6CS-ifq.mjs","names":["React.useState"],"sources":["https:/framerusercontent.com/modules/44t563I23YWgjFfbPcRj/X7jGvh2BDB22SY9Wibct/AddCart.js"],"sourcesContent":["import{jsx as _jsx}from\"react/jsx-runtime\";import*as React from\"react\";import{addPropertyControls,ControlType}from\"framer\";export default function ShopifyAddToCartByHandle(props){const[label,setLabel]=React.useState(props.buttonText);const[loading,setLoading]=React.useState(false);const[available,setAvailable]=React.useState(true);const[checkingStock,setCheckingStock]=React.useState(true);React.useEffect(()=>{checkStock();},[props.productHandle,props.storeDomain,props.storefrontToken,props.apiVersion]);async function shopifyRequest(query,variables){const response=await fetch(`https://${props.storeDomain}/api/${props.apiVersion}/graphql.json`,{method:\"POST\",headers:{\"Content-Type\":\"application/json\",\"X-Shopify-Storefront-Access-Token\":props.storefrontToken},body:JSON.stringify({query,variables})});return await response.json();}async function getVariantByHandle(handle){const query=`\n            query getProductVariant($handle: String!) {\n                product(handle: $handle) {\n                    variants(first: 1) {\n                        nodes {\n                            id\n                            availableForSale\n                        }\n                    }\n                }\n            }\n        `;const data=await shopifyRequest(query,{handle});return data.data?.product?.variants?.nodes?.[0]||null;}async function checkStock(){if(!props.productHandle){setLabel(props.missingHandleText);setAvailable(false);setCheckingStock(false);return;}setCheckingStock(true);try{const variant=await getVariantByHandle(props.productHandle);if(!variant?.id){setLabel(props.missingVariantText);setAvailable(false);setCheckingStock(false);return;}if(!variant.availableForSale){setLabel(props.outOfStockText);setAvailable(false);}else{setLabel(props.buttonText);setAvailable(true);}}catch(error){setLabel(props.errorText);setAvailable(false);}setCheckingStock(false);}async function createCart(variantId){const query=`\n            mutation createCart($lines: [CartLineInput!]) {\n                cartCreate(input: { lines: $lines }) {\n                    cart {\n                        id\n                        totalQuantity\n                        checkoutUrl\n                    }\n                    userErrors {\n                        field\n                        message\n                    }\n                }\n            }\n        `;return await shopifyRequest(query,{lines:[{merchandiseId:variantId,quantity:props.quantity}]});}async function addToExistingCart(cartId,variantId){const query=`\n            mutation addToCart($cartId: ID!, $lines: [CartLineInput!]!) {\n                cartLinesAdd(cartId: $cartId, lines: $lines) {\n                    cart {\n                        id\n                        totalQuantity\n                        checkoutUrl\n                    }\n                    userErrors {\n                        field\n                        message\n                    }\n                }\n            }\n        `;return await shopifyRequest(query,{cartId,lines:[{merchandiseId:variantId,quantity:props.quantity}]});}function resetLabel(){setTimeout(()=>{if(available){setLabel(props.buttonText);}},props.resetDelay);}async function handleClick(){if(!available||checkingStock){return;}if(!props.productHandle){setLabel(props.missingHandleText);resetLabel();return;}setLoading(true);setLabel(props.loadingText);try{const variant=await getVariantByHandle(props.productHandle);if(!variant?.id){setLabel(props.missingVariantText);setLoading(false);resetLabel();return;}if(!variant.availableForSale){setLabel(props.outOfStockText);setAvailable(false);setLoading(false);return;}const existingCartId=localStorage.getItem(props.cartStorageKey);let result;if(existingCartId){result=await addToExistingCart(existingCartId,variant.id);const errors=result.data?.cartLinesAdd?.userErrors||[];if(errors.length>0){result=await createCart(variant.id);}}else{result=await createCart(variant.id);}const cart=result.data?.cartLinesAdd?.cart||result.data?.cartCreate?.cart;if(cart?.id){localStorage.setItem(props.cartStorageKey,cart.id);localStorage.setItem(\"shopify_checkout_url\",cart.checkoutUrl);window.dispatchEvent(new Event(\"shopify-cart-updated\"));setLabel(props.addedText);}else{setLabel(props.errorText);}}catch(error){setLabel(props.errorText);}setLoading(false);resetLabel();}return /*#__PURE__*/_jsx(\"button\",{onClick:handleClick,disabled:loading||checkingStock||!available,style:{width:\"100%\",height:\"100%\",border:\"none\",borderRadius:props.radius,background:props.background,color:props.color,fontSize:props.fontSize,fontWeight:600,cursor:loading||checkingStock||!available?\"default\":\"pointer\",opacity:loading||checkingStock||!available?.65:1},children:checkingStock?props.checkingStockText:label});}addPropertyControls(ShopifyAddToCartByHandle,{storeDomain:{type:ControlType.String,title:\"Store\",defaultValue:\"sinu-pood.myshopify.com\"},storefrontToken:{type:ControlType.String,title:\"Token\",defaultValue:\"\"},apiVersion:{type:ControlType.String,title:\"API\",defaultValue:\"2026-01\"},productHandle:{type:ControlType.String,title:\"Handle\",defaultValue:\"\"},quantity:{type:ControlType.Number,title:\"Quantity\",defaultValue:1,min:1,step:1},cartStorageKey:{type:ControlType.String,title:\"Cart Key\",defaultValue:\"shopify_cart_id\"},buttonText:{type:ControlType.String,title:\"Button Text\",defaultValue:\"Add to Cart\"},checkingStockText:{type:ControlType.String,title:\"Checking Stock\",defaultValue:\"Checking...\"},loadingText:{type:ControlType.String,title:\"Loading Text\",defaultValue:\"Adding...\"},addedText:{type:ControlType.String,title:\"Added Text\",defaultValue:\"Added\"},outOfStockText:{type:ControlType.String,title:\"Out of Stock\",defaultValue:\"Out of Stock\"},missingHandleText:{type:ControlType.String,title:\"No Handle\",defaultValue:\"Product Not Found\"},missingVariantText:{type:ControlType.String,title:\"No Variant\",defaultValue:\"Variant Not Available\"},errorText:{type:ControlType.String,title:\"Error Text\",defaultValue:\"Something Went Wrong\"},resetDelay:{type:ControlType.Number,title:\"Reset Delay\",defaultValue:1500,min:500,max:8e3,step:100},background:{type:ControlType.Color,title:\"Background\",defaultValue:\"#000000\"},color:{type:ControlType.Color,title:\"Text Color\",defaultValue:\"#ffffff\"},fontSize:{type:ControlType.Number,title:\"Font Size\",defaultValue:16},radius:{type:ControlType.Number,title:\"Radius\",defaultValue:8}});\nexport const __FramerMetadata__ = {\"exports\":{\"default\":{\"type\":\"reactComponent\",\"name\":\"ShopifyAddToCartByHandle\",\"slots\":[],\"annotations\":{\"framerContractVersion\":\"1\"}},\"__FramerMetadata__\":{\"type\":\"variable\"}}}\n//# sourceMappingURL=./AddCart.map"],"mappings":"+LAA2H,SAAwB,EAAyB,EAAM,CAAC,GAAK,CAAC,EAAM,GAAUA,EAAe,EAAM,WAAW,CAAM,CAAC,EAAQ,GAAYA,EAAe,GAAM,CAAM,CAAC,EAAU,GAAcA,EAAe,GAAK,CAAM,CAAC,EAAc,GAAkBA,EAAe,GAAK,CAAC,MAAoB,CAAC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