import BTModule, { BTTile, IntArray2D, createOneTileResult, getOppositeColor, } from '../data.js';
export default class ViewpointBTModule extends BTModule {
    constructor(instr) {
        super();
        Object.defineProperty(this, "instr", {
            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 });
        const tilesNeedCheck = IntArray2D.create(grid.width, grid.height);
        const ratings = [];
        let completed = 1;
        let possible = 1;
        const traverse = (dirX, dirY) => {
            let connected = true;
            let x = this.instr.x + dirX;
            let y = this.instr.y + dirY;
            while (grid.isInBound(x, y)) {
                const curTile = grid.getTile(x, y);
                if (connected) {
                    if (tile === curTile) {
                        completed += 1;
                        if (completed > this.instr.number)
                            return true;
                    }
                    else {
                        if (curTile === BTTile.Empty) {
                            tilesNeedCheck.set(x, y, 1);
                            ratings.push({ pos: { x, y }, score: 1 });
                        }
                        connected = false;
                    }
                }
                if (getOppositeColor(tile) === curTile ||
                    curTile === BTTile.NonExist)
                    break;
                possible += 1;
                x += dirX;
                y += dirY;
            }
            return false;
        };
        if (traverse(-1, 0) || traverse(1, 0) || traverse(0, -1) || traverse(0, 1))
            return false;
        if (possible < this.instr.number)
            return false;
        return { tilesNeedCheck, ratings };
    }
}
