import { Color } from '../../primitives.js';
import { instance as undercluedInstance } from '../../rules/undercluedRule.js';
import { Serializer } from '../../serializer/allSerializers.js';
import Solver from '../solver.js';
export default class UndercluedSolver extends Solver {
    constructor() {
        super(...arguments);
        Object.defineProperty(this, "id", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: 'underclued'
        });
        Object.defineProperty(this, "description", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: 'Solves every puzzle as if it were underclued. Supports all rules and symbols and is decently fast for small puzzles. Very slow for large puzzles.'
        });
    }
    async *solve(grid) {
        const worker = new Worker(new URL('./undercluedWorker.js', import.meta.url), {
            type: 'module',
        });
        try {
            const solved = await new Promise(resolve => {
                worker.addEventListener('message', e => {
                    const solution = Serializer.parseGrid(e.data);
                    console.timeEnd('Solve time');
                    if (solution.resetTiles().equals(solution))
                        resolve(null);
                    else
                        resolve(solution);
                });
                worker.postMessage(Serializer.stringifyGrid(grid));
                console.time('Solve time');
            });
            yield solved;
            if (solved) {
                if (solved.getTileCount(true, undefined, Color.Gray) === 0) {
                    yield null; // the grid is completely filled, which means the solution is unique
                }
            }
        }
        finally {
            worker.terminate();
        }
    }
    isInstructionSupported(instructionId) {
        if (super.isInstructionSupported(instructionId)) {
            return true;
        }
        return instructionId === undercluedInstance.id;
    }
}
