// mariusson — theme (light / dark)
//
// The actual palette flip happens via CSS variables in tokens.css —
// dark/light overrides keyed off `<html data-theme="dark|light">`.
// This module just exposes a hook + setter so React components can
// flip the attribute and we persist the choice.

(() => {
  const KEY = 'mariusson:theme';

  function readTheme() {
    try { return localStorage.getItem(KEY) || 'light'; }
    catch { return 'light'; }
  }
  function writeTheme(t) {
    try { localStorage.setItem(KEY, t); } catch {}
    document.documentElement.setAttribute('data-theme', t);
    window.dispatchEvent(new CustomEvent('an-theme-changed', { detail: t }));
  }

  function useTheme() {
    const [theme, setTheme] = React.useState(() => readTheme());
    React.useEffect(() => {
      const onChange = (e) => setTheme(e.detail || readTheme());
      const onStorage = (e) => { if (e.key === KEY) setTheme(readTheme()); };
      window.addEventListener('an-theme-changed', onChange);
      window.addEventListener('storage', onStorage);
      return () => {
        window.removeEventListener('an-theme-changed', onChange);
        window.removeEventListener('storage', onStorage);
      };
    }, []);
    return {
      theme,
      setTheme: (t) => writeTheme(t),
      toggle: () => writeTheme(theme === 'dark' ? 'light' : 'dark'),
    };
  }

  // Compact pill button — sits in the chrome bar. Sun/moon glyph swaps.
  function ThemeToggle({ style = {} }) {
    const { theme, toggle } = useTheme();
    const isDark = theme === 'dark';
    return (
      <button
        onClick={toggle}
        title={isDark ? 'Switch to light theme' : 'Switch to dark theme'}
        aria-label={isDark ? 'Switch to light theme' : 'Switch to dark theme'}
        style={{
          pointerEvents: 'auto',
          width: 28, height: 28, padding: 0,
          background: 'transparent',
          border: '1px solid var(--ink)',
          color: 'var(--ink)',
          cursor: 'pointer',
          display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
          transition: 'background 0.2s var(--ease-out), transform 0.25s var(--ease-out)',
          ...style,
        }}
        onMouseEnter={(e) => { e.currentTarget.style.transform = 'rotate(15deg)'; }}
        onMouseLeave={(e) => { e.currentTarget.style.transform = 'rotate(0)'; }}>
        {isDark ? (
          // sun
          <svg width="14" height="14" viewBox="0 0 14 14" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round">
            <circle cx="7" cy="7" r="2.6" />
            <line x1="7" y1="0.6" x2="7" y2="2.4" />
            <line x1="7" y1="11.6" x2="7" y2="13.4" />
            <line x1="0.6" y1="7" x2="2.4" y2="7" />
            <line x1="11.6" y1="7" x2="13.4" y2="7" />
            <line x1="2.5" y1="2.5" x2="3.7" y2="3.7" />
            <line x1="10.3" y1="10.3" x2="11.5" y2="11.5" />
            <line x1="2.5" y1="11.5" x2="3.7" y2="10.3" />
            <line x1="10.3" y1="3.7" x2="11.5" y2="2.5" />
          </svg>
        ) : (
          // moon
          <svg width="13" height="13" viewBox="0 0 13 13" fill="currentColor">
            <path d="M11.4 7.6a4.7 4.7 0 1 1-6-6c.3-.1.5.2.4.5a3.7 3.7 0 0 0 5.1 5.1c.3-.1.6.1.5.4z" />
          </svg>
        )}
      </button>
    );
  }

  window.useTheme = useTheme;
  window.ThemeToggle = ThemeToggle;
})();
