import { n as e } from "./button-BsvHGWPc.js";
import { c as t } from "./misc-aGQBhywJ.js";
import { o as n } from "./flex-C7njxZ74.js";
import { r } from "./grid-dI94AAbi.js";
import { Children as i, createContext as a, useCallback as o, useContext as s, useEffect as c, useId as l, useRef as u, useState as d } from "react";
import { cn as f } from "@ryuzaki13/react-foundation-lib/utils";
import { getOrCreatePortalRoot as p, useEscapeDismiss as m, useOverlayFocus as h } from "@ryuzaki13/react-foundation-lib/dom";
import { jsx as g, jsxs as _ } from "react/jsx-runtime";
import { createPortal as v } from "react-dom";
import { X as y } from "lucide-react";
import { AnimatePresence as b, motion as x, useReducedMotion as S } from "motion/react";
var C = {
	modal: "vGPUKV",
	sm: "O7gb7P",
	md: "RVq8v3",
	lg: "PCSUAL",
	xl: "XDCHzq",
	xxl: "t1kl8L",
	inside: "o3PEEB",
	header: "WhUkxm",
	body: "NDgdMi",
	headerText: "JDlse5",
	headerButton: "FaDKSt"
}, w = {
	hidden: {
		opacity: 0,
		scale: .96,
		y: 12
	},
	visible: {
		opacity: 1,
		scale: 1,
		y: 0
	}
}, T = {
	duration: .18,
	ease: [
		.22,
		1,
		.36,
		1
	]
}, E = { duration: 0 }, D = a({
	modals: [],
	openModal: () => {},
	closeModal: () => {},
	isTopModal: () => !1
}), O = () => s(D), k = ({ children: e, className: t }) => /* @__PURE__ */ g("div", {
	className: f("surface2 paddingMd borderBottom", t),
	children: e
});
k.displayName = "Modal.Toolbar";
var A = ({ children: e, className: n, scrollable: r }) => /* @__PURE__ */ g("div", {
	className: f("paddingMd h100 overflowHidden", n),
	children: r ? /* @__PURE__ */ g(t, {
		className: "h100",
		children: e
	}) : e
});
A.displayName = "Modal.Content";
var j = ({ children: e, className: t }) => /* @__PURE__ */ g("div", {
	className: f("surface2 borderTop paddingMd", t),
	children: /* @__PURE__ */ g(n, {
		gap: "sm",
		align: "center",
		justify: "end",
		children: e
	})
});
j.displayName = "Modal.Footer";
var M = ({ isOpen: t, title: n, size: a = "sm", height: o, onClose: s, children: d }) => {
	let D = l(), M = l(), { modals: N, openModal: P, closeModal: F, isTopModal: I } = O(), L = S(), R = u(null), z = u(0), B = t && (N.length === 0 || I(D));
	c(() => {
		t && (R.current = document.activeElement instanceof HTMLElement ? document.activeElement : null);
	}, [t]);
	let V = h({
		active: t || !1,
		trapFocus: !0,
		restoreFocus: !1
	});
	m({
		active: B || !1,
		onDismiss: s,
		containerRef: V
	}), c(() => {
		t && P(D);
	}, [
		t,
		D,
		P
	]), c(() => () => {
		window.cancelAnimationFrame(z.current), F(D);
	}, [F, D]);
	function H() {
		F(D);
		let e = R.current;
		z.current = window.requestAnimationFrame(() => {
			e?.isConnected && e.focus(), R.current = null;
		});
	}
	let U = p("modal-root");
	if (!U) return null;
	let W = null, G = null, K = null, q = i.toArray(d);
	for (let e of q) switch (e.type.displayName) {
		case k.displayName:
			if (W) throw Error("Modal: duplicate <Toolbar> found");
			W = e;
			break;
		case j.displayName:
			if (K) throw Error("Modal: duplicate <Footer> found");
			K = e;
			break;
		case A.displayName:
			if (G) throw Error("Modal: duplicate <Content> found");
			G = e;
	}
	return v(/* @__PURE__ */ g(b, {
		onExitComplete: H,
		children: t ? /* @__PURE__ */ _(x.div, {
			ref: V,
			className: f(C.modal, C[a]),
			style: { height: o },
			role: "dialog",
			"aria-modal": "true",
			"aria-labelledby": M,
			"aria-describedby": `${M}-content`,
			initial: "hidden",
			animate: "visible",
			exit: "hidden",
			variants: w,
			transition: L ? E : T,
			children: [/* @__PURE__ */ _("div", {
				className: f(C.header, W ? "" : "borderBottom"),
				children: [/* @__PURE__ */ g("h3", {
					className: C.headerText,
					id: M,
					children: n
				}), /* @__PURE__ */ g(e, {
					icon: /* @__PURE__ */ g(y, {}),
					className: C.headerButton,
					variant: "ghost",
					"aria-label": "Закрыть модальное окно",
					onClick: s
				})]
			}), /* @__PURE__ */ _(r, {
				id: `${M}-content`,
				templateRows: `${W ? "auto" : ""} 1fr ${K ? "auto" : ""}`.trim(),
				className: C.body,
				children: [
					W,
					G,
					K
				]
			})]
		}) : null
	}), U);
};
//#endregion
//#region src/modal/ModalManagerProvider.tsx
function N({ children: e, compensateScrollbar: t = !1 }) {
	let [n, r] = d([]), i = u(0), a = u(0), s = u(""), l = u(!1), f = o((e) => {
		r((t) => [...t, e]);
	}, []), p = o((e) => {
		r((t) => t.filter((t) => t !== e));
	}, []), m = o((e) => n[n.length - 1] === e, [n]);
	return c(() => {
		let e = i.current === 0;
		if (e && n.length > 0) {
			let e = window.scrollY, r = document.querySelector("#app-root");
			a.current = Math.max(0, window.innerWidth - document.documentElement.clientWidth), s.current = document.body.style.paddingRight, document.body.style.position = "fixed", document.body.style.top = `-${e}px`, document.body.style.width = "100%", t && (document.body.style.paddingRight = `${a.current}px`, l.current = !0), r?.setAttribute("inert", "true"), i.current = n.length;
			return;
		}
		if (!e && n.length > 0 && t !== l.current && (document.body.style.paddingRight = t ? `${a.current}px` : s.current, l.current = t), !e && n.length === 0) {
			let e = document.querySelector("#app-root"), t = parseInt(document.body.style.top || "0") * -1;
			document.body.style.position = "", document.body.style.top = "", document.body.style.width = "", l.current &&= (document.body.style.paddingRight = s.current, !1), e?.removeAttribute("inert"), window.scrollTo({
				top: t,
				left: 0,
				behavior: "instant"
			});
		}
		i.current = n.length;
	}, [t, n.length]), /* @__PURE__ */ g(D.Provider, {
		value: {
			modals: n,
			openModal: f,
			closeModal: p,
			isTopModal: m
		},
		children: e
	});
}
//#endregion
export { k as a, j as i, M as n, D as o, A as r, O as s, N as t };

//# sourceMappingURL=modal-DLRYn-Lr.js.map