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

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
}) {
  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>
  );
}
