import * as s from "react";
import { composeEventHandlers as p } from "../../../../../@radix-ui_primitive@1.1.0/node_modules/@radix-ui/primitive/dist/index.js";
import { createCollection as H } from "../../../../../@radix-ui_react-collection@1.1.0_@types_react-dom@18.3.6_@types_react@18.3.3__@types_re_068c4d9b06eb1baa6b6ecff66d582472/node_modules/@radix-ui/react-collection/dist/index.js";
import { useComposedRefs as V } from "../../../../../@radix-ui_react-compose-refs@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-compose-refs/dist/index.js";
import { createContextScope as z } from "../../../../../@radix-ui_react-context@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-context/dist/index.js";
import { useId as q } from "../../../../../@radix-ui_react-id@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-id/dist/index.js";
import { Primitive as P } from "../../../../../@radix-ui_react-primitive@2.0.0_@types_react-dom@18.3.6_@types_react@18.3.3__@types_rea_bdcf182289a3a76356f07e550726e479/node_modules/@radix-ui/react-primitive/dist/index.js";
import { useCallbackRef as J } from "../../../../../@radix-ui_react-use-callback-ref@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-use-callback-ref/dist/index.js";
import { useControllableState as Q } from "../../../../../@radix-ui_react-use-controllable-state@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-use-controllable-state/dist/index.js";
import { useDirection as W } from "../../../../../@radix-ui_react-direction@1.1.0_@types_react@18.3.3_react@18.3.0-canary-3d9b20132-20240124/node_modules/@radix-ui/react-direction/dist/index.js";
import { j as d } from "../../../../../../../_virtual/jsx-runtime.js";
var _ = "rovingFocusGroup.onEntryFocus", X = { bubbles: !1, cancelable: !0 }, b = "RovingFocusGroup", [h, G, Z] = H(b), [$, Fe] = z(
  b,
  [Z]
), [ee, oe] = $(b), N = s.forwardRef(
  (e, r) => /* @__PURE__ */ d.jsx(h.Provider, { scope: e.__scopeRovingFocusGroup, children: /* @__PURE__ */ d.jsx(h.Slot, { scope: e.__scopeRovingFocusGroup, children: /* @__PURE__ */ d.jsx(te, { ...e, ref: r }) }) })
);
N.displayName = b;
var te = s.forwardRef((e, r) => {
  const {
    __scopeRovingFocusGroup: c,
    orientation: o,
    loop: F = !1,
    dir: g,
    currentTabStopId: R,
    defaultCurrentTabStopId: E,
    onCurrentTabStopIdChange: m,
    onEntryFocus: u,
    preventScrollOnEntryFocus: w = !1,
    ...C
  } = e, v = s.useRef(null), I = V(r, v), t = W(g), [f = null, T] = Q({
    prop: R,
    defaultProp: E,
    onChange: m
  }), [a, i] = s.useState(!1), S = J(u), M = G(c), x = s.useRef(!1), [k, y] = s.useState(0);
  return s.useEffect(() => {
    const n = v.current;
    if (n)
      return n.addEventListener(_, S), () => n.removeEventListener(_, S);
  }, [S]), /* @__PURE__ */ d.jsx(
    ee,
    {
      scope: c,
      orientation: o,
      dir: t,
      loop: F,
      currentTabStopId: f,
      onItemFocus: s.useCallback(
        (n) => T(n),
        [T]
      ),
      onItemShiftTab: s.useCallback(() => i(!0), []),
      onFocusableItemAdd: s.useCallback(
        () => y((n) => n + 1),
        []
      ),
      onFocusableItemRemove: s.useCallback(
        () => y((n) => n - 1),
        []
      ),
      children: /* @__PURE__ */ d.jsx(
        P.div,
        {
          tabIndex: a || k === 0 ? -1 : 0,
          "data-orientation": o,
          ...C,
          ref: I,
          style: { outline: "none", ...e.style },
          onMouseDown: p(e.onMouseDown, () => {
            x.current = !0;
          }),
          onFocus: p(e.onFocus, (n) => {
            const L = !x.current;
            if (n.target === n.currentTarget && L && !a) {
              const D = new CustomEvent(_, X);
              if (n.currentTarget.dispatchEvent(D), !D.defaultPrevented) {
                const A = M().filter((l) => l.focusable), U = A.find((l) => l.active), B = A.find((l) => l.id === f), Y = [U, B, ...A].filter(
                  Boolean
                ).map((l) => l.ref.current);
                j(Y, w);
              }
            }
            x.current = !1;
          }),
          onBlur: p(e.onBlur, () => i(!1))
        }
      )
    }
  );
}), O = "RovingFocusGroupItem", K = s.forwardRef(
  (e, r) => {
    const {
      __scopeRovingFocusGroup: c,
      focusable: o = !0,
      active: F = !1,
      tabStopId: g,
      ...R
    } = e, E = q(), m = g || E, u = oe(O, c), w = u.currentTabStopId === m, C = G(c), { onFocusableItemAdd: v, onFocusableItemRemove: I } = u;
    return s.useEffect(() => {
      if (o)
        return v(), () => I();
    }, [o, v, I]), /* @__PURE__ */ d.jsx(
      h.ItemSlot,
      {
        scope: c,
        id: m,
        focusable: o,
        active: F,
        children: /* @__PURE__ */ d.jsx(
          P.span,
          {
            tabIndex: w ? 0 : -1,
            "data-orientation": u.orientation,
            ...R,
            ref: r,
            onMouseDown: p(e.onMouseDown, (t) => {
              o ? u.onItemFocus(m) : t.preventDefault();
            }),
            onFocus: p(e.onFocus, () => u.onItemFocus(m)),
            onKeyDown: p(e.onKeyDown, (t) => {
              if (t.key === "Tab" && t.shiftKey) {
                u.onItemShiftTab();
                return;
              }
              if (t.target !== t.currentTarget) return;
              const f = se(t, u.orientation, u.dir);
              if (f !== void 0) {
                if (t.metaKey || t.ctrlKey || t.altKey || t.shiftKey) return;
                t.preventDefault();
                let a = C().filter((i) => i.focusable).map((i) => i.ref.current);
                if (f === "last") a.reverse();
                else if (f === "prev" || f === "next") {
                  f === "prev" && a.reverse();
                  const i = a.indexOf(t.currentTarget);
                  a = u.loop ? ce(a, i + 1) : a.slice(i + 1);
                }
                setTimeout(() => j(a));
              }
            })
          }
        )
      }
    );
  }
);
K.displayName = O;
var re = {
  ArrowLeft: "prev",
  ArrowUp: "prev",
  ArrowRight: "next",
  ArrowDown: "next",
  PageUp: "first",
  Home: "first",
  PageDown: "last",
  End: "last"
};
function ne(e, r) {
  return r !== "rtl" ? e : e === "ArrowLeft" ? "ArrowRight" : e === "ArrowRight" ? "ArrowLeft" : e;
}
function se(e, r, c) {
  const o = ne(e.key, c);
  if (!(r === "vertical" && ["ArrowLeft", "ArrowRight"].includes(o)) && !(r === "horizontal" && ["ArrowUp", "ArrowDown"].includes(o)))
    return re[o];
}
function j(e, r = !1) {
  const c = document.activeElement;
  for (const o of e)
    if (o === c || (o.focus({ preventScroll: r }), document.activeElement !== c)) return;
}
function ce(e, r) {
  return e.map((c, o) => e[(r + o) % e.length]);
}
var ge = N, Re = K;
export {
  Re as Item,
  ge as Root,
  N as RovingFocusGroup,
  K as RovingFocusGroupItem,
  Fe as createRovingFocusGroupScope
};
//# sourceMappingURL=index.js.map
