"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ProjectItemUtilities_1 = require("../../app/ProjectItemUtilities");
const IProjectItemData_1 = require("../../app/IProjectItemData");
const StorageUtilities_1 = require("../../storage/StorageUtilities");
const TestDefinition_1 = require("../tests/TestDefinition");
const ArrayUtilities_1 = require("../../core/ArrayUtilities");
const ProjectInfoUtilities_1 = require("../ProjectInfoUtilities");
var CheckBetaTest;
(function (CheckBetaTest) {
    CheckBetaTest["UsingBetaFeatures"] = "UsingBetaFeatures";
    CheckBetaTest["FailedToParseJson"] = "FailedToParseJson";
    CheckBetaTest["FailedToReadFile"] = "FailedToReadFile";
})(CheckBetaTest || (CheckBetaTest = {}));
const CheckBetaTests = {
    FailedToReadFile: { id: 101, title: "Failed to read file" },
    FailedToParseJson: { id: 102, title: "Failed to parse Json", defaultMessage: "Failed to parse json in file" },
    UsingBetaFeatures: { id: 103, title: "Using beta features flag in custom definitions is not allowed" },
};
const JsonTypesToRead = new Set([
    IProjectItemData_1.ProjectItemType.behaviorPackManifestJson,
    IProjectItemData_1.ProjectItemType.entityTypeBehavior,
    IProjectItemData_1.ProjectItemType.blockTypeBehavior,
    IProjectItemData_1.ProjectItemType.itemTypeBehavior,
]);
class CheckBetaFeaturesGenerator {
    id = "CBFG";
    title = "Check Beta Features Generator";
    canAlwaysProcess = true;
    async generate(project) {
        const items = project.getItemsCopy();
        const [readJsonItems, failedItems] = await (0, ProjectItemUtilities_1.tryEnsureFiles)(items, (item) => JsonTypesToRead.has(item.itemType));
        const failedReadResults = failedItems.map((item) => (0, TestDefinition_1.resultFromTest)(CheckBetaTests.FailedToReadFile, { id: this.id, item }));
        const jsonObjects = await Promise.all(readJsonItems.map(async (item) => [item, item.primaryFile && (await StorageUtilities_1.default.getJsonObject(item.primaryFile))]));
        const [parsedJson, failedToParse] = (0, ArrayUtilities_1.filterAndSeparate)(jsonObjects, ([_item, json]) => !!json);
        const jsonParseResults = failedToParse
            .map(([item]) => ({ id: this.id, item, data: item.primaryFile?.name }))
            .map((testData) => (0, TestDefinition_1.resultFromTest)(CheckBetaTests.FailedToParseJson, testData));
        const useBetaResults = parsedJson
            .filter(([, json]) => json.use_beta_features === true)
            .map(([item]) => (0, TestDefinition_1.resultFromTest)(CheckBetaTests.UsingBetaFeatures, { id: this.id, item }));
        return [...failedReadResults, ...jsonParseResults, ...useBetaResults];
    }
    getTopicData(topicId) {
        const title = ProjectInfoUtilities_1.default.getGeneralTopicTitle(topicId) || (0, TestDefinition_1.getTestTitleById)(CheckBetaTests, topicId);
        return { title };
    }
    summarize() { }
}
exports.default = CheckBetaFeaturesGenerator;

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