import * as React from "react";
import * as DialogPrimitive from "@radix-ui/react-dialog";
import { X } from "lucide-react";

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

const Dialog = DialogPrimitive.Root;

const DialogTrigger = DialogPrimitive.Trigger;

const DialogPortal = DialogPrimitive.Portal;

const DialogClose = DialogPrimitive.Close;

const DialogOverlay = React.forwardRef<
  React.ElementRef<typeof DialogPrimitive.Overlay>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
  <DialogPrimitive.Overlay
    ref={ref}
    className={cn(
      "tw-figr-fixed tw-figr-inset-0 tw-figr-z-50 tw-figr-bg-black/80 data-[state=open]:tw-figr-animate-in data-[state=closed]:tw-figr-animate-out data-[state=closed]:tw-figr-fade-out-0 data-[state=open]:tw-figr-fade-in-0",
      className
    )}
    {...props}
  />
));
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;

const DialogContent = React.forwardRef<
  React.ElementRef<typeof DialogPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
>(({ className, children, ...props }, ref) => (
  <DialogPortal>
    <DialogOverlay />
    <DialogPrimitive.Content
      ref={ref}
      className={cn(
        "tw-figr-fixed tw-figr-left-1/2 tw-figr-top-1/2 tw-figr-z-50 tw-figr-grid tw-figr-w-full tw-figr-max-w-lg tw-figr-translate-x-[-50%] tw-figr-translate-y-[-50%] tw-figr-gap-l tw-figr-rounded-m tw-figr-border tw-figr-border-neutral-200 tw-figr-bg-base-white tw-figr-p-ml tw-figr-font-secondary tw-figr-shadow-lg tw-figr-duration-200 data-[state=close]:tw-figr-hidden data-[state=open]:tw-figr-animate-in data-[state=closed]:tw-figr-animate-out data-[state=closed]:tw-figr-fade-out-0 data-[state=open]:tw-figr-fade-in-0 data-[state=closed]:tw-figr-slide-out-to-left-1/2 data-[state=closed]:tw-figr-slide-out-to-top-[48%] data-[state=open]:tw-figr-slide-in-from-left-1/2 data-[state=open]:tw-figr-slide-in-from-top-[48%] sm:tw-figr-rounded-lg",
        className
      )}
      {...props}
    >
      {children}
      <DialogPrimitive.Close className="focus:tw-figr-ring-ring tw-figr-absolute tw-figr-right-4 tw-figr-top-4 tw-figr-rounded-sm tw-figr-opacity-70 tw-figr-ring-offset-background tw-figr-transition-opacity hover:tw-figr-opacity-100 focus:tw-figr-outline-none focus:tw-figr-ring-2 focus:tw-figr-ring-offset-2 disabled:tw-figr-pointer-events-none data-[state=open]:tw-figr-bg-background">
        <X className="tw-figr-h-4 tw-figr-w-4 tw-figr-text-base-black" />
        <span className="tw-figr-sr-only">Close</span>
      </DialogPrimitive.Close>
    </DialogPrimitive.Content>
  </DialogPortal>
));
DialogContent.displayName = DialogPrimitive.Content.displayName;

const DialogHeader = ({
  className,
  ...props
}: React.HTMLAttributes<HTMLDivElement>) => (
  <div
    className={cn(
      "tw-figr-flex tw-figr-flex-col tw-figr-space-y-1.5 tw-figr-text-center sm:tw-figr-text-left",
      className
    )}
    {...props}
  />
);
DialogHeader.displayName = "DialogHeader";

const DialogFooter = ({
  className,
  ...props
}: React.HTMLAttributes<HTMLDivElement>) => (
  <div
    className={cn(
      "tw-figr-flex tw-figr-flex-col-reverse sm:tw-figr-flex-row sm:tw-figr-justify-end sm:tw-figr-space-x-2",
      className
    )}
    {...props}
  />
);
DialogFooter.displayName = "DialogFooter";

const DialogTitle = React.forwardRef<
  React.ElementRef<typeof DialogPrimitive.Title>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
>(({ className, ...props }, ref) => (
  <DialogPrimitive.Title
    ref={ref}
    className={cn(
      "tw-figr-text-desktop-highlight-accent tw-figr-text-base-black",
      className
    )}
    {...props}
  />
));
DialogTitle.displayName = DialogPrimitive.Title.displayName;

const DialogDescription = React.forwardRef<
  React.ElementRef<typeof DialogPrimitive.Description>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
>(({ className, ...props }, ref) => (
  <DialogPrimitive.Description
    ref={ref}
    className={cn(
      "tw-figr-text-desktop-caption-regular tw-figr-text-neutral-700",
      className
    )}
    {...props}
  />
));
DialogDescription.displayName = DialogPrimitive.Description.displayName;

export {
  Dialog,
  DialogPortal,
  DialogOverlay,
  DialogClose,
  DialogTrigger,
  DialogContent,
  DialogHeader,
  DialogFooter,
  DialogTitle,
  DialogDescription
};
