/** @jsx React.createElement */
/* Sidebar — replicates the real allianceboard nav.
   Flat list, white text + light outline icons on slate-navy,
   thin dividers between items, white left-edge bar + bold label on active. */

const SIDEBAR_ITEMS = [
  { name: 'Dashboard',                icon: 'gauge'     },
  { name: 'Reports',                  icon: 'report'    },
  { name: 'Calendar',                 icon: 'calendar',   dividerBefore: true },
  { name: 'Organizations & Contacts', icon: 'address'   },
  { name: 'Documents',                icon: 'doc'       },
  { name: 'Opportunities',            icon: 'bulb',       dividerBefore: true },
  { name: 'Evaluations',              icon: 'clipboard' },
  { name: 'Contracts & Assets',       icon: 'contract'  },
  { name: 'Partners & Projects',      icon: 'network'   },
  { name: 'Account',                  icon: 'user',       dividerBefore: true },
  { name: 'Settings',                 icon: 'gear'      },
  { name: 'Help & Resources',         icon: 'help'      },
];

const CHROME_NAVY = '#3F507B';

function SidebarIcon({ name }) {
  const sw = 1.5;
  const common = {
    width: 18, height: 18, viewBox: '0 0 24 24',
    fill: 'none', stroke: 'currentColor', strokeWidth: sw,
    strokeLinecap: 'round', strokeLinejoin: 'round',
  };
  const paths = {
    gauge: (
      <svg {...common}>
        <path d="M3 14a9 9 0 1 1 18 0"/>
        <path d="M3 14h2M19 14h2M12 5v2"/>
        <path d="M12 14l4.5-4.5"/>
        <circle cx="12" cy="14" r="1.2" fill="currentColor" stroke="none"/>
      </svg>
    ),
    report: (
      <svg {...common}>
        <path d="M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z"/>
        <path d="M14 3v5h5"/>
        <path d="M8.5 17v-3M11.5 17v-5M14.5 17v-2"/>
      </svg>
    ),
    calendar: (
      <svg {...common}>
        <rect x="3.5" y="5" width="17" height="15.5" rx="1.5"/>
        <path d="M3.5 9.5h17M8 3.5v3M16 3.5v3"/>
      </svg>
    ),
    address: (
      <svg {...common}>
        <rect x="5" y="3.5" width="14" height="17" rx="1.5"/>
        <path d="M3 7h2M3 12h2M3 17h2"/>
        <circle cx="12" cy="10.5" r="2.2"/>
        <path d="M8.5 16.5c.4-1.6 1.8-2.6 3.5-2.6s3.1 1 3.5 2.6"/>
      </svg>
    ),
    doc: (
      <svg {...common}>
        <path d="M8 3h6l5 5v11a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z"/>
        <path d="M14 3v5h5"/>
        <path d="M9 13h6M9 16.5h5"/>
      </svg>
    ),
    bulb: (
      <svg {...common}>
        <path d="M9.5 18.5h5"/>
        <path d="M10.5 21h3"/>
        <path d="M12 3.5a5.5 5.5 0 0 0-3.5 9.8c.7.7 1 1.6 1 2.5v1.7h5v-1.7c0-.9.3-1.8 1-2.5A5.5 5.5 0 0 0 12 3.5z"/>
      </svg>
    ),
    clipboard: (
      <svg {...common}>
        <rect x="6" y="4.5" width="12" height="16" rx="1.5"/>
        <rect x="9" y="2.5" width="6" height="3.5" rx="1"/>
        <path d="M9 12.5l1.6 1.6L14 10.5"/>
        <path d="M9 17h5"/>
      </svg>
    ),
    contract: (
      <svg {...common}>
        <path d="M8 3h6l5 5v11a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z"/>
        <path d="M14 3v5h5"/>
        <path d="M9 16c.7-.9 1.4-.9 2.1 0s1.4.9 2.1 0 1.4-.9 2.1 0"/>
      </svg>
    ),
    network: (
      <svg {...common}>
        <rect x="2.5" y="9.5" width="5" height="5" rx="0.5"/>
        <rect x="16.5" y="2.5" width="5" height="5" rx="0.5"/>
        <rect x="16.5" y="16.5" width="5" height="5" rx="0.5"/>
        <path d="M7.5 12h4M11.5 5v14M11.5 5h5M11.5 19h5"/>
      </svg>
    ),
    tools: null,
    user: (
      <svg {...common}>
        <circle cx="12" cy="12" r="9"/>
        <circle cx="12" cy="10" r="3"/>
        <path d="M6 19a6.5 6.5 0 0 1 12 0"/>
      </svg>
    ),
    gear: (
      <svg {...common}>
        <circle cx="12" cy="12" r="2.5"/>
        <path d="M19.4 14.6a1.5 1.5 0 0 0 .3 1.7l.1.1a1.8 1.8 0 1 1-2.5 2.5l-.1-.1a1.5 1.5 0 0 0-1.7-.3 1.5 1.5 0 0 0-.9 1.4V20a1.8 1.8 0 0 1-3.6 0v-.1a1.5 1.5 0 0 0-1-1.4 1.5 1.5 0 0 0-1.6.3l-.1.1a1.8 1.8 0 1 1-2.5-2.5l.1-.1a1.5 1.5 0 0 0 .3-1.7 1.5 1.5 0 0 0-1.4-.9H4.6a1.8 1.8 0 0 1 0-3.6h.1a1.5 1.5 0 0 0 1.4-.9 1.5 1.5 0 0 0-.3-1.7l-.1-.1a1.8 1.8 0 1 1 2.5-2.5l.1.1a1.5 1.5 0 0 0 1.7.3h0a1.5 1.5 0 0 0 .9-1.4V4.6a1.8 1.8 0 0 1 3.6 0v.1a1.5 1.5 0 0 0 .9 1.4 1.5 1.5 0 0 0 1.7-.3l.1-.1a1.8 1.8 0 1 1 2.5 2.5l-.1.1a1.5 1.5 0 0 0-.3 1.7v0a1.5 1.5 0 0 0 1.4.9H20a1.8 1.8 0 0 1 0 3.6h-.1a1.5 1.5 0 0 0-1.4.9z"/>
      </svg>
    ),
    help: (
      <svg {...common}>
        <circle cx="12" cy="12" r="9"/>
        <path d="M9.5 9.5a2.5 2.5 0 0 1 4.9.6c0 1.6-2.4 1.8-2.4 3.5"/>
        <circle cx="12" cy="17" r=".7" fill="currentColor" stroke="none"/>
      </svg>
    ),
  };
  return paths[name];
}

