'use client'

import type { ComponentProps } from 'react'
import { Drawer as VaulDrawer } from 'vaul'

import { cn } from '@/shared/lib/utils'

export function Drawer({
  ...props
}: ComponentProps<typeof VaulDrawer.Root>) {
  return <VaulDrawer.Root data-slot='drawer' {...props} />
}

export function DrawerTrigger({
  ...props
}: ComponentProps<typeof VaulDrawer.Trigger>) {
  return <VaulDrawer.Trigger data-slot='drawer-trigger' {...props} />
}

export function DrawerPortal({
  ...props
}: ComponentProps<typeof VaulDrawer.Portal>) {
  return <VaulDrawer.Portal data-slot='drawer-portal' {...props} />
}

export function DrawerClose({
  ...props
}: ComponentProps<typeof VaulDrawer.Close>) {
  return <VaulDrawer.Close data-slot='drawer-close' {...props} />
}

export function DrawerOverlay({
  className,
  ...props
}: ComponentProps<typeof VaulDrawer.Overlay>) {
  return (
    <VaulDrawer.Overlay
      data-slot='drawer-overlay'
      className={cn('data-open:animate-in data-closed:animate-out data-closed:fade-out-0 data-open:fade-in-0 bg-black/80 supports-backdrop-filter:backdrop-blur-xs fixed inset-0 z-50', className)}
      {...props}
    />
  )
}

export function DrawerContent({
  className,
  children,
  ...props
}: ComponentProps<typeof VaulDrawer.Content>) {
  return (
    <DrawerPortal data-slot='drawer-portal'>
      <DrawerOverlay />
      <VaulDrawer.Content
        data-slot='drawer-content'
        className={cn(
          'before:bg-background flex h-auto flex-col bg-transparent p-4 text-sm before:absolute before:inset-2 before:-z-10 before:rounded-4xl data-[vaul-drawer-direction=bottom]:inset-x-0 data-[vaul-drawer-direction=bottom]:bottom-0 data-[vaul-drawer-direction=bottom]:mt-24 data-[vaul-drawer-direction=bottom]:max-h-[80vh] data-[vaul-drawer-direction=left]:inset-y-0 data-[vaul-drawer-direction=left]:left-0 data-[vaul-drawer-direction=left]:w-3/4 data-[vaul-drawer-direction=right]:inset-y-0 data-[vaul-drawer-direction=right]:right-0 data-[vaul-drawer-direction=right]:w-3/4 data-[vaul-drawer-direction=top]:inset-x-0 data-[vaul-drawer-direction=top]:top-0 data-[vaul-drawer-direction=top]:mb-24 data-[vaul-drawer-direction=top]:max-h-[80vh] data-[vaul-drawer-direction=left]:sm:max-w-sm data-[vaul-drawer-direction=right]:sm:max-w-sm group/drawer-content fixed z-50',
          className
        )}
        {...props}
      >
        <div className='mt-4 h-1.5 w-25 rounded-full group-data-[vaul-drawer-direction=bottom]/drawer-content:block bg-muted mx-auto hidden shrink-0' />
        {children}
      </VaulDrawer.Content>
    </DrawerPortal>
  )
}

export function DrawerHeader({ className, ...props }: ComponentProps<'div'>) {
  return (
    <div
      data-slot='drawer-header'
      className={cn('gap-0.5 p-4 group-data-[vaul-drawer-direction=bottom]/drawer-content:text-center group-data-[vaul-drawer-direction=top]/drawer-content:text-center md:gap-1.5 md:text-left flex flex-col', className)}
      {...props}
    />
  )
}

export function DrawerFooter({ className, ...props }: ComponentProps<'div'>) {
  return (
    <div
      data-slot='drawer-footer'
      className={cn('gap-2 p-4 mt-auto flex flex-col', className)}
      {...props}
    />
  )
}

export function DrawerTitle({
  className,
  ...props
}: ComponentProps<typeof VaulDrawer.Title>) {
  return (
    <VaulDrawer.Title
      data-slot='drawer-title'
      className={cn('text-foreground text-base font-medium', className)}
      {...props}
    />
  )
}

export function DrawerDescription({
  className,
  ...props
}: ComponentProps<typeof VaulDrawer.Description>) {
  return (
    <VaulDrawer.Description
      data-slot='drawer-description'
      className={cn('text-muted-foreground text-sm', className)}
      {...props}
    />
  )
}
