// engine.mjs: the radif detector. The page runs it on any ghazal you fetch from
// Ganjoor; research/the-word-that-comes-back/measure.mjs runs the same file over
// all 60,059 ghazals; the verifier holds it against an independent Python
// implementation and against planted faults.
//
// The rule it measures is the ghazal's own. Both half-lines of the opening
// couplet (the matla) and the second half-line of every later couplet end the
// same way: a rhyme (qafia), then, very often, one or more words repeated
// exactly (the radif). So the radif is the longest run of whole words that all
// of those "rhyme lines" end with, with at least one rhyming letter still shared
// in front of it. Everything below is that sentence, plus the one hard part:
// deciding what a whole word is, when Persian editions space the same words
// three different ways.

// Vocalisation marks (harakat, tanwin, shadda, sukun, superscript alef, the
// madda of آ, the small hamza of the ezafe "هٔ" and of ئ أ ؤ, Quranic marks)
// and tatweel. Not letters. The text is decomposed (NFD) first, so آ becomes
// ا + madda and loses the madda: a rhyme on "-ā" is one rhyme however it is written.
const MARKS = /[\u064B-\u065F\u0670\u06D6-\u06ED\u0640]/g;
// Punctuation, Persian and Latin, plus the invisible bidi controls editions leave in.
const PUNCT = /[\u060C\u061B\u061F\u066A-\u066D\u06D4!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~\u00AB\u00BB\u2018-\u201F\u2026\u200E\u200F\u202A-\u202E]/g;
const FOLD = { 'ي': 'ی', 'ى': 'ی', 'ك': 'ک', 'ۀ': 'ه', 'ە': 'ه', 'أ': 'ا', 'إ': 'ا', 'ٱ': 'ا', 'ؤ': 'و', '\u00A0': ' ' };
const FOLD_RE = new RegExp('[' + Object.keys(FOLD).join('') + ']', 'g');
const ZWNJ = '\u200C';

// Suffixes that classical prosody counts inside the rhyme, never as a radif:
// plural -hā, the personal and pronominal endings, comparative -tar, the yā,
// and the clitic -st of ast. A radif may not begin with one of these, however
// the edition spaces it.
export const BOUND = new Set('ها های هایی ام ات اش مان تان شان ایم اید اند یم ید ند ی ای یی تر ترین گان ان ست'.split(' '));

// A joined-up word may hide a boundary (بر + گرفت, a preverb; مینا + شکست, a
// typesetter's slip). The letters in front count as a word if they occur as a
// separate word at least LEX_MIN times in the whole corpus.
export const LEX_MIN = 50;
// The verbal prefixes that editions write joined (برگرفت, درآمد, سرگرفتی,
// فروداری); بر, در and سر are, by a count over the whole corpus, the three that
// most often hide a refrain's first letter inside one written word. Any
// other hidden boundary needs at least three letters in front, because a short
// word hides by accident inside a longer one: یاد آری is not یا + داری.
export const PREVERBS = new Set(['بر', 'در', 'سر', 'فرو', 'فرا', 'وا', 'باز', 'ور']);

export function norm(t) {
  t = String(t ?? '').normalize('NFD').replace(MARKS, '').normalize('NFC').replace(FOLD_RE, (c) => FOLD[c]);
  t = t.replace(PUNCT, ' ');
  t = t.replace(/\u200C+/g, ZWNJ).replace(/\s*\u200C\s*/g, ZWNJ);
  return stripEdge(t, ' ' + ZWNJ);
}

// Python's str.strip(chars) for a set of characters.
function stripEdge(t, chars) {
  let a = 0, b = t.length;
  while (a < b && chars.includes(t[a])) a++;
  while (b > a && chars.includes(t[b - 1])) b--;
  return t.slice(a, b);
}

// Python's str.split() with no argument: split on runs of whitespace, drop empties.
const WS = /[\s\u001C-\u001F\u0085]+/;
function splitWS(t) { return t.split(WS).filter(Boolean); }

