import { t as e } from "../../utils-B7XIe3qq.js";
import * as t from "react";
import { jsx as n } from "react/jsx-runtime";
import { createPortal as r } from "react-dom";
//#region src/react/primitives/popover.tsx
var i = t.createContext(null);
function a() {
	let e = t.useContext(i);
	if (!e) throw Error("Popover compound components must be used within <Popover>");
	return e;
}
function o({ open: e, defaultOpen: r = !1, onOpenChange: a, children: o }) {
	let [s, c] = t.useState(r), l = e !== void 0, u = l ? e : s, d = t.useRef(null), f = t.useCallback((e) => {
		let t = typeof e == "function" ? e(u) : e;
		l || c(t), a?.(t);
	}, [
		u,
		l,
		a
	]);
	return /* @__PURE__ */ n(i.Provider, {
		value: {
			open: u,
			setOpen: f,
			triggerRef: d
		},
		children: o
	});
}
function s({ className: e, ...t }) {
	let { open: r, setOpen: i, triggerRef: o } = a();
	return /* @__PURE__ */ n("button", {
		ref: o,
		"data-slot": "popover-trigger",
		type: "button",
		"aria-haspopup": "dialog",
		"aria-expanded": r,
		onClick: () => {
			i((e) => !e);
		},
		className: e,
		...t
	});
}
function c({ className: e, ...t }) {
	return /* @__PURE__ */ n("div", {
		"data-slot": "popover-anchor",
		className: e,
		...t
	});
}
function l({ className: i, align: o = "center", sideOffset: s = 8, side: c = "bottom", children: l, ...u }) {
	let { open: d, setOpen: f, triggerRef: p } = a(), m = t.useRef(null), [h, g] = t.useState({
		top: 0,
		left: 0
	});
	if (t.useEffect(() => {
		if (!d || !p.current) return;
		let e = () => {
			let e = p.current;
			if (!e) return;
			let t = e.getBoundingClientRect(), n = o === "start" ? t.left : o === "end" ? t.right : t.left + t.width / 2;
			g((() => {
				switch (c) {
					case "top": return {
						top: t.top - s,
						left: n
					};
					case "left": return {
						top: t.top + t.height / 2,
						left: t.left - s
					};
					case "right": return {
						top: t.top + t.height / 2,
						left: t.right + s
					};
					default: return {
						top: t.bottom + s,
						left: n
					};
				}
			})());
		};
		return e(), window.addEventListener("scroll", e, !0), window.addEventListener("resize", e), () => {
			window.removeEventListener("scroll", e, !0), window.removeEventListener("resize", e);
		};
	}, [
		d,
		c,
		o,
		s,
		p
	]), t.useEffect(() => {
		if (!d) return;
		let e = (e) => {
			let t = e.target;
			m.current && !m.current.contains(t) && p.current && !p.current.contains(t) && f(!1);
		}, t = (e) => {
			e.key === "Escape" && (e.preventDefault(), f(!1), p.current?.focus());
		}, n = setTimeout(() => {
			document.addEventListener("mousedown", e);
		}, 0);
		return document.addEventListener("keydown", t), () => {
			clearTimeout(n), document.removeEventListener("mousedown", e), document.removeEventListener("keydown", t);
		};
	}, [
		d,
		f,
		p
	]), !d) return null;
	let _ = c === "top" || c === "bottom" ? o === "start" ? "0%" : o === "end" ? "-100%" : "-50%" : c === "left" ? "-100%" : "0%", v = c === "left" || c === "right" ? "-50%" : c === "top" ? "-100%" : "0%";
	return r(/* @__PURE__ */ n("div", {
		ref: m,
		"data-slot": "popover-content",
		role: "dialog",
		className: e("z-50 w-72 rounded-xl border border-border-glass bg-glass p-4 shadow-lg backdrop-blur-[20px]", "text-foreground outline-none", "animate-in fade-in-0 zoom-in-95", i),
		style: {
			position: "fixed",
			top: h.top,
			left: h.left,
			transform: `translate(${_}, ${v})`
		},
		...u,
		children: l
	}), document.body);
}
function u({ className: e, onClick: t, ...r }) {
	let { setOpen: i } = a();
	return /* @__PURE__ */ n("button", {
		"data-slot": "popover-close",
		type: "button",
		onClick: (e) => {
			i(!1), t?.(e);
		},
		className: e,
		...r
	});
}
//#endregion
export { o as Popover, c as PopoverAnchor, u as PopoverClose, l as PopoverContent, s as PopoverTrigger };

//# sourceMappingURL=popover.js.map