import { t as e } from "./ui.module-BisoTevy.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 { jsx as d, jsxs as f } from "react/jsx-runtime";
import { autoUpdate as p, flip as m, offset as h, shift as g, useFloating as _ } from "@floating-ui/react";
import { useClickOutside as v, useEscapeDismiss as y, useOverlayFocus as b } from "@ryuzaki13/react-foundation-lib/dom";
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: f, setFloating: p, closeMenu: m } = F(), h = i(() => {
		let e = l.current;
		return e ? Array.from(e.querySelectorAll("[data-menu-item=\"true\"]:not([data-disabled=\"true\"])")) : [];
	}, [l]);
	b({
		active: s,
		initialFocus: (e) => h()[0] ?? e,
		restoreFocus: o,
		restoreFocusTarget: () => f.current,
		containerRef: l
	}), y({
		active: s,
		enabled: r,
		onDismiss: m,
		containerRef: l
	});
	let g = i(() => {
		!s || a || n && m();
	}, [
		s,
		a,
		n,
		m
	]);
	v([l, f], g);
	let _ = i((e) => {
		let t = h();
		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());
	}, [h]);
	return typeof document > "u" ? null : S(/* @__PURE__ */ d(C, { children: s && /* @__PURE__ */ d(w.div, {
		ref: p,
		style: c,
		role: "menu",
		tabIndex: -1,
		className: u(L.menuPanel, t),
		onKeyDown: _,
		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__ */ d("div", {
			className: u(L.menuScroller, "scrollable"),
			children: typeof e == "function" ? e({ closeMenu: m }) : e
		})
	}) }), document.body);
}, z = ({ children: t, className: n }) => /* @__PURE__ */ d("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: p = !0, onSelect: m }) => {
	let { closeMenu: h } = F(), g = i((e) => {
		if (o) {
			e.preventDefault(), e.stopPropagation();
			return;
		}
		m?.(e), !e.defaultPrevented && p && h();
	}, [
		o,
		m,
		p,
		h
	]), _ = u(e.uiPopupOption, L.menuItem, a);
	return s ? /* @__PURE__ */ f("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: _,
		onClick: g,
		children: [
			n && /* @__PURE__ */ d("span", {
				className: u(e.uiPopupOptionIcon, L.menuItemIcon),
				children: n
			}),
			/* @__PURE__ */ d("span", {
				className: u(e.uiOptionText, L.menuItemText),
				children: t
			}),
			r && /* @__PURE__ */ d("kbd", {
				className: u(e.keyboard, L.menuItemHotKey),
				children: r
			})
		]
	}) : /* @__PURE__ */ f("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: _,
		onClick: g,
		children: [
			n && /* @__PURE__ */ d("span", {
				className: u(e.uiPopupOptionIcon, L.menuItemIcon),
				children: n
			}),
			/* @__PURE__ */ d("span", {
				className: u(e.uiOptionText, L.menuItemText),
				children: t
			}),
			r && /* @__PURE__ */ d("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: u }) => {
	let [f, v] = l(k), [y, b] = l(a ?? !1), x = r !== void 0, S = x ? r : y, C = c(null), { refs: w, floatingStyles: j, update: M } = _({
		placement: n,
		strategy: t === "contextmenu" ? "fixed" : "absolute",
		transform: !1,
		middleware: [
			h(6),
			m({ padding: 8 }),
			g({ padding: 8 })
		],
		whileElementsMounted: p
	}), N = i((e) => {
		x || b(e), u?.(e), e || v((e) => T(e));
	}, [x, u]), F = i(() => {
		N(!1);
	}, [N]);
	o(() => {
		S || v((e) => T(e));
	}, [S]);
	let I = i((e, t) => {
		w.setReference(e), v(A({
			source: t,
			anchor: {
				type: "element",
				element: e
			}
		})), N(!0);
	}, [w, N]), L = i((e, t, n) => {
		w.setReference(E(e, n)), v(A({
			source: t,
			anchor: {
				type: "point",
				point: e,
				contextElement: n
			}
		})), N(!0), M();
	}, [
		w,
		N,
		M
	]), R = i((e) => {
		C.current = e, t === "click" && w.setReference(e);
	}, [t, w]), z = i((e) => {
		if (t !== "click") return;
		let n = e.currentTarget;
		if (S && f.anchor?.type === "element" && f.anchor.element === n) {
			F();
			return;
		}
		I(n, "click");
	}, [
		t,
		S,
		f.anchor,
		F,
		I
	]), B = i((e) => {
		if (t !== "contextmenu") return;
		e.preventDefault();
		let n = e.currentTarget, r = D(e);
		L(r, "contextmenu", n);
	}, [t, L]), V = i((e) => {
		if (!(e.key === "ContextMenu" || e.shiftKey && e.key === "F10")) return;
		e.preventDefault();
		let n = e.currentTarget;
		if (t === "click") {
			I(n, "keyboard");
			return;
		}
		let r = O(n.getBoundingClientRect());
		L(r, "keyboard", n);
	}, [
		t,
		I,
		L
	]), H = S && f.anchor !== null, U = s(() => f.anchor ? f.anchor.type === "point" ? f.anchor.point : O(f.anchor.element.getBoundingClientRect()) : null, [f.anchor]), W = s(() => ({
		mode: t,
		open: H,
		openSource: f.source,
		anchorPoint: U,
		floatingStyles: j,
		floatingRef: w.floating,
		triggerRef: C,
		setFloating: w.setFloating,
		registerTriggerElement: R,
		onTriggerClick: z,
		onTriggerContextMenu: B,
		onTriggerKeyDown: V,
		closeMenu: F
	}), [
		t,
		H,
		f.source,
		U,
		j,
		w.floating,
		w.setFloating,
		R,
		z,
		B,
		V,
		F
	]);
	return /* @__PURE__ */ d(P.Provider, {
		value: W,
		children: e
	});
}, H = ({ className: t }) => /* @__PURE__ */ d("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__ */ d(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: p = 76, itemSize: m = 64, closeLabel: h = "Закрыть" }) => {
		let { open: g, anchorPoint: _, 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 = p * 2 + m, M = s(() => {
			if (typeof window > "u" || !_) return null;
			let e = j / 2 + 8;
			return {
				x: U(_.x, e, window.innerWidth - e),
				y: U(_.y, e, window.innerHeight - e)
			};
		}, [_, j]), N = i(() => {
			let e = x.current;
			return e ? Array.from(e.querySelectorAll("[data-menu-item=\"true\"]:not([data-disabled=\"true\"])")) : [];
		}, [x]);
		b({
			active: g,
			initialFocus: (e) => N()[0] ?? e,
			restoreFocus: l,
			restoreFocusTarget: () => T.current,
			containerRef: x
		}), o(() => {
			if (!g || !_) return;
			let e = 0;
			return e = window.requestAnimationFrame(() => {
				(N()[0] ?? x.current)?.focus();
			}), () => window.cancelAnimationFrame(e);
		}, [
			g,
			_,
			N,
			x
		]), y({
			active: g,
			enabled: a,
			onDismiss: D,
			containerRef: x
		});
		let P = i(() => {
			!g || c || r && D();
		}, [
			g,
			c,
			r,
			D
		]);
		v([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__ */ d(C, { children: g && M && /* @__PURE__ */ f(w.div, {
			ref: E,
			style: {
				"--radial-panel-size": `${j}px`,
				"--radial-radius": `${p}px`,
				"--radial-item-size": `${m}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__ */ d("div", {
					className: L.radialRing,
					"aria-hidden": "true"
				}),
				/* @__PURE__ */ d("button", {
					type: "button",
					role: "menuitem",
					className: L.radialCenterButton,
					"aria-label": h,
					"data-menu-item": "true",
					"data-radial-close": "true",
					onClick: D,
					children: /* @__PURE__ */ d("span", {
						className: L.radialCenterButtonText,
						children: h
					})
				}),
				/* @__PURE__ */ d("div", {
					className: L.radialItems,
					children: k.map((e, t) => {
						let n = A > 0 ? -90 + 360 / A * t : -90;
						return /* @__PURE__ */ d("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__ */ f("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__ */ d("span", {
				className: L.radialItemIcon,
				children: t
			}), /* @__PURE__ */ d("span", {
				className: L.radialItemText,
				children: e
			})]
		});
	},
	Separator: H,
	GroupLabel: z
}), G = Object.assign(({ children: e, placement: t, open: n, defaultOpen: r, onOpenChange: i }) => /* @__PURE__ */ d(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-DQeR3y7M.js.map