"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 StorageUtilities_1 = require("../storage/StorageUtilities");
const IProjectItemData_1 = require("../app/IProjectItemData");
const ItemTypeDefinition_1 = require("./ItemTypeDefinition");
const Utilities_1 = require("../core/Utilities");
const Database_1 = require("./Database");
class LootTableBehaviorDefinition {
    _file;
    _isLoaded = false;
    data;
    _onLoaded = new ste_events_1.EventDispatcher();
    get isLoaded() {
        return this._isLoaded;
    }
    get file() {
        return this._file;
    }
    get onLoaded() {
        return this._onLoaded.asEvent();
    }
    set file(newFile) {
        this._file = newFile;
    }
    _ensureDataInitialized() {
        if (this.data === undefined) {
            this.data = { pools: [] };
        }
    }
    static async ensureOnFile(file, loadHandler) {
        let ltb;
        if (file.manager === undefined) {
            ltb = new LootTableBehaviorDefinition();
            ltb.file = file;
            file.manager = ltb;
        }
        if (file.manager !== undefined && file.manager instanceof LootTableBehaviorDefinition) {
            ltb = file.manager;
            if (!ltb.isLoaded && loadHandler) {
                ltb.onLoaded.subscribe(loadHandler);
            }
            await ltb.load();
        }
        return ltb;
    }
    getTargetItemTypeIdList() {
        if (!this.data || !this.data.pools) {
            return;
        }
        const targetItems = [];
        for (const pool of this.data.pools) {
            if (pool.entries) {
                for (const entry of pool.entries) {
                    if (entry.type === "item" && entry.name) {
                        targetItems.push(entry.name);
                    }
                }
            }
        }
        return targetItems;
    }
    getTargetLootTablePathList() {
        if (!this.data || !this.data.pools) {
            return;
        }
        const targetLootTablePaths = [];
        for (const pool of this.data.pools) {
            if (pool.entries) {
                for (const entry of pool.entries) {
                    if (entry.type === "loot_table" && entry.name) {
                        targetLootTablePaths.push(this.canonicalizeLootTablePath(entry.name));
                    }
                }
            }
        }
        return targetLootTablePaths;
    }
    persist() {
        if (this._file === undefined) {
            return;
        }
        const bpString = JSON.stringify(this.data, null, 2);
        this._file.setContent(bpString);
    }
    async load() {
        if (this._file === undefined || this._isLoaded) {
            return;
        }
        await this._file.loadContent();
        if (this._file.content === null || this._file.content instanceof Uint8Array) {
            return;
        }
        this.data = StorageUtilities_1.default.getJsonObject(this._file);
        this._isLoaded = true;
    }
    canonicalizeLootTablePath(lootTablePath) {
        lootTablePath = Utilities_1.default.ensureNotStartsWithSlash(lootTablePath);
        const lastPeriod = lootTablePath.lastIndexOf(".");
        if (lastPeriod > 0) {
            lootTablePath = lootTablePath.substring(0, lastPeriod);
        }
        return lootTablePath.toLowerCase();
    }
    async addChildItems(project, item) {
        const itemsCopy = project.getItemsCopy();
        let itemList = this.getTargetItemTypeIdList();
        let lootTableList = this.getTargetLootTablePathList();
        for (const candItem of itemsCopy) {
            if (candItem.itemType === IProjectItemData_1.ProjectItemType.itemTypeBehavior && itemList) {
                await candItem.ensureStorage();
                if (candItem.defaultFile) {
                    const itemType = await ItemTypeDefinition_1.default.ensureOnFile(candItem.defaultFile);
                    if (itemType) {
                        if (itemList.includes(itemType.id)) {
                            item.addChildItem(candItem);
                            itemList = Utilities_1.default.removeItemInArray(itemType.id, itemList);
                            continue;
                        }
                    }
                }
            }
            else if (candItem.itemType === IProjectItemData_1.ProjectItemType.lootTableBehavior && lootTableList) {
                await candItem.ensureStorage();
                if (candItem.defaultFile) {
                    let lootTablePath = await candItem.getPackRelativePath();
                    if (lootTablePath) {
                        lootTablePath = this.canonicalizeLootTablePath(lootTablePath);
                        if (lootTableList.includes(lootTablePath)) {
                            item.addChildItem(candItem);
                            lootTableList = Utilities_1.default.removeItemInArray(lootTablePath, lootTableList);
                            continue;
                        }
                    }
                }
            }
        }
        if (itemList) {
            for (const itemTypeId of itemList) {
                const isVanilla = await Database_1.default.isVanillaToken(itemTypeId);
                item.addUnfulfilledRelationship(itemTypeId, IProjectItemData_1.ProjectItemType.itemTypeBehavior, isVanilla);
            }
        }
        if (lootTableList) {
            for (const lootTablePath of lootTableList) {
                const isVanilla = await Database_1.default.matchesVanillaPathFromIndex(lootTablePath);
                item.addUnfulfilledRelationship(lootTablePath, IProjectItemData_1.ProjectItemType.lootTableBehavior, isVanilla);
            }
        }
    }
}
exports.default = LootTableBehaviorDefinition;

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