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

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

const badgeVariants = cva(
  'inline-flex items-center justify-center rounded-full border border-transparent px-2 py-0.5 text-xs font-medium w-fit whitespace-nowrap shrink-0 [&>svg]:size-3 gap-1 [&>svg]:pointer-events-none focus-visible:ring-2 focus-visible:ring-[var(--bl-focus-ring)] focus-visible:ring-offset-2 aria-invalid:ring-[var(--bl-danger)] aria-invalid:border-[var(--bl-danger)] transition-[color,box-shadow] overflow-hidden',
  {
    variants: {
      variant: {
        default:
          'bg-[var(--bl-accent)] text-[var(--bl-accent-foreground)] [a&]:hover:bg-[var(--bl-accent)]/90',
        secondary:
          'bg-[var(--bl-surface-muted)] text-[var(--bl-text-primary)] [a&]:hover:bg-[var(--bl-surface-muted)]/90',
        destructive:
          'bg-[var(--bl-danger)] text-[var(--bl-danger-foreground)] [a&]:hover:bg-[var(--bl-danger)]/90 focus-visible:ring-[var(--bl-danger)]/20 dark:focus-visible:ring-[var(--bl-danger)]/40 dark:bg-[var(--bl-danger)]/60',
        outline:
          'border-[var(--bl-border)] text-[var(--bl-text-primary)] [a&]:hover:bg-[var(--bl-surface-muted)] [a&]:hover:text-[var(--bl-text-primary)]',
        ghost: '[a&]:hover:bg-[var(--bl-surface-muted)] [a&]:hover:text-[var(--bl-text-primary)]',
        link: 'text-[var(--bl-accent)] underline-offset-4 [a&]:hover:underline',
      },
    },
    defaultVariants: {
      variant: 'default',
    },
  }
);

function Badge({
  className,
  variant = 'default',
  asChild = false,
  ...props
}: React.ComponentProps<'span'> & VariantProps<typeof badgeVariants> & { asChild?: boolean }) {
  const Comp = asChild ? Slot.Root : 'span';

  return (
    <Comp
      data-slot="badge"
      data-variant={variant}
      className={cn(badgeVariants({ variant }), className)}
      {...props}
    />
  );
}

export { Badge, badgeVariants };
