"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckResourcePackDependenciesGeneratorTest = void 0;
const ProjectInfoItem_1 = require("../ProjectInfoItem");
const IInfoItemData_1 = require("../IInfoItemData");
const ProjectInfoUtilities_1 = require("../ProjectInfoUtilities");
const IProjectItemData_1 = require("../../app/IProjectItemData");
const Utilities_1 = require("../../core/Utilities");
const BehaviorManifestDefinition_1 = require("../../minecraft/BehaviorManifestDefinition");
const ResourceManifestDefinition_1 = require("../../minecraft/ResourceManifestDefinition");
var CheckResourcePackDependenciesGeneratorTest;
(function (CheckResourcePackDependenciesGeneratorTest) {
    CheckResourcePackDependenciesGeneratorTest[CheckResourcePackDependenciesGeneratorTest["invalidManifestJson"] = 101] = "invalidManifestJson";
    CheckResourcePackDependenciesGeneratorTest[CheckResourcePackDependenciesGeneratorTest["missingResourcePackDependency"] = 102] = "missingResourcePackDependency";
    CheckResourcePackDependenciesGeneratorTest[CheckResourcePackDependenciesGeneratorTest["internalProcessingError"] = 103] = "internalProcessingError";
})(CheckResourcePackDependenciesGeneratorTest = exports.CheckResourcePackDependenciesGeneratorTest || (exports.CheckResourcePackDependenciesGeneratorTest = {}));
class CheckResourcePackDependenciesGenerator {
    id = "RPDEPENDS";
    title = "Resource Pack Dependencies";
    canAlwaysProcess = true;
    getTopicData(topicId) {
        return {
            title: ProjectInfoUtilities_1.default.getTitleFromEnum(CheckResourcePackDependenciesGeneratorTest, topicId),
        };
    }
    summarize(info, infoSet) {
        info.invalidManifestJson = infoSet.getSummedDataValue(this.id, CheckResourcePackDependenciesGeneratorTest.invalidManifestJson);
        info.missingResourcePackDependencies = infoSet.getSummedDataValue(this.id, CheckResourcePackDependenciesGeneratorTest.missingResourcePackDependency);
        info.internalProcessingErrors = infoSet.getSummedDataValue(this.id, CheckResourcePackDependenciesGeneratorTest.internalProcessingError);
    }
    async generate(project) {
        const items = [];
        const projItems = project.getItemsCopy();
        const manifestDependencies = [];
        const availablePacks = [];
        // Loop through once to find and store all uuids of various packs
        for (const item of projItems) {
            if (item.itemType !== IProjectItemData_1.ProjectItemType.resourcePackManifestJson &&
                item.itemType !== IProjectItemData_1.ProjectItemType.behaviorPackManifestJson) {
                continue;
            }
            if (!item.isContentLoaded) {
                await item.loadContent();
            }
            if (!item.primaryFile) {
                continue;
            }
            if (!item.primaryFile.isContentLoaded) {
                await item.primaryFile.loadContent();
            }
            try {
                if (item.itemType === IProjectItemData_1.ProjectItemType.resourcePackManifestJson) {
                    const resourceManifest = await ResourceManifestDefinition_1.default.ensureOnFile(item.primaryFile);
                    if (!resourceManifest || !resourceManifest.isLoaded) {
                        items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, CheckResourcePackDependenciesGeneratorTest.invalidManifestJson, this.getTopicData(CheckResourcePackDependenciesGeneratorTest.invalidManifestJson).title, item));
                        continue;
                    }
                    if (resourceManifest.id && Utilities_1.default.isValidUuid(resourceManifest.id)) {
                        availablePacks.push({
                            uuid: resourceManifest.id,
                            manifestItem: item,
                        });
                    }
                    if (resourceManifest.dependencies) {
                        for (const dependency of resourceManifest.dependencies) {
                            if (dependency.uuid && typeof dependency.uuid === "string" && Utilities_1.default.isValidUuid(dependency.uuid)) {
                                manifestDependencies.push({
                                    uuid: dependency.uuid,
                                    manifestItem: item,
                                });
                            }
                        }
                    }
                }
                else if (item.itemType === IProjectItemData_1.ProjectItemType.behaviorPackManifestJson) {
                    const behaviorManifest = await BehaviorManifestDefinition_1.default.ensureOnFile(item.primaryFile);
                    if (!behaviorManifest || !behaviorManifest.isLoaded) {
                        items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, CheckResourcePackDependenciesGeneratorTest.invalidManifestJson, this.getTopicData(CheckResourcePackDependenciesGeneratorTest.invalidManifestJson).title, item));
                        continue;
                    }
                    if (behaviorManifest.id && Utilities_1.default.isValidUuid(behaviorManifest.id)) {
                        availablePacks.push({
                            uuid: behaviorManifest.id,
                            manifestItem: item,
                        });
                    }
                }
            }
            catch (error) {
                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, CheckResourcePackDependenciesGeneratorTest.internalProcessingError, `Error processing manifest ${item.name}: ${error}`, item));
            }
        }
        // Loop through again to check if each dependency exists in available packs
        for (const dependency of manifestDependencies) {
            const foundPack = availablePacks.find((pack) => pack.uuid === dependency.uuid);
            if (!foundPack) {
                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, CheckResourcePackDependenciesGeneratorTest.missingResourcePackDependency, `Dependency with uuid [${dependency.uuid}] specified but uuid is not located in any included resource or behavior packs.`, dependency.manifestItem));
            }
        }
        return items;
    }
}
exports.default = CheckResourcePackDependenciesGenerator;

//# sourceMappingURL=../../maps/info/projectGenerators/CheckResourcePackDependenciesGenerator.js.map
