import React from 'react';
import { Icon } from './icon.js';

/**
 * Square icon-only button; label is mandatory (becomes aria-label + title).
 */
export interface IconButtonProps {
  /** Lucide icon name */
  icon: string;
  /** Required accessible label (also the tooltip) */
  label: string;
  /** @default 'ghost' */
  variant?: 'ghost' | 'outline';
  /** @default 'md' */
  size?: 'sm' | 'md';
  disabled?: boolean;
  onClick?: (e: React.MouseEvent) => void;
  style?: React.CSSProperties;
}

const CSS = `
.ct-iconbtn {
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: var(--radius-2);
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink-2);
  cursor: pointer;
  transition: background var(--duration-fast) var(--ease-out), color var(--duration-fast) var(--ease-out);
}
.ct-iconbtn:hover:not(:disabled) { background: var(--paper-2); color: var(--ink-1); }
.ct-iconbtn:active:not(:disabled) { background: var(--paper-3); }
.ct-iconbtn:disabled { opacity: 0.45; cursor: not-allowed; }
.ct-iconbtn--md { width: 34px; height: 34px; }
.ct-iconbtn--sm { width: 28px; height: 28px; }
.ct-iconbtn--outline { border-color: var(--line-2); background: var(--surface-card); }
.ct-iconbtn--outline:hover:not(:disabled) { background: var(--paper-2); }
`;
(function inject() {
  if (typeof document !== 'undefined' && !document.getElementById('ct-iconbtn-css')) {
    const s = document.createElement('style'); s.id = 'ct-iconbtn-css'; s.textContent = CSS;
    document.head.appendChild(s);
  }
})();

export function IconButton({
  icon,
  label,
  variant = 'ghost',
  size = 'md',
  disabled = false,
  style,
  ...rest
}: IconButtonProps) {
  return (
    <button
      type="button"
      className={`ct-iconbtn ct-iconbtn--${size}${variant === 'outline' ? ' ct-iconbtn--outline' : ''}`}
      aria-label={label}
      title={label}
      disabled={disabled}
      style={style}
      {...rest}
    >
      <Icon name={icon} size={size === 'sm' ? 15 : 17} />
    </button>
  );
}
