"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const ProjectInfoItem_1 = require("./ProjectInfoItem");
const IProjectItemData_1 = require("../app/IProjectItemData");
const IInfoItemData_1 = require("./IInfoItemData");
const StorageUtilities_1 = require("../storage/StorageUtilities");
const Utilities_1 = require("../core/Utilities");
const MinecraftUtilities_1 = require("../minecraft/MinecraftUtilities");
class PackInformationGenerator {
    constructor() {
        this.id = "PACK";
        this.title = "General info";
    }
    getTopicData(topicId) {
        return {
            title: topicId.toString(),
        };
    }
    summarize(info, infoSet) {
        info.defaultBehaviorPackUuid = infoSet.getFirstStringValue("PACK", 2);
        info.defaultIcon = infoSet.getFirstStringValue("PACK", 21);
        if (info.defaultIcon === undefined) {
            info.defaultIcon = infoSet.getFirstStringValue("PACK", 22);
        }
        // because it's heavy, remove pack icon from this list of issues. Though the summarize op is probably
        // the wrong place to do this.
        infoSet.removeItems("PACK", [21, 22]);
        info.defaultBehaviorPackMinEngineVersion = infoSet.getFirstNumberArrayValue("PACK", 1);
        info.defaultBehaviorPackName = infoSet.getFirstNumberArrayValue("PACK", 4);
        info.defaultBehaviorPackDescription = infoSet.getFirstNumberArrayValue("PACK", 5);
        info.defaultResourcePackUuid = infoSet.getFirstStringValue("PACK", 12);
        info.defaultResourcePackMinEngineVersion = infoSet.getFirstNumberArrayValue("PACK", 11);
        info.defaultResourcePackName = infoSet.getFirstNumberArrayValue("PACK", 14);
        info.defaultResourcePackDescription = infoSet.getFirstNumberArrayValue("PACK", 15);
        info.subpackCount = infoSet.getFirstNumberValue("PACK", 18);
        info.subpackTier1Count = infoSet.getCount("PACK", 41);
        info.subpackTier2Count = infoSet.getCount("PACK", 42);
        info.subpackTier3Count = infoSet.getCount("PACK", 43);
        info.subpackTier4Count = infoSet.getCount("PACK", 44);
    }
    async generate(project, contentIndex) {
        const items = [];
        const itemsCopy = project.getItemsCopy();
        for (let i = 0; i < itemsCopy.length; i++) {
            const pi = itemsCopy[i];
            if (pi.itemType === IProjectItemData_1.ProjectItemType.iconImage) {
                if (pi.file) {
                    if (StorageUtilities_1.default.getBaseFromName(pi.file.name) === "pack_icon" &&
                        StorageUtilities_1.default.getTypeFromName(pi.file.name) === "png") {
                        await pi.file.loadContent(false);
                        if (pi.file.content && typeof pi.file.content !== "string") {
                            let index = 21;
                            let description = "Resource pack icon";
                            if (MinecraftUtilities_1.default.pathLooksLikeBehaviorPackName(pi.file.storageRelativePath)) {
                                index = 22;
                                description = "Behavior pack icon";
                            }
                            else if (MinecraftUtilities_1.default.pathLooksLikeSkinPackName(pi.file.storageRelativePath)) {
                                index = 23;
                                description = "Skin pack icon";
                            }
                            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, index, description, pi, Utilities_1.default.uint8ArrayToBase64(pi.file.content)));
                        }
                    }
                }
            }
            else if (pi.itemType === IProjectItemData_1.ProjectItemType.behaviorPackManifestJson) {
                const obj = (await pi.getJsonObject());
                if (obj) {
                    items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 3, "Behavior pack manifest", pi, obj));
                    if (obj.format_version) {
                        items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 1, "Behavior pack format version", pi, obj.format_version));
                    }
                    if (obj.header) {
                        if (obj.header.uuid) {
                            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 2, "Behavior pack UUID", pi, obj.header.uuid));
                        }
                        if (obj.header.uuid && obj.header.version) {
                            if (obj.header.version.length === 3) {
                                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 6, "Behavior pack Id", pi, obj.header.uuid +
                                    "|" +
                                    obj.header.version[0] +
                                    "." +
                                    obj.header.version[1] +
                                    "." +
                                    obj.header.version[2]));
                            }
                        }
                        if (obj.header.name) {
                            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 4, "Behavior pack name", pi, project.loc.getExpandedValue(obj.header.name)));
                        }
                        if (obj.header.description) {
                            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 5, "Behavior pack description", pi, project.loc.getExpandedValue(obj.header.description)));
                        }
                        if (obj.header.min_engine_version) {
                            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 1, "Behavior pack min_engine version", pi, obj.header.min_engine_version));
                        }
                    }
                }
            }
            else if (pi.itemType === IProjectItemData_1.ProjectItemType.resourcePackManifestJson) {
                const obj = (await pi.getJsonObject());
                if (obj) {
                    items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 13, "Resource pack manifest", pi, obj));
                    if (obj.subpacks) {
                        if (Array.isArray(obj.subpacks)) {
                            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 18, "Subpacks", pi, obj.subpacks.length));
                            for (const sp of obj.subpacks) {
                                if (sp.memory_tier) {
                                    items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 40 + sp.memory_tier, "Subpack Memory Tier", pi, sp.name));
                                }
                            }
                        }
                    }
                    if (obj.format_version) {
                        items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 10, "Resource pack format version", pi, obj.format_version));
                    }
                    if (obj.header) {
                        if (obj.header.uuid) {
                            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 12, "Resource pack UUID", pi, obj.header.uuid));
                        }
                        if (obj.header.uuid && obj.header.version) {
                            if (obj.header.version.length === 3) {
                                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 16, "Resource pack Id", pi, obj.header.uuid +
                                    "|" +
                                    obj.header.version[0] +
                                    "." +
                                    obj.header.version[1] +
                                    "." +
                                    obj.header.version[2]));
                            }
                        }
                        if (obj.header.name) {
                            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 14, "Resource pack name", pi, project.loc.getExpandedValue(obj.header.name)));
                        }
                        if (obj.header.description) {
                            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 15, "Resource pack description", pi, project.loc.getExpandedValue(obj.header.description)));
                        }
                        if (obj.header.min_engine_version) {
                            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 11, "Behavior pack min_engine version", pi, obj.header.min_engine_version));
                        }
                    }
                }
            }
        }
        return items;
    }
}
exports.default = PackInformationGenerator;

//# sourceMappingURL=../maps/info/PackInfoGenerator.js.map
