import Z3Module from './z3Module.js';
import { instance as connectAllInstance, } from '../../../rules/connectAllRule.js';
export default class ConnectAllModule extends Z3Module {
    constructor() {
        super(...arguments);
        Object.defineProperty(this, "id", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: connectAllInstance.id
        });
    }
    encode(grid, ctx) {
        const rules = grid.rules.filter(rule => rule.id === this.id);
        // optimizations to try to simplify the encoding
        if (rules.length === 0) {
            return;
        }
        // encode for real
        const rc = ctx.regionConstrainer;
        for (const color of new Set(rules.map(rule => rule.color))) {
            const sumTerms = [];
            for (const [p, cell] of ctx.grid.grid.entries()) {
                sumTerms.push(ctx.ctx.If(cell
                    .eq(ctx.symbolSet.indices[color])
                    .and(rc.parentGrid.get(p).eq(1)), 1, 0));
            }
            ctx.solver.add(ctx.ctx.Sum(sumTerms[0], ...sumTerms.slice(1)).eq(1));
        }
    }
}
export const instance = new ConnectAllModule();
