"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.MCFunctionInfoGeneratorTest = void 0;
const ProjectInfoItem_1 = require("./ProjectInfoItem");
const IInfoItemData_1 = require("./IInfoItemData");
const ProjectInfoUtilities_1 = require("./ProjectInfoUtilities");
const CommandStructure_1 = require("../app/CommandStructure");
const CommandRegistry_1 = require("../app/CommandRegistry");
const IProjectItemData_1 = require("../app/IProjectItemData");
var MCFunctionInfoGeneratorTest;
(function (MCFunctionInfoGeneratorTest) {
    MCFunctionInfoGeneratorTest[MCFunctionInfoGeneratorTest["invalidCommandSyntax"] = 102] = "invalidCommandSyntax";
    MCFunctionInfoGeneratorTest[MCFunctionInfoGeneratorTest["lineBeginsWithSlash"] = 103] = "lineBeginsWithSlash";
})(MCFunctionInfoGeneratorTest = exports.MCFunctionInfoGeneratorTest || (exports.MCFunctionInfoGeneratorTest = {}));
/***********
 * Generator for validating MCFunction Files
 *
 * Will ensure:
 *  * command syntax is correct
 *  * no slashes are present before commands
 *
 */
class MCFunctionInfoGenerator {
    id = "MCFUNCTION";
    title = "MC Function Validation";
    getTopicData(topicId) {
        return {
            title: ProjectInfoUtilities_1.default.getTitleFromEnum(MCFunctionInfoGeneratorTest, topicId),
        };
    }
    summarize(info, infoSet) {
        info.invalidCommandSyntaxCount = infoSet.getSummedDataValue(this.id, MCFunctionInfoGeneratorTest.invalidCommandSyntax);
        info.linesWithSlashCount = infoSet.getSummedDataValue(this.id, MCFunctionInfoGeneratorTest.lineBeginsWithSlash);
    }
    async generate(project) {
        const items = [];
        const projItems = project.getItemsCopy();
        for (const item of projItems) {
            if (item.itemType !== IProjectItemData_1.ProjectItemType.MCFunction) {
                continue;
            }
            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 lines = content
                .trim()
                .split(/\r?\n/)
                .filter((line) => line.trim() !== "");
            const commands = lines.map(CommandStructure_1.default.parse);
            items.push(...this.getNewLineBeginsWithSlashesResult(lines, item));
            items.push(...this.getCommandSyntaxResult(commands, item));
        }
        return items;
    }
    getNewLineBeginsWithSlashesResult(lines, item) {
        const results = [];
        for (const line of lines) {
            if (line.startsWith("/")) {
                results.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.warning, this.id, MCFunctionInfoGeneratorTest.lineBeginsWithSlash, this.getTopicData(MCFunctionInfoGeneratorTest.lineBeginsWithSlash).title, item, `Relevant line: ${line}`));
            }
        }
        return results;
    }
    getCommandSyntaxResult(commands, item) {
        const results = [];
        for (const command of commands) {
            if (!command.isNamespaced &&
                !CommandRegistry_1.default.isMinecraftBuiltInCommand(command.fullName) &&
                !CommandRegistry_1.default.isAddOnBlockedCommand(command.fullName)) {
                results.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, MCFunctionInfoGeneratorTest.invalidCommandSyntax, this.getTopicData(MCFunctionInfoGeneratorTest.invalidCommandSyntax).title, item, `${command.fullName} ${command.commandArguments.join(" ")}`));
            }
        }
        return results;
    }
}
exports.default = MCFunctionInfoGenerator;

//# sourceMappingURL=../maps/info/MCFunctionInfoGenerator.js.map
