"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Carousel_exports = {};
__export(Carousel_exports, {
  default: () => Carousel_default
});
module.exports = __toCommonJS(Carousel_exports);
var import_embla_carousel_autoplay = __toESM(require("embla-carousel-autoplay"));
var import_embla_carousel_react = __toESM(require("embla-carousel-react"));
var import_debounce = __toESM(require("lodash/debounce"));
var import_react = __toESM(require("react"));
var import_styled_components = __toESM(require("styled-components"));
var import_Footer = __toESM(require("./Footer"));
var import_ChevronArrow = require("./ChevronArrow");
const Embla = import_styled_components.default.div`
  overflow: hidden;
`;
const EmblaContainer = import_styled_components.default.div`
  display: flex;
`;
const EmblaSlide = import_styled_components.default.div`
  display: flex;
  flex: 0 0 100%;
  min-width: 0;
  > * {
    flex-basis: 100%;
  }
`;
const CarouselContainer = import_styled_components.default.div`
  position: relative;

  --hover-transition: 0;
  &:hover {
    --hover-transition: 1;
  }
`;
const Carousel = ({
  children,
  variant = "default",
  initialDelay = 0,
  autoPlay = 0,
  onNext,
  onPrev
}) => {
  const [currentIndex, setCurrentIndex] = (0, import_react.useState)(0);
  const [autoplayEnabled, setAutoplayEnabled] = (0, import_react.useState)(false);
  const plugins = (0, import_react.useMemo)(
    () => autoplayEnabled && autoPlay ? [(0, import_embla_carousel_autoplay.default)({ delay: autoPlay, ...AutoplayFlags })] : [],
    [autoplayEnabled, autoPlay]
  );
  const [emblaRef, emblaApi] = (0, import_embla_carousel_react.default)({ loop: true }, plugins);
  (0, import_react.useEffect)(() => {
    if (autoPlay) {
      const timer = setTimeout(() => setAutoplayEnabled(true), initialDelay);
      return () => clearTimeout(timer);
    }
  }, [autoPlay]);
  const updateIndex = (0, import_react.useCallback)(() => {
    if (!emblaApi)
      return;
    const newIndex = emblaApi.selectedScrollSnap();
    setCurrentIndex(newIndex);
    emblaApi.scrollTo(newIndex);
  }, [emblaApi]);
  (0, import_react.useEffect)(() => {
    if (!emblaApi)
      return;
    updateIndex();
    const dragX = watchDragX(emblaApi);
    const handleAnySelect = (0, import_debounce.default)((mightBeASwipe) => {
      updateIndex();
      if (!mightBeASwipe || variant !== "default")
        return;
      if (dragX.value > 0)
        return onPrev == null ? void 0 : onPrev();
      if (dragX.value < 0)
        return onNext == null ? void 0 : onNext();
    }, 0);
    emblaApi.on("select", handleSelect);
    emblaApi.on("autoplay:select", handleAutoPlaySelect);
    emblaApi.on("button:prev", handlePrevButton);
    emblaApi.on("button:next", handleNextButton);
    emblaApi.on("reInit", updateIndex);
    return () => {
      dragX.clean();
      emblaApi.off("select", handleSelect);
      emblaApi.off("autoplay:select", handleAutoPlaySelect);
      emblaApi.off("button:prev", handlePrevButton);
      emblaApi.off("button:next", handleNextButton);
      emblaApi.off("reInit", updateIndex);
    };
    function handleSelect() {
      handleAnySelect(true);
    }
    function handleAutoPlaySelect() {
      handleAnySelect(false);
    }
    function handlePrevButton() {
      emblaApi == null ? void 0 : emblaApi.scrollPrev();
      onPrev == null ? void 0 : onPrev();
      handleAnySelect(false);
    }
    function handleNextButton() {
      emblaApi == null ? void 0 : emblaApi.scrollNext();
      onNext == null ? void 0 : onNext();
      handleAnySelect(false);
    }
  }, [emblaApi, updateIndex, variant]);
  if (!children.length)
    return null;
  const handleGotoPrevSlide = () => emblaApi == null ? void 0 : emblaApi.emit("button:prev");
  const handleGotoNextSlide = () => emblaApi == null ? void 0 : emblaApi.emit("button:next");
  return /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement(CarouselContainer, { variant }, variant === "default" && children.length > 1 && /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, /* @__PURE__ */ import_react.default.createElement(
    import_ChevronArrow.ChevronArrow,
    {
      "data-testid": "carousel-arrow-prev",
      direction: "left",
      onClick: handleGotoPrevSlide
    }
  ), /* @__PURE__ */ import_react.default.createElement(
    import_ChevronArrow.ChevronArrow,
    {
      "data-testid": "carousel-arrow-next",
      direction: "right",
      onClick: handleGotoNextSlide
    }
  )), /* @__PURE__ */ import_react.default.createElement(Embla, { ref: emblaRef }, /* @__PURE__ */ import_react.default.createElement(EmblaContainer, null, children.map((child) => /* @__PURE__ */ import_react.default.createElement(EmblaSlide, { key: child.key }, child))))), /* @__PURE__ */ import_react.default.createElement(
    import_Footer.default,
    {
      children,
      variant,
      emblaApi,
      currentIndex
    }
  ));
};
var Carousel_default = Carousel;
const AutoplayFlags = {
  play: true,
  stopOnMouseEnter: true,
  stopOnInteraction: false
};
function watchDragX(emblaApi) {
  emblaApi.on("pointerDown", watchMouse);
  let start;
  let end;
  return {
    get value() {
      return typeof start === "undefined" || typeof end === "undefined" ? 0 : end - start;
    },
    clean: () => {
      emblaApi.off("pointerDown", watchMouse);
      document.removeEventListener("mouseup", handleMouseUp);
    }
  };
  function watchMouse() {
    document.addEventListener("mousemove", handleMouseMove);
    document.addEventListener("mouseup", handleMouseUp);
  }
  function handleMouseMove(event) {
    document.removeEventListener("mousemove", handleMouseMove);
    start = event.clientX;
    end = void 0;
  }
  function handleMouseUp(event) {
    document.removeEventListener("mouseup", handleMouseUp);
    end = event.clientX;
  }
}
//# sourceMappingURL=index.js.map
