"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");
const Molang_1 = require("./Molang");
const Utilities_1 = require("../core/Utilities");
class ManagedPermutation {
    _data;
    _managed;
    _conditionMolang;
    _onComponentAdded = new ste_events_1.EventDispatcher();
    _onComponentRemoved = new ste_events_1.EventDispatcher();
    _onComponentChanged = new ste_events_1.EventDispatcher();
    constructor(data) {
        this._data = data;
        this._managed = {};
    }
    id = "";
    get onComponentAdded() {
        return this._onComponentAdded.asEvent();
    }
    get onComponentRemoved() {
        return this._onComponentRemoved.asEvent();
    }
    get onComponentChanged() {
        return this._onComponentChanged.asEvent();
    }
    get condition() {
        return this._data?.condition;
    }
    set condition(value) {
        if (value) {
            if (!this._data) {
                this._data = {
                    condition: "",
                    components: {},
                };
            }
            this._data.condition = value;
        }
    }
    get conditionExpression() {
        if (this._conditionMolang) {
            return this._conditionMolang;
        }
        const cond = this.condition;
        if (!cond) {
            return undefined;
        }
        this._conditionMolang = new Molang_1.default(cond);
        return this._conditionMolang;
    }
    addComponent(id, componentOrData) {
        if (!this._data) {
            this._data = {
                condition: "",
                components: {},
            };
        }
        if (!this._managed) {
            this._managed = {};
        }
        const mc = componentOrData instanceof ManagedComponent_1.ManagedComponent
            ? componentOrData
            : new ManagedComponent_1.ManagedComponent(this._data.components, id, componentOrData);
        if (Utilities_1.default.isUsableAsObjectKey(mc.id)) {
            this._data.components[mc.id] = mc.getData();
            this._managed[mc.id] = mc;
        }
        return mc;
    }
    removeComponent(id) {
        if (!this._data) {
            return;
        }
        if (!Utilities_1.default.isUsableAsObjectKey(id)) {
            throw new Error();
        }
        this._data.components[id] = undefined;
        if (this._managed) {
            this._managed[id] = undefined;
        }
    }
    getComponent(id) {
        if (!this._data) {
            return undefined;
        }
        if (!this._managed) {
            this._managed = {};
        }
        if (!Utilities_1.default.isUsableAsObjectKey(id)) {
            throw new Error();
        }
        if (!this._managed[id]) {
            const comps = this._data.components;
            if (!comps) {
                return undefined;
            }
            const data = comps[id];
            if (data) {
                this._managed[id] = new ManagedComponent_1.ManagedComponent(this._data.components, id, data);
            }
        }
        return this._managed[id];
    }
    getAllComponents() {
        return this.getComponents();
    }
    getComponents() {
        if (!this._data) {
            return [];
        }
        if (!this._managed) {
            this._managed = {};
        }
        const comparr = [];
        for (const c in this._data) {
            const comp = this._data.components[c];
            if (!this._managed[c] && comp) {
                this._managed[c] = new ManagedComponent_1.ManagedComponent(this._data.components, 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 = ManagedPermutation;

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