/**
 * Toast Store
 *
 * Lightweight toast notification system for canvas operations.
 * Shows brief, non-interactive messages (e.g., "Undo: Split node").
 */

import { atom } from 'jotai';

export interface CanvasToastData {
	id: string;
	message: string;
	timestamp: number;
}

/** Current visible toast, or null */
export const canvasToastAtom = atom<CanvasToastData | null>(null);

/** Show a toast message that auto-clears after 2 seconds */
export const showToastAtom = atom(
	null,
	(_get, set, message: string) => {
		const id = `toast-${Date.now()}`;
		set(canvasToastAtom, { id, message, timestamp: Date.now() });
		setTimeout(() => {
			set(canvasToastAtom, (current) => (current?.id === id ? null : current));
		}, 2000);
	},
);
