"use client";
import { b as e, t } from "./createBaseUIEventDetails-ChcQXp54.js";
import { d as n, f as r, l as i } from "./floating-ui.utils.dom-5U5b4xPf.js";
import { a, i as o } from "./detectBrowser-FBYSu3xM.js";
import { n as s, r as c, t as l } from "./shadowDom-C-4Ejp9R.js";
import { l as u, s as d, u as f } from "./shim-ieZ_MRlN.js";
import { B as p, R as m } from "./popupStateMapping-PdIQcWEz.js";
import { t as h } from "./owner-BrzQrszh.js";
import { t as g } from "./addEventListener-B4Gs94GY.js";
import { n as _ } from "./useTimeout-B1x6NSZb.js";
import * as v from "react";
//#region ../../node_modules/.bun/@base-ui+react@1.5.0+f4ada88178b00e6f/node_modules/@base-ui/react/esm/floating-ui-react/hooks/useFocus.js
var y = o && a;
function b(a, o = {}) {
	let { enabled: b = !0, delay: x } = o, S = "rootStore" in a ? a.rootStore : a, { events: C, dataRef: w } = S.context, T = v.useRef(!1), E = v.useRef(null), D = v.useRef(!0), O = _();
	v.useEffect(() => {
		let e = S.select("domReferenceElement");
		if (!b) return;
		let t = i(e);
		function n() {
			let e = S.select("domReferenceElement");
			!S.select("open") && r(e) && e === l(h(e)) && (T.current = !0);
		}
		function a() {
			D.current = !0;
		}
		function o() {
			D.current = !1;
		}
		return p(g(t, "blur", n), y && g(t, "keydown", a, !0), y && g(t, "pointerdown", o, !0));
	}, [S, b]), v.useEffect(() => {
		if (!b) return;
		function e(e) {
			if (e.reason === "trigger-press" || e.reason === "escape-key") {
				let e = S.select("domReferenceElement");
				n(e) && (E.current = e, T.current = !0);
			}
		}
		return C.on("openchange", e), () => {
			C.off("openchange", e);
		};
	}, [
		C,
		b,
		S
	]);
	let k = v.useMemo(() => {
		function r() {
			T.current = !1, E.current = null;
		}
		return {
			onMouseLeave() {
				r();
			},
			onFocus(i) {
				let a = i.currentTarget;
				if (T.current) {
					if (E.current === a) return;
					r();
				}
				let o = c(i.nativeEvent);
				if (n(o)) {
					if (y && !i.relatedTarget) {
						if (!D.current && !u(o)) return;
					} else if (!f(o)) return;
				}
				let s = d(i.relatedTarget, S.context.triggerElements), { nativeEvent: l, currentTarget: p } = i, m = typeof x == "function" ? x() : x;
				if (S.select("open") && s || m === 0 || m === void 0) {
					S.setOpen(!0, t(e, l, p));
					return;
				}
				O.start(m, () => {
					T.current || S.setOpen(!0, t(e, l, p));
				});
			},
			onBlur(i) {
				r();
				let a = i.relatedTarget, o = i.nativeEvent, c = n(a) && a.hasAttribute(m("focus-guard")) && a.getAttribute("data-type") === "outside";
				O.start(0, () => {
					let n = S.select("domReferenceElement"), r = l(h(n));
					!a && r === n || s(w.current.floatingContext?.refs.floating.current, r) || s(n, r) || c || d(a ?? r, S.context.triggerElements) || S.setOpen(!1, t(e, o));
				});
			}
		};
	}, [
		w,
		x,
		S,
		O
	]);
	return v.useMemo(() => b ? {
		reference: k,
		trigger: k
	} : {}, [b, k]);
}
//#endregion
export { b as t };
