import { Dialog as DialogPrimitive } from "pettyui/dialog";
import { cn } from "../utils";
import type { JSX } from "solid-js";
import { splitProps } from "solid-js";

const Dialog = DialogPrimitive;
const DialogTrigger = DialogPrimitive.Trigger;
const DialogClose = DialogPrimitive.Close;
const DialogPortal = DialogPrimitive.Portal;
const DialogTitle = DialogPrimitive.Title;
const DialogDescription = DialogPrimitive.Description;

/** Renders a dimmed backdrop behind the dialog panel. */
function DialogOverlay(props: JSX.HTMLAttributes<HTMLDivElement>): JSX.Element {
  const [local, rest] = splitProps(props, ["class"]);
  return (
    <DialogPrimitive.Overlay
      class={cn("fixed inset-0 z-50 bg-black/80", local.class)}
      {...rest}
    />
  );
}

/** Pre-composed dialog panel: wraps Content inside Portal with Overlay included. */
function DialogContent(props: JSX.HTMLAttributes<HTMLDivElement> & { children?: JSX.Element }): JSX.Element {
  const [local, rest] = splitProps(props, ["class", "children"]);
  return (
    <DialogPortal>
      <DialogOverlay />
      <DialogPrimitive.Content
        class={cn("fixed left-1/2 top-1/2 z-50 grid w-full max-w-lg -translate-x-1/2 -translate-y-1/2 gap-4 rounded-lg border bg-background p-6 shadow-lg", local.class)}
        {...rest}
      >
        {local.children}
      </DialogPrimitive.Content>
    </DialogPortal>
  );
}

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