"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const TestDefinition_1 = require("../tests/TestDefinition");
const ProjectItemUtilities_1 = require("../../app/ProjectItemUtilities");
const ForbiddenFiles_1 = require("./data/ForbiddenFiles");
const StorageUtilities_1 = require("../../storage/StorageUtilities");
const ProjectInfoUtilities_1 = require("../ProjectInfoUtilities");
var ForbiddenTest;
(function (ForbiddenTest) {
    ForbiddenTest["FailedToReadFile"] = "FailedToReadFile";
    ForbiddenTest["ExtNotInAllowList"] = "ExtNotInAllowList";
    ForbiddenTest["InvalidFileName"] = "InvalidFileName";
    ForbiddenTest["ContainsInvalidCharacter"] = "ContainsInvalidCharacter";
})(ForbiddenTest || (ForbiddenTest = {}));
const ForbiddenTests = {
    FailedToReadFile: { id: 101, title: "Failed To Read File" },
    ExtNotInAllowList: { id: 102, title: "File Does Not Have Allowed Extension" },
    InvalidFileName: { id: 103, title: "File Name Is Blocked" },
    ContainsInvalidCharacter: { id: 104, title: "File Name Contains Invalid Character" },
};
class CheckForbiddenFilesGenerator {
    id = "FORBFILE";
    title = "Check Forbidden Files Generator";
    canAlwaysProcess = true;
    async generate(project) {
        const packs = project.packs.map((pack) => [pack.getPackItems(), pack.packType]);
        const templates = (0, ProjectItemUtilities_1.getWorldTemplates)(project.items).map((template) => [template.items, "WorldTemplate"]);
        const packages = [...packs, ...templates];
        const results = await Promise.all(packages.map(([items, type]) => this.validateItemPackage(items, type)));
        return results.flat();
    }
    async validateItemPackage(items, type) {
        // I'm avoiding reading ("ensuring") the whole file, we just need the path, we do need to extract the extension though
        const files = items.map((item) => [item, "." + StorageUtilities_1.default.getTypeFromName(item.name)]);
        const fileResults = files.flatMap(([item, ext]) => this.resultsForFile(type, item, ext));
        return fileResults;
    }
    resultsForFile(type, item, ext) {
        const results = [];
        const allowedExts = ForbiddenFiles_1.AllowedExtensionsByType[type];
        const isFolder = item.projectPath?.endsWith("/");
        if (!isFolder && allowedExts !== "*" && !allowedExts.has(ext)) {
            results.push((0, TestDefinition_1.resultFromTest)(ForbiddenTests.ExtNotInAllowList, { id: this.id, item, data: ext }));
        }
        if (ForbiddenFiles_1.BlockedFilesByType[type].has(item.name)) {
            results.push((0, TestDefinition_1.resultFromTest)(ForbiddenTests.InvalidFileName, { id: this.id, item }));
        }
        if (item.projectPath?.includes("$")) {
            results.push((0, TestDefinition_1.resultFromTest)(ForbiddenTests.ContainsInvalidCharacter, { id: this.id, item }));
        }
        return results;
    }
    getTopicData(topicId) {
        const title = ProjectInfoUtilities_1.default.getGeneralTopicTitle(topicId) || (0, TestDefinition_1.getTestTitleById)(ForbiddenTests, topicId);
        return { title };
    }
    summarize() { }
}
exports.default = CheckForbiddenFilesGenerator;

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