"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var variable_1 = require("./variable");
var VariableManager = /** @class */ (function () {
    function VariableManager(device) {
        this.device = device;
        this._variables = {};
    }
    Object.defineProperty(VariableManager.prototype, "variables", {
        // set variables(value: { [key: number]: VariableInteraction<any> }) {
        //     this._variables = value;
        // }
        get: function () {
            return this._variables;
        },
        enumerable: true,
        configurable: true
    });
    /**
     * Get variable by name
     * If it does not exist, it will throw an error
     */
    VariableManager.prototype.get = function (identifier) {
        return this.variable(identifier);
    };
    /**
     * Check is manager has variable
     * @param identifier variable identifier
     * @returns true if the variable exists
     */
    VariableManager.prototype.has = function (identifier) {
        try {
            this.variable(identifier);
            return true;
        }
        catch (err) {
            return false;
        }
    };
    /**
     * Get variable by name
     * If it does not exist, it will throw an error
     *
     * TODO remove as it's a duplicate of get()
     * @deprecated will be removed. Use {@link #get()} instead
     */
    VariableManager.prototype.variable = function (identifier) {
        // if (typeof identifier === "number"){
        //     return this.variableById(identifier as number);
        // }
        // else{
        return this.variableByName(identifier);
        // }
    };
    /**
     * Get variable by id
     * If it does not exist, it will throw an error
     */
    VariableManager.prototype.getById = function (id) {
        for (var identifier in this._variables) {
            var variable = this._variables[identifier];
            if (variable instanceof variable_1.VariableConfig) {
                if (variable.variableId == id) {
                    return this._variables[identifier];
                }
            }
        }
        throw new Error("Variable with id \"" + id + "\" does not exist");
    };
    VariableManager.prototype.createFromConfig = function (identifier, variableId, converter) {
        return variable_1.VariableConfig.create(identifier, variableId, this.device.service.variable, converter);
    };
    VariableManager.prototype.addFromConfig = function (identifier, variableId, converter) {
        this.addVariable(this.createFromConfig(identifier, variableId, converter));
        return this;
    };
    VariableManager.prototype.createFromMemoryAddress = function (identifier, memoryInfo, converter) {
        return variable_1.VariableMemory.create(identifier, memoryInfo, this.device.service.target, converter);
    };
    VariableManager.prototype.addFromMemoryAddress = function (identifier, memoryInfo, converter) {
        this.addVariable(this.createFromMemoryAddress(identifier, memoryInfo, converter));
        return this;
    };
    VariableManager.prototype.createFromModbusRegister = function (identifier, modbusOptions, converter) {
        return new variable_1.ModbusRegister(identifier, modbusOptions, this.device.service.target, converter);
    };
    VariableManager.prototype.addFromModbusRegister = function (identifier, modbusOptions, converter) {
        this.addVariable(this.createFromModbusRegister(identifier, modbusOptions, converter));
        return this;
    };
    VariableManager.prototype.addVariable = function () {
        var variables = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            variables[_i] = arguments[_i];
        }
        for (var _a = 0, variables_1 = variables; _a < variables_1.length; _a++) {
            var variable = variables_1[_a];
            var id = variable.identifier();
            console.debug("VariableManager add variable " + id);
            if (id in this.variables) {
                console.warn("Variable \"" + id + "\" is already registered. It will be overwritten");
            }
            this.variables[id] = variable;
        }
        return this;
    };
    /**
     * @deprecated use addFromConfig instead
     *
     * @param identifier variable identifier
     * @param configOrVariable
     */
    VariableManager.prototype.add = function (identifier, configOrVariable) {
        // if (configOrVariable instanceof VariableConfig) {
        //     this.addVariable(configOrVariable)
        // }
        // else {
        var converter;
        if (typeof configOrVariable.converter === "string") {
            converter = this.device.apiConfig.getBodyConverter(configOrVariable.converter);
        }
        else {
            converter = configOrVariable.converter;
        }
        this.addFromConfig(identifier, configOrVariable.id, converter);
        // }
        return this;
    };
    VariableManager.prototype.clear = function () {
        for (var id in this._variables) {
            this._variables[id].monitor().stop();
        }
        this._variables = {};
    };
    /**
     * Get variable by name
     * If it does not exist, it will throw an error
     */
    VariableManager.prototype.variableByName = function (identifier) {
        // let id = this.variableNameToId(name);
        if (!(identifier in this._variables)) {
            throw new Error("Variable \"" + identifier + "\" does not exist");
        }
        return this._variables[identifier];
    };
    return VariableManager;
}());
exports.VariableManager = VariableManager;