function Sidebar({ active, onNavigate, onCollapse, collapsed = false }) {
  const W = collapsed ? 64 : 240;
  return (
    <aside style={{
      width: W,
      background: CHROME_NAVY,
      color: '#fff',
      display: 'flex', flexDirection: 'column', flexShrink: 0,
      transition: 'width 200ms var(--ab-ease)',
    }}>
      {/* Collapse / expand toggle row */}
      <div style={{ display: 'flex', justifyContent: collapsed ? 'center' : 'flex-end', padding: '6px 14px 2px' }}>
        <button
          onClick={onCollapse}
          aria-label={collapsed ? 'Expand sidebar' : 'Collapse sidebar'}
          title={collapsed ? 'Expand sidebar' : 'Collapse sidebar'}
          style={{
            width: 26, height: 26, border: 'none', background: 'transparent',
            color: 'rgba(255,255,255,.85)', borderRadius: 4, cursor: 'pointer',
            display: 'flex', alignItems: 'center', justifyContent: 'center',
          }}
          onMouseEnter={(e) => e.currentTarget.style.background = 'rgba(255,255,255,.08)'}
          onMouseLeave={(e) => e.currentTarget.style.background = 'transparent'}
        >
          {collapsed ? (
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
              <path d="M13 6l6 6-6 6M19 12H7M4 5v14"/>
            </svg>
          ) : (
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
              <path d="M11 6l-6 6 6 6M5 12h12M20 5v14"/>
            </svg>
          )}
        </button>
      </div>

      <nav style={{
        flex: 1,
        display: 'flex', flexDirection: 'column',
        overflow: 'auto hidden',
      }}>
        {SIDEBAR_ITEMS.map((item) => {
          const isActive = active === item.name;
          return (
            <React.Fragment key={item.name}>
              {item.dividerBefore && (
                <div style={{
                  height: 1, margin: collapsed ? '8px 14px' : '8px 16px',
                  background: 'rgba(255,255,255,.18)',
                }} />
              )}
              <button
                onClick={() => onNavigate?.(item.name)}
                title={collapsed ? item.name : undefined}
                style={{
                  position: 'relative',
                  display: 'flex', alignItems: 'center',
                  gap: collapsed ? 0 : 14,
                  justifyContent: collapsed ? 'center' : 'flex-start',
                  width: '100%', padding: collapsed ? '12px 0' : '11px 18px 11px 22px',
                  border: 'none',
                  background: isActive ? '#fff' : 'transparent',
                  color: isActive ? CHROME_NAVY : '#fff',
                  fontFamily: 'inherit', fontSize: 14,
                  fontWeight: isActive ? 600 : 400,
                  cursor: 'pointer',
                  textAlign: 'left',
                  transition: 'background 160ms var(--ab-ease), color 160ms var(--ab-ease)',
                }}
                onMouseEnter={(e) => { if (!isActive) e.currentTarget.style.background = 'rgba(255,255,255,.06)'; }}
                onMouseLeave={(e) => { if (!isActive) e.currentTarget.style.background = 'transparent'; }}
              >
                {isActive && (
                  <span style={{
                    position: 'absolute', left: 0, top: 0, bottom: 0, width: collapsed ? 4 : 6,
                    background: CHROME_NAVY,
                  }} />
                )}
                <span style={{ display: 'inline-flex', width: 18, height: 18, flexShrink: 0 }}>
                  {item.icon && <SidebarIcon name={item.icon} />}
                </span>
                {!collapsed && <span>{item.name}</span>}
              </button>
            </React.Fragment>
          );
        })}
      </nav>
    </aside>
  );
}

Object.assign(window, { Sidebar });
