import BTModule, { BTTile, IntArray2D, createOneTileResult, getOppositeColor, } from '../data.js';
export default class MinesweeperBTModule extends BTModule {
    constructor(instr) {
        super();
        Object.defineProperty(this, "instr", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        Object.defineProperty(this, "cachedCheckResult", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.instr = instr;
    }
    checkGlobal(grid) {
        const tile = grid.getTile(this.instr.x, this.instr.y);
        if (tile === BTTile.Empty)
            return createOneTileResult(grid, { x: this.instr.x, y: this.instr.y });
        let gray = 0;
        let opposite = 0;
        for (let y = this.instr.y - 1; y <= this.instr.y + 1; y++) {
            for (let x = this.instr.x - 1; x <= this.instr.x + 1; x++) {
                if (!grid.isInBound(x, y) || (x === this.instr.x && y === this.instr.y))
                    continue;
                const checkTile = grid.getTile(x, y);
                if (checkTile === BTTile.Empty)
                    gray++;
                else if (checkTile === getOppositeColor(tile))
                    opposite++;
            }
        }
        if (opposite > this.instr.number || opposite + gray < this.instr.number)
            return false;
        if (!this.cachedCheckResult)
            this.cachedCheckResult = this.buildCheckAndRating(grid);
        return this.cachedCheckResult;
    }
    buildCheckAndRating(grid) {
        const tilesNeedCheck = IntArray2D.create(grid.width, grid.height);
        const ratings = [];
        for (let y = this.instr.y - 1; y <= this.instr.y + 1; y++) {
            for (let x = this.instr.x - 1; x <= this.instr.x + 1; x++) {
                if (!grid.isInBound(x, y) || (x === this.instr.x && y === this.instr.y))
                    continue;
                tilesNeedCheck.set(x, y, 1);
                ratings.push({ pos: { x, y }, score: 1 });
            }
        }
        return { tilesNeedCheck, ratings };
    }
}
