"use client";

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

import { preventDismissOnPrototypePluginChrome } from "../../lib/prototype-comments/core/comment-capture-blocked";
import {
  getPrototypeDialogPortalContainer,
  getPrototypeToolDialogPortalContainer,
} from "../../lib/tool-portal";
import { cn } from "../../lib/utils";

import styles from "./dialog.module.scss";

const Dialog = DialogPrimitive.Root;
const DialogTrigger = DialogPrimitive.Trigger;
const DialogClose = DialogPrimitive.Close;
const DialogPortal = DialogPrimitive.Portal;

const DialogOverlay = React.forwardRef<
  React.ComponentRef<typeof DialogPrimitive.Overlay>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
  <DialogPrimitive.Overlay
    ref={ref}
    className={cn(styles.overlay, "fixed inset-0 z-50", className)}
    {...props}
  />
));
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;

export type DialogPortalScope = "viewport" | "tool";

const DialogContent = React.forwardRef<
  React.ComponentRef<typeof DialogPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & {
    showCloseButton?: boolean;
    overlayClassName?: string;
    portalScope?: DialogPortalScope;
  }
>(({ className, children, showCloseButton = true, overlayClassName, portalScope = "viewport", onPointerDownOutside, onInteractOutside, onFocusOutside, ...props }, ref) => (
  <DialogPortal
    container={
      portalScope === "tool"
        ? getPrototypeToolDialogPortalContainer()
        : getPrototypeDialogPortalContainer()
    }
  >
    {portalScope === "tool" ? (
      <DialogOverlay
        className={cn("tool-dialog-overlay", overlayClassName)}
      />
    ) : (
      // Radix `Dialog.Overlay` returns null when `modal={false}` (required for
      // product dialogs so review chrome stays interactive). Render a plain
      // scrim instead so viewport-scoped dialogs still dim the preview.
      <div
        aria-hidden
        data-state="open"
        className={cn(
          styles.overlay,
          styles.productOverlay,
          "fixed inset-0",
          overlayClassName,
        )}
      />
    )}
    <DialogPrimitive.Content
      ref={ref}
      className={cn(
        styles.content,
        portalScope === "viewport" ? styles.productContent : "z-50",
        "fixed left-[50%] top-[50%] grid w-full max-w-lg gap-4 border bg-background p-6 shadow-lg sm:rounded-lg",
        className,
      )}
      onPointerDownOutside={(event) => {
        preventDismissOnPrototypePluginChrome(event);
        onPointerDownOutside?.(event);
      }}
      onInteractOutside={(event) => {
        preventDismissOnPrototypePluginChrome(event);
        onInteractOutside?.(event);
      }}
      onFocusOutside={(event) => {
        preventDismissOnPrototypePluginChrome(event);
        onFocusOutside?.(event);
      }}
      {...props}
    >
      {children}
      {showCloseButton ? (
        <DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none">
          <X className="size-4" />
          <span className="sr-only">Close</span>
        </DialogPrimitive.Close>
      ) : null}
    </DialogPrimitive.Content>
  </DialogPortal>
));
DialogContent.displayName = DialogPrimitive.Content.displayName;

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

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

const DialogTitle = React.forwardRef<
  React.ComponentRef<typeof DialogPrimitive.Title>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
>(({ className, ...props }, ref) => (
  <DialogPrimitive.Title
    ref={ref}
    className={cn("text-lg font-semibold leading-none tracking-tight", className)}
    {...props}
  />
));
DialogTitle.displayName = DialogPrimitive.Title.displayName;

const DialogDescription = React.forwardRef<
  React.ComponentRef<typeof DialogPrimitive.Description>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
>(({ className, ...props }, ref) => (
  <DialogPrimitive.Description
    ref={ref}
    className={cn("text-sm text-muted-foreground", className)}
    {...props}
  />
));
DialogDescription.displayName = DialogPrimitive.Description.displayName;

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