"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityTypeInfo = exports.EntityTypeUpdate = 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 EntityTypeDefinition_1 = require("../minecraft/EntityTypeDefinition");
const ProjectInfoUtilities_1 = require("../info/ProjectInfoUtilities");
var EntityTypeUpdate;
(function (EntityTypeUpdate) {
    EntityTypeUpdate[EntityTypeUpdate["UpdateFormatVersionToLatest"] = 1051] = "UpdateFormatVersionToLatest";
})(EntityTypeUpdate = exports.EntityTypeUpdate || (exports.EntityTypeUpdate = {}));
var EntityTypeInfo;
(function (EntityTypeInfo) {
    EntityTypeInfo[EntityTypeInfo["RuntimeIdentifier"] = 52] = "RuntimeIdentifier";
    EntityTypeInfo[EntityTypeInfo["Identifier"] = 53] = "Identifier";
    EntityTypeInfo[EntityTypeInfo["Metadata"] = 54] = "Metadata";
    EntityTypeInfo[EntityTypeInfo["FormatVersionDefined"] = 100] = "FormatVersionDefined";
    EntityTypeInfo[EntityTypeInfo["FormatVersionMajorVersionLowerThanCurrent"] = 110] = "FormatVersionMajorVersionLowerThanCurrent";
    EntityTypeInfo[EntityTypeInfo["FormatVersionMajorVersionHigherThanCurrent"] = 111] = "FormatVersionMajorVersionHigherThanCurrent";
    EntityTypeInfo[EntityTypeInfo["FormatVersionMinorVersionLowerThanCurrent"] = 120] = "FormatVersionMinorVersionLowerThanCurrent";
    EntityTypeInfo[EntityTypeInfo["FormatVersionMinorVersionHigherThanCurrent"] = 121] = "FormatVersionMinorVersionHigherThanCurrent";
    EntityTypeInfo[EntityTypeInfo["FormatVersionPatchVersionLowerThanCurrent"] = 130] = "FormatVersionPatchVersionLowerThanCurrent";
    EntityTypeInfo[EntityTypeInfo["FormatVersionPatchVersionHigherThanCurrent"] = 131] = "FormatVersionPatchVersionHigherThanCurrent";
    EntityTypeInfo[EntityTypeInfo["FailedToRetrieveLatestMinecraftVersion"] = 500] = "FailedToRetrieveLatestMinecraftVersion";
    EntityTypeInfo[EntityTypeInfo["FailedToParseLatestMinecraftVersion"] = 501] = "FailedToParseLatestMinecraftVersion";
})(EntityTypeInfo = exports.EntityTypeInfo || (exports.EntityTypeInfo = {}));
class EntityTypeManager {
    id;
    title = "Entity Type";
    constructor() {
        this.id = "ENTITYTYPE";
    }
    getTopicData(topicId) {
        const formatVersion = {
            updaterId: this.id,
            updaterIndex: EntityTypeUpdate.UpdateFormatVersionToLatest,
            action: "Set behavior pack entity type format to latest version.",
        };
        const title = ProjectInfoUtilities_1.default.getTitleFromEnum(EntityTypeInfo, topicId);
        switch (topicId) {
            case EntityTypeInfo.FormatVersionMajorVersionLowerThanCurrent:
            case EntityTypeInfo.FormatVersionMajorVersionHigherThanCurrent:
            case EntityTypeInfo.FormatVersionMinorVersionLowerThanCurrent:
            case EntityTypeInfo.FormatVersionMinorVersionHigherThanCurrent:
            case EntityTypeInfo.FormatVersionPatchVersionLowerThanCurrent:
            case EntityTypeInfo.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, 509, "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 piiRuntimeIdentifier = new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.featureAggregate, this.id, EntityTypeInfo.RuntimeIdentifier, "Entity Type Runtime Identifier");
        const piiIdentifier = new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.featureAggregate, this.id, EntityTypeInfo.Identifier, "Entity Type Identifier");
        const piiMetadata = new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.featureAggregate, this.id, EntityTypeInfo.Metadata, "Entity Type Metadata");
        infoItems.push(piiRuntimeIdentifier);
        infoItems.push(piiIdentifier);
        infoItems.push(piiMetadata);
        const itemsCopy = project.getItemsCopy();
        for (let i = 0; i < itemsCopy.length; i++) {
            const pi = itemsCopy[i];
            if (pi.itemType === IProjectItemData_1.ProjectItemType.entityTypeBehavior) {
                await pi.ensureFileStorage();
                if (pi.primaryFile) {
                    const bpEntityType = await EntityTypeDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (bpEntityType) {
                        await bpEntityType.load();
                        if (bpEntityType && bpEntityType._data && bpEntityType._data.description) {
                            const desc = bpEntityType._data.description;
                            if (desc.identifier !== undefined && desc.identifier.toLowerCase().startsWith("minecraft:")) {
                                piiIdentifier.incrementFeature(desc.identifier.toLowerCase());
                            }
                            else {
                                piiIdentifier.incrementFeature("(no override identifier)");
                            }
                            if (desc.runtime_identifier !== undefined &&
                                desc.runtime_identifier.toLowerCase !== undefined &&
                                desc.runtime_identifier.toLowerCase().startsWith("minecraft:")) {
                                piiRuntimeIdentifier.incrementFeature(desc.runtime_identifier.toLowerCase());
                            }
                            else {
                                piiRuntimeIdentifier.incrementFeature("(no runtime identifier)");
                            }
                            if (desc.aliases) {
                                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                                for (const aliasName in desc.aliases) {
                                    piiMetadata.incrementFeature("Entity Type Alias");
                                }
                            }
                            if (desc.properties) {
                                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                                for (const propName in desc.properties) {
                                    piiMetadata.incrementFeature("Entity Type Property");
                                }
                            }
                            if (desc.is_experimental) {
                                piiMetadata.incrementFeature("Experimental Entity Type");
                            }
                            if (desc.is_spawnable) {
                                piiMetadata.incrementFeature("Spawnable Entity Type");
                            }
                            if (desc.is_summonable) {
                                piiMetadata.incrementFeature("Summonable Entity Type");
                            }
                        }
                        else {
                            piiMetadata.incrementFeature("Entity Type without description");
                        }
                        const fv = bpEntityType.getFormatVersion();
                        if (!bpEntityType ||
                            !bpEntityType.behaviorPackWrapper ||
                            !bpEntityType.behaviorPackWrapper.format_version ||
                            !fv) {
                            infoItems.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, 100, "Entity 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, 110, "Behavior pack entity 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, 111, "Behavior pack entity 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, 120, "Behavior pack entity 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, 121, "Behavior pack entity 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, 130, "Behavior pack entity 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, 131, "Behavior pack entity 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 EntityTypeUpdate.UpdateFormatVersionToLatest:
                const localResults = await this.updateFormatVersionToLatestVersion(project);
                results.push(...localResults);
                break;
        }
        return results;
    }
    getUpdateIds() {
        return [EntityTypeUpdate.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, EntityTypeInfo.FailedToRetrieveLatestMinecraftVersion, "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 EntityTypeDefinition_1.default.ensureOnFile(pi.primaryFile);
                    if (wtManifest) {
                        const mev = wtManifest.behaviorPackWrapper?.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 entity type manager format_version to '" +
                                    major +
                                    "." +
                                    minor +
                                    "." +
                                    patch +
                                    "'.", pi, ver));
                            }
                        }
                    }
                }
            }
        }
        return results;
    }
}
exports.default = EntityTypeManager;

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