import BTModule, { BTTile, colorToBTTile, } from '../data.js';
export default class BanPatternBTModule extends BTModule {
    constructor(instr) {
        super();
        Object.defineProperty(this, "instr", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.instr = instr;
    }
    checkGlobal(_) {
        // TODO: Impl this properly
        return { tilesNeedCheck: null, ratings: null };
    }
    checkLocal(grid, positions) {
        const ratings = [];
        // TODO: Do not iterate positions
        for (const { x, y } of positions) {
            const tile = grid.getTile(x, y);
            for (const shape of this.instr.cache) {
                for (const origin of shape.elements) {
                    if (colorToBTTile(origin.color) !== tile)
                        continue;
                    if (origin.x > x ||
                        origin.y > y ||
                        shape.width - origin.x + x > grid.width ||
                        shape.height - origin.y + y > grid.height)
                        continue;
                    // We add a tile into ratings if that tile is the only mismatched tile
                    let match = true;
                    let mismatchPos = null;
                    for (const element of shape.elements) {
                        const eleTile = grid.getTile(element.x - origin.x + x, element.y - origin.y + y);
                        if (eleTile === BTTile.Empty) {
                            if (match) {
                                match = false;
                                mismatchPos = {
                                    x: element.x - origin.x + x,
                                    y: element.y - origin.y + y,
                                };
                            }
                            else {
                                mismatchPos = null;
                                break;
                            }
                        }
                        else if (eleTile !== colorToBTTile(element.color)) {
                            match = false;
                            mismatchPos = null;
                            break;
                        }
                    }
                    if (match)
                        return false;
                    if (mismatchPos)
                        ratings.push({ pos: mismatchPos, score: 10000 });
                }
            }
        }
        // TODO: Redesign API - This is not the best!
        // Ratings refresh on every backtrack! Ratings should be appended on the previous result instead of over-writing!
        return { tilesNeedCheck: null, ratings };
    }
}
