import { o as O } from "./rolldown-runtime.js";
import { i as S } from "./vendor/react.js";
import { f as T, p as x, u as L } from "./safeReact.js";
import { n as _ } from "./useTimeout.js";
import { a as A, o as B } from "./detectBrowser.js";
import { n as w, r as K, t as F } from "./shadowDom.js";
import { a as k, c as P, s as V } from "./element.js";
import { D as q, k as G } from "./popupStateMapping.js";
import { t as D } from "./owner.js";
import { t as v } from "./addEventListener.js";
import { t as b, x as y } from "./createBaseUIEventDetails.js";
var c = /* @__PURE__ */ O(S()), M = A && B;
function ee(m, C = {}) {
  const e = "rootStore" in m ? m.rootStore : m, { events: g, dataRef: h } = e.context, { enabled: u = !0, delay: l } = C, a = c.useRef(!1), i = c.useRef(null), d = _(), p = c.useRef(!0);
  c.useEffect(() => {
    const r = e.select("domReferenceElement");
    if (!u) return;
    const t = L(r);
    function o() {
      const n = e.select("domReferenceElement");
      !e.select("open") && x(n) && n === F(D(n)) && (a.current = !0);
    }
    function f() {
      p.current = !0;
    }
    function s() {
      p.current = !1;
    }
    return G(v(t, "blur", o), M && v(t, "keydown", f, !0), M && v(t, "pointerdown", s, !0));
  }, [e, u]), c.useEffect(() => {
    if (!u) return;
    function r(t) {
      if (t.reason === "trigger-press" || t.reason === "escape-key") {
        const o = e.select("domReferenceElement");
        T(o) && (i.current = o, a.current = !0);
      }
    }
    return g.on("openchange", r), () => {
      g.off("openchange", r);
    };
  }, [
    g,
    u,
    e
  ]);
  const E = c.useMemo(() => ({
    onMouseLeave() {
      a.current = !1, i.current = null;
    },
    onFocus(r) {
      const t = r.currentTarget;
      if (a.current) {
        if (i.current === t) return;
        a.current = !1, i.current = null;
      }
      const o = K(r.nativeEvent);
      if (T(o)) {
        if (M && !r.relatedTarget) {
          if (!p.current && !V(o)) return;
        } else if (!P(o)) return;
      }
      const f = k(r.relatedTarget, e.context.triggerElements), { nativeEvent: s, currentTarget: n } = r, R = typeof l == "function" ? l() : l;
      if (e.select("open") && f || R === 0 || R === void 0) {
        e.setOpen(!0, b(y, s, n));
        return;
      }
      d.start(R, () => {
        a.current || e.setOpen(!0, b(y, s, n));
      });
    },
    onBlur(r) {
      a.current = !1, i.current = null;
      const t = r.relatedTarget, o = r.nativeEvent, f = T(t) && t.hasAttribute(q("focus-guard")) && t.getAttribute("data-type") === "outside";
      d.start(0, () => {
        const s = e.select("domReferenceElement"), n = F(D(s));
        !t && n === s || w(h.current.floatingContext?.refs.floating.current, n) || w(s, n) || f || k(t ?? n, e.context.triggerElements) || e.setOpen(!1, b(y, o));
      });
    }
  }), [
    h,
    e,
    d,
    l
  ]);
  return c.useMemo(() => u ? {
    reference: E,
    trigger: E
  } : {}, [u, E]);
}
export {
  ee as t
};

//# sourceMappingURL=useFocus.js.map