import { assert } from "../../../core/assert.js";
import { seededRandom } from "../../../core/math/random/seededRandom.js";
import { MarkerNodeTransformer } from "./MarkerNodeTransformer.js";

export class MarkerNodeTransformerRecordPropertyClosure extends MarkerNodeTransformer {

    /**
     *
     * @type {function}
     */
    value = null;

    /**
     *
     * @type {string}
     */
    propertyName = null;

    random = seededRandom(0);

    /**
     *
     * @param {string} propertyName
     * @param {function(node:MarkerNode, random:function, grid:GridData):*} value
     * @returns {MarkerNodeTransformerRecordPropertyClosure}
     */
    static from(propertyName, value) {
        assert.isFunction(value, "value");
        assert.isString(propertyName, "propertyName");

        const r = new MarkerNodeTransformerRecordPropertyClosure();

        r.propertyName = propertyName;
        r.value = value;

        return r;
    }


    initialize(grid, seed) {
        this.random.setCurrentSeed(seed);
    }


    transform(node, grid) {
        const value = this.value(node, this.random, grid);

        const result = node.clone();

        result.properties[this.propertyName] = value;

        return result;
    }
}
