"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class MultipleInstanceManager {
    constructor(type) {
        this.instances = {};
        this.instanceType = type;
    }
    newInstance(token = "") {
        let instance = new this.instanceType(token);
        if (token != null && token != "")
            this.set(instance, token);
        return instance;
    }
    get(token = "global") {
        let instance = this.instances != null ? this.instances[token] : null;
        if (token == "global" && instance == null) {
            instance = this.newInstance("global");
        }
        return instance;
    }
    getValues() {
        let tokens = this.getTokensList();
        let values = [];
        tokens.forEach(token => {
            let val = this.get(token);
            if (val != null) {
                values.push(val);
            }
        });
        return values;
    }
    getTokensList() {
        return Object.keys(this.instances);
    }
    set(instance, token) {
        this.delete(token);
        this.instances[token] = instance;
    }
    delete(token = "global") {
        let instance = this.instances != null ? this.instances[token] : null;
        if (instance != null) {
            if (typeof instance.destroy === "function") {
                instance.destroy();
            }
            delete this.instances[token];
        }
    }
    getStatus() {
        let body = {};
        if (this.instances != null) {
            for (var key in this.instances) {
                let instance = this.instances[key];
                if (typeof instance.getStatus === "function") {
                    body[key] = instance.getStatus();
                }
            }
        }
        if (Object.keys(body).length === 0) {
            body = "inactive";
        }
        return body;
    }
    isFunctional() {
        let isFunctional = true;
        if (this.instances != null) {
            for (var key in this.instances) {
                let instance = this.instances[key];
                if (typeof instance.isFunctional === "function") {
                    if (isFunctional && !instance.isFunctional()) {
                        isFunctional = false;
                    }
                }
            }
        }
        return isFunctional;
    }
    reset() {
        if (this.instances != null) {
            for (var key in this.instances) {
                let instance = this.instances[key];
                if (typeof instance.destroy === "function") {
                    instance.destroy();
                }
            }
        }
        this.instances = {};
    }
}
exports.default = MultipleInstanceManager;
//# sourceMappingURL=multipleInstanceManager.js.map