// Browser-only orchestration for the offline dictionary preload. Kept apart from the pure
// preloadDicts (preload.ts) — which unit-tests in the node env — and lazily imported by
// offline.svelte.ts's kickDictPreload, so the dict loader/generator it pulls in stays out of the
// main bundle. It supplies the real side effects (getDawg, the session miss-breaker) and raises the
// in-lobby notice when a first-lobby preload cannot fetch every enabled variant's dictionary.

import type { Profile } from '../model';
import { preloadDicts } from './preload';
import { getDawg, dictLoadingDisabled } from '../dict';
import { setDictPreloadWarning } from '../offline.svelte';

/**
 * runPreload warms the dictionaries for the profile's enabled variants (using the versions the
 * profile advertises) via the real dict loader, so a later switch to offline mode has the data.
 * When warnOnFail is set (the first lobby entry), it raises the in-lobby notice if a variant is
 * still missing afterwards, and clears it on a run where every variant is available.
 */
export async function runPreload(prof: Profile, warnOnFail: boolean): Promise<void> {
  const res = await preloadDicts(prof.dictVersions, prof.variantPreferences, {
    getDawg,
    disabled: dictLoadingDisabled,
  });
  if (warnOnFail) setDictPreloadWarning(res.failed.length > 0);
}