// mode 'S': words are what the edition separates with spaces (ZWNJ joins).
// mode 'Z': a ZWNJ separates too.
export function words(t, mode) {
  t = norm(t);
  if (mode === 'Z') t = t.split(ZWNJ).join(' ');
  return splitWS(t).map((w) => stripEdge(w, ZWNJ)).filter(Boolean);
}

// The rhyme lines of a ghazal given as [[right, left], [right, left], ...]:
// both halves of the first couplet, then the left half of every other.
export function rhymeLines(couplets) {
  return [couplets[0][0], ...couplets.map((c) => c[1])];
}

// Word-level refrain as the edition writes it: the longest run of identical
// final words, never the whole line.
export function wordRadif(lines, mode) {
  const L = lines.map((x) => words(x, mode));
  if (L.some((l) => l.length < 2)) return null;
  const shortest = Math.min(...L.map((l) => l.length));
  let n = 0;
  while (n < shortest - 1) {
    const w = L[0][L[0].length - 1 - n];
    if (L.every((l) => l[l.length - 1 - n] === w)) n++; else break;
  }
  return { n, radif: n ? L[1].slice(-n).join(' ') : '' };
}

function lettersAndBounds(t) {
  const raw = t;
  const toks = words(t, 'Z');
  const s = toks.join('');
  const starts = new Set();
  let pos = 0;
  for (const w of toks) { starts.add(s.length - pos); pos += w.length; }
  return { s, starts, toks, raw };
}

// The separator-blind refrain (the page's main rule). Compare letters only.
// The refrain is the longest common letter-suffix that (a) leaves at least one
// shared letter in front of it for the rhyme, (b) begins a word in every line
// that has it, either at a written boundary or after letters that are
// themselves a word of the corpus, (c) does not begin with a bound suffix or a
// lone letter (و, "and", excepted), and whose first word is a word of the
// corpus. tol = how many rhyme lines may disagree (0 strict, 1 tolerant).
export function letterRadif(lines, lex, tol = 0) {
  const L = lines.map(lettersAndBounds);
  const n = L.length;
  const count = (w) => (lex instanceof Map ? lex.get(w) : lex[w]) || 0;
  let best = { j: 0, radif: '' };
  const refs = tol === 0 ? [1] : [0, 1, 2];
  for (const ri of refs) {
    if (ri >= n) continue;
    const ref = L[ri].s;
    let k = 0;
    for (let j = 1; j < ref.length; j++) {
      const suf = ref.slice(-j);
      const miss = L.filter((x) => !(x.s.length > j && x.s.endsWith(suf))).length;
      if (miss <= tol) k = j; else break;
    }
    for (let j = k - 1; j > 0; j--) {
      if (j <= best.j) break;
      const suf = ref.slice(-j);
      const agree = L.filter((x) => x.s.length > j && x.s.endsWith(suf));
      if (agree.length < n - tol) continue;
      const aligned = (x) => {
        if (x.starts.has(j)) return true;
        let acc = 0;
        for (let i = x.toks.length - 1; i >= 0; i--) {
          const w = x.toks[i];
          acc += w.length;
          if (acc > j) {
            const pre = w.slice(0, acc - j);
            return PREVERBS.has(pre) || (pre.length >= 3 && count(pre) >= LEX_MIN);
          }
        }
        return false;
      };
      if (!agree.every(aligned)) continue;
      // and at least one line must write it as a separate word: a split that the
      // edition never makes anywhere in the poem (با|شد, بیدار|شکند) is not a refrain.
      const src = agree.find((x) => x.starts.has(j));
      if (!src) continue;
      const ws = []; let acc = 0;
      for (let i = src.toks.length - 1; i >= 0; i--) {
        if (acc >= j) break;
        ws.unshift(src.toks[i]); acc += src.toks[i].length;
      }
      // the bound-suffix test reads the edition's own spelling: آید is a verb, not the ending -ید
      const firstShown = tailLetters(src.raw, j).split(/[\s\u200C]+/)[0];
      if (BOUND.has(firstShown) || count(ws[0]) < LEX_MIN) continue;
      if (ws[0].length < 2 && ws[0] !== 'و') continue;
      best = { j, radif: ws.join(' ') };
      break;
    }
  }
  return best;
}

