"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlocksCatalogInfo = exports.BlocksCatalogUpdate = void 0;
const ProjectInfoItem_1 = require("../info/ProjectInfoItem");
const IProjectItemData_1 = require("../app/IProjectItemData");
const IInfoItemData_1 = require("../info/IInfoItemData");
const ProjectUpdateResult_1 = require("../updates/ProjectUpdateResult");
const IUpdateResult_1 = require("../updates/IUpdateResult");
const BlocksCatalogDefinition_1 = require("../minecraft/BlocksCatalogDefinition");
var BlocksCatalogUpdate;
(function (BlocksCatalogUpdate) {
    BlocksCatalogUpdate[BlocksCatalogUpdate["removeUnusedBlockResourceIdentifiers"] = 1051] = "removeUnusedBlockResourceIdentifiers";
})(BlocksCatalogUpdate = exports.BlocksCatalogUpdate || (exports.BlocksCatalogUpdate = {}));
var BlocksCatalogInfo;
(function (BlocksCatalogInfo) {
    BlocksCatalogInfo[BlocksCatalogInfo["unusedBlockCatalogResource"] = 100] = "unusedBlockCatalogResource";
    BlocksCatalogInfo[BlocksCatalogInfo["foundBlockCatalogResource"] = 101] = "foundBlockCatalogResource";
    BlocksCatalogInfo[BlocksCatalogInfo["vanillaOverrideBlockCatalogResource"] = 102] = "vanillaOverrideBlockCatalogResource";
    BlocksCatalogInfo[BlocksCatalogInfo["blockResourceIdentifier"] = 53] = "blockResourceIdentifier";
})(BlocksCatalogInfo = exports.BlocksCatalogInfo || (exports.BlocksCatalogInfo = {}));
class BlocksCatalogManager {
    id = "BLOCKSCAT";
    title = "Blocks Catalog";
    getTopicData(topicId) {
        switch (topicId) {
            case BlocksCatalogInfo.unusedBlockCatalogResource:
                return {
                    title: "Block Resource Identifier",
                };
        }
        return {
            title: topicId.toString(),
        };
    }
    getUpdaterData(updaterId) {
        return {
            title: updaterId.toString(),
        };
    }
    summarize(info, infoSet) { }
    async generate(project, contentIndex) {
        const results = [];
        const itemsCopy = project.getItemsCopy();
        for (let i = 0; i < itemsCopy.length; i++) {
            const pi = itemsCopy[i];
            if (pi.itemType === IProjectItemData_1.ProjectItemType.blocksCatalogResourceJson) {
                if (!pi.isContentLoaded) {
                    await pi.loadContent();
                }
                if (pi.primaryFile) {
                    const blocksCatalog = await BlocksCatalogDefinition_1.default.ensureOnFile(pi.primaryFile);
                    results.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, BlocksCatalogInfo.foundBlockCatalogResource, `Blocks catalog resource found`, pi));
                    if (blocksCatalog) {
                        const dependencies = await blocksCatalog.getDependenciesList(project);
                        for (const id of dependencies.unused) {
                            results.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.warning, this.id, BlocksCatalogInfo.unusedBlockCatalogResource, `Blocks catalog resource is not used`, pi, id));
                        }
                        for (const id of dependencies.vanillaOverride) {
                            results.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.recommendation, this.id, BlocksCatalogInfo.vanillaOverrideBlockCatalogResource, `Overrides vanilla resource, which is not recommended`, pi, id));
                        }
                    }
                }
            }
        }
        return results;
    }
    async update(project, updateId) {
        const results = [];
        const itemsCopy = project.getItemsCopy();
        for (let i = 0; i < itemsCopy.length; i++) {
            const pi = itemsCopy[i];
            if (pi.itemType === IProjectItemData_1.ProjectItemType.blocksCatalogResourceJson) {
                if (!pi.isContentLoaded) {
                    await pi.loadContent();
                }
                if (pi.primaryFile) {
                    const blocksCatalog = await BlocksCatalogDefinition_1.default.ensureOnFile(pi.primaryFile);
                    let wasUpdated = false;
                    if (blocksCatalog) {
                        switch (updateId) {
                            case BlocksCatalogUpdate.removeUnusedBlockResourceIdentifiers:
                                const dependencies = await blocksCatalog.getDependenciesList(project);
                                for (const id of dependencies.unused) {
                                    blocksCatalog.removeId(id);
                                    wasUpdated = true;
                                    results.push(new ProjectUpdateResult_1.default(IUpdateResult_1.UpdateResultType.updatedFile, this.id, BlocksCatalogUpdate.removeUnusedBlockResourceIdentifiers, "Removed ununused blocks catalog resource", pi, id));
                                }
                                break;
                        }
                    }
                    if (wasUpdated) {
                        blocksCatalog?.persist();
                    }
                }
            }
        }
        return results;
    }
    getUpdateIds() {
        return [BlocksCatalogUpdate.removeUnusedBlockResourceIdentifiers];
    }
}
exports.default = BlocksCatalogManager;

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