// Basic toast hook for notifications
import { useState } from "react";

type ToastVariant = "default" | "destructive";

export interface ToastOptions {
  title?: string;
  description?: string;
  variant?: ToastVariant;
  duration?: number;
  id?: number;
  className?: string;
}

// Export stand-alone toast function for convenience
let addToast: (options: ToastOptions) => number = () => 0;

export function toast(options: ToastOptions) {
  return addToast(options);
}

export function useToast() {
  const [toasts, setToasts] = useState<ToastOptions[]>([]);

  // Create a function that matches the expected signature
  const toast = (options: ToastOptions) => {
    const id = Date.now();
    const newToast = {
      ...options,
      id,
      duration: options.duration || 3000,
    };
    
    setToasts((prev) => [...prev, newToast]);
    
    // Auto dismiss
    setTimeout(() => {
      dismiss(id);
    }, newToast.duration);
    
    return id;
  };

  // Set the exported toast function to use this implementation
  addToast = toast;

  const dismiss = (id?: number) => {
    if (id) {
      setToasts((prev) => prev.filter((toast) => toast.id !== id));
    } else {
      setToasts([]);
    }
  };

  return {
    toast,
    dismiss,
    toasts
  };
}
