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

//# sourceMappingURL=modal-C0j0hEIt.js.map