import { INFO_TOAST_DURATION_MS as e } from "./constants.js";
import { ToastViewport as t } from "./ToastViewport.js";
import { createContext as n, useCallback as r, useContext as i, useEffect as a, useMemo as o, useRef as s, useState as c } from "react";
import { jsx as l, jsxs as u } from "react/jsx-runtime";
//#region src/atomic/molecules/toast/ToastProvider.tsx
var d = n(null), f = (e, t) => (n, r) => e.push({
	kind: t,
	title: n,
	message: r
}), p = { pushWithKind: f }, m = ({ children: n, maxToasts: i = 4 }) => {
	let [p, m] = c([]), h = s(0), g = s(/* @__PURE__ */ new Map()), _ = r((e) => {
		let t = g.current.get(e);
		t != null && (clearTimeout(t), g.current.delete(e)), m((t) => t.filter((t) => t.id !== e));
	}, []), v = r(() => {
		g.current.forEach((e) => {
			clearTimeout(e);
		}), g.current.clear(), m([]);
	}, []), y = r((e) => {
		let t = e.id ?? `toast-${h.current++}`;
		return m((n) => {
			let r = [...n, {
				...e,
				kind: e.kind ?? "info",
				id: t
			}];
			return r.length > i ? r.slice(r.length - i) : r;
		}), t;
	}, [i]);
	a(() => {
		let t = /* @__PURE__ */ new Set();
		p.forEach((n) => {
			if (n.kind !== "info" || (t.add(n.id), g.current.has(n.id))) return;
			let r = setTimeout(() => {
				g.current.delete(n.id), m((e) => e.filter((e) => e.id !== n.id));
			}, e);
			g.current.set(n.id, r);
		}), g.current.forEach((e, n) => {
			t.has(n) || (clearTimeout(e), g.current.delete(n));
		});
	}, [p]), a(() => {
		let e = g.current;
		return () => {
			e.forEach((e) => {
				clearTimeout(e);
			}), e.clear();
		};
	}, []);
	let b = o(() => {
		let e = {
			push: y,
			dismiss: _,
			clear: v,
			info: () => "",
			warning: () => "",
			error: () => ""
		};
		return {
			...e,
			info: f(e, "info"),
			warning: f(e, "warning"),
			error: f(e, "error")
		};
	}, [
		v,
		_,
		y
	]);
	return /* @__PURE__ */ u(d.Provider, {
		value: b,
		children: [n, /* @__PURE__ */ l(t, {
			toasts: p,
			onDismiss: _
		})]
	});
}, h = () => {
	let e = i(d);
	if (e == null) throw Error("useToast must be used within a ToastProvider");
	return e;
};
//#endregion
export { m as ToastProvider, p as __test, h as useToast };

//# sourceMappingURL=ToastProvider.js.map