import { jsx as r, jsxs as A } from "react/jsx-runtime";
import * as m from "react";
import { useState as T, useCallback as v } from "react";
import { cn as f } from "../../lib/utils.js";
import { Button as x } from "../atoms/button.js";
import { useShopCartActions as C } from "../../hooks/shop/useShopCartActions.js";
import { useErrorToast as y } from "../../hooks/util/useErrorToast.js";
import { AnimatePresence as N } from "../../shop-minis-react/node_modules/.pnpm/motion@12.17.3_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/motion/dist/es/framer-motion/dist/es/components/AnimatePresence/index.js";
import { motion as k } from "../../shop-minis-react/node_modules/.pnpm/motion@12.17.3_react-dom@19.1.0_react@19.1.0__react@19.1.0/node_modules/motion/dist/es/framer-motion/dist/es/render/components/motion/proxy.js";
import w from "../../shop-minis-react/node_modules/.pnpm/lucide-react@0.513.0_react@19.1.0/node_modules/lucide-react/dist/esm/icons/check.js";
function P({
  disabled: o = !1,
  className: u,
  size: d = "default",
  productId: a,
  productVariantId: i,
  discountCodes: n
}) {
  const { addToCart: c } = C(), [t, s] = T(!1), e = m.useRef(void 0), { showErrorToast: l } = y(), h = v(async () => {
    if (!(t || o))
      try {
        a && i && c({
          productId: a,
          productVariantId: i,
          quantity: 1,
          discountCodes: n
        }).then(() => {
        }).catch(() => {
          l({ message: "Failed to add to cart" });
        }), s(!0), e.current && clearTimeout(e.current), e.current = window.setTimeout(() => {
          s(!1);
        }, 2e3);
      } catch (p) {
        s(!1), console.error("Failed to add to cart:", p);
      }
  }, [
    t,
    o,
    c,
    a,
    i,
    n,
    l
  ]);
  return m.useEffect(() => () => {
    e.current && clearTimeout(e.current);
  }, []), /* @__PURE__ */ r(
    x,
    {
      onClick: h,
      disabled: o,
      className: f(
        "relative overflow-hidden transition-all duration-300",
        u
      ),
      size: d,
      children: /* @__PURE__ */ A("div", { className: "relative flex items-center justify-center", children: [
        /* @__PURE__ */ r(N, { children: t && /* @__PURE__ */ r(
          k.div,
          {
            initial: { scale: 0, rotate: -180 },
            animate: { scale: 1, rotate: 0 },
            exit: { scale: 0, rotate: 180 },
            transition: {
              duration: 0.4,
              ease: [0.175, 0.885, 0.32, 1.275]
              // bounce effect
            },
            className: "absolute left-0",
            style: { x: -8 },
            children: /* @__PURE__ */ r(w, { className: "size-4" })
          }
        ) }),
        /* @__PURE__ */ r("span", { className: f(t && "pl-5", "transition-all duration-300"), children: t ? "Added to cart" : "Add to cart" })
      ] })
    }
  );
}
export {
  P as AddToCartButton
};
//# sourceMappingURL=add-to-cart.js.map
