import { o as e } from "./flex-C7njxZ74.js";
import { n as t } from "./button-BU2kRzwu.js";
import { r as n } from "./grid-dI94AAbi.js";
import { c as r } from "./misc-BYxxlJz6.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 { X as p } from "lucide-react";
import { jsx as m, jsxs as h } from "react/jsx-runtime";
import { getOrCreatePortalRoot as g, useEscapeDismiss as _, useOverlayFocus as v } from "@ryuzaki13/react-foundation-lib/dom";
import { createPortal as y } from "react-dom";
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__ */ m("div", {
	className: f("surface2 paddingMd borderBottom", t),
	children: e
});
k.displayName = "Modal.Toolbar";
var A = ({ children: e, className: t, scrollable: n }) => /* @__PURE__ */ m("div", {
	className: f("paddingMd h100 overflowHidden", t),
	children: n ? /* @__PURE__ */ m(r, {
		className: "h100",
		children: e
	}) : e
});
A.displayName = "Modal.Content";
var j = ({ children: t, className: n }) => /* @__PURE__ */ m("div", {
	className: f("surface2 borderTop paddingMd", n),
	children: /* @__PURE__ */ m(e, {
		gap: "sm",
		align: "center",
		justify: "end",
		children: t
	})
});
j.displayName = "Modal.Footer";
var M = ({ isOpen: e, title: r, 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 = e && (N.length === 0 || I(D));
	c(() => {
		e && (R.current = document.activeElement instanceof HTMLElement ? document.activeElement : null);
	}, [e]);
	let V = v({
		active: e || !1,
		trapFocus: !0,
		restoreFocus: !1
	});
	_({
		active: B || !1,
		onDismiss: s,
		containerRef: V
	}), c(() => {
		e && P(D);
	}, [
		e,
		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 = g("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 y(/* @__PURE__ */ m(b, {
		onExitComplete: H,
		children: e ? /* @__PURE__ */ h(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__ */ h("div", {
				className: f(C.header, W ? "" : "borderBottom"),
				children: [/* @__PURE__ */ m("h3", {
					className: C.headerText,
					id: M,
					children: r
				}), /* @__PURE__ */ m(t, {
					icon: /* @__PURE__ */ m(p, {}),
					className: C.headerButton,
					variant: "ghost",
					"aria-label": "Закрыть модальное окно",
					onClick: s
				})]
			}), /* @__PURE__ */ h(n, {
				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));
	}, []), h = 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__ */ m(D.Provider, {
		value: {
			modals: n,
			openModal: f,
			closeModal: p,
			isTopModal: h
		},
		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-paexEF0j.js.map