import { describe, expect, it } from 'vitest';
import {
  alphabetLetters,
  BLANK_INDEX,
  hasAlphabet,
  indexForLetter,
  letterForIndex,
  setAlphabet,
  valueForLetter,
} from './alphabet';

// The cache module is per-file-isolated by vitest, so only what these tests seed exists.
describe('alphabet cache', () => {
  it('upper-cases letters for display and maps indices and values case-insensitively', () => {
    setAlphabet('scrabble_en', [
      { index: 0, letter: 'a', value: 1 },
      { index: 16, letter: 'q', value: 10 },
    ]);
    expect(hasAlphabet('scrabble_en')).toBe(true);
    expect(letterForIndex('scrabble_en', 0)).toBe('A');
    expect(letterForIndex('scrabble_en', 16)).toBe('Q');
    expect(indexForLetter('scrabble_en', 'a')).toBe(0);
    expect(indexForLetter('scrabble_en', 'Q')).toBe(16);
    expect(valueForLetter('scrabble_en', 'a')).toBe(1);
    expect(valueForLetter('scrabble_en', 'Q')).toBe(10);
  });

  it('handles the blank sentinel and unknown letters/indices', () => {
    setAlphabet('scrabble_en', [{ index: 0, letter: 'a', value: 1 }]);
    expect(letterForIndex('scrabble_en', BLANK_INDEX)).toBe('?');
    expect(indexForLetter('scrabble_en', '?')).toBe(BLANK_INDEX);
    expect(valueForLetter('scrabble_en', '?')).toBe(0);
    expect(letterForIndex('scrabble_en', 99)).toBe(''); // out of range
    expect(valueForLetter('scrabble_en', 'Z')).toBe(0); // not in this alphabet
    expect(() => indexForLetter('scrabble_en', 'Z')).toThrow();
  });

  it('lists the alphabet for the blank chooser and is empty for an uncached variant', () => {
    setAlphabet('scrabble_en', [
      { index: 0, letter: 'a', value: 1 },
      { index: 1, letter: 'b', value: 3 },
    ]);
    expect(alphabetLetters('scrabble_en')).toEqual(['A', 'B']);
    expect(hasAlphabet('erudit_ru')).toBe(false);
    expect(alphabetLetters('erudit_ru')).toEqual([]);
    expect(valueForLetter('erudit_ru', 'A')).toBe(0);
  });
});
