"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.SoundsDefinitionInfoGeneratorTest = void 0;
const ProjectInfoItem_1 = require("../ProjectInfoItem");
const IInfoItemData_1 = require("../IInfoItemData");
const ProjectInfoUtilities_1 = require("../ProjectInfoUtilities");
const IProjectItemData_1 = require("../../app/IProjectItemData");
const ISoundDefinitionCatalog_1 = require("../../minecraft/ISoundDefinitionCatalog");
const zod_1 = require("zod");
const StorageUtilities_1 = require("../../storage/StorageUtilities");
var SoundsDefinitionInfoGeneratorTest;
(function (SoundsDefinitionInfoGeneratorTest) {
    SoundsDefinitionInfoGeneratorTest[SoundsDefinitionInfoGeneratorTest["multipleSoundsDefinitionManifests"] = 101] = "multipleSoundsDefinitionManifests";
    SoundsDefinitionInfoGeneratorTest[SoundsDefinitionInfoGeneratorTest["invalidSoundsDefinitionManifest"] = 102] = "invalidSoundsDefinitionManifest";
    SoundsDefinitionInfoGeneratorTest[SoundsDefinitionInfoGeneratorTest["soundsDefinitionManifestInvalidJson"] = 103] = "soundsDefinitionManifestInvalidJson";
    SoundsDefinitionInfoGeneratorTest[SoundsDefinitionInfoGeneratorTest["foundALooseSoundDefinition"] = 104] = "foundALooseSoundDefinition";
})(SoundsDefinitionInfoGeneratorTest = exports.SoundsDefinitionInfoGeneratorTest || (exports.SoundsDefinitionInfoGeneratorTest = {}));
/***********
 * Generator for validating Sounds Definition Manifest Files
 *
 * Will ensure:
 *  * only one Sounds Definition Manifest exists
 *  * Sounds Definition Manifest is valid JSON
 *  * Sounds Definition Manifest is formatted correctly
 *
 */
class SoundsDefinitionInfoGenerator {
    id = "SNDSDEF";
    title = "Sounds Definition Manifest Validation";
    getTopicData(topicId) {
        return {
            title: ProjectInfoUtilities_1.default.getTitleFromEnum(SoundsDefinitionInfoGeneratorTest, topicId),
        };
    }
    summarize(info, infoSet) {
        info.multipleSoundsManifests = infoSet.getSummedDataValue(this.id, SoundsDefinitionInfoGeneratorTest.multipleSoundsDefinitionManifests);
        info.soundsDefinitionManifestFormatIsValid = infoSet.getSummedDataValue(this.id, SoundsDefinitionInfoGeneratorTest.invalidSoundsDefinitionManifest);
        info.soundsDefinitionManifestIsNotValidJson = infoSet.getSummedDataValue(this.id, SoundsDefinitionInfoGeneratorTest.soundsDefinitionManifestInvalidJson);
    }
    async generate(project) {
        const items = [];
        const projItems = project.getItemsCopy();
        const packsWithSoundManifests = {};
        for (const item of projItems) {
            if (item.itemType !== IProjectItemData_1.ProjectItemType.soundDefinitionCatalog && item.itemType) {
                continue;
            }
            const pack = await item.getPack();
            if (!pack || !pack.projectItem.projectPath) {
                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.internalProcessingError, this.id, SoundsDefinitionInfoGeneratorTest.foundALooseSoundDefinition, this.getTopicData(SoundsDefinitionInfoGeneratorTest.multipleSoundsDefinitionManifests).title, item));
                continue;
            }
            if (packsWithSoundManifests[pack.projectItem.projectPath]) {
                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, SoundsDefinitionInfoGeneratorTest.multipleSoundsDefinitionManifests, this.getTopicData(SoundsDefinitionInfoGeneratorTest.multipleSoundsDefinitionManifests).title, item));
                continue;
            }
            else {
                packsWithSoundManifests[pack.projectItem.projectPath] = true;
            }
            if (!item.isContentLoaded) {
                await item.loadContent();
            }
            if (!item.primaryFile) {
                continue;
            }
            if (!item.primaryFile.isContentLoaded) {
                await item.primaryFile.loadContent();
            }
            const content = item.primaryFile.content;
            if (!content || typeof content !== "string") {
                continue;
            }
            const parsedContent = StorageUtilities_1.default.getJsonObject(item.primaryFile);
            if (!parsedContent) {
                items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, SoundsDefinitionInfoGeneratorTest.soundsDefinitionManifestInvalidJson, this.getTopicData(SoundsDefinitionInfoGeneratorTest.soundsDefinitionManifestInvalidJson).title, item));
                continue;
            }
            items.push(...this.getIsManifestValidResult(parsedContent));
        }
        return items;
    }
    // Uses a union to check for both possible versions of a Sound Manifest (per comment in ISoundDefinitionCatalog)
    getIsManifestValidResult(fileContent) {
        const results = [];
        const parseResult = ISoundDefinitionCatalog_1.SoundDefinitionCatalogSchema.safeParse(fileContent);
        if (!parseResult.success) {
            const parsedErrors = [];
            for (const issue of parseResult.error.issues) {
                if (issue.code === zod_1.ZodIssueCode.invalid_union) {
                    for (const e of issue.unionErrors) {
                        for (const i of e.issues) {
                            parsedErrors.push(`${i.message} at ${i.path.join("/")}`);
                        }
                    }
                }
            }
            results.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, SoundsDefinitionInfoGeneratorTest.invalidSoundsDefinitionManifest, `${this.getTopicData(SoundsDefinitionInfoGeneratorTest.invalidSoundsDefinitionManifest).title}: ${parsedErrors.join(", ")}`));
        }
        return results;
    }
}
exports.default = SoundsDefinitionInfoGenerator;

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