"use client";
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var react = require('react');
var react$1 = require('motion/react');
var utils = require('@/lib/utils');
var jsxRuntime = require('react/jsx-runtime');

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
function usePrefersReducedMotion() {
  const [prefersReducedMotion, setPrefersReducedMotion] = react.useState(false);
  react.useEffect(() => {
    if (typeof window === "undefined") return;
    const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
    setPrefersReducedMotion(mediaQuery.matches);
    const handleChange = /* @__PURE__ */ __name((event) => {
      setPrefersReducedMotion(event.matches);
    }, "handleChange");
    mediaQuery.addEventListener("change", handleChange);
    return () => mediaQuery.removeEventListener("change", handleChange);
  }, []);
  return prefersReducedMotion;
}
__name(usePrefersReducedMotion, "usePrefersReducedMotion");
function useAnimatedNumber(targetValue, from, duration, decimals, format, currency, locale, easing, autoStart, useSpringAnim, springConfig, separator) {
  const [displayValue, setDisplayValue] = react.useState(from);
  const [isAnimating, setIsAnimating] = react.useState(false);
  const animationRef = react.useRef(null);
  const prefersReducedMotion = usePrefersReducedMotion();
  const springValue = react$1.useSpring(from, {
    stiffness: springConfig.tension || 100,
    damping: springConfig.friction || 10,
    mass: springConfig.mass || 1
  });
  const [motionValue, setMotionValue] = react.useState(from);
  const startAnimation = react.useCallback(() => {
    if (prefersReducedMotion) {
      setDisplayValue(targetValue);
      return;
    }
    setIsAnimating(true);
    if (useSpringAnim) {
      springValue.set(targetValue);
    } else {
      const startTime = Date.now();
      const startValue = displayValue;
      const distance = targetValue - startValue;
      const animate = /* @__PURE__ */ __name(() => {
        const elapsed = Date.now() - startTime;
        const progress = Math.min(elapsed / duration, 1);
        const easedProgress = progress;
        const currentValue = startValue + distance * easedProgress;
        setDisplayValue(currentValue);
        setMotionValue(currentValue);
        if (progress < 1) {
          animationRef.current = requestAnimationFrame(animate);
        } else {
          setIsAnimating(false);
          animationRef.current = null;
        }
      }, "animate");
      animationRef.current = requestAnimationFrame(animate);
    }
  }, [
    targetValue,
    displayValue,
    duration,
    useSpringAnim,
    springValue,
    prefersReducedMotion
  ]);
  react.useEffect(() => {
    return () => {
      if (animationRef.current) {
        cancelAnimationFrame(animationRef.current);
      }
    };
  }, []);
  react.useEffect(() => {
    if (useSpringAnim) {
      const unsubscribe = springValue.onChange((latest) => {
        setDisplayValue(latest);
        if (Math.abs(latest - targetValue) < 0.01) {
          setIsAnimating(false);
        }
      });
      return () => {
        unsubscribe();
      };
    }
  }, [springValue, useSpringAnim, targetValue]);
  react.useEffect(() => {
    if (autoStart) {
      startAnimation();
    }
  }, [autoStart, startAnimation]);
  const formattedValue = react.useMemo(() => {
    const roundedValue = Math.round(displayValue * Math.pow(10, decimals)) / Math.pow(10, decimals);
    try {
      switch (format) {
        case "currency":
          return new Intl.NumberFormat(locale, {
            style: "currency",
            currency,
            minimumFractionDigits: decimals,
            maximumFractionDigits: decimals
          }).format(roundedValue);
        case "percent":
          return new Intl.NumberFormat(locale, {
            style: "percent",
            minimumFractionDigits: decimals,
            maximumFractionDigits: decimals
          }).format(roundedValue / 100);
        default:
          let formatted = new Intl.NumberFormat(locale, {
            minimumFractionDigits: decimals,
            maximumFractionDigits: decimals
          }).format(roundedValue);
          if (separator && separator !== ",") {
            formatted = formatted.replace(/,/g, separator);
          }
          return formatted;
      }
    } catch (error) {
      return roundedValue.toFixed(decimals);
    }
  }, [displayValue, decimals, format, currency, locale, separator]);
  return {
    formattedValue,
    displayValue,
    isAnimating,
    startAnimation
  };
}
__name(useAnimatedNumber, "useAnimatedNumber");
function AnimatedNumber({
  value,
  from = 0,
  duration = 2e3,
  decimals = 0,
  format = "number",
  currency = "USD",
  locale = "en-US",
  easing = "easeOut",
  autoStart = true,
  onComplete,
  className,
  style,
  prefix = "",
  suffix = "",
  useSpring: useSpring2 = false,
  springConfig = {},
  separator,
  animateOnHover = false,
  hoverValue
}) {
  const [isHovered, setIsHovered] = react.useState(false);
  const [animationKey, setAnimationKey] = react.useState(0);
  const currentValue = animateOnHover && isHovered && hoverValue !== void 0 ? hoverValue : value;
  const { formattedValue, displayValue, isAnimating, startAnimation } = useAnimatedNumber(
    currentValue,
    from,
    duration,
    decimals,
    format,
    currency,
    locale,
    easing,
    autoStart,
    useSpring2,
    springConfig,
    separator
  );
  react.useEffect(() => {
    if (!isAnimating && onComplete && Math.abs(displayValue - currentValue) < 1e-3) {
      onComplete(displayValue);
    }
  }, [isAnimating, displayValue, currentValue, onComplete]);
  react.useEffect(() => {
    if (animateOnHover && hoverValue !== void 0) {
      startAnimation();
    }
  }, [isHovered, animateOnHover, hoverValue, startAnimation]);
  const handleMouseEnter = /* @__PURE__ */ __name(() => {
    if (animateOnHover) {
      setIsHovered(true);
    }
  }, "handleMouseEnter");
  const handleMouseLeave = /* @__PURE__ */ __name(() => {
    if (animateOnHover) {
      setIsHovered(false);
    }
  }, "handleMouseLeave");
  const prefersReducedMotion = usePrefersReducedMotion();
  return /* @__PURE__ */ jsxRuntime.jsxs(
    react$1.motion.span,
    {
      className: utils.cn("inline-block tabular-nums", className),
      style,
      onMouseEnter: handleMouseEnter,
      onMouseLeave: handleMouseLeave,
      initial: { scale: 1 },
      animate: !prefersReducedMotion && isAnimating ? { scale: [1, 1.05, 1] } : { scale: 1 },
      transition: { duration: 0.3 },
      children: [
        prefix,
        /* @__PURE__ */ jsxRuntime.jsx(
          react$1.motion.span,
          {
            initial: { opacity: 0, y: 20 },
            animate: { opacity: 1, y: 0 },
            transition: { duration: 0.3 },
            children: formattedValue
          },
          animationKey
        ),
        suffix
      ]
    }
  );
}
__name(AnimatedNumber, "AnimatedNumber");
function AnimatedCounter({
  initialValue = 0,
  min = -Infinity,
  max = Infinity,
  step = 1,
  showControls = true,
  incrementButton,
  decrementButton,
  onChange,
  className,
  ...props
}) {
  const [currentValue, setCurrentValue] = react.useState(initialValue);
  const increment = /* @__PURE__ */ __name(() => {
    const newValue = Math.min(currentValue + step, max);
    setCurrentValue(newValue);
    onChange?.(newValue);
  }, "increment");
  const decrement = /* @__PURE__ */ __name(() => {
    const newValue = Math.max(currentValue - step, min);
    setCurrentValue(newValue);
    onChange?.(newValue);
  }, "decrement");
  if (!showControls) {
    return /* @__PURE__ */ jsxRuntime.jsx(AnimatedNumber, { value: currentValue, className, ...props });
  }
  return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: utils.cn("flex items-center gap-2", className), children: [
    /* @__PURE__ */ jsxRuntime.jsx(
      react$1.motion.button,
      {
        onClick: decrement,
        disabled: currentValue <= min,
        className: "flex h-8 w-8 items-center justify-center rounded border bg-background hover:bg-accent disabled:opacity-50",
        whileTap: { scale: 0.95 },
        whileHover: { scale: 1.05 },
        children: decrementButton || "\u2212"
      }
    ),
    /* @__PURE__ */ jsxRuntime.jsx(AnimatedNumber, { value: currentValue, ...props }),
    /* @__PURE__ */ jsxRuntime.jsx(
      react$1.motion.button,
      {
        onClick: increment,
        disabled: currentValue >= max,
        className: "flex h-8 w-8 items-center justify-center rounded border bg-background hover:bg-accent disabled:opacity-50",
        whileTap: { scale: 0.95 },
        whileHover: { scale: 1.05 },
        children: incrementButton || "+"
      }
    )
  ] });
}
__name(AnimatedCounter, "AnimatedCounter");
var number_default = AnimatedNumber;

exports.AnimatedCounter = AnimatedCounter;
exports.AnimatedNumber = AnimatedNumber;
exports.default = number_default;
