import * as l from "react";
import g from "react";
import { a as G, P as L, c as A, u as $ } from "./index-DCcK_d4W.mjs";
import { c as K, u as J } from "./index-DOfn3ixy.mjs";
import { u as P } from "./index-DLcqcWxM.mjs";
import { jsx as m } from "react/jsx-runtime";
import { u as Q } from "./index-CEQ35j8b.mjs";
import { u as W } from "./index-ByaXH_ih.mjs";
function X(e) {
  const r = e + "CollectionProvider", [a, n] = K(r), [_, v] = a(
    r,
    { collectionRef: { current: null }, itemMap: /* @__PURE__ */ new Map() }
  ), I = (f) => {
    const { scope: o, children: i } = f, d = g.useRef(null), t = g.useRef(/* @__PURE__ */ new Map()).current;
    return /* @__PURE__ */ m(_, { scope: o, itemMap: t, collectionRef: d, children: i });
  };
  I.displayName = r;
  const R = e + "CollectionSlot", h = G(R), p = g.forwardRef(
    (f, o) => {
      const { scope: i, children: d } = f, t = v(R, i), c = P(o, t.collectionRef);
      return /* @__PURE__ */ m(h, { ref: c, children: d });
    }
  );
  p.displayName = R;
  const s = e + "CollectionItemSlot", C = "data-radix-collection-item", F = G(s), b = g.forwardRef(
    (f, o) => {
      const { scope: i, children: d, ...t } = f, c = g.useRef(null), T = P(o, c), w = v(s, i);
      return g.useEffect(() => (w.itemMap.set(c, { ref: c, ...t }), () => {
        w.itemMap.delete(c);
      })), /* @__PURE__ */ m(F, { [C]: "", ref: T, children: d });
    }
  );
  b.displayName = s;
  function E(f) {
    const o = v(e + "CollectionConsumer", f);
    return g.useCallback(() => {
      const d = o.collectionRef.current;
      if (!d) return [];
      const t = Array.from(d.querySelectorAll(`[${C}]`));
      return Array.from(o.itemMap.values()).sort(
        (w, M) => t.indexOf(w.ref.current) - t.indexOf(M.ref.current)
      );
    }, [o.collectionRef, o.itemMap]);
  }
  return [
    { Provider: I, Slot: p, ItemSlot: b },
    E,
    n
  ];
}
var N = "rovingFocusGroup.onEntryFocus", Z = { bubbles: !1, cancelable: !0 }, y = "RovingFocusGroup", [O, k, ee] = X(y), [oe, Re] = K(
  y,
  [ee]
), [te, re] = oe(y), U = l.forwardRef(
  (e, r) => /* @__PURE__ */ m(O.Provider, { scope: e.__scopeRovingFocusGroup, children: /* @__PURE__ */ m(O.Slot, { scope: e.__scopeRovingFocusGroup, children: /* @__PURE__ */ m(ne, { ...e, ref: r }) }) })
);
U.displayName = y;
var ne = l.forwardRef((e, r) => {
  const {
    __scopeRovingFocusGroup: a,
    orientation: n,
    loop: _ = !1,
    dir: v,
    currentTabStopId: I,
    defaultCurrentTabStopId: R,
    onCurrentTabStopIdChange: h,
    onEntryFocus: p,
    preventScrollOnEntryFocus: s = !1,
    ...C
  } = e, F = l.useRef(null), b = P(r, F), E = W(v), [f, o] = $({
    prop: I,
    defaultProp: R ?? null,
    onChange: h,
    caller: y
  }), [i, d] = l.useState(!1), t = J(p), c = k(a), T = l.useRef(!1), [w, M] = l.useState(0);
  return l.useEffect(() => {
    const u = F.current;
    if (u)
      return u.addEventListener(N, t), () => u.removeEventListener(N, t);
  }, [t]), /* @__PURE__ */ m(
    te,
    {
      scope: a,
      orientation: n,
      dir: E,
      loop: _,
      currentTabStopId: f,
      onItemFocus: l.useCallback(
        (u) => o(u),
        [o]
      ),
      onItemShiftTab: l.useCallback(() => d(!0), []),
      onFocusableItemAdd: l.useCallback(
        () => M((u) => u + 1),
        []
      ),
      onFocusableItemRemove: l.useCallback(
        () => M((u) => u - 1),
        []
      ),
      children: /* @__PURE__ */ m(
        L.div,
        {
          tabIndex: i || w === 0 ? -1 : 0,
          "data-orientation": n,
          ...C,
          ref: b,
          style: { outline: "none", ...e.style },
          onMouseDown: A(e.onMouseDown, () => {
            T.current = !0;
          }),
          onFocus: A(e.onFocus, (u) => {
            const H = !T.current;
            if (u.target === u.currentTarget && H && !i) {
              const D = new CustomEvent(N, Z);
              if (u.currentTarget.dispatchEvent(D), !D.defaultPrevented) {
                const x = c().filter((S) => S.focusable), j = x.find((S) => S.active), q = x.find((S) => S.id === f), z = [j, q, ...x].filter(
                  Boolean
                ).map((S) => S.ref.current);
                Y(z, s);
              }
            }
            T.current = !1;
          }),
          onBlur: A(e.onBlur, () => d(!1))
        }
      )
    }
  );
}), B = "RovingFocusGroupItem", V = l.forwardRef(
  (e, r) => {
    const {
      __scopeRovingFocusGroup: a,
      focusable: n = !0,
      active: _ = !1,
      tabStopId: v,
      children: I,
      ...R
    } = e, h = Q(), p = v || h, s = re(B, a), C = s.currentTabStopId === p, F = k(a), { onFocusableItemAdd: b, onFocusableItemRemove: E, currentTabStopId: f } = s;
    return l.useEffect(() => {
      if (n)
        return b(), () => E();
    }, [n, b, E]), /* @__PURE__ */ m(
      O.ItemSlot,
      {
        scope: a,
        id: p,
        focusable: n,
        active: _,
        children: /* @__PURE__ */ m(
          L.span,
          {
            tabIndex: C ? 0 : -1,
            "data-orientation": s.orientation,
            ...R,
            ref: r,
            onMouseDown: A(e.onMouseDown, (o) => {
              n ? s.onItemFocus(p) : o.preventDefault();
            }),
            onFocus: A(e.onFocus, () => s.onItemFocus(p)),
            onKeyDown: A(e.onKeyDown, (o) => {
              if (o.key === "Tab" && o.shiftKey) {
                s.onItemShiftTab();
                return;
              }
              if (o.target !== o.currentTarget) return;
              const i = ie(o, s.orientation, s.dir);
              if (i !== void 0) {
                if (o.metaKey || o.ctrlKey || o.altKey || o.shiftKey) return;
                o.preventDefault();
                let t = F().filter((c) => c.focusable).map((c) => c.ref.current);
                if (i === "last") t.reverse();
                else if (i === "prev" || i === "next") {
                  i === "prev" && t.reverse();
                  const c = t.indexOf(o.currentTarget);
                  t = s.loop ? ae(t, c + 1) : t.slice(c + 1);
                }
                setTimeout(() => Y(t));
              }
            }),
            children: typeof I == "function" ? I({ isCurrentTabStop: C, hasTabStop: f != null }) : I
          }
        )
      }
    );
  }
);
V.displayName = B;
var ce = {
  ArrowLeft: "prev",
  ArrowUp: "prev",
  ArrowRight: "next",
  ArrowDown: "next",
  PageUp: "first",
  Home: "first",
  PageDown: "last",
  End: "last"
};
function se(e, r) {
  return r !== "rtl" ? e : e === "ArrowLeft" ? "ArrowRight" : e === "ArrowRight" ? "ArrowLeft" : e;
}
function ie(e, r, a) {
  const n = se(e.key, a);
  if (!(r === "vertical" && ["ArrowLeft", "ArrowRight"].includes(n)) && !(r === "horizontal" && ["ArrowUp", "ArrowDown"].includes(n)))
    return ce[n];
}
function Y(e, r = !1) {
  const a = document.activeElement;
  for (const n of e)
    if (n === a || (n.focus({ preventScroll: r }), document.activeElement !== a)) return;
}
function ae(e, r) {
  return e.map((a, n) => e[(r + n) % e.length]);
}
var Ce = U, be = V;
export {
  be as I,
  Ce as R,
  Re as c
};
//# sourceMappingURL=index-IpWiejWQ.mjs.map
