import { assert } from "../../../../core/assert.js";
import List from '../../../../core/collection/list/List.js';
import Signal from "../../../../core/events/signal/Signal.js";
import { InputBinding } from "./InputBinding.js";


class InputController {
    /**
     *
     * @param {Array} [bindings]
     * @constructor
     */
    constructor(bindings = []) {
        assert.isArray(bindings, 'bindings');

        this.mapping = new List();

        const inputControllerBindings = bindings.map(b => new InputBinding(b));

        this.mapping.addAll(inputControllerBindings);

        this.on = {
            unlinked: new Signal()
        };
    }

    /**
     *
     * @param {string} path
     * @param {function} action
     * @returns {InputBinding}
     */
    bind(path, action) {
        assert.isFunction(action, 'action');
        assert.isString(path, 'path');

        const binding = new InputBinding({ path, listener: action });

        this.mapping.add(binding);

        return binding;
    }

    /**
     *
     * @param {Array} bindings
     */
    static from(bindings) {
        const ic = new InputController();

        const n = bindings.length;
        for (let i = 0; i < n; i++) {
            const binding = bindings[i];

            const inputBinding = new InputBinding(binding);

            ic.mapping.add(inputBinding);
        }

        return ic;
    }
}

InputController.typeName = "InputController";
InputController.serializable = false;

export default InputController;
