'use client'

import type { ComponentProps } from 'react'
import {
  Root as RadixDialogRoot,
  Trigger as RadixDialogTrigger,
  Portal as RadixDialogPortal,
  Close as RadixDialogClose,
  Overlay as RadixDialogOverlay,
  Content as RadixDialogContent,
  Title as RadixDialogTitle,
  Description as RadixDialogDescription
} from '@radix-ui/react-dialog'

import { cn } from '@/shared/lib/utils'
import { Button } from './button'
import { XIcon } from 'lucide-react'

export function Dialog({
  ...props
}: ComponentProps<typeof RadixDialogRoot>) {
  return <RadixDialogRoot data-slot='dialog' {...props} />
}

export function DialogTrigger({
  ...props
}: ComponentProps<typeof RadixDialogTrigger>) {
  return <RadixDialogTrigger data-slot='dialog-trigger' {...props} />
}

export function DialogPortal({
  ...props
}: ComponentProps<typeof RadixDialogPortal>) {
  return <RadixDialogPortal data-slot='dialog-portal' {...props} />
}

export function DialogClose({
  ...props
}: ComponentProps<typeof RadixDialogClose>) {
  return <RadixDialogClose data-slot='dialog-close' {...props} />
}

export function DialogOverlay({
  className,
  ...props
}: ComponentProps<typeof RadixDialogOverlay>) {
  return (
    <RadixDialogOverlay
      data-slot='dialog-overlay'
      className={cn('data-open:animate-in data-closed:animate-out data-closed:fade-out-0 data-open:fade-in-0 bg-black/80 duration-100 supports-backdrop-filter:backdrop-blur-xs fixed inset-0 isolate z-50', className)}
      {...props}
    />
  )
}

export function DialogContent({
  className,
  children,
  showCloseButton = true,
  ...props
}: ComponentProps<typeof RadixDialogContent> & {
  showCloseButton?: boolean
}) {
  return (
    <DialogPortal>
      <DialogOverlay />
      <RadixDialogContent
        data-slot='dialog-content'
        className={cn(
          'bg-background data-open:animate-in data-closed:animate-out data-closed:fade-out-0 data-open:fade-in-0 data-closed:zoom-out-95 data-open:zoom-in-95 ring-foreground/5 grid max-w-[calc(100%-2rem)] gap-6 rounded-4xl p-6 text-sm ring-1 duration-100 sm:max-w-md fixed top-1/2 left-1/2 z-50 w-full -translate-x-1/2 -translate-y-1/2',
          className
        )}
        {...props}
      >
        {children}
        {showCloseButton && (
          <RadixDialogClose data-slot='dialog-close' asChild>
            <Button variant='ghost' className='absolute top-4 right-4' size='icon-sm'>
              <XIcon
              />
              <span className='sr-only'>Close</span>
            </Button>
          </RadixDialogClose>
        )}
      </RadixDialogContent>
    </DialogPortal>
  )
}

export function DialogHeader({ className, ...props }: ComponentProps<'div'>) {
  return (
    <div
      data-slot='dialog-header'
      className={cn('gap-2 flex flex-col', className)}
      {...props}
    />
  )
}

export function DialogFooter({
  className,
  showCloseButton = false,
  children,
  ...props
}: ComponentProps<'div'> & {
  showCloseButton?: boolean
}) {
  return (
    <div
      data-slot='dialog-footer'
      className={cn(
        'flex flex-col-reverse gap-2 sm:flex-row sm:justify-end',
        className
      )}
      {...props}
    >
      {children}
      {showCloseButton && (
        <RadixDialogClose asChild>
          <Button variant='outline'>Close</Button>
        </RadixDialogClose>
      )}
    </div>
  )
}

export function DialogTitle({
  className,
  ...props
}: ComponentProps<typeof RadixDialogTitle>) {
  return (
    <RadixDialogTitle
      data-slot='dialog-title'
      className={cn('text-base leading-none font-medium', className)}
      {...props}
    />
  )
}

export function DialogDescription({
  className,
  ...props
}: ComponentProps<typeof RadixDialogDescription>) {
  return (
    <RadixDialogDescription
      data-slot='dialog-description'
      className={cn('text-muted-foreground *:[a]:hover:text-foreground text-sm *:[a]:underline *:[a]:underline-offset-3', className)}
      {...props}
    />
  )
}
