import { n as mergeRefs } from "../../refs.js";
import { t as Toast_default } from "../../Toast.js";
import React from "react";
import { jsx } from "react/jsx-runtime";
import { AnimatePresence, motion } from "motion/react";
import styles from "./styles.module.scss";
var DURATION = 8e3;
var ToastList = (props) => {
	const { testId, toasts = [], ref } = props;
	const internalRef = React.useRef(null);
	const [visibleToasts, setVisibleToasts] = React.useState([]);
	const [removedToastIds, setRemovedToastIds] = React.useState(/* @__PURE__ */ new Set());
	const timeoutRefs = React.useRef(/* @__PURE__ */ new Map());
	React.useEffect(() => {
		const newToasts = toasts.filter((toast) => !visibleToasts.some((vt) => vt.id === toast.id) && !removedToastIds.has(toast.id));
		if (newToasts.length > 0) setVisibleToasts((prev) => [...prev, ...newToasts]);
	}, [
		toasts,
		visibleToasts,
		removedToastIds
	]);
	const handleRemoveToast = React.useCallback((id) => {
		setVisibleToasts((prev) => prev.filter((t) => t.id !== id));
		setRemovedToastIds((prev) => new Set([...prev, id]));
		const timeout = timeoutRefs.current.get(id);
		if (timeout) {
			clearTimeout(timeout);
			timeoutRefs.current.delete(id);
		}
	}, []);
	React.useEffect(() => {
		const timeouts = timeoutRefs.current;
		visibleToasts.forEach((toast) => {
			if (!timeouts.has(toast.id)) {
				const timeout = setTimeout(() => {
					handleRemoveToast(toast.id);
				}, DURATION);
				timeouts.set(toast.id, timeout);
			}
		});
	}, [visibleToasts, handleRemoveToast]);
	React.useEffect(() => {
		const timeouts = timeoutRefs.current;
		const currentToastIds = new Set(visibleToasts.map((t) => t.id));
		timeouts.forEach((timeout, id) => {
			if (!currentToastIds.has(id)) {
				clearTimeout(timeout);
				timeouts.delete(id);
			}
		});
	}, [visibleToasts]);
	React.useEffect(() => {
		return () => {
			timeoutRefs.current.forEach((timeout) => clearTimeout(timeout));
			timeoutRefs.current.clear();
		};
	}, []);
	return /* @__PURE__ */ jsx("section", {
		ref: mergeRefs([internalRef, ref]),
		className: styles["toast-list"],
		"data-testid": testId,
		children: /* @__PURE__ */ jsx(AnimatePresence, {
			mode: "popLayout",
			children: visibleToasts.map((toast) => /* @__PURE__ */ jsx(motion.div, {
				layout: true,
				initial: {
					opacity: 0,
					y: 50
				},
				animate: {
					opacity: 1,
					y: 0
				},
				exit: {
					opacity: 0,
					y: -20
				},
				transition: { layout: {
					type: "spring",
					duration: .4,
					bounce: .1
				} },
				children: /* @__PURE__ */ jsx(Toast_default, {
					testId: `${testId}-${toast.id}`,
					title: toast.title,
					message: toast.message,
					onClose: () => handleRemoveToast(toast.id)
				})
			}, toast.id))
		})
	});
};
ToastList.displayName = "ToastList";
var ToastList_default = ToastList;
export { ToastList_default as default };

//# sourceMappingURL=index.js.map