"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.ManagedComponent = void 0;
const ste_events_1 = require("ste-events");
const Utilities_1 = require("../core/Utilities");
class ManagedComponent {
    _data;
    _parent;
    _uniqueId;
    id;
    _onPropertyChanged = new ste_events_1.EventDispatcher();
    get onPropertyChanged() {
        return this._onPropertyChanged.asEvent();
    }
    constructor(parent, id, data) {
        this._parent = parent;
        this._data = data;
        this.id = id;
        this._uniqueId = Utilities_1.default.createRandomLowerId(8);
        this.handlePropertyChanged = this.handlePropertyChanged.bind(this);
    }
    getData() {
        return this._data;
    }
    getBaseValue() {
        return this._data;
    }
    setData(newData) {
        if (Utilities_1.default.isUsableAsObjectKey(this.id)) {
            this._parent[this.id] = newData;
        }
        this._data = newData;
    }
    setBaseValue(value) {
        this._parent[this.id] = value;
        this._data = value;
    }
    handlePropertyChanged(props, property, newValue, updatedObject) {
        if (updatedObject) {
            this.setData(updatedObject);
        }
    }
    getProperty(id) {
        if (!this._data) {
            return undefined;
        }
        return this._data[id];
    }
    setProperty(propertyId, value) {
        if (typeof this._data === "string" ||
            typeof this._data === "number" ||
            typeof this._data === "boolean" ||
            Array.isArray(this._data)) {
            if (typeof this._data === typeof value || typeof value === "object") {
                this.setData(value);
                return;
            }
            else {
                this.setData({
                    value: this._data,
                });
            }
        }
        if (this._data && Utilities_1.default.isUsableAsObjectKey(propertyId)) {
            this._data[propertyId] = value;
        }
    }
}
exports.ManagedComponent = ManagedComponent;

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