"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const BlockRenderType_1 = require("./BlockRenderType");
const MinecraftUtilities_1 = require("./MinecraftUtilities");
const Database_1 = require("./Database");
const Utilities_1 = require("../core/Utilities");
const ste_events_1 = require("ste-events");
const Log_1 = require("../core/Log");
const ManagedComponent_1 = require("./ManagedComponent");
const StorageUtilities_1 = require("../storage/StorageUtilities");
class BlockTypeBehaviorDefinition {
    get numericId() {
        return this.data.id;
    }
    get baseTypeId() {
        return this._baseTypeId;
    }
    get mapColor() {
        return this.data.mapColor;
    }
    get isCustom() {
        return this._isCustom;
    }
    get baseType() {
        if (this._baseType !== undefined) {
            return this._baseType;
        }
        return Database_1.default.defaultBlockBaseType;
    }
    async getFormatVersionIsCurrent() {
        const fv = this.getFormatVersion();
        if (fv === undefined || fv.length !== 3) {
            return false;
        }
        return await Database_1.default.isRecentVersionFromVersionArray(fv);
    }
    getFormatVersion() {
        if (!this._behaviorPackData) {
            return undefined;
        }
        return MinecraftUtilities_1.default.getVersionArrayFrom(this._behaviorPackData.format_version);
    }
    set baseType(baseType) {
        this._baseType = baseType;
        this._baseTypeId = baseType.name;
    }
    get material() {
        return this._material;
    }
    get icon() {
        let val = this.data.icon;
        if (val === undefined && this.baseType !== undefined) {
            val = this.baseType.icon;
        }
        return val;
    }
    get typeId() {
        return this._typeId;
    }
    get shortTypeName() {
        let name = this._typeId;
        const colonIndex = name.indexOf(":");
        if (colonIndex >= 0) {
            name = name.substring(colonIndex + 1, name.length);
        }
        return name;
    }
    get title() {
        const id = this.shortTypeName;
        return Utilities_1.default.humanifyMinecraftName(id);
    }
    constructor(name) {
        this._typeId = "";
        this._baseTypeId = "";
        this._material = "";
        this._isCustom = false;
        this._behaviorPackData = 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();
        this.renderType = BlockRenderType_1.BlockRenderType.Custom;
        this._typeId = name;
        this.data = {
            name: name,
        };
        if (name.indexOf(":") >= 0 && !name.startsWith("minecraft:")) {
            this._isCustom = true;
        }
    }
    get id() {
        if (this.behaviorPackBlockTypeDef && this.behaviorPackBlockTypeDef.description) {
            return this.behaviorPackBlockTypeDef.description.identifier;
        }
        return this._id;
    }
    set id(newId) {
        this._id = newId;
        if (this.behaviorPackBlockTypeDef && this.behaviorPackBlockTypeDef.description && newId) {
            this.behaviorPackBlockTypeDef.description.identifier = newId;
        }
    }
    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 shortId() {
        if (this._id !== undefined) {
            if (this._id.startsWith("minecraft:")) {
                return this._id.substring(10, this._id.length);
            }
            return this._id;
        }
        return undefined;
    }
    getComponent(id) {
        if (this.behaviorPackBlockTypeDef === undefined) {
            return undefined;
        }
        if (!this._managed[id]) {
            const comp = this.behaviorPackBlockTypeDef.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("BTNCU");
        }
        else {
            this._onComponentChanged.dispatch(this, component);
        }
    }
    getComponents() {
        const componentSet = [];
        if (this.behaviorPackBlockTypeDef !== undefined) {
            for (const componentName in this.behaviorPackBlockTypeDef.components) {
                const component = this.getComponent(componentName);
                if (component !== undefined) {
                    componentSet.push(component);
                }
            }
        }
        return componentSet;
    }
    addComponent(id, component) {
        this._ensureBehaviorPackDataInitialized();
        const bpData = this.behaviorPackBlockTypeDef;
        bpData.components[id] = component.getData();
        this._managed[id] = component;
        this._onComponentAdded.dispatch(this, component);
    }
    removeComponent(id) {
        if (this.behaviorPackBlockTypeDef === undefined) {
            return;
        }
        const newBehaviorPacks = {};
        const newComponents = {};
        for (const name in this.behaviorPackBlockTypeDef.components) {
            if (name !== id) {
                const component = this.behaviorPackBlockTypeDef.components[name];
                newBehaviorPacks[name] = component;
            }
        }
        for (const name in this._managed) {
            if (name !== id) {
                newComponents[name] = this._managed[name];
            }
        }
        this.behaviorPackBlockTypeDef.components = newBehaviorPacks;
        this._managed = newComponents;
    }
    _ensureBehaviorPackDataInitialized() {
        if (this.behaviorPackBlockTypeDef === undefined) {
            this.behaviorPackBlockTypeDef = {
                description: {
                    identifier: "unknown",
                },
                components: {},
                events: {},
            };
        }
    }
    static async ensureOnFile(file, loadHandler) {
        let bt;
        if (file.manager === undefined) {
            bt = new BlockTypeBehaviorDefinition("custom:" + file.name);
            bt.behaviorPackFile = file;
            file.manager = bt;
        }
        if (file.manager !== undefined && file.manager instanceof BlockTypeBehaviorDefinition) {
            bt = file.manager;
            if (!bt.isLoaded && loadHandler) {
                bt.onLoaded.subscribe(loadHandler);
            }
            await bt.load();
        }
        return bt;
    }
    persist() {
        if (this._behaviorPackFile === undefined) {
            return;
        }
        const bpString = JSON.stringify(this._behaviorPackData, null, 2);
        this._behaviorPackFile.setContent(bpString);
    }
    async load() {
        if (this._behaviorPackFile === undefined || this._isLoaded) {
            return;
        }
        await this._behaviorPackFile.loadContent();
        if (!this._behaviorPackFile.content || this._behaviorPackFile.content instanceof Uint8Array) {
            return;
        }
        let data = {};
        let result = StorageUtilities_1.default.getJsonObject(this._behaviorPackFile);
        if (result) {
            data = result;
        }
        this._behaviorPackData = data;
        const block = data["minecraft:block"];
        if (block && block.description) {
            this.id = block.description.identifier;
        }
        this.behaviorPackBlockTypeDef = block;
        this._onLoaded.dispatch(this, this);
        this._isLoaded = true;
    }
}
exports.default = BlockTypeBehaviorDefinition;

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