// SidebarV2 — v1 sidebar with:
//  - renamed tradition labels (Preterocanonical, Zadokite Settlement)
//  - Study Tools demoted into a "Specialized tools" subsection
//  - generic user (Jordan Reed — Reader)
function AmosSidebarV2({ active='library-all', onNav, onOpenCollection, onOpenLanguageModal }) {
  // Every tradition now routes to its own collection page.
  const COLLECTION_PAGE_KEYS = ['patristic','parabiblical','greek','hebrew','latin','qumran','vernacular'];
  const I = window.useI18n();
  const { TRADITIONS } = window.LIBRARY_DATA;

  // Label overrides — reader-intent voice. The scholarly category names
  // (Pre-Nicene Fathers, Preterocanonical Library, etc.) live in the corpus
  // metadata; the sidebar surfaces them in everyday English.
  const traditionLabelOverrides = {
    patristic:    { name: 'Early church voices' },
    parabiblical: { name: "The authors' library" },
    greek:        { name: 'New Testament (Greek)' },
    hebrew:       { name: 'Hebrew Bible' },
    latin:        { name: 'Greek & Latin Old Testament' },
    qumran:       { name: 'Dead Sea Scrolls' },
    vernacular:   { name: 'Modern translations' },
  };

  // 'home' removed — it routed to the same view as 'All Documents' (audit rank 6).
  const navTop = [
    { key:'library-all', labelKey:'nav.library', icon:'library' },
    { key:'testament',   labelKey:'nav.testament', icon:'book' },
    { key:'recent',      labelKey:'nav.recent',  icon:'book' },
    { key:'saved',       labelKey:'nav.saved',   icon:'bookmark' },
    { key:'teachings',   labelKey:'nav.teachings', icon:'feather' },
    { key:'about',       labelKey:'nav.about',   icon:'globe' },
  ];
  // Search is live (routes to the semantic-search page). The other four tools
  // have no destination yet, so they are honestly marked "Soon" and disabled
  // rather than silently dead-ending on the library home (audit rank 6).
  const specialized = [
    { key:'search',  labelKey:'nav.search', icon:'search' },
    // Owner-facing: the parked decisions/queues of the reception apparatus.
    // Only meaningful with the review API alive; harmless (self-explains)
    // without. Hidden entirely in frozen public builds (AMOS_STATIC) — the
    // owner-only endpoint is never frozen, so the page would be an empty shell.
    ...(window.AMOS_STATIC ? [] : [{ key:'reviewdesk', label:'Review Desk', icon:'cite' }]),
    { key:'cite',    labelKey:'nav.cite',   icon:'cite',    soon:true },
    { key:'lex',     labelKey:'nav.lex',    icon:'hash',    soon:true },
    { key:'var',     labelKey:'nav.var',    icon:'layers',  soon:true },
    { key:'trans',   labelKey:'nav.trans',  icon:'sparkle', soon:true },
  ];

  const Item = ({k, label, icon, count, extraClass, soon}) =>
    React.createElement('button', {
      type: 'button',
      className: `amos-item ${active===k?'active':''} ${extraClass||''} ${soon?'soon':''}`,
      'aria-current': active===k ? 'page' : undefined,
      'aria-disabled': soon ? 'true' : undefined,
      disabled: soon || undefined,
      onClick: soon ? undefined : (()=>onNav && onNav(k))
    },
      icon && window.I[icon](15),
      React.createElement('span', { style:{flex:1} }, label),
      soon && React.createElement('span', { className:'amos-soon' }, 'Soon'),
      count != null && React.createElement('span', { className:'count' }, count.toLocaleString())
    );

  return React.createElement('aside', { className:'amos-sidebar' },
    React.createElement('div', { className:'amos-brand' },
      React.createElement('div', { className:'amos-brand-mark' }, 'AP'),
      React.createElement('div', { className:'amos-brand-word' },
        'The Amos Project',
        React.createElement('small', null,
          'by ',
          React.createElement('a', {
            href: 'https://worldmission.media',
            target: '_blank',
            rel: 'noopener noreferrer',
            className: 'amos-brand-link'
          }, 'WorldMission.Media')
        )
      )
    ),
    React.createElement('div', { className:'amos-search' },
      // Wired (audit rank 6): the box + ⌘K now open the semantic-search page
      // instead of being decorative. It's an entry point, so it routes on
      // focus/click/Enter rather than capturing text here.
      React.createElement('div', {
        className:'input', role:'button', tabIndex:0,
        title:'Search the corpus (⌘K)',
        onClick:()=>onNav && onNav('search'),
        onKeyDown:(e)=>{ if(e.key==='Enter'||e.key===' '){ e.preventDefault(); onNav && onNav('search'); } }
      },
        window.I.search(14),
        React.createElement('input', {
          placeholder:'Search the corpus…', readOnly:true, tabIndex:-1,
          onFocus:()=>onNav && onNav('search')
        }),
        React.createElement('kbd', null, '⌘K')
      )
    ),
    React.createElement('div', { className:'amos-sec' }, I.t('nav.library_sec')),
    ...navTop.map(n=>React.createElement(Item, { key:n.key, k:n.key, label:I.t(n.labelKey), icon:n.icon })),

    React.createElement('div', { className:'amos-sec' }, I.t('nav.corpus')),
    ...TRADITIONS.map(t => {
      const override = traditionLabelOverrides[t.key] || {};
      const hasPage = COLLECTION_PAGE_KEYS.includes(t.key);
      return React.createElement('button', {
        type: 'button',
        key: t.key,
        className: `amos-item ${active===`trad-${t.key}`?'active':''}`,
        'aria-current': active===`trad-${t.key}` ? 'page' : undefined,
        onClick: () => {
          if (hasPage && onOpenCollection) onOpenCollection(t.key);
          else onNav && onNav(`trad-${t.key}`);
        },
        title: hasPage ? 'Open collection page' : undefined
      },
        window.I[({
          patristic:'feather', parabiblical:'scroll', greek:'book', hebrew:'book',
          latin:'amphora', qumran:'fragments', vernacular:'globe'
        })[t.key]](15),
        React.createElement('span', { style:{flex:1} }, override.name || t.name.replace(/ · .*/,'')),
        React.createElement('span', { className:'count' }, t.docs.toLocaleString())
      );
    }),

    React.createElement('div', { className:'amos-sec specialized' }, I.t('nav.specialized')),
    ...specialized.map(n => React.createElement(Item, {
      key: n.key, k: n.key, label: n.label || I.t(n.labelKey), icon: n.icon, extraClass: 'specialized', soon: n.soon
    })),

    // Language — reachable from the drawer at every width (the topbar pill
    // is hidden on phones for space; this is the phone-friendly entry point)
    onOpenLanguageModal && React.createElement('button', {
      type: 'button', className: 'amos-item specialized',
      onClick: () => onOpenLanguageModal()
    },
      window.I.globe(15),
      React.createElement('span', { style:{flex:1} }, I.t('nav.language')),
      React.createElement('span', { className:'count' }, (I.getLang() || 'en').toUpperCase())
    ),

    React.createElement('div', { className:'amos-user' },
      React.createElement('div', { className:'avatar' }, 'JR'),
      React.createElement('div', null,
        React.createElement('div', { className:'name' }, 'Jordan Reed'),
        React.createElement('div', { className:'role' }, I.t('topbar.reader'))
      )
    )
  );
}
Object.assign(window, { AmosSidebarV2 });
