"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const Log_1 = require("../core/Log");
const ste_events_1 = require("ste-events");
const ManagedComponent_1 = require("./ManagedComponent");
const StorageUtilities_1 = require("../storage/StorageUtilities");
const Database_1 = require("./Database");
const MinecraftUtilities_1 = require("./MinecraftUtilities");
class ItemTypeBehaviorDefinition {
    constructor() {
        this.wrapper = null;
        this._isLoaded = false;
        this._managed = {};
        this._onLoaded = new ste_events_1.EventDispatcher();
        this._onComponentAdded = new ste_events_1.EventDispatcher();
        this._onComponentRemoved = new ste_events_1.EventDispatcher();
        this._onComponentChanged = new ste_events_1.EventDispatcher();
    }
    get onComponentAdded() {
        return this._onComponentAdded.asEvent();
    }
    get onComponentRemoved() {
        return this._onComponentRemoved.asEvent();
    }
    get onComponentChanged() {
        return this._onComponentChanged.asEvent();
    }
    get isLoaded() {
        return this._isLoaded;
    }
    get behaviorPackFile() {
        return this._behaviorPackFile;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    set behaviorPackFile(newFile) {
        this._behaviorPackFile = newFile;
    }
    get id() {
        return this._id;
    }
    set id(newId) {
        this._id = newId;
    }
    get shortId() {
        if (this._id !== undefined) {
            if (this._id.startsWith("minecraft:")) {
                return this._id.substring(10, this._id.length);
            }
            return this._id;
        }
        return undefined;
    }
    async getFormatVersionIsCurrent() {
        const fv = this.getFormatVersion();
        if (fv === undefined || fv.length !== 3) {
            return false;
        }
        return await Database_1.default.isRecentVersionFromVersionArray(fv);
    }
    getFormatVersion() {
        if (!this.wrapper) {
            return undefined;
        }
        return MinecraftUtilities_1.default.getVersionArrayFrom(this.wrapper.format_version);
    }
    getComponent(id) {
        if (this.data === undefined) {
            return undefined;
        }
        if (!this._managed[id]) {
            const comp = this.data.components[id];
            if (comp) {
                this._managed[id] = new ManagedComponent_1.ManagedComponent(id, comp);
            }
        }
        return this._managed[id];
    }
    notifyComponentUpdated(id) {
        const component = this.getComponent(id);
        if (component === undefined) {
            Log_1.default.unexpectedUndefined("ITNCU");
        }
        else {
            this._onComponentChanged.dispatch(this, component);
        }
    }
    getComponents() {
        const componentSet = [];
        if (this.data !== undefined) {
            for (const componentName in this.data.components) {
                const component = this.getComponent(componentName);
                if (component !== undefined) {
                    componentSet.push(component);
                }
            }
        }
        return componentSet;
    }
    setBehaviorPackFormatVersion(versionStr) {
        this._ensureBehaviorPackDataInitialized();
        if (this.wrapper) {
            this.wrapper.format_version = versionStr;
        }
    }
    addComponent(id, component) {
        this._ensureBehaviorPackDataInitialized();
        const bpData = this.data;
        bpData.components[id] = component.getData();
        this._managed[id] = component;
        this._onComponentAdded.dispatch(this, component);
    }
    removeComponent(id) {
        if (this.data === undefined) {
            return;
        }
        const newBehaviorPacks = {};
        const newManagedComponents = {};
        for (const name in this.data.components) {
            if (name !== id) {
                const component = this.data.components[name];
                newBehaviorPacks[name] = component;
            }
        }
        for (const name in this._managed) {
            if (name !== id) {
                newManagedComponents[name] = this._managed[name];
            }
        }
        this.data.components = newBehaviorPacks;
        this._managed = newManagedComponents;
    }
    _ensureBehaviorPackDataInitialized() {
        if (this.data === undefined) {
            this.data = {
                description: {
                    identifier: "unknown",
                },
                components: {},
                events: {},
            };
        }
    }
    static async ensureOnFile(file, loadHandler) {
        let itt;
        if (file.manager === undefined) {
            itt = new ItemTypeBehaviorDefinition();
            itt.behaviorPackFile = file;
            file.manager = itt;
        }
        if (file.manager !== undefined && file.manager instanceof ItemTypeBehaviorDefinition) {
            itt = file.manager;
            if (!itt.isLoaded && loadHandler) {
                itt.onLoaded.subscribe(loadHandler);
            }
            await itt.load();
        }
        return itt;
    }
    persist() {
        if (this._behaviorPackFile === undefined) {
            return;
        }
        const bpString = JSON.stringify(this.wrapper, null, 2);
        this._behaviorPackFile.setContent(bpString);
    }
    async load() {
        if (this._behaviorPackFile === undefined || this._isLoaded) {
            return;
        }
        await this._behaviorPackFile.loadContent();
        if (this._behaviorPackFile.content === null || this._behaviorPackFile.content instanceof Uint8Array) {
            return;
        }
        this.wrapper = StorageUtilities_1.default.getJsonObject(this._behaviorPackFile);
        if (this.wrapper) {
            const item = this.wrapper["minecraft:item"];
            if (item && item.description) {
                this.id = item.description.identifier;
            }
            this.data = item;
        }
        this._isLoaded = true;
    }
}
exports.default = ItemTypeBehaviorDefinition;

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