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

//# sourceMappingURL=CompositeRoot.js.map