/**
 * Hook for managing toast notifications
 */

import { useState, useCallback } from 'react';
import type { ToastMessage, ToastType } from '../components/Toast';

let toastId = 0;

export function useToast() {
  const [messages, setMessages] = useState<ToastMessage[]>([]);

  const addToast = useCallback((message: string, type: ToastType = 'info', duration?: number) => {
    const id = `toast-${++toastId}`;
    const newMessage: ToastMessage = {
      id,
      message,
      type,
      duration,
    };

    setMessages((prev) => [...prev, newMessage]);
    return id;
  }, []);

  const removeToast = useCallback((id: string) => {
    setMessages((prev) => prev.filter((msg) => msg.id !== id));
  }, []);

  const success = useCallback((message: string, duration?: number) => {
    return addToast(message, 'success', duration);
  }, [addToast]);

  const error = useCallback((message: string, duration?: number) => {
    return addToast(message, 'error', duration);
  }, [addToast]);

  const info = useCallback((message: string, duration?: number) => {
    return addToast(message, 'info', duration);
  }, [addToast]);

  const warning = useCallback((message: string, duration?: number) => {
    return addToast(message, 'warning', duration);
  }, [addToast]);

  return {
    messages,
    addToast,
    removeToast,
    success,
    error,
    info,
    warning,
  };
}
