"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
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 WorldTemplateManifestDefinition_1 = require("../minecraft/WorldTemplateManifestDefinition");
class BaseGameVersionManager {
    id = "BASEGAMEVER";
    title = "Base Game Version";
    performPlatformVersionValidations = false;
    getTopicData(topicId) {
        const baseGameVersion = {
            updaterId: this.id,
            updaterIndex: 51,
            action: "Set base_game_version to latest version.",
        };
        switch (topicId) {
            case 100:
                return {
                    title: "World Template Base Game Version Defined",
                };
            case 110:
                return {
                    title: "World Template Base Game Version Major Version Lower than Current",
                    updaters: [baseGameVersion],
                };
            case 111:
                return {
                    title: "World Template Base Game Version Major Version Higher than Current",
                    updaters: [baseGameVersion],
                };
            case 120:
                return {
                    title: "World Template Base Game Version Minor Version Lower than Current",
                    updaters: [baseGameVersion],
                };
            case 121:
                return {
                    title: "World Template Base Game Version Minor Version Higher than Current",
                    updaters: [baseGameVersion],
                };
            case 130:
                return {
                    title: "World Template Base Game Version Patch Version Lower than Current",
                    updaters: [baseGameVersion],
                };
            case 131:
                return {
                    title: "World Template Base Game Version Patch Version Higher than Current",
                    updaters: [baseGameVersion],
                };
            case 500:
                return {
                    title: "Retrieve Latest Minecraft Version",
                };
            case 501:
                return {
                    title: "Parse Latest Minecraft Version",
                };
        }
        return {
            title: topicId.toString(),
        };
    }
    getUpdaterData(updaterId) {
        return {
            title: updaterId.toString(),
        };
    }
    summarize(info, infoSet) { }
    async generate(project, content) {
        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, 501, "Could not latest product retrieve 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.worldTemplateManifestJson) {
                if (!pi.isContentLoaded) {
                    await pi.loadContent();
                }
                if (pi.primaryFile) {
                    const bpManifest = await WorldTemplateManifestDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (bpManifest) {
                        if (!bpManifest.definition ||
                            !bpManifest.definition.header ||
                            !bpManifest.definition.header.base_game_version) {
                            infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, 100, "World template manifest does not define a header/base_game_version.", pi));
                        }
                        else {
                            const bpVer = bpManifest?.definition?.header.base_game_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, 110, "World template manifest base game version (" +
                                    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, 111, "World template manifest base game version (" +
                                    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, 120, "World template manifest base game version (" +
                                    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, 121, "World template manifest base game version (" +
                                    bpVer.join(".") +
                                    ") has a higher minor version number compared to current version (" +
                                    verShort +
                                    ")", pi));
                            }
                            else if (bpVer[2] < parseInt(verSplit[2])) {
                                infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.recommendation, this.id, 130, "World template manifest base game version (" +
                                    bpVer.join(".") +
                                    ") has a lower patch version number compared to current version (" +
                                    verShort +
                                    ")", pi));
                            }
                            else if (bpVer[2] > parseInt(verSplit[2]) && bpVer[1] === parseInt(verSplit[1])) {
                                infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, 131, "World template manifest base game version (" +
                                    bpVer.join(".") +
                                    ") has a higher patch version number compared to current version (" +
                                    verShort +
                                    ")", pi));
                            }
                        }
                    }
                }
            }
        }
        return infoItems;
    }
    async update(project, updateId) {
        const results = [];
        switch (updateId) {
            case 1:
                const localResults = await this.updateBaseGameVersionToLatestVersion(project);
                results.push(...localResults);
                break;
        }
        return results;
    }
    getUpdateIds() {
        return [1];
    }
    async updateBaseGameVersionToLatestVersion(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.worldTemplateManifestJson) {
                if (!pi.isContentLoaded) {
                    await pi.loadContent();
                }
                if (pi.primaryFile) {
                    const wtManifest = await WorldTemplateManifestDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (wtManifest) {
                        const mev = wtManifest.baseGameVersion;
                        if (!mev || mev.length < 3 || mev.length > 4 || mev[0] !== major || mev[1] !== minor || mev[2] !== patch) {
                            wtManifest.setBaseGameVersion([major, minor, patch], project);
                            wtManifest.persist();
                            results.push(new ProjectUpdateResult_1.default(IUpdateResult_1.UpdateResultType.updatedFile, this.id, 200, "Updated world template base_game_version to '" + major + "." + minor + "." + patch + "'.", pi, ver));
                        }
                    }
                }
            }
        }
        return results;
    }
}
exports.default = BaseGameVersionManager;

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