import { assert } from "../../core/assert.js";
import List from "../../core/collection/list/List.js";
import ObservedBoolean from "../../core/model/ObservedBoolean.js";
import DataType from "../../core/parser/simple/DataType.js";

export class InteractionCommand {
    /**
     *
     * @param {string} id
     * @param {ObservedBoolean|ReactiveExpression} [enabled]
     * @param {string[]} [features]
     * @param {function} action
     */
    constructor({ id, enabled = new ObservedBoolean(true), features = [], action }) {
        assert.isString(id, 'id');
        assert.isFunction(action, 'action');

        assert.defined(enabled, 'enabled');
        assert.notNull(enabled, 'enabled');
        assert.ok(enabled.isObservedBoolean || (enabled.isReactiveExpression && enabled.dataType === DataType.Boolean), `enabled is not an ObservedBoolean`);

        assert.isArray(features, 'features');

        /**
         *
         * @type {string}
         */
        this.id = id;

        /**
         *
         * @type {ObservedBoolean|ReactiveExpression}
         */
        this.enabled = enabled;

        /**
         *
         * @type {List<String>}
         */
        this.features = new List(features);

        /**
         *
         * @type {Function}
         */
        this.action = action;
    }

    /**
     *
     * @param {string} feature
     * @param {boolean} value
     */
    setFeature(feature, value) {
        if (value && !this.features.contains(feature)) {
            this.features.add(feature);
        } else if (!value && this.features.contains(feature)) {
            this.features.removeOneOf(feature);
        }
    }
}
