// DocCardV2 — wraps the v1 DocCard with a seal badge row, book-level
// availability badge (derived from doc.langs + current UI language),
// and a whole-book commission affordance.
function DocCardV2({ d, onOpen, seals, onCommissionBook }) {
  const I = window.useI18n();
  const uiLang = I.getLang();
  const langObj = I.getLangObj();
  const nativeName = langObj && langObj.native ? langObj.native : uiLang.toUpperCase();

  // `d.langs` is an array of ISO2-ish codes (e.g. ['en','es','ca']).
  // Compute availability state for the CURRENT UI language.
  const codes = (d.langs || []).map(c => String(c).toLowerCase());
  const hasExact = codes.includes(uiLang.toLowerCase());
  // Explicit partial marker wins: doc can declare { partialLangs: { ca: 'Odes 11–12 only' } }
  const partialLangs = d.partialLangs || {};
  const partialNote = partialLangs[uiLang.toLowerCase()] || null;
  // Heuristic partial: if the doc lives in a close cousin (es↔pt, ca↔es)
  // call it 'partial'; otherwise 'missing'.
  const cousins = { ca:['es'], es:['ca','pt'], pt:['es'], en:[], fr:[], de:[], zh:[], hi:[], bn:[], ar:[], sw:[], am:[], ru:[] };
  const partial = !!partialNote || (!hasExact && (cousins[uiLang] || []).some(c => codes.includes(c)));
  const state = partialNote ? 'partial' : (hasExact ? 'has' : partial ? 'partial' : 'missing');
  const labelKey = 'book.availability.' + state;
  const label = partialNote
    ? I.tf('book.availability.partial', { lang: nativeName }) + ' · ' + partialNote
    : I.tf(labelKey, { lang: nativeName });

  return React.createElement('div', { className: 'amos-doc-v2-wrap' },
    React.createElement(window.DocCard, { d, onOpen }),
    window.ShareIconButton && React.createElement(window.ShareIconButton, {
      refFor: () => ({
        kind:'work',
        title: d.title,
        subtitle: d.author && d.author !== 'Anonymous' ? d.author : (d.dates || ''),
        edition: 'Amos Project ' + (d.collection || 'Library'),
        editor: d.author && d.author !== 'Anonymous' ? d.author : 'World Mission Media',
        year: 2026,
        url: 'https://worldmission.media/doc/' + d.id,
        description: d.collection || '',
        collectionKey: d.tradition || null
      })
    }),
    React.createElement('div', { className: 'amos-avail-badge ' + state },
      React.createElement('span', { className:'sw' }),
      label
    ),
    seals && seals.length > 0 && React.createElement('div', { className: 'amos-doc-seals' },
      seals.map(s => React.createElement(window.SealBadge, { key: s, code: s, sm: true }))
    ),
    state !== 'has' && React.createElement('div', { className: 'amos-doc-actions' },
      React.createElement('button', {
        className: 'btn-commission-book',
        onClick: (e) => {
          e.stopPropagation();
          // Synthesize a whole-book ref. Prefer d.bookId if data has one,
          // else use the doc id as the book key (pericope data will fall through).
          const bookId = d.bookId || d.id;
          const bookName = d.title;
          onCommissionBook && onCommissionBook({
            whole: true,
            book: bookId,
            ref: bookName,
            targetLang: uiLang
          });
        }
      }, '+ ', I.t('commission.whole_book'))
    )
  );
}

function DocListRowV2({ d, onOpen, seals }) {
  const colorMap = {
    patristic:'#b8443a', parabiblical:'#8b6d9e', greek:'#5b7aa8', hebrew:'#7a9579',
    latin:'#d4a24c', qumran:'#8a7b58', vernacular:'#26828F'
  };
  const I = window.useI18n();
  const uiLang = I.getLang();
  const codes = (d.langs || []).map(c => String(c).toLowerCase());
  const hasExact = codes.includes(uiLang.toLowerCase());
  return React.createElement('div', { className:'amos-list-row', onClick: () => onOpen && onOpen(d) },
    React.createElement('div', { className:'spine', style:{ background: colorMap[d.tradition] } }, d.glyph || '·'),
    React.createElement('div', null,
      React.createElement('div', { className:'title' }, d.title),
      d.author && React.createElement('div', { className:'by' }, d.author + ' · ' + (d.dates||''))
    ),
    React.createElement('div', { className:'muted' }, d.collection),
    React.createElement('div', { className:'muted', style:{textAlign:'right'} }, d.words),
    React.createElement('div', null,
      d.langs.slice(0,4).map(l => React.createElement('span', {
        key:l,
        className:'lang-pip' + (l.toLowerCase() === uiLang.toLowerCase() ? ' current' : '')
      }, l)),
      !hasExact && React.createElement('span', {
        className:'lang-pip missing',
        title: I.tf('book.availability.missing', { lang: I.getLangObj().native })
      }, '+ ' + uiLang.toUpperCase())
    ),
    React.createElement('div', null,
      seals && seals.length > 0
        ? seals.map(s => React.createElement(window.SealBadge, { key: s, code: s, sm: true }))
        : React.createElement('span', { style: { color:'rgba(6,31,39,.35)', font:'400 11px Roboto' } }, '—')
    ),
    React.createElement('div', { style:{ textAlign:'right' } },
      React.createElement('button', {
        className:'amos-icon-btn',
        onClick:(e)=>{ e.stopPropagation(); onOpen && onOpen(d); }
      }, window.I.open(15))
    )
  );
}

Object.assign(window, { DocCardV2, DocListRowV2 });
