import * as React from "react";

type ToastType = "success" | "error" | "info";

interface Toast {
  id: string;
  message: string;
  type: ToastType;
}

let subscribers: ((toasts: Toast[]) => void)[] = [];
let toasts: Toast[] = [];

const notify = () => {
  for (const sub of subscribers) {
    sub(toasts);
  }
};

export const toast = (message: string, type: ToastType = "success") => {
  const id = Math.random().toString(36).substring(2, 9);
  toasts = [...toasts, { id, message, type }];
  notify();

  setTimeout(() => {
    toasts = toasts.filter((t) => t.id !== id);
    notify();
  }, 3000);
};

export const useToastState = () => {
  const [state, setState] = React.useState<Toast[]>(toasts);

  React.useEffect(() => {
    subscribers.push(setState);
    return () => {
      subscribers = subscribers.filter((sub) => sub !== setState);
    };
  }, []);

  return state;
};
