import { CellFilter } from "../../CellFilter.js";
import { max2 } from "../../../../core/math/max2.js";
import { CellFilterUnaryOperation } from "../../core/CellFilterUnaryOperation.js";
import { assert } from "../../../../core/assert.js";

export class CellFilterDilate extends CellFilterUnaryOperation {
    /**
     *
     * @param {CellFilter} source
     * @returns {CellFilterDilate}
     */
    static from(source) {
        const r = new CellFilterDilate();
        assert.equal(source.isCellFilter, true, 'source.isCellFilter !== true');

        r.source = source;

        return r;
    }

    execute(grid, x, y, rotation) {
        const filter = this.source;

        let result = Number.NEGATIVE_INFINITY;

        for (let _y = -1; _y <= 1; _y++) {
            for (let _x = -1; _x <= 1; _x++) {
                const v = filter.execute(grid, x + _x, y + _y, 0);

                result = max2(result, v);
            }
        }

        return result;
    }
}
