import * as React from 'react';
import { Slot } from '@radix-ui/react-slot';
import { cva, type VariantProps } from 'class-variance-authority';
import { cn } from '../../lib/utils';

const buttonVariants = cva('shButton', {
  variants: {
    variant: {
      default: 'shButtonDefault',
      secondary: 'shButtonSecondary',
      outline: 'shButtonOutline',
      ghost: 'shButtonGhost',
      destructive: 'shButtonDestructive',
    },
    size: {
      xs: 'shButtonXs',
      sm: 'shButtonSm',
      md: 'shButtonMd',
      default: 'shButtonMd',
      lg: 'shButtonLg',
      xl: 'shButtonXl',
      icon: 'shButtonIcon',
    },
  },
  defaultVariants: {
    variant: 'default',
    size: 'md',
  },
});

export interface ButtonProps
  extends React.ButtonHTMLAttributes<HTMLButtonElement>,
    VariantProps<typeof buttonVariants> {
  asChild?: boolean;
}

export const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
  ({ className, variant, size, asChild = false, ...props }, ref) => {
    const Comp = asChild ? Slot : 'button';
    return <Comp className={cn(buttonVariants({ variant, size, className }))} ref={ref} {...props} />;
  },
);

Button.displayName = 'Button';
