// Mock alphabet fixtures. In production the per-variant (index, letter, value)
// table comes from the server; the mock seeds the same client cache from a local copy so
// the rack, the blank chooser and the mock's scoring work with no backend. The data is the
// solver's value tables (scrabble-solver/rules/rules.go), in alphabet-index order, so a
// token's position is its index — the same shape the wire delivers.

import { setAlphabet, type AlphabetEntryWire } from '../alphabet';
import type { Variant } from '../model';

// "letter+value" tokens in alphabet-index order. English Latin a..z; Russian а..я incl. ё;
// Эрудит а..я incl. ё=0.
const SPECS: Record<Variant, string> = {
  scrabble_en:
    'a1 b3 c3 d2 e1 f4 g2 h4 i1 j8 k5 l1 m3 n1 o1 p3 q10 r1 s1 t1 u1 v4 w4 x8 y4 z10',
  scrabble_ru:
    'а1 б3 в1 г3 д2 е1 ё3 ж5 з5 и1 й4 к2 л2 м2 н1 о1 п2 р1 с1 т1 у2 ф10 х5 ц5 ч5 ш8 щ10 ъ10 ы4 ь3 э8 ю8 я3',
  erudit_ru:
    'а1 б3 в2 г3 д2 е1 ё0 ж5 з5 и1 й2 к2 л2 м2 н1 о1 п2 р2 с2 т2 у3 ф10 х5 ц10 ч5 ш10 щ10 ъ10 ы5 ь5 э10 ю10 я3',
};

function parse(spec: string): AlphabetEntryWire[] {
  return spec
    .trim()
    .split(/\s+/)
    .map((tok, index) => {
      const m = tok.match(/^(.+?)(\d+)$/);
      return { index, letter: m ? m[1] : tok, value: m ? Number(m[2]) : 0 };
    });
}

let seeded = false;

/** seedMockAlphabets populates the alphabet cache for every variant once, mirroring the
 * server-sent tables so the mock-driven UI is alphabet-agnostic too. */
export function seedMockAlphabets(): void {
  if (seeded) return;
  for (const variant of Object.keys(SPECS) as Variant[]) {
    setAlphabet(variant, parse(SPECS[variant]));
  }
  seeded = true;
}
