"use client";

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

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

type DialogLayout = "fixed" | "compact";

const DialogLayoutContext = React.createContext<DialogLayout>("compact");
const DialogShowCloseContext = React.createContext<boolean>(true);

const dialogCloseButtonClassName =
  "shrink-0 inline-flex size-9 items-center rounded justify-center border border-border/70 bg-background text-muted-foreground shadow-xs transition-colors hover:bg-red-600 hover:text-white focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-60 [&_svg]:pointer-events-none [&_svg]:shrink-0";

function Dialog({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Root>) {
  return <DialogPrimitive.Root data-slot="dialog" {...props} />;
}

function DialogTrigger({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Trigger>) {
  return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />;
}

function DialogPortal({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Portal>) {
  return <DialogPrimitive.Portal data-slot="dialog-portal" {...props} />;
}

function DialogClose({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Close>) {
  return <DialogPrimitive.Close data-slot="dialog-close" {...props} />;
}

type DialogHeaderProps = React.ComponentProps<"div">;

function DialogOverlay({
  className,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Overlay>) {
  return (
    <DialogPrimitive.Overlay
      data-slot="dialog-overlay"
      className={cn(
        "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 fixed inset-0 z-50 bg-black/50",
        className
      )}
      {...props}
    />
  );
}

function DialogContent({
  className,
  children,
  layout = "fixed",
  showCloseButton = true,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Content> & {
  layout?: DialogLayout;
  showCloseButton?: boolean;
}) {
  return (
    <DialogPortal data-slot="dialog-portal">
      <DialogOverlay />
      <DialogLayoutContext.Provider value={layout}>
        <DialogShowCloseContext.Provider value={showCloseButton}>
          <DialogPrimitive.Content
            data-slot="dialog-content"
            className={cn(
              "bg-background data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 fixed top-[50%] left-[50%] z-50 w-full max-w-[calc(100%-2rem)] translate-x-[-50%] translate-y-[-50%] rounded-2xl border shadow-xl duration-200 outline-none sm:max-w-lg",
              layout === "fixed"
                ? "flex h-[calc(100dvh-2rem)] max-h-[calc(100dvh-2rem)] flex-col gap-0 overflow-hidden p-0 sm:h-[min(90dvh,48rem)]"
                : "grid max-h-[calc(100dvh-2rem)] gap-6 overflow-y-auto p-6",
              className,
            )}
            {...props}
          >
            {children}
          </DialogPrimitive.Content>
        </DialogShowCloseContext.Provider>
      </DialogLayoutContext.Provider>
    </DialogPortal>
  );
}

function DialogHeader({ className, children, ...props }: DialogHeaderProps) {
  const layout = React.useContext(DialogLayoutContext);
  const showCloseButton = React.useContext(DialogShowCloseContext);

  return (
    <div
      data-slot="dialog-header"
      className={cn(
        "flex items-center justify-between gap-3",
        layout === "fixed" &&
          "shrink-0 border-b border-border/70 bg-background px-6 py-5",
        className,
      )}
      {...props}
    >
      <div className="flex flex-1 flex-col gap-2">{children}</div>
      {showCloseButton && (
        <DialogPrimitive.Close
          data-slot="dialog-close"
          className={dialogCloseButtonClassName}
        >
          <XIcon className="size-4" />
          <span className="sr-only">Close</span>
        </DialogPrimitive.Close>
      )}
    </div>
  );
}

function DialogBody({ className, ...props }: React.ComponentProps<"div">) {
  const layout = React.useContext(DialogLayoutContext);

  return (
    <div
      data-slot="dialog-body"
      className={cn(
        layout === "fixed"
          ? "min-h-0 flex-1 overflow-y-auto px-6 py-5"
          : "flex flex-col gap-4",
        className,
      )}
      {...props}
    />
  );
}

function DialogFooter({ className, ...props }: React.ComponentProps<"div">) {
  const layout = React.useContext(DialogLayoutContext);

  return (
    <div
      data-slot="dialog-footer"
      className={cn(
        "flex shrink-0 flex-col-reverse gap-2 sm:flex-row sm:items-center sm:justify-end",
        layout === "fixed" &&
          "border-t border-border/70 bg-background px-6 py-4",
        className,
      )}
      {...props}
    />
  );
}

function DialogTitle({
  className,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Title>) {
  return (
    <DialogPrimitive.Title
      data-slot="dialog-title"
      className={cn("text-lg leading-none font-semibold", className)}
      {...props}
    />
  );
}

function DialogDescription({
  className,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Description>) {
  return (
    <DialogPrimitive.Description
      data-slot="dialog-description"
      className={cn("text-muted-foreground text-sm", className)}
      {...props}
    />
  );
}

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