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