import { type ReactNode } from 'react';
import { Dialog as RDialog } from 'radix-ui';
import { X } from '@phosphor-icons/react';
import { IconButton } from '../icon-button';

export const Dialog = ({
  trigger,
  title,
  description,
  children,
  footer,
  open,
  defaultOpen,
  onOpenChange,
  modal,
}: {
  trigger?: ReactNode;
  title: string;
  description?: ReactNode;
  children?: ReactNode;
  footer?: ReactNode;
  open?: boolean;
  defaultOpen?: boolean;
  onOpenChange?: (o: boolean) => void;
  modal?: boolean;
}) => (
  <RDialog.Root open={open} defaultOpen={defaultOpen} onOpenChange={onOpenChange} modal={modal}>
    {trigger && <RDialog.Trigger asChild>{trigger}</RDialog.Trigger>}
    <RDialog.Portal>
      <RDialog.Overlay className="mta-overlay" />
      <RDialog.Content className="mta-dialog">
        <RDialog.Title className="mta-dialog__title">{title}</RDialog.Title>
        {description && (
          <RDialog.Description className="mta-dialog__desc">
            {description}
          </RDialog.Description>
        )}
        {children && <div className="mta-dialog__body">{children}</div>}
        {footer && <div className="mta-dialog__footer">{footer}</div>}
        <RDialog.Close asChild>
          <IconButton
            variant="ghost"
            size="sm"
            className="mta-dialog__close"
            aria-label="Close"
          >
            <X size={14} weight="bold" />
          </IconButton>
        </RDialog.Close>
      </RDialog.Content>
    </RDialog.Portal>
  </RDialog.Root>
);

export const DialogClose = RDialog.Close;
