// Shared sub-nav rendered under the topbar on both loss-curve pages.
// Four anchors:
//   1) Καμπύλες απώλειας ανά εποχή  (in-page #chart)
//   2) Per-epoch metrics              (in-page #per-epoch)
//   3) Per-class metrics              (in-page #per-class)
//   4) Επιστροφή στο Dashboard        (external -> index.html)
//
// Active state is updated immediately on click for instant visual feedback,
// and also by scroll position for natural feel while scrolling.
const { useEffect: lossUseEffect, useState: lossUseState } = React;

function LossSubNav({ accentColor }) {
  const [active, setActive] = lossUseState('chart');

  lossUseEffect(() => {
    const onScroll = () => {
      const ids = ['chart', 'per-epoch', 'per-class'];
      let current = ids[0];
      for (const id of ids) {
        const el = document.getElementById(id);
        if (!el) continue;
        // 140px offset accounts for sticky topbar + sub-nav height
        if (el.getBoundingClientRect().top - 140 <= 0) current = id;
      }
      setActive(current);
    };
    window.addEventListener('scroll', onScroll, { passive: true });
    onScroll();
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  const items = [
  { id: 'chart', label: 'Καμπύλες απώλειας ανά εποχή', internal: true },
  { id: 'per-epoch', label: 'Per-epoch metrics', internal: true },
  { id: 'per-class', label: 'Per-class metrics', internal: true },
  { id: 'back', label: 'Επιστροφή στο Dashboard',
    href: 'index.html', internal: false, isBack: true }];


  // Handle click on internal anchor: update active state immediately AND
  // smooth-scroll to the target element with the proper offset for the
  // sticky topbar + subnav (~140px combined).
  const handleInternalClick = (id) => (e) => {
    e.preventDefault();
    setActive(id);
    const el = document.getElementById(id);
    if (el) {
      const y = el.getBoundingClientRect().top + window.pageYOffset - 140;
      window.scrollTo({ top: y, behavior: 'smooth' });
      // Update the URL hash without triggering a jump
      if (history.replaceState) history.replaceState(null, '', '#' + id);
    }
  };

  return (
    <nav className="subnav" aria-label="Section navigation">
      <div className="subnav-inner">
        {items.map((it, i) => {
          const isActive = it.internal && active === it.id;
          const extraClass =
            (isActive ? ' active' : '') +
            (it.internal ? '' : ' subnav-external') +
            (it.isBack ? ' subnav-back' : '');
          const styleObj = isActive
            ? { '--accent': accentColor }
            : it.isBack
              ? { fontWeight: 600, letterSpacing: '0.01em' }
              : {};
          return (
            <a key={it.id}
            className={'subnav-item' + extraClass}
            href={it.internal ? '#' + it.id : it.href}
            onClick={it.internal ? handleInternalClick(it.id) : undefined}
            style={styleObj}>
              <span className="subnav-num mono">{String(i + 1).padStart(2, '0')}</span>
              <span className="subnav-label">{it.label}</span>
              {!it.internal && <span className="subnav-ext-icon">{it.isBack ? '↩' : '↗'}</span>}
            </a>);

        })}
      </div>
    </nav>);

}

window.LossSubNav = LossSubNav;
