"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.MinEngineVersionManagerTest = void 0;
const ProjectInfoItem_1 = require("../info/ProjectInfoItem");
const IProjectItemData_1 = require("../app/IProjectItemData");
const IInfoItemData_1 = require("../info/IInfoItemData");
const BehaviorManifestDefinition_1 = require("../minecraft/BehaviorManifestDefinition");
const Database_1 = require("../minecraft/Database");
const ProjectUpdateResult_1 = require("../updates/ProjectUpdateResult");
const ResourceManifestDefinition_1 = require("../minecraft/ResourceManifestDefinition");
const IUpdateResult_1 = require("../updates/IUpdateResult");
const SkinManifestDefinition_1 = require("../minecraft/SkinManifestDefinition");
const WorldTemplateManifestDefinition_1 = require("../minecraft/WorldTemplateManifestDefinition");
const PersonaManifestDefinition_1 = require("../minecraft/PersonaManifestDefinition");
const ProjectItemUtilities_1 = require("../app/ProjectItemUtilities");
var MinEngineVersionManagerTest;
(function (MinEngineVersionManagerTest) {
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["behaviorPackMinEngineVersion"] = 100] = "behaviorPackMinEngineVersion";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["behaviorPackMinEngineVersionMajorLowerThanCurrent"] = 110] = "behaviorPackMinEngineVersionMajorLowerThanCurrent";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["behaviorPackMinEngineVersionMajorHigherThanCurrent"] = 111] = "behaviorPackMinEngineVersionMajorHigherThanCurrent";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["behaviorPackMinEngineVersionMinorLowerThanCurrent"] = 120] = "behaviorPackMinEngineVersionMinorLowerThanCurrent";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["behaviorPackMinEngineVersionMinorHigherThanCurrent"] = 121] = "behaviorPackMinEngineVersionMinorHigherThanCurrent";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["behaviorPackMinEngineVersionPatchLowerThanCurrent"] = 130] = "behaviorPackMinEngineVersionPatchLowerThanCurrent";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["behaviorPackMinEngineVersionPatchHigherThanCurrent"] = 131] = "behaviorPackMinEngineVersionPatchHigherThanCurrent";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["noPackManifestFound"] = 180] = "noPackManifestFound";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["versionProcessingErrorsFound"] = 181] = "versionProcessingErrorsFound";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["resourcePackMinEngineVersion"] = 200] = "resourcePackMinEngineVersion";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["resourcePackMinEngineVersionMajorLowerThanCurrent"] = 210] = "resourcePackMinEngineVersionMajorLowerThanCurrent";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["resourcePackMinEngineVersionMajorHigherThanCurrent"] = 211] = "resourcePackMinEngineVersionMajorHigherThanCurrent";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["resourcePackMinEngineVersionMinorLowerThanCurrent"] = 220] = "resourcePackMinEngineVersionMinorLowerThanCurrent";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["resourcePackMinEngineVersionMinorHigherThanCurrent"] = 221] = "resourcePackMinEngineVersionMinorHigherThanCurrent";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["resourcePackMinEngineVersionPatchLowerThanCurrent"] = 230] = "resourcePackMinEngineVersionPatchLowerThanCurrent";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["resourcePackMinEngineVersionPatchHigherThanCurrent"] = 231] = "resourcePackMinEngineVersionPatchHigherThanCurrent";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["retrieveLatestMinecraftVersion"] = 500] = "retrieveLatestMinecraftVersion";
    MinEngineVersionManagerTest[MinEngineVersionManagerTest["parseLatestMinecraftVersion"] = 501] = "parseLatestMinecraftVersion";
})(MinEngineVersionManagerTest = exports.MinEngineVersionManagerTest || (exports.MinEngineVersionManagerTest = {}));
class MinEngineVersionManager {
    id = "MINENGINEVER";
    title = "Min Engine Version";
    performPlatformVersionValidations = false;
    getTopicData(topicId) {
        const updateMinEngineVersion = {
            updaterId: this.id,
            updaterIndex: 1,
            action: "Set min_engine_version to latest version.",
        };
        switch (topicId) {
            case MinEngineVersionManagerTest.behaviorPackMinEngineVersion:
                return {
                    title: "Behavior Pack Min Engine Version Defined",
                };
            case MinEngineVersionManagerTest.behaviorPackMinEngineVersionMajorLowerThanCurrent:
                return {
                    title: "Behavior Pack Min Engine Version Major Version Lower than Current",
                    updaters: [updateMinEngineVersion],
                };
            case MinEngineVersionManagerTest.behaviorPackMinEngineVersionMajorHigherThanCurrent:
                return {
                    title: "Behavior Pack Min Engine Version Major Version Higher than Current",
                    updaters: [updateMinEngineVersion],
                };
            case MinEngineVersionManagerTest.behaviorPackMinEngineVersionMinorLowerThanCurrent:
                return {
                    title: "Behavior Pack Min Engine Version Minor Version Lower than Current",
                    updaters: [updateMinEngineVersion],
                };
            case MinEngineVersionManagerTest.behaviorPackMinEngineVersionMinorHigherThanCurrent:
                return {
                    title: "Behavior Pack Min Engine Version Minor Version Higher than Current",
                    updaters: [updateMinEngineVersion],
                };
            case MinEngineVersionManagerTest.behaviorPackMinEngineVersionPatchLowerThanCurrent:
                return {
                    title: "Behavior Pack Min Engine Version Patch Version Lower than Current",
                    updaters: [updateMinEngineVersion],
                };
            case MinEngineVersionManagerTest.behaviorPackMinEngineVersionPatchHigherThanCurrent:
                return {
                    title: "Behavior Pack Min Engine Version Patch Version Higher than Current",
                    updaters: [updateMinEngineVersion],
                };
            case MinEngineVersionManagerTest.resourcePackMinEngineVersion:
                return {
                    title: "Resource Pack Min Engine Version Defined",
                };
            case MinEngineVersionManagerTest.resourcePackMinEngineVersionMajorLowerThanCurrent:
                return {
                    title: "Resource Pack Min Engine Version Major Version Lower than Current",
                    updaters: [updateMinEngineVersion],
                };
            case MinEngineVersionManagerTest.resourcePackMinEngineVersionMajorHigherThanCurrent:
                return {
                    title: "Resource Pack Min Engine Version Major Version Higher than Current",
                    updaters: [updateMinEngineVersion],
                };
            case MinEngineVersionManagerTest.resourcePackMinEngineVersionMinorLowerThanCurrent:
                return {
                    title: "Resource Pack Min Engine Version Minor Version Lower than Current",
                    updaters: [updateMinEngineVersion],
                };
            case MinEngineVersionManagerTest.resourcePackMinEngineVersionMinorHigherThanCurrent:
                return {
                    title: "Resource Pack Min Engine Version Minor Version Higher than Current",
                    updaters: [updateMinEngineVersion],
                };
            case MinEngineVersionManagerTest.resourcePackMinEngineVersionPatchLowerThanCurrent:
                return {
                    title: "Resource Pack Min Engine Version Patch Version Lower than Current",
                    updaters: [updateMinEngineVersion],
                };
            case MinEngineVersionManagerTest.resourcePackMinEngineVersionPatchHigherThanCurrent:
                return {
                    title: "Resource Pack Min Engine Version Patch Version Higher than Current",
                    updaters: [updateMinEngineVersion],
                };
            case MinEngineVersionManagerTest.retrieveLatestMinecraftVersion:
                return {
                    title: "Retrieve Latest Minecraft Version",
                };
            case MinEngineVersionManagerTest.parseLatestMinecraftVersion:
                return {
                    title: "Parse Latest Minecraft Version",
                };
        }
        return {
            title: topicId.toString(),
        };
    }
    getUpdaterData(updaterId) {
        return {
            title: updaterId.toString(),
        };
    }
    summarize(info, infoSet) { }
    async generate(project, contentIndex) {
        const infoItems = [];
        const ver = await Database_1.default.getLatestVersionInfo(project.effectiveTrack);
        let foundBpManifest = false;
        let foundRpManifest = false;
        let foundSpManifest = false;
        let foundWorldTemplateManifest = false;
        let foundPersonaManifest = false;
        if (!ver) {
            infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.internalProcessingError, this.id, MinEngineVersionManagerTest.retrieveLatestMinecraftVersion, "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, MinEngineVersionManagerTest.parseLatestMinecraftVersion, "Could not process latest product version.", undefined, ver));
            return infoItems;
        }
        const verShort = verSplit[0] + "." + verSplit[1] + "." + 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) {
                    foundBpManifest = true;
                    const bpManifest = await BehaviorManifestDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (bpManifest) {
                        if (!bpManifest.definition ||
                            !bpManifest.definition.header ||
                            !bpManifest.definition.header.min_engine_version) {
                            infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, MinEngineVersionManagerTest.behaviorPackMinEngineVersion, "Behavior pack manifest does not define a header/min_engine_version.", pi));
                        }
                        else {
                            infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 600 + pi.itemType, ProjectItemUtilities_1.default.getDescriptionForType(pi.itemType) + " min_engine_version", pi, bpManifest.minEngineVersion));
                            const bpVer = bpManifest?.definition?.header.min_engine_version;
                            if (bpVer[0] < parseInt(verSplit[0])) {
                                infoItems.push(new ProjectInfoItem_1.default(this.performPlatformVersionValidations ? IInfoItemData_1.InfoItemType.error : IInfoItemData_1.InfoItemType.recommendation, this.id, MinEngineVersionManagerTest.behaviorPackMinEngineVersionMajorLowerThanCurrent, "Behavior pack manifest (" +
                                    bpVer.join(".") +
                                    ") has a lower major version number compared to current version (" +
                                    verShort +
                                    ")", pi));
                            }
                            else if (bpVer[0] > parseInt(verSplit[0])) {
                                infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, MinEngineVersionManagerTest.behaviorPackMinEngineVersionMajorHigherThanCurrent, "Behavior pack manifest (" +
                                    bpVer.join(".") +
                                    ") has a higher major version number compared to current version (" +
                                    verShort +
                                    ")", pi));
                            }
                            else if (bpVer[1] < parseInt(verSplit[1]) - 1) {
                                infoItems.push(new ProjectInfoItem_1.default(this.performPlatformVersionValidations ? IInfoItemData_1.InfoItemType.error : IInfoItemData_1.InfoItemType.recommendation, this.id, MinEngineVersionManagerTest.behaviorPackMinEngineVersionMinorLowerThanCurrent, "Behavior pack manifest (" +
                                    bpVer.join(".") +
                                    ") has a lower minor version number compared to the current version or the previous current minor version (" +
                                    verShort +
                                    ")", pi));
                            }
                            else if (bpVer[1] > parseInt(verSplit[1])) {
                                infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, MinEngineVersionManagerTest.behaviorPackMinEngineVersionMinorHigherThanCurrent, "Behavior pack manifest (" +
                                    bpVer.join(".") +
                                    ") has a higher minor version number compared to current version (" +
                                    verShort +
                                    ")", pi));
                            }
                        }
                    }
                }
            }
            else if (pi.itemType === IProjectItemData_1.ProjectItemType.resourcePackManifestJson) {
                await pi.ensureFileStorage();
                if (pi.primaryFile) {
                    const rpManifest = await ResourceManifestDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (rpManifest) {
                        foundRpManifest = true;
                        if (!rpManifest.definition ||
                            !rpManifest.definition.header ||
                            !rpManifest.definition.header.min_engine_version) {
                            infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, MinEngineVersionManagerTest.resourcePackMinEngineVersion, "Resource pack manifest does not define a header/min_engine_version.", pi));
                        }
                        else {
                            infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 600 + pi.itemType, ProjectItemUtilities_1.default.getDescriptionForType(pi.itemType) + " format_version", pi, rpManifest.minEngineVersion));
                            const rpVer = rpManifest?.definition?.header.min_engine_version;
                            if (rpVer[0] < parseInt(verSplit[0])) {
                                infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, MinEngineVersionManagerTest.resourcePackMinEngineVersionMajorLowerThanCurrent, "Resource pack manifest (" +
                                    rpVer.join(".") +
                                    ") has a lower major version number compared to current version (" +
                                    verShort +
                                    ")", pi));
                            }
                            else if (rpVer[0] > parseInt(verSplit[0])) {
                                infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, MinEngineVersionManagerTest.resourcePackMinEngineVersionMajorHigherThanCurrent, "Resource pack manifest (" +
                                    rpVer.join(".") +
                                    ") has a higher major version number compared to current version (" +
                                    verShort +
                                    ")", pi));
                            }
                            else if (rpVer[1] < parseInt(verSplit[1]) - 1) {
                                infoItems.push(new ProjectInfoItem_1.default(this.performPlatformVersionValidations ? IInfoItemData_1.InfoItemType.error : IInfoItemData_1.InfoItemType.recommendation, this.id, MinEngineVersionManagerTest.resourcePackMinEngineVersionMinorLowerThanCurrent, "Resource pack manifest (" +
                                    rpVer.join(".") +
                                    ") has a lower minor version number compared to current version or the previous current minor version (" +
                                    verShort +
                                    ")", pi));
                            }
                            else if (rpVer[1] > parseInt(verSplit[1])) {
                                infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, MinEngineVersionManagerTest.resourcePackMinEngineVersionMinorHigherThanCurrent, "Resource pack manifest (" +
                                    rpVer.join(".") +
                                    ") has a higher minor version number compared to current version (" +
                                    verShort +
                                    ")", pi));
                            }
                        }
                    }
                }
            }
            else if (pi.itemType === IProjectItemData_1.ProjectItemType.skinPackManifestJson) {
                await pi.ensureFileStorage();
                if (pi.primaryFile) {
                    const spManifest = await SkinManifestDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (spManifest) {
                        foundSpManifest = true;
                    }
                }
            }
            else if (pi.itemType === IProjectItemData_1.ProjectItemType.worldTemplateManifestJson) {
                await pi.ensureFileStorage();
                if (pi.primaryFile) {
                    const wtManifest = await WorldTemplateManifestDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (wtManifest) {
                        foundWorldTemplateManifest = true;
                    }
                }
            }
            else if (pi.itemType === IProjectItemData_1.ProjectItemType.personaManifestJson) {
                await pi.ensureFileStorage();
                if (pi.primaryFile) {
                    const paManifest = await PersonaManifestDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (paManifest) {
                        foundPersonaManifest = true;
                    }
                }
            }
        }
        if (!foundBpManifest &&
            !foundRpManifest &&
            !foundSpManifest &&
            !foundWorldTemplateManifest &&
            !foundPersonaManifest) {
            infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, MinEngineVersionManagerTest.noPackManifestFound, "No resource/behavior/skin pack manifest or world template manifest was found."));
        }
        return infoItems;
    }
    async update(project, updateId) {
        const results = [];
        switch (updateId) {
            case 1:
                const localResults = await this.updateMinEngineVersionToLatestVersion(project);
                results.push(...localResults);
                break;
        }
        return results;
    }
    getUpdateIds() {
        return [1];
    }
    async updateMinEngineVersionToLatestVersion(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 bpManifest = await BehaviorManifestDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (bpManifest) {
                        const mev = bpManifest.minEngineVersion;
                        if (!mev || mev.length < 3 || mev.length > 4 || mev[0] !== major || mev[1] !== minor || mev[2] !== patch) {
                            bpManifest.setMinEngineVersion([major, minor, patch], project);
                            bpManifest.persist();
                            results.push(new ProjectUpdateResult_1.default(IUpdateResult_1.UpdateResultType.updatedFile, this.id, 200, "Updated behavior pack min_engine_version to '" + major + "." + minor + "." + patch + "'.", pi, ver));
                        }
                    }
                }
            }
            if (pi.itemType === IProjectItemData_1.ProjectItemType.resourcePackManifestJson) {
                await pi.ensureFileStorage();
                if (pi.primaryFile) {
                    const rpManifest = await ResourceManifestDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (rpManifest) {
                        const mev = rpManifest.minEngineVersion;
                        if (!mev || mev.length < 3 || mev.length > 4 || mev[0] !== major || mev[1] !== minor || mev[2] !== patch) {
                            rpManifest.setMinEngineVersion([major, minor, patch], project);
                            rpManifest.persist();
                            results.push(new ProjectUpdateResult_1.default(IUpdateResult_1.UpdateResultType.updatedFile, this.id, 201, "Updated resource pack min_engine_version to '" + major + "." + minor + "." + patch + "'.", pi, ver));
                        }
                    }
                }
            }
        }
        return results;
    }
}
exports.default = MinEngineVersionManager;

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