import { o as Ke } from "./rolldown-runtime.js";
import { i as Ne } from "./vendor/react.js";
import { p as Le } from "./safeReact.js";
import { n as v, t as q } from "./useIsoLayoutEffect.js";
import { n as Ve } from "./useTimeout.js";
import { a as _e, i as Fe, n as Ge, r as Pe } from "./constants.js";
import { n as X, r as We, t as he } from "./shadowDom.js";
import { o as qe, t as ze } from "./element.js";
import { _ as He, v as ve } from "./popupStateMapping.js";
import { a as Be, i as Ue, o as P } from "./event.js";
import { a as $e, c as Qe, i as je, l as Te, n as se, o as Se, r as Je, s as ye, t as Xe, u as Ie } from "./composite.js";
import { t as ke } from "./owner.js";
import { c as Ye, h as we, t as ie } from "./createBaseUIEventDetails.js";
import { t as J } from "./useValueAsRef.js";
import { o as Ze } from "./useOpenInteractionType.js";
var f = /* @__PURE__ */ Ke(Ne()), er = "Escape";
function fe(c, h, u) {
  switch (c) {
    case "vertical":
      return h;
    case "horizontal":
      return u;
    default:
      return h || u;
  }
}
function ae(c, h) {
  return fe(h, c === "ArrowUp" || c === "ArrowDown", c === "ArrowLeft" || c === "ArrowRight");
}
function xe(c, h, u) {
  return fe(h, c === "ArrowDown", u ? c === "ArrowLeft" : c === "ArrowRight") || c === "Enter" || c === " " || c === "";
}
function rr(c, h, u) {
  return fe(h, u ? c === Pe : c === Fe, c === Ge);
}
function tr(c, h, u, l) {
  const p = u ? c === Fe : c === Pe, x = c === _e;
  return h === "both" || h === "horizontal" && l && l > 1 ? c === er : fe(h, p, x);
}
function yr(c, h) {
  const u = "rootStore" in c ? c.rootStore : c, l = u.useState("open"), p = u.useState("floatingElement"), x = u.useState("domReferenceElement"), Y = u.context.dataRef, { listRef: n, activeIndex: O, onNavigate: Z = () => {
  }, enabled: E = !0, selectedIndex: z = null, allowEscape: M = !1, loopFocus: D = !1, nested: i = !1, rtl: y = !1, virtual: m = !1, focusItemOnOpen: w = "auto", focusItemOnHover: K = !0, openOnArrowKeyDown: N = !0, disabledIndices: I = void 0, orientation: R = "vertical", parentOrientation: a, cols: C = 1, id: H, resetOnPointerLeave: V = !0, externalTree: B } = h, L = J(ze(p)), _ = He(), g = ve(B);
  q(() => {
    Y.current.orientation = R;
  }, [Y, R]);
  const G = qe(x), T = f.useRef(w), t = f.useRef(z ?? -1), S = f.useRef(null), A = f.useRef(!0), d = v((e) => {
    Z(t.current === -1 ? null : t.current, e);
  }), Ee = f.useRef(d), U = f.useRef(!!p), Oe = f.useRef(l), b = f.useRef(!1), ee = f.useRef(!1), le = f.useRef(null), de = J(I), re = J(l), $ = J(z), Me = J(V), me = v(() => {
    function e(s) {
      m ? g?.events.emit("virtualfocus", s) : le.current = Ze(s, {
        sync: b.current,
        preventScroll: !0
      });
    }
    const o = n.current[t.current], r = ee.current;
    o && e(o), (b.current ? (s) => s() : requestAnimationFrame)(() => {
      const s = n.current[t.current] || o;
      s && (o || e(s), pe && (r || !A.current) && s.scrollIntoView?.({
        block: "nearest",
        inline: "nearest"
      }));
    });
  });
  q(() => {
    E && (l && p ? (t.current = z ?? -1, T.current && z != null && (ee.current = !0, d())) : U.current && (t.current = -1, Ee.current()));
  }, [
    E,
    l,
    p,
    z,
    d
  ]), q(() => {
    if (E) {
      if (!l) {
        b.current = !1;
        return;
      }
      if (p)
        if (O == null) {
          if (b.current = !1, $.current != null) return;
          if (U.current && (t.current = -1, me()), (!Oe.current || !U.current) && T.current && (S.current != null || T.current === !0 && S.current == null)) {
            let e = 0;
            const o = () => {
              n.current[0] == null ? (e < 2 && (e ? requestAnimationFrame : queueMicrotask)(o), e += 1) : (t.current = S.current == null || xe(S.current, R, y) || i ? ye(n) : Se(n), S.current = null, d());
            };
            o();
          }
        } else Te(n.current, O) || (t.current = O, me(), ee.current = !1);
    }
  }, [
    E,
    l,
    p,
    O,
    $,
    i,
    n,
    R,
    y,
    d,
    me,
    de
  ]), q(() => {
    if (!E || p || !g || m || !U.current) return;
    const e = g.nodesRef.current, o = e.find((k) => k.id === _)?.context?.elements.floating, r = he(ke(p)), s = e.some((k) => k.context && X(k.context.elements.floating, r));
    o && !s && A.current && o.focus({ preventScroll: !0 });
  }, [
    E,
    p,
    g,
    _,
    m
  ]), q(() => {
    Ee.current = d, Oe.current = l, U.current = !!p;
  }), q(() => {
    l || (S.current = null, T.current = w);
  }, [l, w]);
  const Ce = O != null, ge = v((e) => {
    if (!re.current) return;
    const o = n.current.indexOf(e.currentTarget);
    o !== -1 && (t.current !== o || O !== o) && (t.current = o, d(e));
  }), pe = f.useMemo(() => ({
    onFocus(e) {
      b.current = !0, ge(e);
    },
    onClick: ({ currentTarget: e }) => e.focus({ preventScroll: !0 }),
    onMouseMove(e) {
      b.current = !0, ee.current = !1, K && ge(e);
    },
    onPointerLeave(e) {
      if (!re.current || !A.current || e.pointerType === "touch") return;
      b.current = !0;
      const o = e.relatedTarget;
      if (!(!K || n.current.includes(o)) && Me.current && (le.current?.(), le.current = null, t.current = -1, d(e), !m)) {
        const r = L.current, s = he(ke(r));
        r && X(r, s) && r.focus({ preventScroll: !0 });
      }
    }
  }), [
    ge,
    re,
    L,
    K,
    n,
    d,
    Me,
    m
  ]), te = f.useCallback(() => a ?? g?.nodesRef.current.find((e) => e.id === _)?.context?.dataRef?.current.orientation, [
    _,
    g,
    a
  ]), Q = v((e) => {
    if (A.current = !1, b.current = !0, e.which === 229 || !re.current && e.currentTarget === L.current) return;
    if (i && tr(e.key, R, y, C)) {
      ae(e.key, te()) || P(e), u.setOpen(!1, ie(we, e.nativeEvent)), Le(x) && (m ? g?.events.emit("virtualfocus", x) : x.focus());
      return;
    }
    const o = t.current, r = ye(n, I), s = Se(n, I);
    if (G || (e.key === "Home" && (P(e), t.current = r, d(e)), e.key === "End" && (P(e), t.current = s, d(e))), C > 1) {
      const k = Array.from({ length: n.current.length }, () => ({
        width: 1,
        height: 1
      })), F = Xe(k, C, !1), ce = F.findIndex((W) => W != null && !Ie(n.current, W, I)), Re = F.reduce((W, j, De) => j != null && !Ie(n.current, j, I) ? De : W, -1), ue = F[$e(F.map((W) => W != null ? n.current[W] : null), {
        event: e,
        orientation: R,
        loopFocus: D,
        rtl: y,
        cols: C,
        disabledIndices: je([...(typeof I != "function" ? I : null) || n.current.map((W, j) => Ie(n.current, j, I) ? j : void 0), void 0], F),
        minIndex: ce,
        maxIndex: Re,
        prevIndex: Je(t.current > s ? r : t.current, k, F, C, e.key === "ArrowDown" ? "bl" : e.key === (y ? "ArrowLeft" : "ArrowRight") ? "tr" : "tl"),
        stopEvent: !0
      })];
      if (ue != null && (t.current = ue, d(e)), R === "both") return;
    }
    if (ae(e.key, R)) {
      if (P(e), l && !m && he(e.currentTarget.ownerDocument) === e.currentTarget) {
        t.current = xe(e.key, R, y) ? r : s, d(e);
        return;
      }
      xe(e.key, R, y) ? D ? o >= s ? M && o !== n.current.length ? t.current = -1 : (b.current = !1, t.current = r) : t.current = se(n.current, {
        startingIndex: o,
        disabledIndices: I
      }) : t.current = Math.min(s, se(n.current, {
        startingIndex: o,
        disabledIndices: I
      })) : D ? o <= r ? M && o !== -1 ? t.current = n.current.length : (b.current = !1, t.current = s) : t.current = se(n.current, {
        startingIndex: o,
        decrement: !0,
        disabledIndices: I
      }) : t.current = Math.max(r, se(n.current, {
        startingIndex: o,
        decrement: !0,
        disabledIndices: I
      })), Te(n.current, t.current) && (t.current = -1), d(e);
    }
  }), ne = f.useMemo(() => m && l && Ce && { "aria-activedescendant": `${H}-${O}` }, [
    m,
    l,
    Ce,
    H,
    O
  ]), Ae = f.useMemo(() => ({
    "aria-orientation": R === "both" ? void 0 : R,
    ...G ? {} : ne,
    onKeyDown(e) {
      if (e.key === "Tab" && e.shiftKey && l && !m) {
        const o = We(e.nativeEvent);
        if (o && !X(L.current, o)) return;
        P(e), u.setOpen(!1, ie(Ye, e.nativeEvent)), Le(x) && x.focus();
        return;
      }
      Q(e);
    },
    onPointerMove() {
      A.current = !0;
    }
  }), [
    ne,
    Q,
    L,
    R,
    G,
    u,
    l,
    m,
    x
  ]), oe = f.useMemo(() => {
    function e(r) {
      w === "auto" && Ue(r.nativeEvent) && (T.current = !m);
    }
    function o(r) {
      T.current = w, w === "auto" && Be(r.nativeEvent) && (T.current = !0);
    }
    return {
      onKeyDown(r) {
        const s = u.select("open");
        A.current = !1;
        const k = r.key.startsWith("Arrow"), F = rr(r.key, te(), y), ce = ae(r.key, R), Re = (i ? F : ce) || r.key === "Enter" || r.key.trim() === "";
        if (m && s) return Q(r);
        if (!(!s && !N && k)) {
          if (Re) {
            const ue = ae(r.key, te());
            S.current = i && ue ? null : r.key;
          }
          if (i) {
            F && (P(r), s ? (t.current = ye(n, de.current), d(r)) : u.setOpen(!0, ie(we, r.nativeEvent, r.currentTarget)));
            return;
          }
          ce && ($.current != null && (t.current = $.current), P(r), !s && N ? u.setOpen(!0, ie(we, r.nativeEvent, r.currentTarget)) : Q(r), s && d(r));
        }
      },
      onFocus(r) {
        u.select("open") && !m && (t.current = -1, d(r));
      },
      onPointerDown: o,
      onPointerEnter: o,
      onMouseDown: e,
      onClick: e
    };
  }, [
    Q,
    de,
    w,
    n,
    i,
    d,
    u,
    N,
    R,
    te,
    y,
    $,
    m
  ]), be = f.useMemo(() => ({
    ...ne,
    ...oe
  }), [ne, oe]);
  return f.useMemo(() => E ? {
    reference: be,
    floating: Ae,
    item: pe,
    trigger: oe
  } : {}, [
    E,
    be,
    Ae,
    oe,
    pe
  ]);
}
function Ir(c, h) {
  const u = "rootStore" in c ? c.rootStore : c, l = u.context.dataRef, p = u.useState("open"), { listRef: x, elementsRef: Y, activeIndex: n, onMatch: O, onTypingChange: Z, enabled: E = !0, resetMs: z = 750, selectedIndex: M = null } = h, D = Ve(), i = f.useRef(""), y = f.useRef(M ?? n ?? -1), m = f.useRef(null);
  q(() => {
    !p && M !== null || (D.clear(), m.current = null, i.current !== "" && (i.current = ""));
  }, [
    p,
    M,
    D
  ]), q(() => {
    p && i.current === "" && (y.current = M ?? n ?? -1);
  }, [
    p,
    M,
    n
  ]);
  const w = v((a) => {
    a ? l.current.typing || (l.current.typing = a, Z?.(a)) : l.current.typing && (l.current.typing = a, Z?.(a));
  }), K = v((a) => {
    function C(g) {
      const G = Y?.current[g];
      return !G || Qe(G);
    }
    function H(g, G, T = 0) {
      if (g.length === 0) return -1;
      const t = (T % g.length + g.length) % g.length, S = G.toLocaleLowerCase();
      for (let A = 0; A < g.length; A += 1) {
        const d = (t + A) % g.length;
        if (!(!g[d]?.toLocaleLowerCase().startsWith(S) || !C(d)))
          return d;
      }
      return -1;
    }
    const V = x.current;
    if (i.current.length > 0 && a.key === " " && (P(a), w(!0)), i.current.length > 0 && i.current[0] !== " " && H(V, i.current) === -1 && a.key !== " " && w(!1), V == null || a.key.length !== 1 || a.ctrlKey || a.metaKey || a.altKey) return;
    p && a.key !== " " && (P(a), w(!0));
    const B = i.current === "";
    B && (y.current = M ?? n ?? -1), V.every((g) => g ? g[0]?.toLocaleLowerCase() !== g[1]?.toLocaleLowerCase() : !0) && i.current === a.key && (i.current = "", y.current = m.current), i.current += a.key, D.start(z, () => {
      i.current = "", y.current = m.current, w(!1);
    });
    const L = ((B ? M ?? n ?? -1 : y.current) ?? 0) + 1, _ = H(V, i.current, L);
    _ !== -1 ? (O?.(_), m.current = _) : a.key !== " " && (i.current = "", w(!1));
  }), N = v((a) => {
    const C = a.relatedTarget, H = u.select("domReferenceElement"), V = u.select("floatingElement"), B = X(H, C), L = X(V, C);
    B || L || (D.clear(), i.current = "", y.current = m.current, w(!1));
  }), I = f.useMemo(() => ({
    onKeyDown: K,
    onBlur: N
  }), [K, N]), R = f.useMemo(() => ({
    onKeyDown: K,
    onBlur: N
  }), [K, N]);
  return f.useMemo(() => E ? {
    reference: I,
    floating: R
  } : {}, [
    E,
    I,
    R
  ]);
}
export {
  yr as n,
  Ir as t
};

//# sourceMappingURL=useTypeahead.js.map