import { move } from '../../../dataHelper.js';
import BTModule, { BTTile, IntArray2D, createOneTileResult, getOppositeColor, } from '../data.js';
export default class DartBTModule 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 pos = move({ x: this.instr.x, y: this.instr.y }, this.instr.orientation);
        let completed = 0;
        let empty = 0;
        while (grid.isInBound(pos.x, pos.y)) {
            // Opposite tiles
            if (grid.getTile(pos.x, pos.y) === getOppositeColor(tile)) {
                completed += 1;
                if (completed > this.instr.number)
                    return false;
            }
            // Empty tiles
            if (grid.getTile(pos.x, pos.y) === BTTile.Empty)
                empty += 1;
            pos = move(pos, this.instr.orientation);
        }
        if (completed + empty < 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 = [];
        let pos = { x: this.instr.x, y: this.instr.y };
        while (grid.isInBound(pos.x, pos.y)) {
            if (grid.getTile(pos.x, pos.y) === BTTile.Empty) {
                tilesNeedCheck.set(pos.x, pos.y, 1);
                ratings.push({ pos, score: 1 });
            }
            pos = move(pos, this.instr.orientation);
        }
        return { tilesNeedCheck, ratings };
    }
}
