import * as i from "react";
import { getWindow as x, isElement as d, isHTMLElement as S } from "../../../../../../../@floating-ui_utils@0.2.10/node_modules/@floating-ui/utils/dist/floating-ui.utils.dom.js";
import { isMac as A, isSafari as C } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/detectBrowser.js";
import { useTimeout as B } from "../../../../../../../@base-ui_utils@0.2.4_@types_react@18.3.27_react-dom@18.3.1_react@18.3.1__react@18.3.1/node_modules/@base-ui/utils/esm/useTimeout.js";
import { createChangeEventDetails as T } from "../../utils/createBaseUIEventDetails.js";
import { createAttribute as K } from "../utils/createAttribute.js";
import { activeElement as D, contains as b, getTarget as P, isTypeableElement as V, matchesFocusVisible as G, getDocument as H } from "../utils/element.js";
import { triggerFocus as y, triggerPress as W, escapeKey as j } from "../../utils/reason-parts.js";
const h = A && C;
function X(g, O = {}) {
  const t = "rootStore" in g ? g.rootStore : g, {
    events: E,
    dataRef: w
  } = t.context, {
    enabled: a = !0,
    visibleOnly: L = !0,
    delay: m
  } = O, s = i.useRef(!1), l = i.useRef(null), p = B(), v = i.useRef(!0);
  i.useEffect(() => {
    const r = t.select("domReferenceElement");
    if (!a)
      return;
    const e = x(r);
    function n() {
      const o = t.select("domReferenceElement");
      !t.select("open") && S(o) && o === D(H(o)) && (s.current = !0);
    }
    function f() {
      v.current = !0;
    }
    function c() {
      v.current = !1;
    }
    return e.addEventListener("blur", n), h && (e.addEventListener("keydown", f, !0), e.addEventListener("pointerdown", c, !0)), () => {
      e.removeEventListener("blur", n), h && (e.removeEventListener("keydown", f, !0), e.removeEventListener("pointerdown", c, !0));
    };
  }, [t, a]), i.useEffect(() => {
    if (!a)
      return;
    function r(e) {
      if (e.reason === W || e.reason === j) {
        const n = t.select("domReferenceElement");
        d(n) && (l.current = n, s.current = !0);
      }
    }
    return E.on("openchange", r), () => {
      E.off("openchange", r);
    };
  }, [E, a, t]);
  const R = i.useMemo(() => ({
    onMouseLeave() {
      s.current = !1, l.current = null;
    },
    onFocus(r) {
      const e = r.currentTarget;
      if (s.current) {
        if (l.current === e)
          return;
        s.current = !1, l.current = null;
      }
      const n = P(r.nativeEvent);
      if (L && d(n)) {
        if (h && !r.relatedTarget) {
          if (!v.current && !V(n))
            return;
        } else if (!G(n))
          return;
      }
      const f = r.relatedTarget && t.context.triggerElements.hasElement(r.relatedTarget), {
        nativeEvent: c,
        currentTarget: o
      } = r, u = typeof m == "function" ? m() : m;
      if (t.select("open") && f || u === 0 || u === void 0) {
        t.setOpen(!0, T(y, c, o));
        return;
      }
      p.start(u, () => {
        s.current || t.setOpen(!0, T(y, c, o));
      });
    },
    onBlur(r) {
      s.current = !1, l.current = null;
      const e = r.relatedTarget, n = r.nativeEvent, f = d(e) && e.hasAttribute(K("focus-guard")) && e.getAttribute("data-type") === "outside";
      p.start(0, () => {
        var F;
        const c = t.select("domReferenceElement"), o = D(c ? c.ownerDocument : document);
        if (!e && o === c || b((F = w.current.floatingContext) == null ? void 0 : F.refs.floating.current, o) || b(c, o) || f)
          return;
        const u = e ?? o;
        if (d(u)) {
          const M = t.context.triggerElements;
          if (M.hasElement(u) || M.hasMatchingElement((k) => b(k, u)))
            return;
        }
        t.setOpen(!1, T(y, n));
      });
    }
  }), [w, t, L, p, m]);
  return i.useMemo(() => a ? {
    reference: R,
    trigger: R
  } : {}, [a, R]);
}
export {
  X as useFocus
};
