// limelight-nav.jsx — Ported from limelight-nav.tsx
// TypeScript types removed. No lucide-react — inline SVGs.
// CSS tokens use portfolio vars (--accent, --text, --card, --border).

// ─── Default icons ─────────────────────────────────────────────────────────
const _IcoHome = (p) => <svg {...p} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="m3 9 9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/></svg>;
const _IcoCpu  = (p) => <svg {...p} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><rect x="4" y="4" width="16" height="16" rx="2"/><rect x="9" y="9" width="6" height="6"/><path d="M15 2v2M9 2v2M2 15h2M2 9h2M22 15h-2M22 9h-2M15 22v-2M9 22v-2"/></svg>;
const _IcoPkg  = (p) => <svg {...p} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/><polyline points="3.27 6.96 12 12.01 20.73 6.96"/><line x1="12" y1="22.08" x2="12" y2="12"/></svg>;
const _IcoFlow = (p) => <svg {...p} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="22 12 18 12 15 21 9 3 6 12 2 12"/></svg>;
const _IcoCode = (p) => <svg {...p} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><polyline points="16 18 22 12 16 6"/><polyline points="8 6 2 12 8 18"/></svg>;
const _IcoUser = (p) => <svg {...p} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>;

// ─── LimelightNav ───────────────────────────────────────────────────────────
const LimelightNav = ({
  items,
  defaultActiveIndex = 0,
  onTabChange,
  style = {},
  className = '',
  limelightColor,   // override accent color for the limelight bar
}) => {
  const [activeIndex, setActiveIndex] = React.useState(defaultActiveIndex);
  const [isReady,     setIsReady]     = React.useState(false);
  const navItemRefs  = React.useRef([]);
  const limelightRef = React.useRef(null);

  // Resolve limelight colour: prop > CSS var --accent > fallback blue
  const llColor = limelightColor || 'var(--accent, #4F8CFF)';

  React.useLayoutEffect(() => {
    if (!items || items.length === 0) return;
    const ll   = limelightRef.current;
    const item = navItemRefs.current[activeIndex];
    if (ll && item) {
      const newLeft = item.offsetLeft + item.offsetWidth / 2 - ll.offsetWidth / 2;
      ll.style.left = `${newLeft}px`;
      if (!isReady) setTimeout(() => setIsReady(true), 50);
    }
  }, [activeIndex, isReady, items]);

  if (!items || items.length === 0) return null;

  const handleClick = (index, itemOnClick) => {
    setActiveIndex(index);
    onTabChange?.(index);
    itemOnClick?.();
  };

  return (
    <nav
      className={className}
      style={{
        position: 'relative',
        display: 'inline-flex',
        alignItems: 'center',
        height: '44px',
        borderRadius: '10px',
        background: 'color-mix(in oklab, var(--card, #111) 80%, transparent)',
        border: '1px solid color-mix(in oklab, var(--border, #333) 60%, transparent)',
        backdropFilter: 'blur(12px)',
        WebkitBackdropFilter: 'blur(12px)',
        padding: '0 4px',
        ...style,
      }}
    >
      {items.map(({ id, icon, label, onClick }, index) => (
        <a
          key={id}
          ref={el => (navItemRefs.current[index] = el)}
          title={label}
          aria-label={label}
          style={{
            position: 'relative',
            zIndex: 20,
            display: 'flex',
            alignItems: 'center',
            justifyContent: 'center',
            height: '100%',
            padding: '0 14px',
            cursor: 'pointer',
            gap: '6px',
            textDecoration: 'none',
            color: 'var(--text)',
            fontSize: '0.78rem',
            fontWeight: 500,
            whiteSpace: 'nowrap',
          }}
          onClick={() => handleClick(index, onClick)}
        >
          {React.cloneElement(icon, {
            style: {
              width: 16, height: 16, flexShrink: 0,
              transition: 'opacity 100ms ease',
              opacity: activeIndex === index ? 1 : 0.38,
              ...(icon.props.style || {}),
            },
          })}
          <span style={{
            opacity: activeIndex === index ? 1 : 0.5,
            transition: 'opacity 150ms ease',
          }}>{label}</span>
        </a>
      ))}

      {/* Limelight bar */}
      <div
        ref={limelightRef}
        style={{
          position: 'absolute',
          top: 0,
          zIndex: 10,
          width: '44px',
          height: '3px',
          borderRadius: '0 0 4px 4px',
          background: llColor,
          boxShadow: `0 40px 18px color-mix(in oklab, ${llColor === 'var(--accent, #4F8CFF)' ? 'var(--accent)' : llColor}, transparent 55%)`,
          transition: isReady ? 'left 300ms cubic-bezier(0.4,0,0.2,1)' : 'none',
          left: '-999px',
        }}
      >
        {/* Cone glow */}
        <div style={{
          position: 'absolute',
          left: '-30%', top: '3px',
          width: '160%', height: '40px',
          clipPath: 'polygon(5% 100%, 25% 0, 75% 0, 95% 100%)',
          background: `linear-gradient(to bottom, color-mix(in oklab, ${llColor === 'var(--accent, #4F8CFF)' ? 'var(--accent)' : llColor}, transparent 65%), transparent)`,
          pointerEvents: 'none',
        }} />
      </div>
    </nav>
  );
};

window.LimelightNav = LimelightNav;
