"use client";
import * as p from "react";
import { C as oe } from "./CompositeList-yq5K_yj6.js";
import { i as se } from "./isElementDisabled-Cbd8W9kH.js";
import { u as j } from "./useStableCallback-CudqoENs.js";
import { c as ie, b as q, E as re, u as ae } from "./useRenderElement-DNB2vtF1.js";
import { s as v, A as le, a as de, i as J, b as P, c as B, H as ce, E as fe, d as $, e as U, V as ue, f as he, g as ge, h as me, M as Ie } from "./composite-BSpUD7nI.js";
import { g as pe, a as xe, c as Re, b as X, d as Ee, e as ye, f as Ce, h as be, i as Ae } from "./composite-BASdnlNo.js";
import { C as Ke } from "./useButton-JYPKQKj4.js";
import { u as Me } from "./DirectionContext-BSOheMwC.js";
import { jsx as Q } from "react/jsx-runtime";
const _e = "data-composite-item-active", Se = [];
function ke(x) {
  const {
    itemSizes: C,
    cols: a = 1,
    loopFocus: R = !0,
    dense: _ = !1,
    orientation: o = "both",
    direction: E,
    highlightedIndex: w,
    onHighlightedIndexChange: z,
    rootRef: D,
    enableHomeAndEndKeys: h = !1,
    stopEventPropagation: S = !1,
    disabledIndices: i,
    modifierKeys: k = Se
  } = x, [N, G] = p.useState(0), b = a > 1, g = p.useRef(null), H = ie(g, D), t = p.useRef([]), A = p.useRef(!1), r = w ?? N, m = j((e, s = !1) => {
    if ((z ?? G)(e), s) {
      const d = t.current[e];
      v(g.current, d, E, o);
    }
  }), O = j((e) => {
    if (e.size === 0 || A.current)
      return;
    A.current = !0;
    const s = Array.from(e.keys()), d = s.find((l) => l?.hasAttribute(_e)) ?? null, c = d ? s.indexOf(d) : -1;
    c !== -1 && m(c), v(g.current, d, E, o);
  }), K = p.useMemo(() => ({
    "aria-orientation": o === "both" ? void 0 : o,
    ref: H,
    onFocus(e) {
      !g.current || !J(e.target) || e.target.setSelectionRange(0, e.target.value.length ?? 0);
    },
    onKeyDown(e) {
      const s = h ? le : de;
      if (!s.has(e.key) || He(e, k) || !g.current)
        return;
      const c = E === "rtl", l = c ? $ : B, W = {
        horizontal: l,
        vertical: P,
        both: l
      }[o], y = c ? B : $, T = {
        horizontal: y,
        vertical: U,
        both: y
      }[o];
      if (J(e.target) && !se(e.target)) {
        const I = e.target.selectionStart, f = e.target.selectionEnd, F = e.target.value ?? "";
        if (I == null || e.shiftKey || I !== f || e.key !== T && I < F.length || e.key !== W && I > 0)
          return;
      }
      let n = r;
      const Y = pe(t, i), L = xe(t, i);
      if (b) {
        const I = C || Array.from({
          length: t.current.length
        }, () => ({
          width: 1,
          height: 1
        })), f = Re(I, a, _), F = f.findIndex((u) => u != null && !X(t, u, i)), te = f.reduce((u, M, ne) => M != null && !X(t, M, i) ? ne : u, -1);
        n = f[Ee({
          current: f.map((u) => u ? t.current[u] : null)
        }, {
          event: e,
          orientation: o,
          loopFocus: R,
          cols: a,
          // treat undefined (empty grid spaces) as disabled indices so we
          // don't end up in them
          disabledIndices: Ce([...i || t.current.map((u, M) => X(t, M) ? M : void 0), void 0], f),
          minIndex: F,
          maxIndex: te,
          prevIndex: ye(
            r > L ? Y : r,
            I,
            f,
            a,
            // 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 === P ? "bl" : e.key === B ? "tr" : "tl"
          ),
          rtl: c
        })];
      }
      const Z = {
        horizontal: [l],
        vertical: [P],
        both: [l, P]
      }[o], V = {
        horizontal: [y],
        vertical: [U],
        both: [y, U]
      }[o], ee = b ? s : {
        horizontal: h ? ge : me,
        vertical: h ? ue : he,
        both: s
      }[o];
      h && (e.key === ce ? n = Y : e.key === fe && (n = L)), n === r && (Z.includes(e.key) || V.includes(e.key)) && (R && n === L && Z.includes(e.key) ? n = Y : R && n === Y && V.includes(e.key) ? n = L : n = be(t, {
        startingIndex: n,
        decrement: V.includes(e.key),
        disabledIndices: i
      })), n !== r && !Ae(t, n) && (S && e.stopPropagation(), ee.has(e.key) && e.preventDefault(), m(n, !0), queueMicrotask(() => {
        t.current[n]?.focus();
      }));
    }
  }), [a, _, E, i, t, h, r, b, C, R, H, k, m, o, S]);
  return p.useMemo(() => ({
    props: K,
    highlightedIndex: r,
    onHighlightedIndexChange: m,
    elementsRef: t,
    disabledIndices: i,
    onMapChange: O,
    relayKeyboardEvent: K.onKeyDown
  }), [K, r, m, t, i, O]);
}
function He(x, C) {
  for (const a of Ie.values())
    if (!C.includes(a) && x.getModifierState(a))
      return !0;
  return !1;
}
function Ge(x) {
  const {
    render: C,
    className: a,
    refs: R = q,
    props: _ = q,
    state: o = re,
    stateAttributesMapping: E,
    highlightedIndex: w,
    onHighlightedIndexChange: z,
    orientation: D,
    dense: h,
    itemSizes: S,
    loopFocus: i,
    cols: k,
    enableHomeAndEndKeys: N,
    onMapChange: G,
    stopEventPropagation: b = !0,
    rootRef: g,
    disabledIndices: H,
    modifierKeys: t,
    highlightItemOnHover: A = !1,
    tag: r = "div",
    ...m
  } = x, O = Me(), {
    props: K,
    highlightedIndex: e,
    onHighlightedIndexChange: s,
    elementsRef: d,
    onMapChange: c,
    relayKeyboardEvent: l
  } = ke({
    itemSizes: S,
    cols: k,
    loopFocus: i,
    dense: h,
    orientation: D,
    highlightedIndex: w,
    onHighlightedIndexChange: z,
    rootRef: g,
    stopEventPropagation: b,
    enableHomeAndEndKeys: N,
    direction: O,
    disabledIndices: H,
    modifierKeys: t
  }), W = ae(r, x, {
    state: o,
    ref: R,
    props: [K, ..._, m],
    stateAttributesMapping: E
  }), y = p.useMemo(() => ({
    highlightedIndex: e,
    onHighlightedIndexChange: s,
    highlightItemOnHover: A,
    relayKeyboardEvent: l
  }), [e, s, A, l]);
  return /* @__PURE__ */ Q(Ke.Provider, {
    value: y,
    children: /* @__PURE__ */ Q(oe, {
      elementsRef: d,
      onMapChange: (T) => {
        G?.(T), c(T);
      },
      children: W
    })
  });
}
export {
  _e as A,
  Ge as C
};
