"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const IProjectItemData_1 = require("../../app/IProjectItemData");
const StorageUtilities_1 = require("../../storage/StorageUtilities");
const TestDefinition_1 = require("../tests/TestDefinition");
const LocalizationCatalog_1 = require("../../app/localization/LocalizationCatalog");
const ProjectInfoUtilities_1 = require("../ProjectInfoUtilities");
const Tests = {
    MissingLanguagesJson: { id: 101, title: "languages.json Not Found" },
    PrimaryLangMissing: { id: 102, title: "en_US lang code is required." },
    FailedToParseFile: { id: 103, title: "Failed To Parse File" },
    LangFileMissing: {
        id: 104,
        title: "Lang File Missing",
        defaultMessage: "All entries in languages.json must have corresponding .lang file.",
    },
    ExtraLangFile: {
        id: 105,
        title: "Lang File Without Catalog Entry",
        defaultMessage: ".lang file exists in pack but its lang code is not referenced in languages.json",
    },
};
/*
  Checks languages.json and the various .lang files in a pack to ensure that they match

  For each language in languages.json there should be a corresponding .lang file,
  and each .lang file should have a corresponding entry in languages.json

  There should always be en_US
*/
class CheckLangFilesGenerator {
    id = "LANGFILES";
    title = "Check Lang File Generator";
    canAlwaysProcess = true;
    async generate(project) {
        const results = await Promise.all(project.packs.map((pack) => this.validatePack(pack)));
        return results.flat();
    }
    async validatePack(pack) {
        const items = pack.getPackItems();
        const catalogItem = items.find((item) => item.itemType === IProjectItemData_1.ProjectItemType.languagesCatalogJson);
        const langItems = items.filter((item) => item.itemType === IProjectItemData_1.ProjectItemType.lang);
        if (!catalogItem) {
            return [(0, TestDefinition_1.resultFromTest)(Tests.MissingLanguagesJson, { id: this.id })];
        }
        const [catalog, parseErrors] = await (0, LocalizationCatalog_1.parseLocalizationCatalogFromItem)(catalogItem);
        if (parseErrors) {
            return parseErrors
                .map((error) => ({ id: this.id, message: error.message, item: catalogItem }))
                .map((data) => (0, TestDefinition_1.resultFromTest)(Tests.FailedToParseFile, data));
        }
        if (!catalog.langs.find((lang) => lang === "en_US")) {
            return [(0, TestDefinition_1.resultFromTest)(Tests.PrimaryLangMissing, { id: this.id, item: catalogItem })];
        }
        const allLangItems = langItems.map((item) => [item, StorageUtilities_1.default.getBaseFromName(item.name)]);
        const knownLangFileLangs = new Set(allLangItems.map(([, lang]) => lang));
        const knownCatalogLangs = new Set(catalog.langs);
        const langFileNotInCatalogResults = allLangItems
            .filter(([, lang]) => !knownCatalogLangs.has(lang))
            .map(([item]) => (0, TestDefinition_1.resultFromTest)(Tests.ExtraLangFile, { id: this.id, item }));
        const catalogLangNotInFile = catalog.langs
            .filter((lang) => !knownLangFileLangs.has(lang))
            .map((lang) => (0, TestDefinition_1.resultFromTest)(Tests.LangFileMissing, { id: this.id, data: lang, item: catalogItem }));
        return [...langFileNotInCatalogResults, ...catalogLangNotInFile];
    }
    getTopicData(topicId) {
        const title = ProjectInfoUtilities_1.default.getGeneralTopicTitle(topicId) || (0, TestDefinition_1.getTestTitleById)(Tests, topicId);
        return { title };
    }
    summarize() { }
}
exports.default = CheckLangFilesGenerator;

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