import * as React from 'react'
import { cva, type VariantProps } from 'class-variance-authority'
import { cn } from '@/lib/utils'

/**
 * IconButton — square button с иконкой. Заменяет {@code @nstart/ui} IconButton.
 * Variants: default (ghost), accent (primary fill), danger (destructive).
 */
const iconButtonVariants = cva(
  [
    'inline-flex items-center justify-center rounded-md transition-colors',
    'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1',
    'disabled:pointer-events-none disabled:opacity-50',
  ].join(' '),
  {
    variants: {
      variant: {
        default: 'text-ink-2 hover:text-ink hover:bg-surface-2',
        accent: 'bg-accent text-on-accent hover:opacity-90',
        danger: 'text-pink hover:bg-pink-bg',
      },
      size: {
        sm: 'h-6 w-6 [&_svg]:size-3.5',
        md: 'h-8 w-8 [&_svg]:size-4',
        lg: 'h-10 w-10 [&_svg]:size-5',
      },
    },
    defaultVariants: { variant: 'default', size: 'md' },
  },
)

export type IconButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement> &
  VariantProps<typeof iconButtonVariants> & {
    /** Required для accessibility — иконки без текста должны иметь label. */
    label: string
    icon: React.ReactNode
  }

export const IconButton = React.forwardRef<HTMLButtonElement, IconButtonProps>(
  function IconButton({ className, variant, size, label, icon, ...props }, ref) {
    return (
      <button
        ref={ref}
        type="button"
        aria-label={label}
        title={label}
        className={cn(iconButtonVariants({ variant, size }), className)}
        {...props}
      >
        {icon}
      </button>
    )
  },
)
