import BTModule, { BTTile, IntArray2D, createOneTileResult, } from '../data.js';
export default class DirectionLinkerBTModule extends BTModule {
    constructor(instr) {
        super();
        Object.defineProperty(this, "instr", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.instr = instr;
    }
    checkGlobal(grid) {
        const thisX = Math.floor(this.instr.x);
        const thisY = Math.floor(this.instr.y);
        const tile = grid.getTile(thisX, thisY);
        if (tile === BTTile.Empty)
            return createOneTileResult(grid, { x: thisX, y: thisY });
        const tilesNeedCheck = IntArray2D.create(grid.width, grid.height);
        const ratings = [];
        const queue = [{ x: thisX, y: thisY }];
        const visited = IntArray2D.create(grid.width, grid.height);
        // Visit all connected tiles
        while (queue.length > 0) {
            const curPos = queue.pop();
            if (visited.get(curPos.x, curPos.y))
                continue;
            visited.set(curPos.x, curPos.y, 1);
            const oppoPos = this.movePos(grid, curPos.x, curPos.y);
            if (oppoPos === null)
                return false;
            const oppoTile = grid.getTile(oppoPos.x, oppoPos.y);
            if (!(oppoTile === BTTile.Empty || oppoTile === tile))
                return false;
            for (const edge of grid.getEdges(curPos)) {
                if (visited.get(edge.x, edge.y))
                    continue;
                const edgeTile = grid.getTile(edge.x, edge.y);
                if (edgeTile === BTTile.Empty) {
                    tilesNeedCheck.set(edge.x, edge.y, 1);
                    ratings.push({ pos: edge, score: 1 });
                }
                else if (edgeTile === tile) {
                    queue.push(edge);
                }
            }
        }
        return { tilesNeedCheck, ratings };
    }
}
