import { create } from "zustand";

export type AlertVariant = "default" | "success" | "warn" | "error";

export interface UiAlert {
  id: string;
  title: string;
  description?: string;
  variant: AlertVariant;
}

interface UiState {
  alerts: UiAlert[];
  pushAlert: (alert: Omit<UiAlert, "id">) => void;
  removeAlert: (id: string) => void;
}

const generateId = () => Math.random().toString(36).slice(2);

export const useUiStore = create<UiState>((set, get) => ({
  alerts: [],
  pushAlert: (alert) => {
    const id = generateId();
    set((state) => ({ alerts: [{ ...alert, id }, ...state.alerts].slice(0, 5) }));
    setTimeout(() => {
      get().removeAlert(id);
    }, 3000);
  },
  removeAlert: (id) =>
    set((state) => ({ alerts: state.alerts.filter((item) => item.id !== id) })),
}));
