#!/usr/bin/env node
// verify.mjs — "The Count That Snowballed" (eskimo-words-for-snow)
//
// The claims this stratum makes are bibliographic and morphological, not numerical-
// discovery. Two things are asserted, and both are checkable against the cited record:
//
//   (A) THE INFLATION. The famous "N words for snow" is a number that grew in the
//       retelling. The page steps through a SELECTION of documented instances, in date
//       order; it is not a chain in which each teller copied the last. Only Boas (1911,
//       4 roots) has data behind the figure. Whorf (1940) implied five or more (Martin:
//       as many as five; Kaplan: five or more; Pullum: at least seven) with no data and
//       no source (Martin 1986). The popular figures (50 in a 1978 play; 100 in the NYT,
//       9 Feb 1984, credited to Whorf; 200 in 1984 TV forecasts; 400 by 1991, from a
//       would-be author) were, in Pullum's words, "just made up". The same sources also
//       record nine (1984) and about four dozen (NYT, 1988), so the record as a whole
//       does not rise steadily: (i) only the selection is in date order, and (ii) the
//       strength of its source never returns to "grounded" once lost.
//
//   (B) THE COUNT IS THE WRONG QUESTION. Because Inuit/Yup'ik are polysynthetic, the
//       answer depends entirely on the unit. True roots: 2 (West Greenlandic) or 3
//       (every other Eskimo language; Kaplan). Lexemes: Woodbury's Central Alaskan
//       Yup'ik guide lists over thirty under fifteen meanings and leaves the count to
//       the reader; he endorses about a dozen, or a couple of dozen (Pullum 1991). The
//       page shows 17 of them. Word-forms: thousands, because each noun lexeme has ~280
//       inflected forms and each verb over 1000 (Woodbury).
//
// This file restates the page's figures as constants and checks their arithmetic and
// the shape of the timeline. It does not read the sources or the page, and it checks no
// quotation; the sources are listed in sources.md. It invents no Yup'ik form: the
// lexemes are a selection from Woodbury's list, glosses after him.
// Run:  node research/eskimo-words-for-snow/verify.mjs

let pass = 0, fail = 0;
const eq = (got, want, msg) => {
  const ok = JSON.stringify(got) === JSON.stringify(want);
  console.log(`${ok ? 'ok  ' : 'FAIL'}  ${msg}${ok ? '' : `  (got ${JSON.stringify(got)}, want ${JSON.stringify(want)})`}`);
  ok ? pass++ : fail++;
};
const ok = (cond, msg) => { console.log(`${cond ? 'ok  ' : 'FAIL'}  ${msg}`); cond ? pass++ : fail++; };

// ── (A) The inflation timeline ────────────────────────────────────────────────
// status: 'grounded' (real source backs the figure) | 'print' (reached print, no
// linguistic source) | 'ghost' (no source given). srcStrength is an ordinal for
// "how well-sourced the NUMBER is": grounded 2 > print 1 > ghost 0.
// hasQuote: the page quotes the teller's own words (not just the figure).
const STRENGTH = { grounded: 2, print: 1, ghost: 0 };
const TIMELINE = [
  { yrSort: 1911, n: 4,   status: 'grounded', who: 'Boas',            hasQuote: true,  hasSource: true  },
  { yrSort: 1940, n: 5,   status: 'print',    who: 'Whorf',           hasQuote: true,  hasSource: false }, // shown as 5+
  { yrSort: 1978, n: 50,  status: 'ghost',    who: 'play',            hasQuote: false, hasSource: false }, // Wilson, The Fifth of July
  { yrSort: 1984, n: 100, status: 'print',    who: 'NYT',             hasQuote: true,  hasSource: false }, // 9 Feb 1984, credited to Whorf
  { yrSort: 1984, n: 200, status: 'ghost',    who: 'TV',              hasQuote: true,  hasSource: false }, // "two hundred words", WEWS-Cleveland
  { yrSort: 1991, n: 400, status: 'ghost',    who: 'would-be author', hasQuote: false, hasSource: false }  // reported by Pullum 1991
];
// Figures the same sources record that the page's selection leaves out.
const ALSO_DOCUMENTED = [
  { yrSort: 1984, n: 9,  who: 'trivia encyclopedia (Adams 1984; Martin 1986)' },
  { yrSort: 1988, n: 48, who: 'NYT, 9 Feb 1988: "about four dozen" (Pullum 1989)' }
];

// (i) the rungs are a selection in date order, not a doubling chain
for (let i = 1; i < TIMELINE.length; i++) {
  ok(TIMELINE[i].yrSort >= TIMELINE[i - 1].yrSort,
     `selection is in date order: ${TIMELINE[i - 1].yrSort} → ${TIMELINE[i].yrSort}`);
}
const RECORD = [...TIMELINE, ...ALSO_DOCUMENTED].sort((a, b) => a.yrSort - b.yrSort);
ok(RECORD.some((t, i) => i > 0 && t.n < RECORD[i - 1].n),
   'the documented record is not a rising chain: with nine (1984) and four dozen (1988) restored, the number falls as well as rises');
// (ii) the evidence is front-loaded: the only grounded rung is the earliest one,
// and once the figure leaves "grounded" it never returns to it. (Note: source
// strength is NOT monotone: the 1984 NYT "print" rung outranks the 1978 ghost "50",
// so we assert the true, weaker shape rather than force the data.)
const firstUngrounded = TIMELINE.findIndex(t => t.status !== 'grounded');
ok(TIMELINE.slice(0, firstUngrounded).every(t => t.status === 'grounded') &&
   TIMELINE.slice(firstUngrounded).every(t => t.status !== 'grounded'),
   'grounded evidence is front-loaded and never returns once lost');
