import { toast as sonnerToast } from "sonner";

type Position =
  | "top-left"
  | "top-center"
  | "top-right"
  | "bottom-left"
  | "bottom-center"
  | "bottom-right";

type Type = "info" | "error" | "success" | "warning";

export type ToastConfig = {
  position?: Position;
  title?: React.ReactNode;
  description?: React.ReactNode;
  action?: React.ReactNode;
  dismissable?: boolean;
  type?: Type;
};

/**
 * Toast wrapper that maintains compatibility with the old react-aria toast API
 * while using Sonner under the hood
 */
export const toast = {
  add: (config: ToastConfig, options?: { timeout?: number }) => {
    const { title, description, type, position, action, dismissable = true } = config;

    // Combine title and description
    const message = title || description;
    const descriptionText = title && description ? description : undefined;

    // Map position to Sonner position format
    let sonnerPosition:
      | "top-left"
      | "top-center"
      | "top-right"
      | "bottom-left"
      | "bottom-center"
      | "bottom-right" = "bottom-right";
    if (position) {
      sonnerPosition = position;
    }

    const sonnerOptions = {
      description: descriptionText,
      position: sonnerPosition,
      action: action ? (action as unknown as { label: string; onClick: () => void }) : undefined,
      dismissible: dismissable,
      duration: options?.timeout || 4000,
    };

    // Call the appropriate Sonner toast method based on type
    switch (type) {
      case "success":
        return sonnerToast.success(message, sonnerOptions);
      case "error":
        return sonnerToast.error(message, sonnerOptions);
      case "warning":
        return sonnerToast.warning(message, sonnerOptions);
      case "info":
        return sonnerToast.info(message, sonnerOptions);
      default:
        return sonnerToast(message, sonnerOptions);
    }
  },
};
