#!/usr/bin/env node
// verify.mjs — the acrostic of Lamentations, checked letter by letter.
//
// Source of the Hebrew: Sefaria's "Miqra According to the Masorah" (MAM),
// a digital edition based on the Aleppo Codex and related manuscripts
// (CC-BY-SA). Fetched per-chapter into ./raw/ch{1..5}.json; the first word of
// each verse and its initial consonant were extracted by ./extract.mjs into
// ./first-letters.json. This script makes every acrostic claim on the page
// re-runnable: it never trusts a prose assertion, only the initial letters.
//
//   node verify.mjs
//
import fs from 'node:fs';

const data = JSON.parse(
  fs.readFileSync(new URL('./first-letters.json', import.meta.url)),
);

// The standard order of the 22 Hebrew consonants.
const STD = [
  'א','ב','ג','ד','ה','ו','ז','ח','ט','י','כ','ל',
  'מ','נ','ס','ע','פ','צ','ק','ר','ש','ת',
];
const NAME = {
  'א':'aleph','ב':'bet','ג':'gimel','ד':'dalet','ה':'he','ו':'vav',
  'ז':'zayin','ח':'het','ט':'tet','י':'yod','כ':'kaf','ל':'lamed',
  'מ':'mem','נ':'nun','ס':'samekh','ע':'ayin','פ':'pe','צ':'tsadi',
  'ק':'qof','ר':'resh','ש':'shin','ת':'tav',
};
// The variant order attested in some early abecedaries: pe before ayin.
const VARIANT = STD.map((l) => (l === 'ע' ? 'פ' : l === 'פ' ? 'ע' : l));

const initials = (ch) => data[ch].map((v) => v.letter);

let pass = 0, fail = 0;
const T = (name, cond, detail = '') => {
  (cond ? (pass++) : (fail++));
  console.log(`  ${cond ? 'PASS' : 'FAIL'}  ${name}${detail ? '  — ' + detail : ''}`);
};

const seq = (arr) => arr.map((l) => NAME[l]).join(' ');

console.log('\nLAMENTATIONS — the acrostic, verified against the Aleppo-based text\n');

// ── Chapter 1: a clean acrostic in the STANDARD order ──────────────────────
console.log('Chapter 1 — single acrostic, 22 verses, standard order (ayin → pe)');
{
  const ini = initials(1);
  T('22 verses', ini.length === 22, `got ${ini.length}`);
  T('initials follow the standard alphabet aleph→tav',
    ini.join('') === STD.join(''));
  T('ayin (v16) stands BEFORE pe (v17) — the normal order',
    ini[15] === 'ע' && ini[16] === 'פ',
    `v16=${NAME[ini[15]]}, v17=${NAME[ini[16]]}`);
}

// ── Chapters 2 & 4: standard, EXCEPT pe before ayin ────────────────────────
for (const ch of [2, 4]) {
  console.log(`\nChapter ${ch} — single acrostic, 22 verses, VARIANT order (pe → ayin)`);
  const ini = initials(ch);
  T('22 verses', ini.length === 22, `got ${ini.length}`);
  T('does NOT match the standard order', ini.join('') !== STD.join(''));
  T('DOES match the pe-before-ayin variant', ini.join('') === VARIANT.join(''));
  T('pe (v16) stands BEFORE ayin (v17) — the inversion',
    ini[15] === 'פ' && ini[16] === 'ע',
    `v16=${NAME[ini[15]]}, v17=${NAME[ini[16]]}`);
}

// ── Chapter 3: TRIPLE acrostic, 66 verses, three per letter, variant order ─
console.log('\nChapter 3 — triple acrostic, 66 verses (3 per letter), VARIANT order');
{
  const ini = initials(3);
  T('66 verses', ini.length === 66, `got ${ini.length}`);
  const triples = [];
  let ok = true;
  for (let i = 0; i < 22; i++) {
    const a = ini[3 * i], b = ini[3 * i + 1], c = ini[3 * i + 2];
    triples.push(a);
    if (!(a === b && b === c)) ok = false;
  }
  T('each consecutive triple of verses shares one initial letter', ok);
  T('the 22 triples spell the pe-before-ayin variant',
    triples.join('') === VARIANT.join(''));
  T('the pe triple (vv46–48) precedes the ayin triple (vv49–51)',
    ini[45] === 'פ' && ini[48] === 'ע',
    `v46=${NAME[ini[45]]}, v49=${NAME[ini[48]]}`);
}

// ── Chapter 5: 22 verses, but the acrostic is ABANDONED ────────────────────
console.log('\nChapter 5 — 22 verses (the alphabet’s count) but NO acrostic');
{
  const ini = initials(5);
  T('22 verses — matches the alphabet’s length', ini.length === 22);
  T('initials do NOT follow the standard order', ini.join('') !== STD.join(''));
  T('initials do NOT follow the variant order either', ini.join('') !== VARIANT.join(''));
  console.log('        actual initials: ' + seq(ini));
}

// ── English cannot carry it: KJV chapter 1 openings show no pattern ─────────
console.log('\nEnglish (KJV) chapter 1 — the acrostic is simply gone');
{
  const kjv = JSON.parse(
    fs.readFileSync(new URL('./raw/kjv-lam1.json', import.meta.url)),
  );
  const inits = kjv.verses.slice(0, 22).map((v) => v.text.trim()[0].toUpperCase());
  T('source is the King James Version',
    /King James/i.test(kjv.translation_name || ''));
  // An English acrostic would need 22 *distinct* initials A,B,C,…; the KJV repeats.
  const distinct = new Set(inits).size;
  T('the 22 English openings are NOT 22 distinct ordered initials',
    distinct < 22, `only ${distinct} distinct first letters: ${inits.join('')}`);
}

console.log(`\n${pass}/${pass + fail} checks passed.\n`);
process.exit(fail ? 1 : 0);