eq(firstUngrounded, 1, 'exactly the first rung (Boas) is grounded');
// (iii) only Boas has data behind the figure; Whorf's five-or-more had none (Martin 1986)
const grounded = TIMELINE.filter(t => t.status === 'grounded');
eq(grounded.map(t => t.n), [4], 'the only grounded figure is Boas=4');
ok(TIMELINE.find(t => t.who === 'Whorf').status === 'print',
   "Whorf's five-or-more reached print with no data and no source (Martin 1986)");
ok(TIMELINE.filter(t => t.n > 4).every(t => t.hasSource === false),
   "no rung above Boas's four has a linguistic source");
ok(TIMELINE.find(t => t.n === 100).status === 'print',
   'the 100 reached print (NYT 1984) but with no linguistic source');

// ── (B) The count is the wrong question ─────────────────────────────────────────
// Boas 1911, the four roots, verbatim glosses.
const BOAS_1911 = [
  ['aput', 'snow on the ground'], ['qana', 'falling snow'],
  ['piqsirpoq', 'drifting snow'], ['qimuqsuq', 'a snowdrift']
];
eq(BOAS_1911.length, 4, 'Boas (1911) listed four snow words');

// West Greenlandic true roots (ANLC / Kaplan): 2.
const WG_ROOTS = [['qanik', 'snow in the air / snowflake'], ['aput', 'snow on the ground']];
eq(WG_ROOTS.length, 2, 'West Greenlandic has 2 true snow roots');

// Proto-Eskimo reconstructed stems (Comparative Eskimo Dictionary, via Kaplan): 3.
const PROTO = ['*qaniɣ', '*aniɣu', '*apun'];
eq(PROTO.length, 3, 'Proto-Eskimo: 3 reconstructed snow stems');

// 17 lexemes SELECTED from Woodbury's Central Alaskan Yup'ik citizen's guide (1991):
// one per meaning (the 4th field is his meaning number, 1-15), plus two synonyms for
// snow on ground (meaning 6). Glosses after Woodbury. His full list has 32 lexeme
// entries (33 if 'cellallir-, cellarrlir-' counts as two); he leaves the count "to the
// discretion of the reader", and endorses "about a dozen words (even a couple of dozen
// if you are fairly liberal about what you count)" (Pullum 1991, p. 171).
const WOODBURY = {
  'Snow particles': [
    ['qanuk', 'snowflake', 'n', 1], ['kaneq', 'frost', 'n', 2],
    ['kanevvluk', 'fine snow/rain particles', 'n', 3], ['natquik', 'drifting snow/etc.', 'n', 4],
    ['nevluk', 'clinging debris/lint/snow/dirt', 'n', 5]
  ],
  'Fallen snow': [
    ['aniu', 'snow on ground', 'n', 6], ['apun', 'snow on ground', 'n', 6],
    ['qanikcaq', 'snow on ground', 'n', 6], ['muruaneq', 'soft deep snow', 'n', 7],
    ['qetrar-', 'for snow to crust', 'v', 8], ['nutaryuk', 'fresh snow', 'n', 9],
    ['qanisqineq', 'snow floating on water', 'n', 10]
  ],
  'Snow formations': [
    ['qengaruk', 'snow bank', 'n', 11], ['utvak', 'snow carved in block', 'n', 12],
    ['navcaq', 'snow cornice, snow (formation) about to collapse', 'n', 13]
  ],
  'Meteorological events': [
    ['pirta', 'blizzard, snowstorm', 'n', 14], ['cellallir-', 'to snow heavily', 'v', 15]
  ]
};
const WOODBURY_MEANINGS = 15;   // "I have counted fifteen of them"
const WOODBURY_LIST = 32;       // lexeme entries in his full list (see above)
const flat = Object.values(WOODBURY).flat();
eq(flat.length, 17, "17 lexemes selected from Woodbury's list");
eq(new Set(flat.map(x => x[3])).size, WOODBURY_MEANINGS,
   "the selection covers each of Woodbury's fifteen meanings once, plus two synonyms (meaning 6)");
ok(flat.length < WOODBURY_LIST,
   "17 is the page's selection, not Woodbury's count: his list has over thirty lexemes");
ok(flat.length >= 12 && flat.length <= 24,
   'the 17 shown fall within the dozen to couple of dozen Woodbury endorses (Pullum 1991)');
const nouns = flat.filter(x => x[2] === 'n').length;
const verbs = flat.filter(x => x[2] === 'v').length;
eq([nouns, verbs], [15, 2], '15 nouns + 2 verbs among the lexemes');
// the verbs are exactly the hyphen-final stems (Woodbury's verb-stem notation)
ok(flat.filter(x => x[2] === 'v').every(x => x[0].endsWith('-')),
   "verb lexemes are written with a trailing hyphen (qetrar-, cellallir-)");

// The "how many words?" answer spans three radically different units.
const FORMS = nouns * 280 + verbs * 1000; // Woodbury's stated multipliers → a lower bound
eq(FORMS, 15 * 280 + 2 * 1000, 'word-form lower bound = 15×280 + 2×1000');
eq(FORMS, 6200, 'word-form lower bound computes to 6,200');
ok(PROTO.length < flat.length && flat.length < FORMS,
   'the count strictly grows by unit: roots (2 or 3) < lexemes shown (17) < word-forms (6,200+)');
ok(FORMS / WG_ROOTS.length > 1000,
   'word-forms exceed true roots by >1000× — "how many words" has no single answer');

// ── summary ─────────────────────────────────────────────────────────────────────
console.log(`\n${fail === 0 ? 'PASS' : 'FAIL'} — ${pass} ok, ${fail} failed`);
process.exit(fail === 0 ? 0 : 1);
