"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Utilities_1 = require("../core/Utilities");
const VanillaProjectManager_1 = require("../minecraft/VanillaProjectManager");
const Database_1 = require("../minecraft/Database");
const MinecraftUtilities_1 = require("../minecraft/MinecraftUtilities");
class LookupUtilities {
    static blockTypeRefs = undefined;
    static entityTypeRefs = undefined;
    static itemTypeRefs = undefined;
    static soundDefRefs = undefined;
    static terrainTextureRefs = undefined;
    static itemTextureRefs = undefined;
    static async getLookup(lookupId) {
        switch (lookupId) {
            case "blockType":
                return await this.getBlockTypeReferences();
            case "entityType":
                return await this.getEntityTypeReferences();
            case "itemType":
                return await this.getItemTypeReferences();
            case "soundDefinition":
                return await this.getSoundDefinitionReferences();
            case "terrainTexture":
                return await this.getTerrainTextureReferences();
            case "entityTypeEvents":
                break;
            case "difficulty":
                break;
            case "damageCause":
                break;
            case "lootTable":
                break;
            case "entityTypePlusVariants":
                break;
        }
        return undefined;
    }
    static async getSoundDefinitionReferences() {
        if (this.soundDefRefs) {
            return this.soundDefRefs;
        }
        const soundData = await VanillaProjectManager_1.default.getSoundDefinitionCatalog();
        const refs = [];
        if (soundData) {
            const soundSets = soundData.getSoundDefinitionSetNameList();
            if (soundSets) {
                for (const soundSet of soundSets) {
                    refs.push({
                        id: soundSet,
                        title: Utilities_1.default.humanifyMinecraftName(soundSet),
                    });
                }
            }
        }
        LookupUtilities.sortReferences(refs);
        this.soundDefRefs = refs;
        return this.soundDefRefs;
    }
    static async getItemTextureReferences() {
        if (this.itemTextureRefs) {
            return this.itemTextureRefs;
        }
        const itemData = await VanillaProjectManager_1.default.getItemTexturesCatalog();
        const refs = [];
        if (itemData) {
            const textureRefs = itemData.getTextureReferences();
            if (textureRefs) {
                for (const textureRef of textureRefs) {
                    refs.push({
                        id: textureRef,
                        title: Utilities_1.default.humanifyMinecraftName(textureRef),
                    });
                }
            }
        }
        LookupUtilities.sortReferences(refs);
        this.itemTextureRefs = refs;
        return this.itemTextureRefs;
    }
    static async getTerrainTextureReferences() {
        if (this.terrainTextureRefs) {
            return this.terrainTextureRefs;
        }
        const terrainData = await VanillaProjectManager_1.default.getTerrainTexturesCatalog();
        const refs = [];
        if (terrainData) {
            const textureRefs = terrainData.getTextureReferences();
            if (textureRefs) {
                for (const textureRef of textureRefs) {
                    refs.push({
                        id: textureRef,
                        title: Utilities_1.default.humanifyMinecraftName(textureRef),
                    });
                }
            }
        }
        LookupUtilities.sortReferences(refs);
        this.terrainTextureRefs = refs;
        return this.terrainTextureRefs;
    }
    static async getBlockTypeReferences() {
        if (this.blockTypeRefs) {
            return this.blockTypeRefs;
        }
        const blockData = await Database_1.default.getBlocksMetadata();
        const refs = [];
        if (blockData && blockData.data_items) {
            for (const dataItem of blockData.data_items) {
                if (dataItem.name && MinecraftUtilities_1.default.isBedrockItem(dataItem.name)) {
                    const iconImage = MinecraftUtilities_1.default.getBlockDefaultTexturePath(dataItem.name);
                    refs.push({
                        id: dataItem.name,
                        title: Utilities_1.default.humanifyMinecraftName(dataItem.name),
                        iconImage: iconImage ? "/res/latest/van/preview/resource_pack/" + iconImage + ".png" : undefined,
                    });
                }
            }
        }
        LookupUtilities.sortReferences(refs);
        this.blockTypeRefs = refs;
        return this.blockTypeRefs;
    }
    static async getEntityTypeReferences() {
        if (this.entityTypeRefs) {
            return this.entityTypeRefs;
        }
        const entitiesData = await Database_1.default.getEntitiesMetadata();
        const refs = [];
        if (entitiesData && entitiesData.data_items) {
            for (const dataItem of entitiesData.data_items) {
                if (dataItem.name && MinecraftUtilities_1.default.isBedrockItem(dataItem.name)) {
                    refs.push({
                        id: dataItem.name,
                        title: Utilities_1.default.humanifyMinecraftName(dataItem.name),
                    });
                }
            }
        }
        LookupUtilities.sortReferences(refs);
        LookupUtilities.entityTypeRefs = refs;
        return LookupUtilities.entityTypeRefs;
    }
    static async getItemTypeReferences() {
        if (this.itemTypeRefs) {
            return this.itemTypeRefs;
        }
        const itemsData = await Database_1.default.getItemsMetadata();
        const refs = [];
        if (itemsData && itemsData.data_items) {
            for (const dataItem of itemsData.data_items) {
                if (dataItem.name && MinecraftUtilities_1.default.isBedrockItem(dataItem.name)) {
                    refs.push({
                        id: dataItem.name,
                        title: Utilities_1.default.humanifyMinecraftName(dataItem.name),
                    });
                }
            }
        }
        LookupUtilities.sortReferences(refs);
        LookupUtilities.itemTypeRefs = refs;
        return LookupUtilities.itemTypeRefs;
    }
    static appendReferences(core, references) {
        if (references === undefined) {
            return;
        }
        for (const ref of references) {
            let foundRef = false;
            for (const targetRef of core) {
                if (targetRef.id === ref.id) {
                    foundRef = true;
                }
            }
            if (!foundRef) {
                core.push(ref);
            }
        }
    }
    static sortReferences(references) {
        references.sort((a, b) => {
            if (a.title && b.title) {
                return a.title.localeCompare(b.title);
            }
            if (a.id && b.id) {
                return Utilities_1.default.staticCompare(a.id.toString(), b.id.toString());
            }
            return 0;
        });
    }
    static getReferencesFromAnnotatedValues(paths, annotationCategory, description, startsWithFilter) {
        if (!paths) {
            return undefined;
        }
        let simpleRefs = [];
        for (let path in paths) {
            const val = paths[path];
            if (path && val && (!startsWithFilter || path.startsWith(startsWithFilter))) {
                if (startsWithFilter) {
                    path = path.substring(startsWithFilter.length);
                }
                for (const annotatedVal of val) {
                    if (annotatedVal.annotation === annotationCategory) {
                        simpleRefs.push({
                            id: path,
                            title: Utilities_1.default.humanifyMinecraftName(path),
                            description: description
                                ? Utilities_1.default.stringFormat(description, Utilities_1.default.humanifyMinecraftName(path))
                                : undefined,
                        });
                        continue;
                    }
                }
            }
        }
        if (simpleRefs && simpleRefs.length === 0) {
            return undefined;
        }
        return simpleRefs;
    }
}
exports.default = LookupUtilities;

//# sourceMappingURL=../maps/app/LookupUtilities.js.map