// The line as the reader should see it: marks and punctuation gone, but آ, أ, ئ, ۀ
// left as the edition wrote them. Each is one character here and one letter in
// norm(), so letter counts agree.
export function display(t) {
  t = String(t ?? '').normalize('NFC').replace(MARKS, '').replace(/[يىك\u00A0]/g, (c) => FOLD[c]);
  t = t.replace(PUNCT, ' ');
  t = t.replace(/\u200C+/g, ZWNJ).replace(/\s*\u200C\s*/g, ZWNJ);
  return stripEdge(t, ' ' + ZWNJ);
}

// The last j letters of a line in the edition's own spelling (spaces and ZWNJ kept).
export function tailLetters(t, j) {
  t = display(t);
  const out = []; let cnt = 0;
  for (let i = t.length - 1; i >= 0; i--) {
    if (cnt === j) break;
    const ch = t[i];
    out.push(ch);
    if (ch !== ' ' && ch !== ZWNJ) cnt++;
  }
  return stripEdge(out.reverse().join(''), ' ' + ZWNJ);
}

// Letters shared by every rhyme line in front of the refrain: the visible rhyme.
export function sharedRhyme(lines, j) {
  const S = lines.map((x) => words(x, 'Z').join(''));
  let k = 0;
  const ref = S[1];
  while (k < ref.length - 1 && S.every((s) => s.length > k + 1 && s[s.length - 1 - k] === ref[ref.length - 1 - k])) k++;
  return Math.max(0, k - j);
}

// For a ghazal with no free-word radif: do all its rhyme lines end in the same
// attached ending (the copula -ast/-st, a personal or pronominal ending, the yā)?
// Some scholars count such an ending as an attached radif. Letters alone cannot
// tell a clitic from a stem letter (the م of جانم is a clitic, the م of جام is
// not), so this is only ever used as a CEILING: the most the other convention
// could add, never a count of what it does add.
export const ENDINGS = ['است', 'ست', 'یم', 'ام', 'م', 'ات', 'ت', 'اش', 'ش', 'ی'];
export function sharedEnding(lines) {
  const S = lines.map((x) => words(x, 'Z').join(''));
  return ENDINGS.find((e) => S.every((s) => s.length > e.length && s.endsWith(e))) || '';
}

// Everything at once, for one ghazal given as couplets.
export function analyse(couplets, lex) {
  const lines = rhymeLines(couplets);
  const S = wordRadif(lines, 'S');
  const Z = wordRadif(lines, 'Z');
  if (!S || !Z) return null;
  const C = letterRadif(lines, lex, 0);
  const D = letterRadif(lines, lex, 1);
  return {
    lines,
    S: S.n, Z: Z.n,
    C: C.radif ? C.radif.split(' ').length : 0,
    D: D.radif ? D.radif.split(' ').length : 0,
    j: C.j,
    shown: C.j ? tailLetters(lines[1], C.j) : '',
    rhyme: sharedRhyme(lines, C.j),
    end: C.j ? '' : sharedEnding(lines),
  };
}

// Build the lexicon from any iterable of lines of text.
export function buildLex(lines) {
  const lex = new Map();
  for (const t of lines) for (const w of words(t, 'Z')) lex.set(w, (lex.get(w) || 0) + 1);
  return lex;
}

// Julian-ish lunar Hijri year to Common Era year, the usual linear rule.
export const hijriToCE = (ah) => Math.round(ah * 0.970229 + 621.5643);
