{"version":3,"file":"CartCheckoutButton.mjs","names":[],"sources":["../../src/CartCheckoutButton.tsx"],"sourcesContent":["import {ReactNode, useEffect, useState} from 'react';\nimport {useCart} from './CartProvider.js';\nimport {\n  BaseButton,\n  type BaseButtonProps,\n  type CustomBaseButtonProps,\n} from './BaseButton.js';\n\ntype ChildrenProps = {\n  /** A `ReactNode` element. */\n  children: ReactNode;\n};\ntype CartCheckoutButtonProps = Omit<BaseButtonProps<'button'>, 'onClick'> &\n  ChildrenProps;\n\n/**\n * The `CartCheckoutButton` component renders a button that redirects to the checkout URL for the cart.\n * It must be a descendent of a `CartProvider` component.\n * @publicDocs\n */\nexport function CartCheckoutButton(\n  props: CartCheckoutButtonProps,\n): JSX.Element {\n  const [requestedCheckout, setRequestedCheckout] = useState(false);\n  const {status, checkoutUrl} = useCart();\n  const {children, ...passthroughProps} = props;\n\n  useEffect(() => {\n    if (requestedCheckout && checkoutUrl && status === 'idle') {\n      window.location.href = checkoutUrl;\n    }\n  }, [requestedCheckout, status, checkoutUrl]);\n\n  return (\n    <BaseButton\n      {...passthroughProps}\n      disabled={requestedCheckout || passthroughProps.disabled}\n      onClick={(): void => setRequestedCheckout(true)}\n    >\n      {children}\n    </BaseButton>\n  );\n}\n\n// This is only for documentation purposes, and it is not used in the code.\n// we ignore this issue because it makes the documentation look better than the equivalent `type` that it wants us to convert to\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type\n/** @publicDocs */\nexport interface CartCheckoutButtonPropsForDocs<\n  AsType extends React.ElementType = 'button',\n> extends Omit<CustomBaseButtonProps<AsType>, 'onClick'> {}\n"],"mappings":";;;;;;;;;;AAoBA,SAAgB,mBACd,OACa;CACb,MAAM,CAAC,mBAAmB,wBAAwB,SAAS,MAAM;CACjE,MAAM,EAAC,QAAQ,gBAAe,SAAS;CACvC,MAAM,EAAC,UAAU,GAAG,qBAAoB;AAExC,iBAAgB;AACd,MAAI,qBAAqB,eAAe,WAAW,OACjD,QAAO,SAAS,OAAO;IAExB;EAAC;EAAmB;EAAQ;EAAY,CAAC;AAE5C,QACE,oBAAC,YAAD;EACE,GAAI;EACJ,UAAU,qBAAqB,iBAAiB;EAChD,eAAqB,qBAAqB,KAAK;EAE9C;EACU,CAAA"}