"use client";
'use strict';

var React = require('react');
var clsx = require('clsx');
var tailwindMerge = require('tailwind-merge');
var react = require('motion/react');
var img = require('@page-speed/img');
var jsxRuntime = require('react/jsx-runtime');

function _interopNamespace(e) {
  if (e && e.__esModule) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var React__namespace = /*#__PURE__*/_interopNamespace(React);

function cn(...inputs) {
  return tailwindMerge.twMerge(clsx.clsx(inputs));
}
var slideVariants = {
  initial: {
    scale: 0.98,
    opacity: 0,
    rotateX: 12
  },
  visible: {
    scale: 1,
    rotateX: 0,
    opacity: 1,
    transition: {
      duration: 0.55,
      ease: [0.645, 0.045, 0.355, 1]
    }
  },
  upExit: {
    opacity: 0.8,
    y: "-120%",
    transition: {
      duration: 0.8
    }
  },
  downExit: {
    opacity: 0.8,
    y: "120%",
    transition: {
      duration: 0.8
    }
  }
};
var fadeVariants = {
  initial: {
    opacity: 0
  },
  visible: {
    opacity: 1,
    transition: {
      duration: 0.8,
      ease: [0.4, 0, 0.2, 1]
    }
  },
  fadeExit: {
    opacity: 0,
    transition: {
      duration: 0.8,
      ease: [0.4, 0, 0.2, 1]
    }
  }
};
var normalizeIndex = (index, length) => {
  if (!length) return 0;
  const safeIndex = index % length;
  return safeIndex < 0 ? safeIndex + length : safeIndex;
};
var ImageSlider = ({
  images,
  children,
  overlay = true,
  overlaySlot,
  overlayClassName,
  className,
  imageClassName,
  contentClassName,
  autoplay = true,
  autoplayIntervalMs = 6e3,
  direction = "up",
  transition = "slide",
  startIndex = 0,
  enableKeyboard = true,
  onSlideChange,
  optixFlowConfig
}) => {
  const hasImages = images.length > 0;
  const [currentIndex, setCurrentIndex] = React__namespace.useState(
    () => normalizeIndex(startIndex, images.length)
  );
  const handleNext = React__namespace.useCallback(() => {
    if (!hasImages) return;
    setCurrentIndex((prevIndex) => {
      const nextIndex = prevIndex + 1 >= images.length ? 0 : prevIndex + 1;
      onSlideChange?.(nextIndex);
      return nextIndex;
    });
  }, [hasImages, images.length, onSlideChange]);
  const handlePrevious = React__namespace.useCallback(() => {
    if (!hasImages) return;
    setCurrentIndex((prevIndex) => {
      const nextIndex = prevIndex - 1 < 0 ? images.length - 1 : prevIndex - 1;
      onSlideChange?.(nextIndex);
      return nextIndex;
    });
  }, [hasImages, images.length, onSlideChange]);
  React__namespace.useEffect(() => {
    if (!hasImages) return;
    setCurrentIndex(normalizeIndex(startIndex, images.length));
  }, [startIndex, images.length, hasImages]);
  React__namespace.useEffect(() => {
    if (!enableKeyboard || !hasImages) return;
    const handleKeyDown = (event) => {
      if (event.key === "ArrowRight") {
        handleNext();
      } else if (event.key === "ArrowLeft") {
        handlePrevious();
      }
    };
    window.addEventListener("keydown", handleKeyDown);
    return () => {
      window.removeEventListener("keydown", handleKeyDown);
    };
  }, [enableKeyboard, handleNext, handlePrevious, hasImages]);
  React__namespace.useEffect(() => {
    if (!autoplay || images.length < 2) return;
    const interval = window.setInterval(handleNext, autoplayIntervalMs);
    return () => window.clearInterval(interval);
  }, [autoplay, autoplayIntervalMs, handleNext, images.length]);
  const activeImage = hasImages ? images[currentIndex] : null;
  const overlayContent = overlaySlot ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: cn("absolute inset-0 z-10", overlayClassName), children: overlaySlot }) : overlay ? /* @__PURE__ */ jsxRuntime.jsx(
    "div",
    {
      className: cn(
        "absolute inset-0 z-10 bg-linear-to-b from-black/35 via-black/50 to-black/70",
        overlayClassName
      )
    }
  ) : null;
  return /* @__PURE__ */ jsxRuntime.jsxs(
    "div",
    {
      className: cn(
        "relative flex min-h-[420px] w-full items-center justify-center overflow-hidden rounded-3xl border border-border/40 bg-muted/30 shadow-2xl",
        className
      ),
      style: {
        perspective: "1000px"
      },
      children: [
        /* @__PURE__ */ jsxRuntime.jsx(
          react.AnimatePresence,
          {
            mode: transition === "fade" ? "sync" : "wait",
            initial: false,
            children: activeImage ? /* @__PURE__ */ jsxRuntime.jsx(
              react.motion.div,
              {
                initial: "initial",
                animate: "visible",
                exit: transition === "fade" ? "fadeExit" : direction === "up" ? "upExit" : "downExit",
                variants: transition === "fade" ? fadeVariants : slideVariants,
                className: "absolute inset-0",
                children: /* @__PURE__ */ jsxRuntime.jsx(
                  img.Img,
                  {
                    src: activeImage.src,
                    alt: activeImage.alt,
                    className: cn(
                      "h-full w-full object-cover object-center",
                      imageClassName,
                      activeImage.className
                    ),
                    optixFlowConfig: activeImage.optixFlowConfig ?? optixFlowConfig,
                    eager: true
                  }
                )
              },
              `${currentIndex}-${activeImage.src ?? "image"}`
            ) : null
          }
        ),
        overlayContent,
        children ? /* @__PURE__ */ jsxRuntime.jsx(
          "div",
          {
            className: cn(
              "relative z-20 flex w-full flex-col items-center justify-center px-6 py-12 text-center text-white md:px-12",
              contentClassName
            ),
            children
          }
        ) : null
      ]
    }
  );
};

exports.ImageSlider = ImageSlider;
