"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const ste_events_1 = require("ste-events");
const ManagedComponent_1 = require("./ManagedComponent");
class ManagedComponentGroup {
    constructor(data, id) {
        this._onComponentAdded = new ste_events_1.EventDispatcher();
        this._onComponentRemoved = new ste_events_1.EventDispatcher();
        this._onComponentChanged = new ste_events_1.EventDispatcher();
        this._data = data;
        this._managed = {};
        this.id = id;
    }
    get onComponentAdded() {
        return this._onComponentAdded.asEvent();
    }
    get onComponentRemoved() {
        return this._onComponentRemoved.asEvent();
    }
    get onComponentChanged() {
        return this._onComponentChanged.asEvent();
    }
    addComponent(id, component) {
        if (!this._data) {
            this._data = {};
        }
        if (!this._managed) {
            this._managed = {};
        }
        this._data[component.id] = component.getData();
        this._managed[component.id] = component;
    }
    removeComponent(id) {
        if (!this._data) {
            return;
        }
        this._data[id] = undefined;
    }
    getComponent(id) {
        if (!this._data) {
            return undefined;
        }
        if (!this._managed) {
            this._managed = {};
        }
        if (!this._managed[id]) {
            const data = this._data[id];
            if (data) {
                this._managed[id] = new ManagedComponent_1.ManagedComponent(id, data);
            }
        }
        return this._managed[id];
    }
    getComponents() {
        if (!this._data) {
            return [];
        }
        if (!this._managed) {
            this._managed = {};
        }
        const comparr = [];
        for (const c in this._data) {
            const comp = this._data[c];
            if (!this._managed[c] && comp) {
                this._managed[c] = new ManagedComponent_1.ManagedComponent(c, comp);
            }
            const mc = this._managed[c];
            if (mc) {
                comparr.push(mc);
            }
        }
        return comparr;
    }
    notifyComponentUpdated(id) {
        if (!this._managed) {
            return;
        }
        const comp = this._managed[id];
        if (comp) {
            this._onComponentChanged.dispatch(this, comp);
        }
    }
}
exports.default = ManagedComponentGroup;

//# sourceMappingURL=../maps/minecraft/ManagedComponentGroup.js.map
