import type { Toast, ToastType } from '$lib/types';

function createToastStore() {
	let toasts = $state<Toast[]>([]);

	function add(type: ToastType, message: string, duration = 4000) {
		const id = crypto.randomUUID();
		toasts = [...toasts, { id, type, message }];
		if (duration > 0) {
			setTimeout(() => remove(id), duration);
		}
	}

	function remove(id: string) {
		toasts = toasts.filter((t) => t.id !== id);
	}

	function success(message: string) { add('success', message); }
	function error(message: string) { add('error', message, 6000); }
	function info(message: string) { add('info', message); }

	return {
		get list() { return toasts; },
		add,
		remove,
		success,
		error,
		info,
	};
}

export const toast = createToastStore();
