// Toast notification store — extracted to its own module so the
// `ToastNotification` component file can stay focused on rendering and
// keep React Fast Refresh's "component-only" boundary intact.
export type ToastType = "success" | "error" | "warning" | "info";

export interface Toast {
  id: string;
  type: ToastType;
  title: string;
  message?: string;
  duration?: number;
}

let toastCounter = 0;
const toastListeners: Set<(toast: Toast) => void> = new Set();

function emit(type: ToastType, title: string, message?: string) {
  const newToast: Toast = { id: `toast-${++toastCounter}`, type, title, message };
  toastListeners.forEach((listener) => listener(newToast));
}

/** Internal subscription helper used by `ToastNotification` to receive
 *  newly-fired toasts. Returns an unsubscribe function. */
export function subscribeToToasts(listener: (toast: Toast) => void): () => void {
  toastListeners.add(listener);
  return () => {
    toastListeners.delete(listener);
  };
}
