import { writable } from "svelte/store";
import type { MeResponse } from "./types.js";

/** 当前登录身份;null = 未登录(显示登录视图)。 */
export const me = writable<MeResponse | null>(null);
export const authChecked = writable(false);

export interface ToastItem {
  readonly id: number;
  readonly message: string;
  readonly kind: "info" | "err";
}

let nextToastId = 1;
export const toasts = writable<ToastItem[]>([]);

export function toast(message: string, kind: ToastItem["kind"] = "info"): void {
  const id = nextToastId++;
  toasts.update((list) => [...list, { id, message, kind }]);
  setTimeout(() => {
    toasts.update((list) => list.filter((t) => t.id !== id));
  }, 3600);
}

export const toastOk = (m: string): void => toast(m, "info");
export const toastErr = (m: string): void => toast(m, "err");
