import { writable } from 'svelte/store';

// Toast 시스템 — UI state(drawer/modal)와 분리된 알림 전용 store
export interface Toast {
  id: number;
  type: 'success' | 'error' | 'warning' | 'info';
  message: string;
}

let toastId = 0;
export const toasts = writable<Toast[]>([]);

export function addToast(type: Toast['type'], message: string, duration = 5000) {
  const id = ++toastId;
  toasts.update((t) => [...t, { id, type, message }]);
  if (duration > 0) {
    setTimeout(() => removeToast(id), duration);
  }
  return id;
}

export function removeToast(id: number) {
  toasts.update((t) => t.filter((toast) => toast.id !== id));
}
