// ============================================================
// Toast 全局状态管理
// 与 Toast 组件配合使用
// ============================================================

export type ToastType = "error" | "warning" | "info";

let nextId = 0;
let _setToasts: React.Dispatch<React.SetStateAction<ToastItem[]>> | null = null;

export interface ToastItem {
  id: number;
  type: ToastType;
  message: string;
}

/** 注册 Toast state setter（由 ToastContainer 组件调用） */
export function registerToastSetter(
  setter: React.Dispatch<React.SetStateAction<ToastItem[]>> | null,
) {
  _setToasts = setter;
}

/** 显示一条 Toast */
export function showToast(message: string, type: ToastType = "info") {
  if (!_setToasts) return;
  const id = nextId++;
  _setToasts((prev) => [...prev, { id, type, message }]);
}
