import * as I from "react";
import { isElementDisabled as te } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/isElementDisabled.js";
import { useStableCallback as F } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useStableCallback.js";
import { useMergedRefs as ne } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useMergedRefs.js";
import { scrollIntoViewIfNeeded as P, ALL_KEYS as re, ARROW_KEYS as ie, isNativeInput as V, ARROW_DOWN as b, ARROW_RIGHT as C, HOME as oe, END as se, ARROW_LEFT as B, ARROW_UP as H, VERTICAL_KEYS_WITH_EXTRA_KEYS as le, VERTICAL_KEYS as ae, HORIZONTAL_KEYS_WITH_EXTRA_KEYS as de, HORIZONTAL_KEYS as ce, MODIFIER_KEYS as fe } from "../composite.js";
import { ACTIVE_COMPOSITE_ITEM as ue } from "../constants.js";
import { getMinListIndex as he, getMaxListIndex as Ie, createGridCellMap as ge, isListIndexDisabled as L, getGridNavigatedIndex as Re, getGridCellIndexOfCorner as xe, getGridCellIndices as ye, findNonDisabledListIndex as me, isIndexOutOfListBounds as Ee } from "../../floating-ui-react/utils/composite.js";
const Ke = [];
function Me(k) {
  const {
    itemSizes: m,
    cols: l = 1,
    loopFocus: E = !0,
    dense: Y = !1,
    orientation: r = "both",
    direction: K,
    highlightedIndex: X,
    onHighlightedIndexChange: Z,
    rootRef: q,
    enableHomeAndEndKeys: g = !1,
    stopEventPropagation: w = !1,
    disabledIndices: o,
    modifierKeys: z = Ke
  } = k, [U, j] = I.useState(0), p = l > 1, R = I.useRef(null), D = ne(R, q), t = I.useRef([]), N = I.useRef(!1), a = X ?? U, x = F((e, s = !1) => {
    if ((Z ?? j)(e), s) {
      const u = t.current[e];
      P(R.current, u, K, r);
    }
  }), G = F((e) => {
    if (e.size === 0 || N.current)
      return;
    N.current = !0;
    const s = Array.from(e.keys()), u = s.find((d) => d == null ? void 0 : d.hasAttribute(ue)) ?? null, h = u ? s.indexOf(u) : -1;
    h !== -1 && x(h), P(R.current, u, K, r);
  }), O = I.useMemo(() => ({
    "aria-orientation": r === "both" ? void 0 : r,
    ref: D,
    onFocus(e) {
      !R.current || !V(e.target) || e.target.setSelectionRange(0, e.target.value.length ?? 0);
    },
    onKeyDown(e) {
      const s = g ? re : ie;
      if (!s.has(e.key) || Se(e, z) || !R.current)
        return;
      const h = K === "rtl", d = h ? B : C, J = {
        horizontal: d,
        vertical: b,
        both: d
      }[r], S = h ? C : B, Q = {
        horizontal: S,
        vertical: H,
        both: S
      }[r];
      if (V(e.target) && !te(e.target)) {
        const i = e.target.selectionStart, c = e.target.selectionEnd, T = e.target.value ?? "";
        if (i == null || e.shiftKey || i !== c || e.key !== Q && i < T.length || e.key !== J && i > 0)
          return;
      }
      let n = a;
      const _ = he(t, o), A = Ie(t, o);
      if (p) {
        const i = m || Array.from({
          length: t.current.length
        }, () => ({
          width: 1,
          height: 1
        })), c = ge(i, l, Y), T = c.findIndex((f) => f != null && !L(t, f, o)), v = c.reduce((f, y, ee) => y != null && !L(t, y, o) ? ee : f, -1);
        n = c[Re({
          current: c.map((f) => f ? t.current[f] : null)
        }, {
          event: e,
          orientation: r,
          loopFocus: E,
          cols: l,
          // treat undefined (empty grid spaces) as disabled indices so we
          // don't end up in them
          disabledIndices: ye([...o || t.current.map((f, y) => L(t, y) ? y : void 0), void 0], c),
          minIndex: T,
          maxIndex: v,
          prevIndex: xe(
            a > A ? _ : a,
            i,
            c,
            l,
            // use a corner matching the edge closest to the direction we're
            // moving in so we don't end up in the same item. Prefer
            // top/left over bottom/right.
            // eslint-disable-next-line no-nested-ternary
            e.key === b ? "bl" : e.key === C ? "tr" : "tl"
          ),
          rtl: h
        })];
      }
      const W = {
        horizontal: [d],
        vertical: [b],
        both: [d, b]
      }[r], M = {
        horizontal: [S],
        vertical: [H],
        both: [S, H]
      }[r], $ = p ? s : {
        horizontal: g ? de : ce,
        vertical: g ? le : ae,
        both: s
      }[r];
      g && (e.key === oe ? n = _ : e.key === se && (n = A)), n === a && (W.includes(e.key) || M.includes(e.key)) && (E && n === A && W.includes(e.key) ? n = _ : E && n === _ && M.includes(e.key) ? n = A : n = me(t, {
        startingIndex: n,
        decrement: M.includes(e.key),
        disabledIndices: o
      })), n !== a && !Ee(t, n) && (w && e.stopPropagation(), $.has(e.key) && e.preventDefault(), x(n, !0), queueMicrotask(() => {
        var i;
        (i = t.current[n]) == null || i.focus();
      }));
    }
  }), [l, Y, K, o, t, g, a, p, m, E, D, z, x, r, w]);
  return I.useMemo(() => ({
    props: O,
    highlightedIndex: a,
    onHighlightedIndexChange: x,
    elementsRef: t,
    disabledIndices: o,
    onMapChange: G,
    relayKeyboardEvent: O.onKeyDown
  }), [O, a, x, t, o, G]);
}
function Se(k, m) {
  for (const l of fe.values())
    if (!m.includes(l) && k.getModifierState(l))
      return !0;
  return !1;
}
export {
  Me as useCompositeRoot
};
