"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.ItemTypeInfo = exports.ItemTypeUpdate = void 0;
const ProjectInfoItem_1 = require("../info/ProjectInfoItem");
const IProjectItemData_1 = require("../app/IProjectItemData");
const IInfoItemData_1 = require("../info/IInfoItemData");
const Database_1 = require("../minecraft/Database");
const ProjectUpdateResult_1 = require("../updates/ProjectUpdateResult");
const IUpdateResult_1 = require("../updates/IUpdateResult");
const ItemTypeDefinition_1 = require("../minecraft/ItemTypeDefinition");
const ProjectInfoUtilities_1 = require("../info/ProjectInfoUtilities");
var ItemTypeUpdate;
(function (ItemTypeUpdate) {
    ItemTypeUpdate[ItemTypeUpdate["UpdateFormatVersionToLatest"] = 1] = "UpdateFormatVersionToLatest";
})(ItemTypeUpdate = exports.ItemTypeUpdate || (exports.ItemTypeUpdate = {}));
var ItemTypeInfo;
(function (ItemTypeInfo) {
    ItemTypeInfo[ItemTypeInfo["identifier"] = 53] = "identifier";
    ItemTypeInfo[ItemTypeInfo["metadata"] = 54] = "metadata";
    ItemTypeInfo[ItemTypeInfo["category"] = 55] = "category";
    ItemTypeInfo[ItemTypeInfo["formatVersionDefined"] = 100] = "formatVersionDefined";
    ItemTypeInfo[ItemTypeInfo["formatVersionMajorVersionLowerThanCurrent"] = 110] = "formatVersionMajorVersionLowerThanCurrent";
    ItemTypeInfo[ItemTypeInfo["formatVersionMajorVersionHigherThanCurrent"] = 111] = "formatVersionMajorVersionHigherThanCurrent";
    ItemTypeInfo[ItemTypeInfo["formatVersionMinorVersionLowerThanCurrent"] = 120] = "formatVersionMinorVersionLowerThanCurrent";
    ItemTypeInfo[ItemTypeInfo["formatVersionMinorVersionHigherThanCurrent"] = 121] = "formatVersionMinorVersionHigherThanCurrent";
    ItemTypeInfo[ItemTypeInfo["formatVersionPatchVersionLowerThanCurrent"] = 130] = "formatVersionPatchVersionLowerThanCurrent";
    ItemTypeInfo[ItemTypeInfo["formatVersionPatchVersionHigherThanCurrent"] = 131] = "formatVersionPatchVersionHigherThanCurrent";
    ItemTypeInfo[ItemTypeInfo["failedToRetrieveLatestMinecraftVersion"] = 500] = "failedToRetrieveLatestMinecraftVersion";
    ItemTypeInfo[ItemTypeInfo["failedToParseLatestMinecraftVersion"] = 501] = "failedToParseLatestMinecraftVersion";
})(ItemTypeInfo = exports.ItemTypeInfo || (exports.ItemTypeInfo = {}));
class ItemTypeManager {
    id = "ITEMTYPE";
    title = "Item Type";
    getTopicData(topicId) {
        const formatVersion = {
            updaterId: this.id,
            updaterIndex: ItemTypeUpdate.UpdateFormatVersionToLatest,
            action: "Set behavior pack item type format version to latest version.",
        };
        const title = ProjectInfoUtilities_1.default.getTitleFromEnum(ItemTypeInfo, topicId);
        switch (topicId) {
            case ItemTypeInfo.formatVersionMajorVersionLowerThanCurrent:
            case ItemTypeInfo.formatVersionMajorVersionHigherThanCurrent:
            case ItemTypeInfo.formatVersionMinorVersionLowerThanCurrent:
            case ItemTypeInfo.formatVersionMinorVersionHigherThanCurrent:
            case ItemTypeInfo.formatVersionPatchVersionLowerThanCurrent:
            case ItemTypeInfo.formatVersionPatchVersionHigherThanCurrent:
                return {
                    title: title,
                    updaters: [formatVersion],
                };
        }
        return {
            title: title,
        };
    }
    getUpdaterData(updaterId) {
        return {
            title: updaterId.toString(),
        };
    }
    summarize(info, infoSet) { }
    async generate(project, contentIndex) {
        const infoItems = [];
        const ver = await Database_1.default.getLatestVersionInfo(project.effectiveTrack);
        if (!ver) {
            infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.internalProcessingError, this.id, 500, "Could not retrieve version."));
            return infoItems;
        }
        const verSplit = ver.split(".");
        if (verSplit.length < 3 || verSplit.length > 4) {
            infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.internalProcessingError, this.id, ItemTypeInfo.failedToRetrieveLatestMinecraftVersion, "Could not latest product retrieve version.", undefined, ver));
            return infoItems;
        }
        const piiIdentifier = new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.featureAggregate, this.id, ItemTypeInfo.identifier, "Item Type Identifier");
        const piiCategory = new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.featureAggregate, this.id, ItemTypeInfo.category, "Item Type Category");
        infoItems.push(piiIdentifier);
        infoItems.push(piiCategory);
        const itemsCopy = project.getItemsCopy();
        for (let i = 0; i < itemsCopy.length; i++) {
            const pi = itemsCopy[i];
            if (pi.itemType === IProjectItemData_1.ProjectItemType.itemTypeBehavior) {
                await pi.ensureFileStorage();
                if (pi.primaryFile) {
                    const bpItemType = await ItemTypeDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (bpItemType) {
                        await bpItemType.load();
                        if (bpItemType && bpItemType.data && bpItemType.data.description) {
                            const desc = bpItemType.data.description;
                            if (desc.identifier !== undefined && desc.identifier.toLowerCase().startsWith("minecraft:")) {
                                piiIdentifier.incrementFeature(desc.identifier.toLowerCase());
                            }
                            else {
                                piiIdentifier.incrementFeature("(no override identifier)");
                            }
                            if (desc.category) {
                                if (desc.category.startsWith("minecraft:") || desc.category.indexOf(":") <= 0) {
                                    piiCategory.incrementFeature(desc.category);
                                }
                            }
                        }
                        else {
                            piiCategory.incrementFeature("(item type without category)");
                        }
                        const fv = bpItemType.getFormatVersion();
                        if (!bpItemType || !bpItemType.wrapper || !bpItemType.wrapper.format_version || !fv) {
                            infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, 100, "Item Type does not define a format_version.", pi));
                        }
                        else {
                            if (fv.length > 0 && fv[0] < parseInt(verSplit[0])) {
                                infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.recommendation, this.id, ItemTypeInfo.formatVersionMajorVersionLowerThanCurrent, "Behavior pack Item Type format version (" +
                                    fv.join(".") +
                                    ") has a lower major version number compared to current version (" +
                                    ver +
                                    ")", pi));
                            }
                            else if (fv[0] > parseInt(verSplit[0])) {
                                infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, ItemTypeInfo.formatVersionMajorVersionHigherThanCurrent, "Behavior pack Item Type format version (" +
                                    fv.join(".") +
                                    ") has a higher major version number compared to current version (" +
                                    ver +
                                    ")", pi));
                            }
                            else if (fv[1] < parseInt(verSplit[1]) - 1) {
                                infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.recommendation, this.id, ItemTypeInfo.formatVersionMinorVersionLowerThanCurrent, "Behavior pack Item Type format version (" +
                                    fv.join(".") +
                                    ") has a lower minor version number compared to the current version or the previous current minor version (" +
                                    ver +
                                    ")", pi));
                            }
                            else if (fv[1] > parseInt(verSplit[1])) {
                                infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, ItemTypeInfo.formatVersionMinorVersionHigherThanCurrent, "Behavior pack Item Type format version (" +
                                    fv.join(".") +
                                    ") has a higher minor version number compared to current version (" +
                                    ver +
                                    ")", pi));
                            }
                            else if (fv[2] < parseInt(verSplit[2])) {
                                infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.recommendation, this.id, ItemTypeInfo.formatVersionPatchVersionLowerThanCurrent, "Behavior pack item type format version (" +
                                    fv.join(".") +
                                    ") has a lower patch version number compared to current version (" +
                                    ver +
                                    ")", pi));
                            }
                            else if (fv[2] > parseInt(verSplit[2]) && fv[1] === parseInt(verSplit[1])) {
                                infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, ItemTypeInfo.formatVersionPatchVersionHigherThanCurrent, "Behavior pack item type format version (" +
                                    fv.join(".") +
                                    ") has a higher patch version number compared to current version (" +
                                    ver +
                                    ")", pi));
                            }
                        }
                    }
                }
            }
        }
        return infoItems;
    }
    async update(project, updateId) {
        const results = [];
        switch (updateId) {
            case ItemTypeUpdate.UpdateFormatVersionToLatest:
                const localResults = await this.updateFormatVersionToLatestVersion(project);
                results.push(...localResults);
                break;
        }
        return results;
    }
    getUpdateIds() {
        return [ItemTypeUpdate.UpdateFormatVersionToLatest];
    }
    async updateFormatVersionToLatestVersion(project) {
        const results = [];
        const ver = await Database_1.default.getLatestVersionInfo(project.effectiveTrack);
        if (!ver) {
            results.push(new ProjectUpdateResult_1.default(IUpdateResult_1.UpdateResultType.internalProcessingError, this.id, 199, "Could not retrieve version."));
            return results;
        }
        const verSplit = ver.split(".");
        if (verSplit.length < 3 || verSplit.length > 4) {
            results.push(new ProjectUpdateResult_1.default(IUpdateResult_1.UpdateResultType.internalProcessingError, this.id, 200, "Could not retrieve latest product version.", undefined, ver));
            return results;
        }
        const major = parseInt(verSplit[0]);
        const minor = parseInt(verSplit[1]);
        const patch = parseInt(verSplit[2]);
        const itemsCopy = project.getItemsCopy();
        for (let i = 0; i < itemsCopy.length; i++) {
            const pi = itemsCopy[i];
            if (pi.itemType === IProjectItemData_1.ProjectItemType.behaviorPackManifestJson) {
                await pi.ensureFileStorage();
                if (pi.primaryFile) {
                    const wtManifest = await ItemTypeDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (wtManifest) {
                        const mev = wtManifest.wrapper?.format_version;
                        if (mev) {
                            const mevY = mev?.split(".");
                            if (mevY &&
                                (mevY.length < 3 ||
                                    mevY.length > 4 ||
                                    parseInt(mevY[0]) !== major ||
                                    parseInt(mevY[1]) !== minor ||
                                    parseInt(mevY[2]) !== patch)) {
                                wtManifest.setBehaviorPackFormatVersion(major + "." + minor + "." + patch);
                                wtManifest.persist();
                                results.push(new ProjectUpdateResult_1.default(IUpdateResult_1.UpdateResultType.updatedFile, this.id, 200, "Updated behavior pack Item Type manager format_version to '" +
                                    major +
                                    "." +
                                    minor +
                                    "." +
                                    patch +
                                    "'.", pi, ver));
                            }
                        }
                    }
                }
            }
        }
        return results;
    }
}
exports.default = ItemTypeManager;

//# sourceMappingURL=../maps/manager/ItemTypeManager.js.map
