// Offline word-check fallback for an online game. An online game's dictionary check normally goes to
// the gateway (game.check_word); while disconnected that call fails, so the check falls back to the
// game's own pinned dictionary read locally. Using the game's pinned (variant, version) means the
// answer matches the server's — no divergence — as long as that dawg is available on-device; if it
// is not cached/bundled offline, the check is simply unavailable (null). The membership test mirrors
// the local engine's dictionaryHas (encode to alphabet indices, then Dawg.indexOf).

import { getDawg } from './loader';
import { indexForLetter } from '../alphabet';
import type { Variant } from '../model';

/**
 * localWordCheck reports whether word is in the (variant, version) dictionary using the on-device
 * dawg, or null when that dawg cannot be obtained (offline and neither cached nor bundled). word
 * must already be sanitised to the variant's alphabet (as the check panel does).
 */
export async function localWordCheck(
  variant: Variant,
  version: string,
  word: string,
): Promise<boolean | null> {
  const dawg = await getDawg(variant, version);
  if (!dawg) return null;
  const idx = Array.from(word, (ch) => indexForLetter(variant, ch));
  return dawg.indexOf(idx) >= 0;
}
