import { assert } from "../../../../core/assert.js";
import { AbstractCellFilterSampleGridLayer } from "./AbstractCellFilterSampleGridLayer.js";

export class CellFilterSampleLayerLinear extends AbstractCellFilterSampleGridLayer {

    /**
     *
     * @param {string} layer
     * @returns {CellFilterSampleLayerLinear}
     */
    static from(layer) {
        assert.isString(layer, 'layer');

        const r = new CellFilterSampleLayerLinear();

        r.layerId = layer;

        return r;
    }

    execute(grid, x, y, rotation) {

        //convert to UV
        const sampler_x = x * this.__u_scale;
        const sampler_y = y * this.__v_scale;


        /**
         *
         * @type {Sampler2D}
         */
        const sampler = this.__sampler;

        const channel_value = sampler.sampleChannelBilinear(sampler_x, sampler_y, 0);

        assert.notNaN(channel_value, 'channel_value');

        return channel_value;
    }
}

