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