'use client'

import * as SheetPrimitive from '@radix-ui/react-dialog'
import { cva, type VariantProps } from 'class-variance-authority'
import type * as React from 'react'
import { cn } from '../../utils'
import { typographyVariants } from '../Typography'

const Sheet = SheetPrimitive.Root
Sheet.displayName = 'Sheet'

const SheetTrigger = SheetPrimitive.Trigger
SheetTrigger.displayName = 'SheetTrigger'

const SheetClose = SheetPrimitive.Close
SheetClose.displayName = 'SheetClose'

const SheetPortal = SheetPrimitive.Portal
SheetPortal.displayName = 'SheetPortal'

type SheetOverlayProps = React.ComponentPropsWithoutRef<
  typeof SheetPrimitive.Overlay
> & {
  ref?: React.Ref<React.ElementRef<typeof SheetPrimitive.Overlay>>
}

const SheetOverlay = ({ className, ref, ...props }: SheetOverlayProps) => (
  <SheetPrimitive.Overlay
    className={cn(
      'fixed inset-0 z-[16] bg-black/80 data-[state=open]:backdrop-blur-sm data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',
      className,
    )}
    {...props}
    ref={ref}
  />
)
SheetOverlay.displayName = 'SheetOverlay'

const sheetVariants = cva(
  'fixed z-[16] bg-background shadow-lg transition ease-in-out data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:duration-300 data-[state=open]:duration-500',
  {
    variants: {
      side: {
        top: 'inset-x-0 top-0 border-b data-[state=closed]:slide-out-to-top data-[state=open]:slide-in-from-top',
        bottom:
          'inset-x-0 bottom-0 border-t data-[state=closed]:slide-out-to-bottom data-[state=open]:slide-in-from-bottom',
        left: 'inset-y-0 left-0 h-full w-3/4 border-r data-[state=closed]:slide-out-to-left data-[state=open]:slide-in-from-left sm:max-w-sm',
        right:
          'inset-y-0 right-0 h-full w-3/4  border-l data-[state=closed]:slide-out-to-right data-[state=open]:slide-in-from-right sm:max-w-sm',
      },
    },
    defaultVariants: {
      side: 'right',
    },
  },
)

interface SheetContentProps
  extends React.ComponentPropsWithoutRef<typeof SheetPrimitive.Content>,
    VariantProps<typeof sheetVariants> {
  ref?: React.Ref<React.ElementRef<typeof SheetPrimitive.Content>>
}

const SheetContent = ({
  side = 'right',
  className,
  children,
  ref,
  ...props
}: SheetContentProps) => (
  <SheetPortal>
    <SheetOverlay />

    <SheetPrimitive.Content
      ref={ref}
      className={cn(sheetVariants({ side }), className)}
      {...props}
    >
      {children}
    </SheetPrimitive.Content>
  </SheetPortal>
)
SheetContent.displayName = 'SheetContent'

const SheetHeader = ({
  className,
  ...props
}: React.HTMLAttributes<HTMLDivElement>) => (
  <div
    className={cn(
      typographyVariants({ variant: 'h5' }),
      'flex flex-col space-y-2 px-6 py-4 text-center sm:text-left',
      className,
    )}
    {...props}
  />
)
SheetHeader.displayName = 'SheetHeader'

const SheetFooter = ({
  className,
  ...props
}: React.HTMLAttributes<HTMLDivElement>) => (
  <div
    className={cn(
      'flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2',
      'border-0 border-t border-solid border-border p-6',
      className,
    )}
    {...props}
  />
)
SheetFooter.displayName = 'SheetFooter'

type SheetTitleProps = React.ComponentPropsWithoutRef<
  typeof SheetPrimitive.Title
> & {
  ref?: React.Ref<React.ElementRef<typeof SheetPrimitive.Title>>
}

const SheetTitle = ({ className, ref, ...props }: SheetTitleProps) => (
  <SheetPrimitive.Title
    data-slot="sheet-title"
    ref={ref}
    className={cn(
      typographyVariants({ variant: 'h5' }),
      'items-center, m-0',
      className,
    )}
    {...props}
  />
)
SheetTitle.displayName = 'SheetTitle'

type SheetDescriptionProps = React.ComponentPropsWithoutRef<
  typeof SheetPrimitive.Description
> & {
  ref?: React.Ref<React.ElementRef<typeof SheetPrimitive.Description>>
}

const SheetDescription = ({
  className,
  ref,
  ...props
}: SheetDescriptionProps) => (
  <SheetPrimitive.Description
    ref={ref}
    className={cn('text-sm text-muted-foreground', className)}
    {...props}
  />
)
SheetDescription.displayName = 'SheetDescription'

const SheetComponents = {
  Root: Sheet,
  Portal: SheetPortal,
  Overlay: SheetOverlay,
  Trigger: SheetTrigger,
  Close: SheetClose,
  Content: SheetContent,
  Header: SheetHeader,
  Footer: SheetFooter,
  Title: SheetTitle,
  Description: SheetDescription,
}

export {
  Sheet,
  SheetPortal,
  SheetOverlay,
  SheetTrigger,
  SheetClose,
  SheetContent,
  SheetHeader,
  SheetFooter,
  SheetTitle,
  SheetDescription,
  SheetComponents,
}
