// ThemeToggle.jsx — tri-state theme cycler
// States: 'dark' → 'light' → 'auto' → 'dark' ...
// Shows the *preference* icon (not the resolved effective theme) so
// 'auto' has its own glyph and the user sees what they picked.
function ThemeToggle({ theme, onCycle, compact }) {
  const next = { dark: "light", light: "auto", auto: "dark" }[theme];
  const label = {
    dark: "Theme: dark · click for light",
    light: "Theme: light · click for auto",
    auto: "Theme: auto (follows OS) · click for dark",
  }[theme];

  // Icons — moon / sun / half-circle
  const icon = {
    dark: (
      <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
        <path d="M13 9.5A5.5 5.5 0 1 1 6.5 3a4.5 4.5 0 0 0 6.5 6.5z" />
      </svg>
    ),
    light: (
      <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
        <circle cx="8" cy="8" r="3" />
        <path d="M8 1.5v1.5M8 13v1.5M1.5 8h1.5M13 8h1.5M3.3 3.3l1.1 1.1M11.6 11.6l1.1 1.1M3.3 12.7l1.1-1.1M11.6 4.4l1.1-1.1" />
      </svg>
    ),
    auto: (
      <svg width="14" height="14" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
        <circle cx="8" cy="8" r="5.5" />
        <path d="M8 2.5 A 5.5 5.5 0 0 1 8 13.5 Z" fill="currentColor" stroke="none" />
      </svg>
    ),
  }[theme];

  const sizing = compact
    ? "h-7 w-7 rounded-md"
    : "h-8 px-2.5 gap-1.5 rounded-lg";

  return (
    <button
      type="button"
      onClick={onCycle}
      title={label}
      aria-label={label}
      className={`${sizing} inline-flex items-center justify-center border border-[color:var(--line-strong)] hover:border-[color:var(--accent-soft,var(--accent-2))] hover:bg-white/[0.04] transition-colors text-[color:var(--ink-dim)] hover:text-[color:var(--ink)] no-print`}>
      {icon}
      {!compact && (
        <span className="font-mono text-[10px] uppercase tracking-[0.22em]">{theme}</span>
      )}
    </button>
  );
}
window.ThemeToggle = ThemeToggle;
