import { assert } from "../../../../../core/assert.js";
import { compileReactiveExpression } from "../../../../../core/lang/reactive/compileReactiveExpression.js";
import { ActionBehavior } from "../../../../intelligence/behavior/primitive/ActionBehavior.js";
import { Blackboard } from "../../../../intelligence/blackboard/Blackboard.js";
import { AbstractActionDescription } from "./AbstractActionDescription.js";

export class WriteToBlackboardActionDescription extends AbstractActionDescription {

    /**
     *
     * @type {ReactiveExpression}
     */
    expression = null;

    /**
     *
     * @type {string}
     */
    name = "";

    execute(actor, dataset, context, system) {

        const expression = this.expression;
        const name = this.name;

        return new ActionBehavior(() => {

            const blackboard = dataset.getComponent(actor, Blackboard);

            const value = blackboard.acquire(name, expression.dataType);

            value.set(expression.evaluate(context));

        });

    }

    /**
     *
     * @param {string} name
     * @param {string} value
     */
    fromJSON({ name, value }) {

        assert.isString(name, 'name');

        this.name = name;

        this.expression = compileReactiveExpression(value);

    }
}


WriteToBlackboardActionDescription.prototype.type = "Write";
