/**
 * The solver, off the main thread.
 *
 * The opening scan is ~4,600 x 4,600 pattern computations. On the main thread
 * that is a visible freeze on a phone, in the exact moment the visitor first
 * touches the board.
 *
 * Constructed with `new Worker(new URL('./solver.worker.ts', import.meta.url),
 * { type: 'module' })`, which produces a same-origin module in the build.
 * Never Vite's `?worker&inline`: that yields a blob: URL, and production CSP
 * has no `worker-src`, so it falls back to `default-src 'self'` and the worker
 * is blocked with no console error at all. The solver would simply never boot,
 * in production only.
 */

import type { Pattern } from './engine';
import { suggest } from './solver';

export interface SolverRequest {
  id: number;
  pool: string[];
  history: { guess: string; pattern: Pattern }[];
}

export interface SolverResponse {
  id: number;
  guess: string;
  bits: number;
  viable: boolean;
  candidatesBefore: number;
}

self.onmessage = (event: MessageEvent<SolverRequest>) => {
  const { id, pool, history } = event.data;
  const result = suggest(pool, history);
  const response: SolverResponse = { id, ...result };
  (self as unknown as Worker).postMessage(response);
};
