"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const SemanticVersion_1 = require("../../core/versioning/SemanticVersion");
const StorageUtilities_1 = require("../../storage/StorageUtilities");
const ProjectInfoItem_1 = require("../ProjectInfoItem");
const IInfoItemData_1 = require("../IInfoItemData");
const ProjectInfoUtilities_1 = require("../ProjectInfoUtilities");
const Pack_1 = require("../../minecraft/Pack");
/* Older versions do not require a check */
const MinVersionForCheck = new SemanticVersion_1.default(1, 20, 60);
/* Used to validate that particle identifier correctly contains a namespace. */
const NamespaceRegexPattern = /^\\w{2,}:\\w+/;
var CheckParticleIdentifier;
(function (CheckParticleIdentifier) {
    CheckParticleIdentifier[CheckParticleIdentifier["FailedToReadFile"] = 101] = "FailedToReadFile";
    CheckParticleIdentifier[CheckParticleIdentifier["FailedToReadVersion"] = 102] = "FailedToReadVersion";
    CheckParticleIdentifier[CheckParticleIdentifier["InvalidParticleIdentifier"] = 103] = "InvalidParticleIdentifier";
})(CheckParticleIdentifier || (CheckParticleIdentifier = {}));
/*
 * Generator for validating Particle Identifier
 *
 * Will ensure particle json files have a particle identifier with a namespace if above version 1.2.6.
 */
class CheckParticleIdentifierGenerator {
    id = "CPARTI";
    title = "Check Particle Identifier Generator";
    canAlwaysProcess = true;
    severity = IInfoItemData_1.InfoItemType.error;
    async generate(project) {
        const results = [];
        for (const pack of project.packs.filter((pack) => pack.type === Pack_1.PackType.resource)) {
            const particleJsonFilesForPack = await pack.getFiles(isParticleJson);
            for (const file of particleJsonFilesForPack) {
                const packResults = await this.getResultsForFile(file);
                results.push(...packResults);
            }
        }
        return results;
    }
    async getResultsForFile(file) {
        const json = await StorageUtilities_1.default.getJsonObject(file);
        if (!json) {
            const message = `Failed to read file: ${file.name}`;
            return [this.createResult(CheckParticleIdentifier.FailedToReadFile, message)];
        }
        const version = SemanticVersion_1.default.parse(json.format_version);
        if (!version) {
            const message = `'format_version' expected in json in file: ${file.name}`;
            return [this.createResult(CheckParticleIdentifier.FailedToReadVersion, message)];
        }
        const checkNeeded = version.compareTo(MinVersionForCheck) >= 0;
        if (!checkNeeded) {
            return [];
        }
        const indentifier = json.particle_effect?.description?.identifier;
        if (!isValidParticleIdentifier(indentifier)) {
            const message = "Invalid Particle Identifier. Particle identifier requires a namespace.";
            return [this.createResult(CheckParticleIdentifier.InvalidParticleIdentifier, message)];
        }
        return [];
    }
    createResult(test, message) {
        return new ProjectInfoItem_1.default(this.severity, this.id, test, message);
    }
    getTopicData(topicId) {
        return {
            title: ProjectInfoUtilities_1.default.getTitleFromEnum(CheckParticleIdentifier, topicId),
        };
    }
    summarize() { }
}
exports.default = CheckParticleIdentifierGenerator;
function isValidParticleIdentifier(identifier) {
    return identifier && NamespaceRegexPattern.test(identifier);
}
function isParticleJson(file) {
    return StorageUtilities_1.default.isJsonFile(file) && file.parentFolder.name === "particles";
}

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