import { jsxs as m, jsx as c, Fragment as H } from "react/jsx-runtime";
import { useState as p, useMemo as J, useCallback as u, useEffect as C } from "react";
import { B as N } from "../button/button.js";
import { c as Q, s as U, d as Y, b as Z, a as y, e as M } from "../tools/tools.js";
import { C as X } from "./carousel-caption.js";
import { C as B } from "./carousel-item.js";
const g = function(j) {
  const {
    as: K = "div",
    className: E,
    controls: V = !0,
    dropOldClass: S,
    fade: O,
    indicators: D,
    onChange: I,
    options: F,
    pause: h,
    ride: r,
    slide: b = !0,
    style: L,
    touch: v,
    variables: P,
    ...R
  } = j, W = (F ?? []).map((e, a) => ({
    ...e,
    id: e.id ?? a,
    interval: e.interval ?? 3e3
  })), [s, _] = p(W), [o, $] = p(0), [f, i] = p(!1), [x, T] = p(null), q = J(() => {
    const e = Q(!S && "carousel", b && "slide", O && "carousel-fade", E), a = U((t, n) => ({ tKey: y(n) }), P, L);
    return { className: e, style: a };
  }, [E, S, O, b, L, P]), k = u(
    (e, a, t) => {
      const n = s[e], d = s[a];
      n.active = !0, n.carouselItemStart = t === "next" || t === "nextIndicator", n.carouselItemEnd = t === "prev" || t === "prevIndicator", n.carouselItemNext = !1, n.carouselItemPrev = !1, d.active = !1, d.carouselItemNext = t === "next" || t === "nextIndicator", d.carouselItemStart = t === "next" || t === "nextIndicator", d.carouselItemPrev = t === "prev" || t === "prevIndicator", d.carouselItemEnd = t === "prev" || t === "prevIndicator", _([...s]);
    },
    [s]
  ), l = u(
    (e) => {
      const a = s.length;
      if (a === 0)
        return;
      r === !0 && !h && a > 1 && i(!0);
      const t = M(o, a, 0), n = M(
        o,
        a,
        e === "next" || e === "nextIndicator" ? 1 : -1
      );
      k(t, n, e), $(n), I == null || I(s[n].id, e);
    },
    [o, I, s, h, r, k]
  ), w = u(() => {
    f && i(!1);
  }, [f]), z = u(() => {
    r === "carousel" && i(!0);
  }, [r]), A = u(
    (e) => {
      v && T(e.touches[0].clientX);
    },
    [v]
  ), G = u(
    (e) => {
      if (v && x !== null) {
        const a = e.changedTouches[0].clientX, t = x - a;
        t > 50 ? l("next") : t < -50 && l("prev"), T(null);
      }
    },
    [l, v, x]
  );
  return C(() => {
    r === "carousel" && s.length > 1 ? i(!0) : r === !0 && i(!1);
  }, [s.length, r]), C(() => {
    const e = () => {
      document.hidden ? i(!1) : (r === "carousel" || r === !0) && !h && i(!0);
    };
    return document.addEventListener("visibilitychange", e), () => document.removeEventListener("visibilitychange", e);
  }, [r, h]), C(() => {
    let e = null;
    return f && (e = setInterval(() => {
      l("next");
    }, s[o].interval)), () => {
      e && clearInterval(e);
    };
  }, [o, f, l, s]), /* @__PURE__ */ m(
    K,
    {
      ...R,
      ...q,
      onMouseEnter: w,
      onMouseLeave: z,
      onTouchEnd: G,
      onTouchStart: A,
      children: [
        D && /* @__PURE__ */ c("div", { className: "carousel-indicators", children: s.map((e, a) => {
          const { isNext: t } = Y(o, a, s.length);
          return /* @__PURE__ */ c(
            N,
            {
              "aria-current": a === o ? "true" : "false",
              "aria-label": `Slide ${a + 1}`,
              className: Z(null, a === o && "active"),
              "data-bs-target": "",
              dropOldClass: !0,
              onClick: () => l(t ? "nextIndicator" : "prevIndicator")
            },
            e.id
          );
        }) }),
        /* @__PURE__ */ c("div", { className: "carousel-inner", children: s.map((e) => /* @__PURE__ */ m(
          B,
          {
            active: e.active,
            carouselItemEnd: e.carouselItemEnd,
            carouselItemNext: e.carouselItemNext,
            carouselItemPrev: e.carouselItemPrev,
            carouselItemStart: e.carouselItemStart,
            children: [
              e.item,
              e.caption && /* @__PURE__ */ c(X, { children: e.caption })
            ]
          },
          e.id
        )) }),
        V && /* @__PURE__ */ m(H, { children: [
          /* @__PURE__ */ m(N, { className: "carousel-control-prev", dropOldClass: !0, onClick: () => l("prev"), children: [
            /* @__PURE__ */ c("span", { "aria-hidden": "true", className: "carousel-control-prev-icon" }),
            /* @__PURE__ */ c("span", { className: "visually-hidden", children: "Previous" })
          ] }),
          /* @__PURE__ */ m(N, { className: "carousel-control-next", dropOldClass: !0, onClick: () => l("next"), children: [
            /* @__PURE__ */ c("span", { "aria-hidden": "true", className: "carousel-control-next-icon" }),
            /* @__PURE__ */ c("span", { className: "visually-hidden", children: "Next" })
          ] })
        ] })
      ]
    }
  );
};
g.Item = B;
g.Caption = X;
g.displayName = "BRL.Carousel";
export {
  g as C
};
//# sourceMappingURL=carousel.js.map
