import * as React from 'react'
import { cva, type VariantProps } from 'class-variance-authority'

import { cn } from '@/lib/utils'

const alertVariants = cva(
  'relative w-full rounded-[var(--ds-radius-m)] border px-[var(--ds-space-4x)] py-[var(--ds-space-3x)] grid has-[>svg]:grid-cols-[calc(var(--ds-space-4x))_1fr] grid-cols-[0_1fr] has-[>svg]:gap-x-[var(--ds-space-3x)] gap-y-[var(--ds-space-05x)] items-start [&>svg]:size-4 [&>svg]:translate-y-0.5 [&>svg]:text-current ds-type-global-xs',
  {
    variants: {
      variant: {
        default:
          'border-[var(--ds-neutral-200)] bg-white text-[var(--ds-text-neutral-default)]',
        destructive:
          'border-[var(--ds-error-700)] bg-[var(--ds-error-100)] text-[var(--ds-error-700)] [&>svg]:text-current *:data-[slot=alert-description]:text-[var(--ds-error-700)]',
      },
    },
    defaultVariants: {
      variant: 'default',
    },
  },
)

function Alert({
  className,
  variant,
  ...props
}: React.ComponentProps<'div'> & VariantProps<typeof alertVariants>) {
  return (
    <div
      data-slot="alert"
      role="alert"
      className={cn(alertVariants({ variant }), className)}
      {...props}
    />
  )
}

function AlertTitle({ className, ...props }: React.ComponentProps<'div'>) {
  return (
    <div
      data-slot="alert-title"
      className={cn(
        'col-start-2 line-clamp-1 min-h-4 ds-type-semibold tracking-[var(--ds-letter-spacing)] text-[var(--ds-text-neutral-default)]',
        className,
      )}
      {...props}
    />
  )
}

function AlertDescription({
  className,
  ...props
}: React.ComponentProps<'div'>) {
  return (
    <div
      data-slot="alert-description"
      className={cn(
        'col-start-2 grid justify-items-start gap-[var(--ds-space-1x)] text-[var(--ds-text-neutral-lighter)] [&_p]:leading-relaxed',
        className,
      )}
      {...props}
    />
  )
}

export { Alert, AlertTitle, AlertDescription }
