import * as React from "react";
import * as ToastPrimitives from "@radix-ui/react-toast";
import { cva, type VariantProps } from "class-variance-authority";
import { X } from "lucide-react";

import { cn } from "../lib/utils";

const ToastProvider = ToastPrimitives.Provider;

const ToastViewport = React.forwardRef<
  React.ElementRef<typeof ToastPrimitives.Viewport>,
  React.ComponentPropsWithoutRef<typeof ToastPrimitives.Viewport>
>(({ className, ...props }, ref) => (
  <ToastPrimitives.Viewport
    ref={ref}
    className={cn(
      "tw-figr-fixed tw-figr-top-0 tw-figr-z-[100] tw-figr-flex tw-figr-max-h-screen tw-figr-w-full tw-figr-flex-col-reverse tw-figr-p-4 sm:tw-figr-bottom-0 sm:tw-figr-right-0 sm:tw-figr-top-auto sm:tw-figr-flex-col md:tw-figr-max-w-[420px]",
      className
    )}
    {...props}
  />
));
ToastViewport.displayName = ToastPrimitives.Viewport.displayName;

const toastVariants = cva(
  "tw-figr-group tw-figr-pointer-events-auto tw-figr-relative tw-figr-flex tw-figr-w-full tw-figr-items-center tw-figr-justify-between tw-figr-space-x-2 tw-figr-overflow-hidden tw-figr-rounded-md tw-figr-p-4 tw-figr-pr-6 tw-figr-shadow-lg tw-figr-transition-all data-[swipe=cancel]:tw-figr-translate-x-0 data-[swipe=end]:tw-figr-translate-x-[var(--radix-toast-swipe-end-x)] data-[swipe=move]:tw-figr-translate-x-[var(--radix-toast-swipe-move-x)] data-[swipe=move]:tw-figr-transition-none data-[state=open]:tw-figr-animate-in data-[state=closed]:tw-figr-animate-out data-[swipe=end]:tw-figr-animate-out data-[state=closed]:tw-figr-fade-out-80 data-[state=closed]:tw-figr-slide-out-to-right-full data-[state=open]:tw-figr-slide-in-from-top-full data-[state=open]:sm:tw-figr-slide-in-from-bottom-full",
  {
    variants: {
      variant: {
        default:
          "tw-figr-border tw-figr-border-neutral-200 tw-figr-bg-base-white tw-figr-text-base-black tw-figr-font-secondary",
        destructive:
          "tw-figr-border tw-figr-border-error-100 tw-figr-bg-error-100 tw-figr-text-base-white tw-figr-font-secondary"
      }
    },
    defaultVariants: {
      variant: "default"
    }
  }
);

const Toast = React.forwardRef<
  React.ElementRef<typeof ToastPrimitives.Root>,
  React.ComponentPropsWithoutRef<typeof ToastPrimitives.Root> &
    VariantProps<typeof toastVariants>
>(({ className, variant, ...props }, ref) => {
  return (
    <ToastPrimitives.Root
      ref={ref}
      className={cn(toastVariants({ variant }), className)}
      {...props}
    />
  );
});
Toast.displayName = ToastPrimitives.Root.displayName;

const ToastAction = React.forwardRef<
  React.ElementRef<typeof ToastPrimitives.Action>,
  React.ComponentPropsWithoutRef<typeof ToastPrimitives.Action>
>(({ className, ...props }, ref) => (
  <ToastPrimitives.Action
    ref={ref}
    className={cn(
      "group-[.destructive]:hover:tw-figr-border-error-300 group-[.destructive]:focus:tw-figr-ring-error-300 tw-figr-inline-flex tw-figr-h-8 tw-figr-shrink-0 tw-figr-border-collapse tw-figr-items-center tw-figr-justify-center tw-figr-rounded-md tw-figr-bg-transparent tw-figr-px-3 tw-figr-text-sm tw-figr-font-medium tw-figr-transition-colors hover:tw-figr-bg-secondary focus:tw-figr-outline-none focus:tw-figr-ring-1 focus:tw-figr-ring-transparent disabled:tw-figr-pointer-events-none disabled:tw-figr-opacity-50 group-[.destructive]:tw-figr-border-neutral-400 group-[.destructive]:hover:tw-figr-bg-error-200 group-[.destructive]:hover:tw-figr-text-base-white",
      className
    )}
    {...props}
  />
));
ToastAction.displayName = ToastPrimitives.Action.displayName;

const ToastClose = React.forwardRef<
  React.ElementRef<typeof ToastPrimitives.Close>,
  React.ComponentPropsWithoutRef<typeof ToastPrimitives.Close>
>(({ className, ...props }, ref) => (
  <ToastPrimitives.Close
    ref={ref}
    className={cn(
      "tw-figr-text-foreground/50 hover:tw-figr-text-foreground tw-figr-absolute tw-figr-right-1 tw-figr-top-1 tw-figr-rounded-md tw-figr-p-1 tw-figr-opacity-0 tw-figr-transition-opacity focus:tw-figr-opacity-100 focus:tw-figr-outline-none focus:tw-figr-ring-1 group-hover:tw-figr-opacity-100 group-[.destructive]:tw-figr-text-red-300 group-[.destructive]:hover:tw-figr-text-red-50 group-[.destructive]:focus:tw-figr-ring-red-400 group-[.destructive]:focus:tw-figr-ring-offset-red-600",
      className
    )}
    toast-close=""
    {...props}
  >
    <X className="tw-figr-h-4 tw-figr-w-4" />
  </ToastPrimitives.Close>
));
ToastClose.displayName = ToastPrimitives.Close.displayName;

const ToastTitle = React.forwardRef<
  React.ElementRef<typeof ToastPrimitives.Title>,
  React.ComponentPropsWithoutRef<typeof ToastPrimitives.Title>
>(({ className, ...props }, ref) => (
  <ToastPrimitives.Title
    ref={ref}
    className={cn(
      "tw-figr-text-sm tw-figr-font-semibold [&+div]:tw-figr-text-xs",
      className
    )}
    {...props}
  />
));
ToastTitle.displayName = ToastPrimitives.Title.displayName;

const ToastDescription = React.forwardRef<
  React.ElementRef<typeof ToastPrimitives.Description>,
  React.ComponentPropsWithoutRef<typeof ToastPrimitives.Description>
>(({ className, ...props }, ref) => (
  <ToastPrimitives.Description
    ref={ref}
    className={cn("tw-figr-text-sm tw-figr-opacity-90", className)}
    {...props}
  />
));
ToastDescription.displayName = ToastPrimitives.Description.displayName;

type ToastProps = React.ComponentPropsWithoutRef<typeof Toast>;

type ToastActionElement = React.ReactElement<typeof ToastAction>;

export {
  type ToastProps,
  type ToastActionElement,
  ToastProvider,
  ToastViewport,
  Toast,
  ToastTitle,
  ToastDescription,
  ToastClose,
  ToastAction
};
