"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var bundle_1 = require("./bundle");
var ObjectIterator = /** @class */ (function () {
    function ObjectIterator(object) {
        this.object = object;
        this.index = 0;
        this.values = Object.keys(object).map(function (key) { return object[key]; });
    }
    ObjectIterator.prototype.next = function () {
        if (this.index < this.values.length) {
            return {
                done: false,
                value: this.values[this.index++]
            };
        }
        else {
            return { done: true };
        }
    };
    return ObjectIterator;
}());
exports.ObjectIterator = ObjectIterator;
var BundleManager = /** @class */ (function () {
    function BundleManager(service, variableManager) {
        this.service = service;
        this.variableManager = variableManager;
        this._bundles = {};
    }
    BundleManager.prototype[Symbol
        .iterator] = function () {
        return new ObjectIterator(this._bundles);
    };
    Object.defineProperty(BundleManager.prototype, "bundles", {
        get: function () {
            return this._bundles;
        },
        enumerable: true,
        configurable: true
    });
    BundleManager.prototype.get = function (identifier) {
        if (!(identifier in this._bundles)) {
            throw new Error("Bundle with id " + identifier + " does not exist");
        }
        return this._bundles[identifier];
    };
    BundleManager.prototype.add = function (identifier, configOrBundle) {
        if (configOrBundle instanceof bundle_1.Bundle) {
            this._bundles[identifier] = configOrBundle;
        }
        else {
            this._bundles[identifier] = this._createBundle(identifier, configOrBundle);
        }
        return this;
    };
    BundleManager.prototype.bundle = function (identifier) {
        if (!(identifier in this._bundles)) {
            throw new Error("Bundle \"" + identifier + "\" does not exist");
        }
        return this._bundles[identifier];
    };
    BundleManager.prototype._createBundle = function (identifier, config) {
        console.debug("VariableManager Creating variable " + identifier);
        return new bundle_1.Bundle(identifier, config.id, this.service, this.variableManager);
    };
    BundleManager.prototype.clear = function () {
        for (var id in this._bundles) {
            this._bundles[id].monitor().stop();
        }
        this._bundles = {};
    };
    return BundleManager;
}());
exports.BundleManager = BundleManager;
