import { t as e } from "./ui.module-DHVTOqZ0.js";
import { Children as t, cloneElement as n, createContext as r, useCallback as i, useContext as a, useEffect as o, useMemo as s, useRef as c, useState as l } from "react";
import { cn as u } from "@ryuzaki13/react-foundation-lib/utils";
import { autoUpdate as d, flip as f, offset as p, shift as m, useFloating as h } from "@floating-ui/react";
import { useClickOutside as g, useEscapeDismiss as _, useOverlayFocus as v } from "@ryuzaki13/react-foundation-lib/dom";
import { jsx as y, jsxs as b } from "react/jsx-runtime";
import { createMissingContextError as x } from "@ryuzaki13/react-foundation-lib/error";
import { createPortal as S } from "react-dom";
import { AnimatePresence as C, motion as w } from "motion/react";
import { closeMenu as T, createVirtualAnchor as E, getMenuPointFromEvent as D, getMenuPointFromRect as O, initialMenuState as k, openMenu as A } from "@ryuzaki13/react-foundation-lib/context-menu";
//#region src/context-menu/components/helpers.ts
function j(e, t) {
	if (e) {
		if (typeof e == "function") {
			e(t);
			return;
		}
		e.current = t;
	}
}
function M(e, t) {
	return (n) => {
		e?.(n), !n.defaultPrevented && t(n);
	};
}
function N(e, t) {
	return (n) => {
		e?.(n), !n.defaultPrevented && t(n);
	};
}
//#endregion
//#region src/context-menu/components/MenuContext.tsx
var P = r(null);
function F() {
	let e = a(P);
	if (!e) throw x({
		hookName: "useMenuContext",
		providerName: "MenuRoot"
	});
	return e;
}
//#endregion
//#region src/context-menu/components/ContextMenuTrigger.tsx
var I = ({ children: e }) => {
	let { mode: t, open: r, registerTriggerElement: i, onTriggerContextMenu: a, onTriggerKeyDown: o } = F();
	if (t !== "contextmenu") throw Error("ContextMenu.Trigger must be used within <ContextMenu>");
	return n(e, {
		ref: (t) => {
			let n = e.props.ref;
			j(n, t), i(t);
		},
		"aria-haspopup": "menu",
		"aria-expanded": r,
		onContextMenu: M(e.props.onContextMenu, a),
		onKeyDown: N(e.props.onKeyDown, o)
	});
}, L = {
	menuPanel: "nPfxbm",
	menuScroller: "T8GF31",
	menuItem: "XM9raj",
	menuItemIcon: "JY0XKk",
	menuItemText: "Jj7pGe",
	menuItemHotKey: "o7v3XA",
	radialPanel: "f9wo9l",
	radialRing: "_9IPwGF",
	radialCenterButton: "CAmmlz",
	radialCenterButtonText: "_5KEBWR",
	radialItems: "L28onY",
	radialItemSlot: "Pg-1Y3",
	radialItem: "ObIrS2",
	radialItemIcon: "_0jf5vt",
	radialItemText: "zKwsQF"
}, R = ({ children: e, className: t, closeOnOutside: n = !0, closeOnEscape: r = !0, disableOutsideClick: a = !1, restoreFocus: o = !0 }) => {
	let { open: s, floatingStyles: c, floatingRef: l, triggerRef: d, setFloating: f, closeMenu: p } = F(), m = i(() => {
		let e = l.current;
		return e ? Array.from(e.querySelectorAll("[data-menu-item=\"true\"]:not([data-disabled=\"true\"])")) : [];
	}, [l]);
	v({
		active: s,
		initialFocus: (e) => m()[0] ?? e,
		restoreFocus: o,
		restoreFocusTarget: () => d.current,
		containerRef: l
	}), _({
		active: s,
		enabled: r,
		onDismiss: p,
		containerRef: l
	});
	let h = i(() => {
		!s || a || n && p();
	}, [
		s,
		a,
		n,
		p
	]);
	g([l, d], h);
	let b = i((e) => {
		let t = m();
		if (t.length === 0) return;
		let n = document.activeElement, r = n ? t.indexOf(n) : -1;
		if (e.key === "ArrowDown") {
			e.preventDefault(), t[r >= 0 ? (r + 1) % t.length : 0]?.focus();
			return;
		}
		if (e.key === "ArrowUp") {
			e.preventDefault(), t[r >= 0 ? (r - 1 + t.length) % t.length : t.length - 1]?.focus();
			return;
		}
		if (e.key === "Home") {
			e.preventDefault(), t[0]?.focus();
			return;
		}
		e.key === "End" && (e.preventDefault(), t[t.length - 1]?.focus());
	}, [m]);
	return typeof document > "u" ? null : S(/* @__PURE__ */ y(C, { children: s && /* @__PURE__ */ y(w.div, {
		ref: f,
		style: c,
		role: "menu",
		tabIndex: -1,
		className: u(L.menuPanel, t),
		onKeyDown: b,
		initial: {
			opacity: 0,
			scale: .96,
			y: -2
		},
		animate: {
			opacity: 1,
			scale: 1,
			y: 0
		},
		exit: {
			opacity: 0,
			scale: .96,
			y: -2
		},
		transition: {
			duration: .14,
			ease: "easeOut"
		},
		children: /* @__PURE__ */ y("div", {
			className: u(L.menuScroller, "scrollable"),
			children: typeof e == "function" ? e({ closeMenu: p }) : e
		})
	}) }), document.body);
}, z = ({ children: t, className: n }) => /* @__PURE__ */ y("div", {
	className: u(e.uiPopupGroupLabel, n),
	children: t
}), B = ({ children: t, icon: n, hotKey: r, className: a, disabled: o, href: s, target: c, rel: l, closeOnSelect: d = !0, onSelect: f }) => {
	let { closeMenu: p } = F(), m = i((e) => {
		if (o) {
			e.preventDefault(), e.stopPropagation();
			return;
		}
		f?.(e), !e.defaultPrevented && d && p();
	}, [
		o,
		f,
		d,
		p
	]), h = u(e.uiPopupOption, L.menuItem, a);
	return s ? /* @__PURE__ */ b("a", {
		href: s,
		target: c,
		rel: l,
		role: "menuitem",
		tabIndex: -1,
		"aria-disabled": o,
		"data-menu-item": "true",
		"data-disabled": o ? "true" : void 0,
		"data-ui": "context-menu-item",
		"data-action": "select-context-menu-item",
		className: h,
		onClick: m,
		children: [
			n && /* @__PURE__ */ y("span", {
				className: u(e.uiPopupOptionIcon, L.menuItemIcon),
				children: n
			}),
			/* @__PURE__ */ y("span", {
				className: u(e.uiOptionText, L.menuItemText),
				children: t
			}),
			r && /* @__PURE__ */ y("kbd", {
				className: u(e.keyboard, L.menuItemHotKey),
				children: r
			})
		]
	}) : /* @__PURE__ */ b("button", {
		type: "button",
		role: "menuitem",
		tabIndex: -1,
		disabled: o,
		"data-menu-item": "true",
		"data-disabled": o ? "true" : void 0,
		"data-ui": "context-menu-item",
		"data-action": "select-context-menu-item",
		className: h,
		onClick: m,
		children: [
			n && /* @__PURE__ */ y("span", {
				className: u(e.uiPopupOptionIcon, L.menuItemIcon),
				children: n
			}),
			/* @__PURE__ */ y("span", {
				className: u(e.uiOptionText, L.menuItemText),
				children: t
			}),
			r && /* @__PURE__ */ y("kbd", {
				className: u(e.keyboard, L.menuItemHotKey),
				children: r
			})
		]
	});
}, V = ({ children: e, mode: t, placement: n = t === "click" ? "bottom-start" : "right-start", open: r, defaultOpen: a, onOpenChange: o }) => {
	let [u, g] = l(k), [_, v] = l(a ?? !1), b = r !== void 0, x = b ? r : _, S = c(null), { refs: C, floatingStyles: w, update: j } = h({
		placement: n,
		strategy: t === "contextmenu" ? "fixed" : "absolute",
		transform: !1,
		middleware: [
			p(6),
			f({ padding: 8 }),
			m({ padding: 8 })
		],
		whileElementsMounted: d
	}), M = i((e) => {
		b || v(e), o?.(e), e || g((e) => T(e));
	}, [b, o]), N = i(() => {
		M(!1);
	}, [M]), F = i((e, t) => {
		C.setReference(e), g(A({
			source: t,
			anchor: {
				type: "element",
				element: e
			}
		})), M(!0);
	}, [C, M]), I = i((e, t, n) => {
		C.setReference(E(e, n)), g(A({
			source: t,
			anchor: {
				type: "point",
				point: e,
				contextElement: n
			}
		})), M(!0), j();
	}, [
		C,
		M,
		j
	]), L = i((e) => {
		S.current = e, t === "click" && C.setReference(e);
	}, [t, C]), R = i((e) => {
		if (t !== "click") return;
		let n = e.currentTarget;
		if (x && u.anchor?.type === "element" && u.anchor.element === n) {
			N();
			return;
		}
		F(n, "click");
	}, [
		t,
		x,
		u.anchor,
		N,
		F
	]), z = i((e) => {
		if (t !== "contextmenu") return;
		e.preventDefault();
		let n = e.currentTarget, r = D(e);
		I(r, "contextmenu", n);
	}, [t, I]), B = i((e) => {
		if (!(e.key === "ContextMenu" || e.shiftKey && e.key === "F10")) return;
		e.preventDefault();
		let n = e.currentTarget;
		if (t === "click") {
			F(n, "keyboard");
			return;
		}
		let r = O(n.getBoundingClientRect());
		I(r, "keyboard", n);
	}, [
		t,
		F,
		I
	]), V = x && u.anchor !== null, H = s(() => {
		if (!u.anchor) return null;
		if (u.anchor.type === "point") return u.anchor.point;
		let e = u.anchor.element.getBoundingClientRect();
		return O(e);
	}, [u.anchor]), U = s(() => ({
		mode: t,
		open: V,
		openSource: u.source,
		anchorPoint: H,
		floatingStyles: w,
		floatingRef: C.floating,
		triggerRef: S,
		setFloating: C.setFloating,
		registerTriggerElement: L,
		onTriggerClick: R,
		onTriggerContextMenu: z,
		onTriggerKeyDown: B,
		closeMenu: N
	}), [
		t,
		V,
		u.source,
		H,
		w,
		C.floating,
		C.setFloating,
		L,
		R,
		z,
		B,
		N
	]);
	return /* @__PURE__ */ y(P.Provider, {
		value: U,
		children: e
	});
}, H = ({ className: t }) => /* @__PURE__ */ y("div", {
	role: "separator",
	className: u(e.uiPopupOptionSeparator, t)
});
//#endregion
//#region src/context-menu/components/RadialMenuContent.tsx
function U(e, t, n) {
	return n < t ? t : Math.min(Math.max(e, t), n);
}
var W = Object.assign(({ children: e, placement: t, open: n, defaultOpen: r, onOpenChange: i }) => /* @__PURE__ */ y(V, {
	mode: "contextmenu",
	placement: t,
	open: n,
	defaultOpen: r,
	onOpenChange: i,
	children: e
}), {
	Trigger: I,
	Content: R,
	RadialContent: ({ children: e, className: n, closeOnOutside: r = !0, closeOnEscape: a = !0, disableOutsideClick: c = !1, restoreFocus: l = !0, radius: d = 76, itemSize: f = 64, closeLabel: p = "Закрыть" }) => {
		let { open: m, anchorPoint: h, floatingRef: x, triggerRef: T, setFloating: E, closeMenu: D } = F(), O = typeof e == "function" ? e({ closeMenu: D }) : e, k = s(() => t.toArray(O), [O]), A = k.length, j = d * 2 + f, M = s(() => {
			if (typeof window > "u" || !h) return null;
			let e = j / 2 + 8;
			return {
				x: U(h.x, e, window.innerWidth - e),
				y: U(h.y, e, window.innerHeight - e)
			};
		}, [h, j]), N = i(() => {
			let e = x.current;
			return e ? Array.from(e.querySelectorAll("[data-menu-item=\"true\"]:not([data-disabled=\"true\"])")) : [];
		}, [x]);
		v({
			active: m,
			initialFocus: (e) => N()[0] ?? e,
			restoreFocus: l,
			restoreFocusTarget: () => T.current,
			containerRef: x
		}), o(() => {
			if (!m || !h) return;
			let e = 0;
			return e = window.requestAnimationFrame(() => {
				(N()[0] ?? x.current)?.focus();
			}), () => window.cancelAnimationFrame(e);
		}, [
			m,
			h,
			N,
			x
		]), _({
			active: m,
			enabled: a,
			onDismiss: D,
			containerRef: x
		});
		let P = i(() => {
			!m || c || r && D();
		}, [
			m,
			c,
			r,
			D
		]);
		g([x, T], P);
		let I = i((e) => {
			let t = N();
			if (t.length === 0) return;
			let n = document.activeElement, r = n ? t.indexOf(n) : -1;
			if (e.key === "ArrowRight" || e.key === "ArrowDown") {
				e.preventDefault(), t[r >= 0 ? (r + 1) % t.length : 0]?.focus();
				return;
			}
			if (e.key === "ArrowLeft" || e.key === "ArrowUp") {
				e.preventDefault(), t[r >= 0 ? (r - 1 + t.length) % t.length : t.length - 1]?.focus();
				return;
			}
			if (e.key === "Home") {
				e.preventDefault(), t[0]?.focus();
				return;
			}
			e.key === "End" && (e.preventDefault(), t[t.length - 1]?.focus());
		}, [N]);
		return typeof document > "u" ? null : S(/* @__PURE__ */ y(C, { children: m && M && /* @__PURE__ */ b(w.div, {
			ref: E,
			style: {
				"--radial-panel-size": `${j}px`,
				"--radial-radius": `${d}px`,
				"--radial-item-size": `${f}px`,
				left: M.x - j / 2,
				top: M.y - j / 2,
				transformOrigin: "center"
			},
			role: "menu",
			tabIndex: -1,
			className: u(L.radialPanel, n),
			onKeyDown: I,
			initial: {
				opacity: 0,
				scale: .78
			},
			animate: {
				opacity: 1,
				scale: 1
			},
			exit: {
				opacity: 0,
				scale: .78
			},
			transition: {
				duration: .16,
				ease: "easeOut"
			},
			children: [
				/* @__PURE__ */ y("div", {
					className: L.radialRing,
					"aria-hidden": "true"
				}),
				/* @__PURE__ */ y("button", {
					type: "button",
					role: "menuitem",
					className: L.radialCenterButton,
					"aria-label": p,
					"data-menu-item": "true",
					"data-radial-close": "true",
					onClick: D,
					children: /* @__PURE__ */ y("span", {
						className: L.radialCenterButtonText,
						children: p
					})
				}),
				/* @__PURE__ */ y("div", {
					className: L.radialItems,
					children: k.map((e, t) => {
						let n = A > 0 ? -90 + 360 / A * t : -90;
						return /* @__PURE__ */ y("div", {
							className: L.radialItemSlot,
							style: { "--radial-item-angle": `${n}deg` },
							children: e
						}, t);
					})
				})
			]
		}) }), document.body);
	},
	Item: B,
	RadialItem: ({ children: e, icon: t, className: n, disabled: r, closeOnSelect: a = !0, onSelect: o }) => {
		let { closeMenu: s } = F(), c = i((e) => {
			if (r) {
				e.preventDefault(), e.stopPropagation();
				return;
			}
			o?.(e), !e.defaultPrevented && a && s();
		}, [
			r,
			o,
			a,
			s
		]);
		return /* @__PURE__ */ b("button", {
			type: "button",
			role: "menuitem",
			tabIndex: -1,
			disabled: r,
			"aria-disabled": r,
			"data-menu-item": "true",
			"data-disabled": r ? "true" : void 0,
			"data-ui": "radial-menu-item",
			"data-action": "select-radial-menu-item",
			className: u(L.radialItem, n),
			onClick: c,
			children: [t && /* @__PURE__ */ y("span", {
				className: L.radialItemIcon,
				children: t
			}), /* @__PURE__ */ y("span", {
				className: L.radialItemText,
				children: e
			})]
		});
	},
	Separator: H,
	GroupLabel: z
}), G = Object.assign(({ children: e, placement: t, open: n, defaultOpen: r, onOpenChange: i }) => /* @__PURE__ */ y(V, {
	mode: "click",
	placement: t,
	open: n,
	defaultOpen: r,
	onOpenChange: i,
	children: e
}), {
	Trigger: ({ children: e }) => {
		let { mode: t, open: r, registerTriggerElement: i, onTriggerClick: a, onTriggerKeyDown: o } = F();
		if (t !== "click") throw Error("DropdownMenu.Trigger must be used within <DropdownMenu>");
		return n(e, {
			ref: (t) => {
				let n = e.props.ref;
				j(n, t), i(t);
			},
			"aria-haspopup": "menu",
			"aria-expanded": r,
			onClick: M(e.props.onClick, a),
			onKeyDown: N(e.props.onKeyDown, o)
		});
	},
	Content: R,
	Item: B,
	Separator: H,
	GroupLabel: z
});
//#endregion
export { W as n, G as t };

//# sourceMappingURL=context-menu-6nJkPmKV.js.map