#!/usr/bin/env node
// verify.mjs — the check behind "The Ham That Isn't There" (the -burger libfix).
//
// The page makes exactly one *mechanical* inference over a table of dated,
// sourced facts, and this script re-runs it so the page can't show a claim its
// own data doesn't support. It does NOT prove the history — the history is
// carried by the cited sources (etymonline, Michael Quinion / World Wide Words,
// Arnold Zwicky). What it proves is that the morphology and the ordering the
// page draws are exactly what the dated record says.
//
//   node research/hamburger-libfix/verify.mjs
//
// Sources with verbatim quotes: research/hamburger-libfix/sources.md

let checks = 0, fails = 0;
function ok(cond, msg) {
  checks++;
  if (!cond) { fails++; console.log('  ✗ ' + msg); }
  else console.log('  ✓ ' + msg);
}

// ── The demonym paradigm hamburger belongs to ──────────────────────────────
// A German (or Austrian) place-name + the demonymic -er, reused as a food name.
// "Hamburger" is not "ham + burger"; it is "Hamburg + er", exactly like these.
const DEMONYMS = [
  { root: 'Hamburg',        demonym: 'Hamburger',   food: 'the beef patty' },
  { root: 'Frankfurt',      demonym: 'Frankfurter', food: 'the sausage / hot dog' },
  { root: 'Wien',           demonym: 'Wiener',      food: 'the Wiener sausage & schnitzel' }, // Wien = Vienna
  { root: 'Berlin',         demonym: 'Berliner',    food: 'the jam doughnut' },
];

console.log('\n[1] The demonym cut — every one of these is City + -er (no "ham"):');
for (const d of DEMONYMS) {
  ok(d.root + 'er' === d.demonym, `${d.root} + er = ${d.demonym}`);
}
// The historically correct cut of "hamburger" falls after the full city name,
// at index 7 ("Hamburg|er"), NOT at index 3 ("ham|burger").
ok('hamburger'.slice(0, 7) === 'hamburg', '"hamburger" begins with the whole city name "hamburg" (index 0–6)');
ok('hamburger'.slice(7) === 'er', 'the residue is the demonym suffix "-er" (index 7–8)');
ok('hamburger'.slice(0, 3) === 'ham', 'the folk cut "ham|..." takes the first 3 letters of "Hamburg" — a coincidence, not a morpheme');

// ── The reanalysis, and the splinter it liberated ─────────────────────────
// Americans reheard Hamburg|er as ham|burger (Quinion), freeing "-burger" as a
// productive combining form. The productive splinter is "-burger" (the last 6
// letters), which every coinage below carries.
const REANALYSIS_ONSET = 1930; // "-burger" goes productive; "lamburger" Sep 1930 (Quinion)

// Dated -burger coinages (year, source). Years from Quinion's "The Mighty
// Burger" unless marked etymonline.
const BURGERS = [
  { pre: 'lamb',    form: 'lamburger',     year: 1930, src: 'Quinion' },
  { pre: 'cheese',  form: 'cheeseburger',  year: 1936, src: 'Quinion' }, // 1938 as a dictionary lemma (etymonline)
  { pre: 'beef',    form: 'beefburger',    year: 1936, src: 'Quinion' }, // 1940 (etymonline)
  { pre: 'fish',    form: 'fishburger',    year: 1937, src: 'Quinion' },
  { pre: 'steak',   form: 'steakburger',   year: 1937, src: 'Quinion' },
  { pre: 'chicken', form: 'chickenburger', year: 1937, src: 'Quinion' },
  { pre: 'turtle',  form: 'turtleburger',  year: 1940, src: 'Quinion' },
  { pre: 'oyster',  form: 'oysterburger',  year: 1941, src: 'Quinion' },
  { pre: 'ocean',   form: 'oceanburger',   year: 1941, src: 'Quinion' },
  { pre: 'sea',     form: 'seaburger',     year: 1941, src: 'Quinion' },
  { pre: 'octo',    form: 'octoburger',    year: 1942, src: 'Quinion' },
  { pre: 'nut',     form: 'nutburger',     year: 1945, src: 'Quinion' },
  { pre: 'turkey',  form: 'turkeyburger',  year: 1945, src: 'Quinion' },
  { pre: 'egg',     form: 'eggburger',     year: 1947, src: 'Quinion' },
  { pre: 'pork',    form: 'porkburger',    year: 1947, src: 'Quinion' },
  { pre: 'moose',   form: 'mooseburger',   year: 1953, src: 'Quinion' },
  { pre: 'gator',   form: 'gatorburger',   year: 1959, src: 'Quinion' },
  { pre: 'vegi',    form: 'vegiburger',    year: 1967, src: 'Quinion' },
];

console.log('\n[2] The productive splinter — every coinage ends in "-burger":');
for (const b of BURGERS) {
  ok(b.form.endsWith('burger'), `${b.pre} → ${b.form} (${b.year})`);
}

console.log('\n[3] The ordering the timeline draws:');
// The demonym / dish senses are all attested by 1909; the productive burst all
// falls on or after ~1930. The reanalysis sits between the two.
const DEMONYM_ERA = [1610, 1767, 1774, 1880, 1902, 1909]; // etymonline word-form dates
ok(Math.max(...DEMONYM_ERA) <= 1909, 'every demonym/dish word-form is attested by 1909');
ok(Math.min(...BURGERS.map(b => b.year)) === REANALYSIS_ONSET,
   `the earliest productive coinage is ${REANALYSIS_ONSET} (lamburger)`);
ok(Math.min(...BURGERS.map(b => b.year)) > 1909,
   'every -burger coinage postdates the "hamburger" sandwich word (1909)');
ok(1610 < 1880 && 1880 < 1909 && 1909 < REANALYSIS_ONSET && REANALYSIS_ONSET < 1936,
   'chain holds: demonym (1610) → hamburg steak (1880) → the sandwich (1909) → -burger productive (1930) → cheeseburger (1936)');

// ── The claim the page will NOT make ──────────────────────────────────────
console.log('\n[4] Honesty guards (things we deliberately do NOT assert):');
ok(true, 'we do NOT claim a documented reason the beef dish carries the city name — etymonline: "no certain connection has ever been put forth"');
ok(true, 'we do NOT assert the Genghis-Khan/steak-tartare origin as fact — it is unsupported folklore, absent from the etymological record');
ok(true, 'we do NOT publish a total count of -burger words — no authority gives one; it is an open, productive set');
ok(true, 'dates are lexicographic first-attestations of the WORD (etymonline / Quinion), not of the dish, and not read from OED directly (paywalled)');

console.log(`\n${fails === 0 ? 'PASS' : 'FAIL'} — ${checks - fails}/${checks} checks green\n`);
process.exit(fails === 0 ? 0 : 1);
