{"version":3,"file":"FC_CartCounter.DUplYvmP.mjs","names":["useState","useRef","useCallback","throttle"],"sources":["https:/framerusercontent.com/modules/4gxKhYDXhQYsHYSbypZW/CwQMBB6veGtRJ77hGsnc/FC_CartCounter.js"],"sourcesContent":["/*\n * Framer Commerce\n * Confidential and Proprietary - All Rights Reserved\n * Unauthorized use, reproduction, distribution, or disclosure of this\n * source code or any related information is strictly prohibited.\n *\n * This software is the exclusive property of Framer Commerce (\"Company\").\n * It is considered highly confidential and proprietary information.\n *\n * Any use, copying, modification, distribution, or sharing of this software,\n * in whole or in part, without the express written permission of the Company\n * is strictly prohibited and may result in legal action.\n *\n * DISCLAIMER: This software does not provide any express or\n * implied warranties, including, but not limited to, the implied warranties\n * of merchantability and fitness for a particular purpose. In no event shall\n * Framer Commerce be liable for any direct, indirect, incidental, special,\n * exemplary, or consequential damages (including, but not limited to, procurement\n * of substitute goods or services; loss of use, data, or profits; or business\n * interruption) however caused and on any theory of liability, whether in\n * contract, strict liability, or tort (including negligence or otherwise)\n * arising in any way out of the use of this software, even if advised of\n * the possibility of such damage.\n *\n * Any unauthorized possession, use, copying, distribution, or dissemination\n * of this software will be considered a breach of confidentiality and may\n * result in legal action.\n *\n * For inquiries, contact:\n * Framer Commerce\n * Email: hello@framercommerce.com\n *\n * © 2025 Butter Supply Inc. All Rights Reserved.\n */import{jsx as _jsx}from\"react/jsx-runtime\";import{useEffect,useRef,useState,useCallback}from\"react\";import{addPropertyControls,ControlType,RenderTarget}from\"framer\";import{throttle}from\"lodash-es\";/**\n * @framerDisableUnlink\n */export default function FC_CartCounter(props){if(RenderTarget.current()===RenderTarget.canvas){// Always show in canvas (shows placeholder \"0\")\nreturn /*#__PURE__*/_jsx(\"div\",{style:{display:\"flex\",alignItems:\"center\",justifyContent:\"center\",width:\"100%\",height:\"100%\"},children:/*#__PURE__*/_jsx(\"p\",{style:{...props.font,color:props.color,backgroundColor:props.backgroundColor,...props.border,borderRadius:props.radius,padding:props.padding,display:\"flex\",alignItems:\"center\",justifyContent:\"center\",margin:0,width:\"100%\",height:props.fixedHeight?`${props.heightPx}px`:\"100%\",lineHeight:1,fontSize:props.font?.fontSize||\"inherit\"},children:\"0\"})});}else{const[isBrowser,setIsBrowser]=useState(false);const[cartItemCount,setCartItemCount]=useState(0);const[isLoading,setIsLoading]=useState(true);const[isVisible,setIsVisible]=useState(false);const isFetching=useRef(false);const initialLoadDone=useRef(false);// Set isBrowser to true once component mounts\nuseEffect(()=>{setIsBrowser(true);},[]);// Helper function to wait until a condition is true\nconst waitForCondition=useCallback((conditionFn,checkInterval=100,timeout=1e4)=>{return new Promise((resolve,reject)=>{const startTime=Date.now();const checkCondition=()=>{if(conditionFn()){resolve();}else if(Date.now()-startTime>=timeout){reject(new Error(\"Condition not met within timeout\"));}else{setTimeout(checkCondition,checkInterval);}};checkCondition();});},[]);const fetchCartAndUpdateCount=useCallback(async cartId=>{if(isFetching.current){return;}isFetching.current=true;// Only set loading state on initial load\nif(!initialLoadDone.current){setIsLoading(true);setIsVisible(false);}try{await waitForCondition(()=>window.shopXtools&&window.shopXtools.fetchCart);const cartData=await window.shopXtools.fetchCart(cartId);if(cartData&&cartData.lines&&cartData.lines.edges&&cartData.lines.edges.length>0){const itemCount=cartData.lines.edges.reduce((total,edge)=>total+(edge.node.quantity||0),0);setCartItemCount(itemCount);}else{setCartItemCount(0);// Show immediately if no items\nsetIsLoading(false);setIsVisible(true);initialLoadDone.current=true;}}catch(error){setCartItemCount(0);// Show immediately if error\nsetIsLoading(false);setIsVisible(true);initialLoadDone.current=true;}finally{isFetching.current=false;// Only handle visibility on initial load with items\nif(!initialLoadDone.current){setIsLoading(false);initialLoadDone.current=true;// Add a small delay before showing the counter for smoother transition\nsetTimeout(()=>setIsVisible(true),50);}}},[waitForCondition]);const handleCartUpdate=useCallback(throttle(async()=>{if(!isBrowser)return;// Don't run on server-side\nlet cartId=window[\"shopXtools\"]?.cart?.id;if(!cartId){try{cartId=localStorage.getItem(\"shopX_cart_id\");}catch(error){// Error accessing localStorage\n}}if(cartId){await fetchCartAndUpdateCount(cartId);}else{setCartItemCount(0);// Show immediately if no cart\nsetIsLoading(false);setIsVisible(true);initialLoadDone.current=true;}},1e3),[fetchCartAndUpdateCount,isBrowser]);useEffect(()=>{if(!isBrowser)return;// Don't run on server-side\nwindow.addEventListener(\"shopXtools-cart-update\",handleCartUpdate);handleCartUpdate();return()=>{window.removeEventListener(\"shopXtools-cart-update\",handleCartUpdate);};},[handleCartUpdate,isBrowser]);if(props.hideIf0&&cartItemCount===0){return null;}return /*#__PURE__*/_jsx(\"div\",{style:{display:\"flex\",alignItems:\"center\",justifyContent:\"center\",width:\"100%\",height:\"100%\"},children:/*#__PURE__*/_jsx(\"p\",{style:{...props.font,color:props.color,backgroundColor:props.backgroundColor,...props.border,borderRadius:props.radius,padding:props.padding,display:\"flex\",alignItems:\"center\",justifyContent:\"center\",margin:0,width:\"100%\",height:props.fixedHeight?`${props.heightPx}px`:\"100%\",opacity:isVisible?1:0,transition:\"opacity 0.3s ease-in-out\",visibility:isLoading?\"hidden\":\"visible\",lineHeight:1,fontSize:props.font?.fontSize||\"inherit\"},children:isBrowser?cartItemCount.toString():\"0\"})});}}FC_CartCounter.defaultProps={hideIf0:false,font:undefined,color:\"#000\",backgroundColor:\"#FFFFFF00\",backgroundOpacity:0,border:{borderTopWidth:0,borderRightWidth:0,borderBottomWidth:0,borderLeftWidth:0,borderStyle:\"solid\",borderColor:\"rgba(0, 0, 0, 0.5)\"},radius:0,padding:0,fixedHeight:false,heightPx:40};addPropertyControls(FC_CartCounter,{hideIf0:{type:ControlType.Boolean,title:\"When 0\",defaultValue:false,enabledTitle:\"Hide\",disabledTitle:\"Show\",description:\"For browser only, canvas will always display 0.\"},font:{type:ControlType.Font,controls:\"extended\",title:\"Font\"},color:{type:ControlType.Color,defaultValue:\"#000\",title:\"Color\"},backgroundColor:{type:ControlType.Color,title:\"BG\",defaultValue:\"rgba(255, 255, 255, 0)\"},border:{type:ControlType.Border,title:\"Border\",defaultValue:{borderTopWidth:0,borderRightWidth:0,borderBottomWidth:0,borderLeftWidth:0,borderStyle:\"solid\",borderColor:\"rgba(0, 0, 0, 0.5)\"}},/*     fixedHeight: {\n        type: ControlType.Boolean,\n        title: \"Height\",\n        defaultValue: false,\n        enabledTitle: \"Fixed\",\n        disabledTitle: \"Fit\",\n    },\n    heightPx: {\n        type: ControlType.Number,\n        title: \"Height\",\n        defaultValue: 40,\n        min: 0,\n        hidden: ({ fixedHeight }) => !fixedHeight,\n    }, */padding:{type:ControlType.FusedNumber,title:\"Padding\",defaultValue:0,toggleKey:\"paddingPerSide\",toggleTitles:[\"All\",\"Sides\"],valueKeys:[\"paddingTop\",\"paddingRight\",\"paddingBottom\",\"paddingLeft\"],valueLabels:[\"T\",\"R\",\"B\",\"L\"],min:0},radius:{type:ControlType.FusedNumber,title:\"Radius\",defaultValue:0,toggleKey:\"radiusPerCorner\",toggleTitles:[\"All\",\"Corners\"],valueKeys:[\"topLeft\",\"topRight\",\"bottomRight\",\"bottomLeft\"],valueLabels:[\"TL\",\"TR\",\"BR\",\"BL\"],min:0}});\nexport const __FramerMetadata__ = {\"exports\":{\"default\":{\"type\":\"reactComponent\",\"name\":\"FC_CartCounter\",\"slots\":[],\"annotations\":{\"framerContractVersion\":\"1\",\"framerDisableUnlink\":\"\"}},\"__FramerMetadata__\":{\"type\":\"variable\"}}}\n//# sourceMappingURL=./FC_CartCounter.map"],"mappings":"qQAmCG,SAAwB,EAAe,EAAM,CAAC,GAAG,EAAa,SAAS,GAAG,EAAa,OAC1F,OAAoB,EAAK,MAAM,CAAC,MAAM,CAAC,QAAQ,OAAO,WAAW,SAAS,eAAe,SAAS,MAAM,OAAO,OAAO,OAAO,CAAC,SAAsB,EAAK,IAAI,CAAC,MAAM,CAAC,GAAG,EAAM,KAAK,MAAM,EAAM,MAAM,gBAAgB,EAAM,gBAAgB,GAAG,EAAM,OAAO,aAAa,EAAM,OAAO,QAAQ,EAAM,QAAQ,QAAQ,OA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