"use client";
import * as g from "react";
import { i as Pe } from "./floating-ui.utils.dom-jr2suqy2.js";
import { u as Z } from "./useValueAsRef-LhoZc6As.js";
import { u as J } from "./useStableCallback-CudqoENs.js";
import { u as W } from "./useIsoLayoutEffect-DpJo20by.js";
import { o as Te } from "./owner-rKN292Q9.js";
import { u as Ve, b as ve } from "./popupStateMapping-BT9Y_jIq.js";
import { c as fe, l as xe, f as ze } from "./createBaseUIEventDetails-CqidROtw.js";
import { e as ke } from "./InternalBackdrop-BVEfe6O1.js";
import { g as We, j as Ge, a as we, c as ee, e as ge, f as te, h as ne, b as He, A as Ne } from "./element-DSo91iOi.js";
import { i as De, g as Ee, a as Ke, c as _e, b as Oe, d as qe, e as Be, f as je, h as de, j as $e } from "./composite-BASdnlNo.js";
import { s as K, i as Ue, a as Je } from "./event-BCa0Un_3.js";
import { u as Qe } from "./useTimeout-DAqPwigF.js";
const Xe = "Escape";
function he(r, i, c) {
  switch (r) {
    case "vertical":
      return i;
    case "horizontal":
      return c;
    default:
      return i || c;
  }
}
function me(r, i) {
  return he(i, r === Ne || r === ge, r === te || r === ne);
}
function be(r, i, c) {
  return he(i, r === ge, c ? r === te : r === ne) || r === "Enter" || r === " " || r === "";
}
function Ye(r, i, c) {
  return he(i, c ? r === te : r === ne, r === ge);
}
function Ze(r, i, c, s) {
  const h = c ? r === ne : r === te, O = r === Ne;
  return i === "both" || i === "horizontal" && s && s > 1 ? r === Xe : he(i, h, O);
}
function dt(r, i) {
  const c = "rootStore" in r ? r.rootStore : r, s = c.useState("open"), h = c.useState("floatingElement"), O = c.useState("domReferenceElement"), H = c.context.dataRef, {
    listRef: n,
    activeIndex: S,
    onNavigate: _ = () => {
    },
    enabled: b = !0,
    selectedIndex: P = null,
    allowEscape: w = !1,
    loopFocus: k = !1,
    nested: a = !1,
    rtl: I = !1,
    virtual: d = !1,
    focusItemOnOpen: E = "auto",
    focusItemOnHover: N = !0,
    openOnArrowKeyDown: A = !0,
    disabledIndices: x = void 0,
    orientation: R = "vertical",
    parentOrientation: l,
    cols: F = 1,
    id: G,
    resetOnPointerLeave: V = !0,
    externalTree: q
  } = i;
  process.env.NODE_ENV !== "production" && (w && (k || console.warn("`useListNavigation` looping must be enabled to allow escaping."), d || console.warn("`useListNavigation` must be virtual to allow escaping.")), R === "vertical" && F > 1 && console.warn("In grid list navigation mode (`cols` > 1), the `orientation` should", 'be either "horizontal" or "both".'));
  const re = We(h), B = Z(re), j = Ve(), L = ve(q);
  W(() => {
    H.current.orientation = R;
  }, [H, R]);
  const y = Ge(O), M = g.useRef(E), o = g.useRef(P ?? -1), D = g.useRef(null), v = g.useRef(!0), m = J((e) => {
    _(o.current === -1 ? null : o.current, e);
  }), $ = g.useRef(m), U = g.useRef(!!h), Me = g.useRef(s), T = g.useRef(!1), oe = g.useRef(!1), pe = Z(x), ce = Z(s), Q = Z(P), Ce = Z(V), Re = J(() => {
    function e(f) {
      d ? L?.events.emit("virtualfocus", f) : ke(f, {
        sync: T.current,
        preventScroll: !0
      });
    }
    const u = n.current[o.current], t = oe.current;
    u && e(u), (T.current ? (f) => f() : requestAnimationFrame)(() => {
      const f = n.current[o.current] || u;
      if (!f)
        return;
      u || e(f), // eslint-disable-next-line @typescript-eslint/no-use-before-define
      ye && (t || !v.current) && f.scrollIntoView?.({
        block: "nearest",
        inline: "nearest"
      });
    });
  });
  W(() => {
    b && (s && h ? (o.current = P ?? -1, M.current && P != null && (oe.current = !0, m())) : U.current && (o.current = -1, $.current()));
  }, [b, s, h, P, m]), W(() => {
    if (b) {
      if (!s) {
        T.current = !1;
        return;
      }
      if (h)
        if (S == null) {
          if (T.current = !1, Q.current != null)
            return;
          if (U.current && (o.current = -1, Re()), (!Me.current || !U.current) && M.current && (D.current != null || M.current === !0 && D.current == null)) {
            let e = 0;
            const u = () => {
              n.current[0] == null ? (e < 2 && (e ? requestAnimationFrame : queueMicrotask)(u), e += 1) : (o.current = D.current == null || be(D.current, R, I) || a ? Ee(n) : Ke(n), D.current = null, m());
            };
            u();
          }
        } else De(n, S) || (o.current = S, Re(), oe.current = !1);
    }
  }, [b, s, h, S, Q, a, n, R, I, m, Re, pe]), W(() => {
    if (!b || h || !L || d || !U.current)
      return;
    const e = L.nodesRef.current, u = e.find((f) => f.id === j)?.context?.elements.floating, t = we(Te(h)), p = e.some((f) => f.context && ee(f.context.elements.floating, t));
    u && !p && v.current && u.focus({
      preventScroll: !0
    });
  }, [b, h, L, j, d]), W(() => {
    $.current = m, Me.current = s, U.current = !!h;
  }), W(() => {
    s || (D.current = null, M.current = E);
  }, [s, E]);
  const Se = S != null, ye = g.useMemo(() => {
    function e(t) {
      if (!ce.current)
        return;
      const p = n.current.indexOf(t.currentTarget);
      p !== -1 && o.current !== p && (o.current = p, m(t));
    }
    return {
      onFocus(t) {
        T.current = !0, e(t);
      },
      onClick: ({
        currentTarget: t
      }) => t.focus({
        preventScroll: !0
      }),
      // Safari
      onMouseMove(t) {
        T.current = !0, oe.current = !1, N && e(t);
      },
      onPointerLeave(t) {
        if (!ce.current || !v.current || t.pointerType === "touch")
          return;
        T.current = !0;
        const p = t.relatedTarget;
        if (!(!N || n.current.includes(p)) && Ce.current && (ke(null, {
          sync: !0
        }), o.current = -1, m(t), !d)) {
          const f = B.current, C = we(Te(f));
          f && ee(f, C) && f.focus({
            preventScroll: !0
          });
        }
      }
    };
  }, [ce, B, N, n, m, Ce, d]), se = g.useCallback(() => l ?? L?.nodesRef.current.find((e) => e.id === j)?.context?.dataRef?.current.orientation, [j, L, l]), X = J((e) => {
    if (v.current = !1, T.current = !0, e.which === 229 || !ce.current && e.currentTarget === B.current)
      return;
    if (a && Ze(e.key, R, I, F)) {
      me(e.key, se()) || K(e), c.setOpen(!1, fe(xe, e.nativeEvent)), Pe(O) && (d ? L?.events.emit("virtualfocus", O) : O.focus());
      return;
    }
    const u = o.current, t = Ee(n, x), p = Ke(n, x);
    if (y || (e.key === "Home" && (K(e), o.current = t, m(e)), e.key === "End" && (K(e), o.current = p, m(e))), F > 1) {
      const f = Array.from({
        length: n.current.length
      }, () => ({
        width: 1,
        height: 1
      })), C = _e(f, F, !1), ae = C.findIndex((z) => z != null && !Oe(n, z, x)), Ie = C.reduce((z, Y, Ae) => Y != null && !Oe(n, Y, x) ? Ae : z, -1), le = C[qe({
        current: C.map((z) => z != null ? n.current[z] : null)
      }, {
        event: e,
        orientation: R,
        loopFocus: k,
        rtl: I,
        cols: F,
        // treat undefined (empty grid spaces) as disabled indices so we
        // don't end up in them
        disabledIndices: je([...(typeof x != "function" ? x : null) || n.current.map((z, Y) => Oe(n, Y, x) ? Y : void 0), void 0], C),
        minIndex: ae,
        maxIndex: Ie,
        prevIndex: Be(
          o.current > p ? t : o.current,
          f,
          C,
          F,
          // 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 === ge ? "bl" : e.key === (I ? te : ne) ? "tr" : "tl"
        ),
        stopEvent: !0
      })];
      if (le != null && (o.current = le, m(e)), R === "both")
        return;
    }
    if (me(e.key, R)) {
      if (K(e), s && !d && we(e.currentTarget.ownerDocument) === e.currentTarget) {
        o.current = be(e.key, R, I) ? t : p, m(e);
        return;
      }
      be(e.key, R, I) ? k ? u >= p ? w && u !== n.current.length ? o.current = -1 : (T.current = !1, o.current = t) : o.current = de(n, {
        startingIndex: u,
        disabledIndices: x
      }) : o.current = Math.min(p, de(n, {
        startingIndex: u,
        disabledIndices: x
      })) : k ? u <= t ? w && u !== -1 ? o.current = n.current.length : (T.current = !1, o.current = p) : o.current = de(n, {
        startingIndex: u,
        decrement: !0,
        disabledIndices: x
      }) : o.current = Math.max(t, de(n, {
        startingIndex: u,
        decrement: !0,
        disabledIndices: x
      })), De(n, o.current) && (o.current = -1), m(e);
    }
  }), ie = g.useMemo(() => d && s && Se && {
    "aria-activedescendant": `${G}-${S}`
  }, [d, s, Se, G, S]), Fe = g.useMemo(() => ({
    "aria-orientation": R === "both" ? void 0 : R,
    ...y ? {} : ie,
    onKeyDown(e) {
      if (e.key === "Tab" && e.shiftKey && s && !d) {
        const u = He(e.nativeEvent);
        if (u && !ee(B.current, u))
          return;
        K(e), c.setOpen(!1, fe(ze, e.nativeEvent)), Pe(O) && O.focus();
        return;
      }
      X(e);
    },
    onPointerMove() {
      v.current = !0;
    }
  }), [ie, X, B, R, y, c, s, d, O]), ue = g.useMemo(() => {
    function e(t) {
      E === "auto" && Ue(t.nativeEvent) && (M.current = !d);
    }
    function u(t) {
      M.current = E, E === "auto" && Je(t.nativeEvent) && (M.current = !0);
    }
    return {
      onKeyDown(t) {
        const p = c.select("open");
        v.current = !1;
        const f = t.key.startsWith("Arrow"), C = Ye(t.key, se(), I), ae = me(t.key, R), Ie = (a ? C : ae) || t.key === "Enter" || t.key.trim() === "";
        if (d && p)
          return X(t);
        if (!(!p && !A && f)) {
          if (Ie) {
            const le = me(t.key, se());
            D.current = a && le ? null : t.key;
          }
          if (a) {
            C && (K(t), p ? (o.current = Ee(n, pe.current), m(t)) : c.setOpen(!0, fe(xe, t.nativeEvent, t.currentTarget)));
            return;
          }
          ae && (Q.current != null && (o.current = Q.current), K(t), !p && A ? c.setOpen(!0, fe(xe, t.nativeEvent, t.currentTarget)) : X(t), p && m(t));
        }
      },
      onFocus(t) {
        c.select("open") && !d && (o.current = -1, m(t));
      },
      onPointerDown: u,
      onPointerEnter: u,
      onMouseDown: e,
      onClick: e
    };
  }, [X, pe, E, n, a, m, c, A, R, se, I, Q, d]), Le = g.useMemo(() => ({
    ...ie,
    ...ue
  }), [ie, ue]);
  return g.useMemo(() => b ? {
    reference: Le,
    floating: Fe,
    item: ye,
    trigger: ue
  } : {}, [b, Le, Fe, ue, ye]);
}
function mt(r, i) {
  const c = "rootStore" in r ? r.rootStore : r, s = c.context.dataRef, h = c.useState("open"), {
    listRef: O,
    elementsRef: H,
    activeIndex: n,
    onMatch: S,
    onTypingChange: _,
    enabled: b = !0,
    resetMs: P = 750,
    selectedIndex: w = null
  } = i, k = Qe(), a = g.useRef(""), I = g.useRef(w ?? n ?? -1), d = g.useRef(null);
  W(() => {
    !h && w !== null || (k.clear(), d.current = null, a.current !== "" && (a.current = ""));
  }, [h, w, k]), W(() => {
    h && a.current === "" && (I.current = w ?? n ?? -1);
  }, [h, w, n]);
  const E = J((l) => {
    l ? s.current.typing || (s.current.typing = l, _?.(l)) : s.current.typing && (s.current.typing = l, _?.(l));
  }), N = J((l) => {
    function F(y) {
      const M = H?.current[y];
      return !M || $e(M);
    }
    function G(y, M, o = 0) {
      if (y.length === 0)
        return -1;
      const D = (o % y.length + y.length) % y.length, v = M.toLocaleLowerCase();
      for (let m = 0; m < y.length; m += 1) {
        const $ = (D + m) % y.length;
        if (!(!y[$]?.toLocaleLowerCase().startsWith(v) || !F($)))
          return $;
      }
      return -1;
    }
    const V = O.current;
    if (a.current.length > 0 && l.key === " " && (K(l), E(!0)), a.current.length > 0 && a.current[0] !== " " && G(V, a.current) === -1 && l.key !== " " && E(!1), V == null || // Character key.
    l.key.length !== 1 || // Modifier key.
    l.ctrlKey || l.metaKey || l.altKey)
      return;
    h && l.key !== " " && (K(l), E(!0));
    const q = a.current === "";
    q && (I.current = w ?? n ?? -1), V.every((y) => y ? y[0]?.toLocaleLowerCase() !== y[1]?.toLocaleLowerCase() : !0) && a.current === l.key && (a.current = "", I.current = d.current), a.current += l.key, k.start(P, () => {
      a.current = "", I.current = d.current, E(!1);
    });
    const j = ((q ? w ?? n ?? -1 : I.current) ?? 0) + 1, L = G(V, a.current, j);
    L !== -1 ? (S?.(L), d.current = L) : l.key !== " " && (a.current = "", E(!1));
  }), A = J((l) => {
    const F = l.relatedTarget, G = c.select("domReferenceElement"), V = c.select("floatingElement"), q = ee(G, F), re = ee(V, F);
    q || re || (k.clear(), a.current = "", I.current = d.current, E(!1));
  }), x = g.useMemo(() => ({
    onKeyDown: N,
    onBlur: A
  }), [N, A]), R = g.useMemo(() => ({
    onKeyDown: N,
    onBlur: A
  }), [N, A]);
  return g.useMemo(() => b ? {
    reference: x,
    floating: R
  } : {}, [b, x, R]);
}
function gt(r) {
  const i = r.getBoundingClientRect();
  if (process.env.NODE_ENV !== "production")
    return i;
  const c = window.getComputedStyle(r, "::before"), s = window.getComputedStyle(r, "::after");
  if (!(c.content !== "none" || s.content !== "none"))
    return i;
  const O = parseFloat(c.width) || 0, H = parseFloat(c.height) || 0, n = parseFloat(s.width) || 0, S = parseFloat(s.height) || 0, _ = Math.max(i.width, O, n), b = Math.max(i.height, H, S), P = _ - i.width, w = b - i.height;
  return {
    left: i.left - P / 2,
    right: i.right + P / 2,
    top: i.top - w / 2,
    bottom: i.bottom + w / 2
  };
}
export {
  mt as a,
  gt as g,
  dt as u
};
