"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
const StorageUtilities_1 = require("../storage/StorageUtilities");
const ZipStorage_1 = require("../storage/ZipStorage");
const eslint_1 = require("eslint");
const ProjectInfoItem_1 = require("../info/ProjectInfoItem");
const IInfoItemData_1 = require("../info/IInfoItemData");
class JsEslintInfoGenerator {
    constructor() {
        this.id = "ESLINT";
        this.title = "JavaScript Lint";
    }
    getTopicData(topicId) {
        switch (topicId) {
            case 100:
                return { title: "ESLint Error" };
        }
        return {
            title: topicId.toString(),
        };
    }
    summarize(info, infoSet) { }
    async generate(project, contentIndex) {
        const items = [];
        var foundError = false;
        if (project.projectFolder) {
            foundError = await this.generateFromFolder(project, project.projectFolder, items);
        }
        if (foundError) {
            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.testCompleteFail, this.id, 261, "JS ESLint check fails."));
        }
        else if (foundError) {
            items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.testCompleteSuccess, this.id, 262, "JS ESLint check passes."));
        }
        return items;
    }
    async generateFromFolder(project, folder, items) {
        await folder.load();
        var foundError = false;
        for (const fileName in folder.files) {
            const baseType = StorageUtilities_1.default.getTypeFromName(fileName);
            const file = folder.files[fileName];
            if ((baseType === "js" || baseType === "mjs" || baseType === "ts") && file) {
                if (await this.generateFromFile(project, file, items)) {
                    foundError = true;
                }
            }
            else if (StorageUtilities_1.default.isContainerFile(fileName) && file) {
                await file.loadContent();
                if (file.content && file.content instanceof Uint8Array) {
                    if (!file.fileContainerStorage) {
                        const zipStorage = new ZipStorage_1.default();
                        zipStorage.storagePath = file.storageRelativePath + "#";
                        await zipStorage.loadFromUint8Array(file.content, file.name);
                        file.fileContainerStorage = zipStorage;
                    }
                    if (file.fileContainerStorage) {
                        await this.generateFromFolder(project, file.fileContainerStorage.rootFolder, items);
                    }
                }
            }
        }
        for (const folderName in folder.folders) {
            const childFolder = folder.folders[folderName];
            if (childFolder && !folder.errorStatus) {
                if (await this.generateFromFolder(project, childFolder, items)) {
                    foundError = true;
                }
            }
        }
        return foundError;
    }
    async generateFromFile(project, file, items) {
        const srPath = file.storageRelativePath.toLowerCase();
        var foundErrors = false;
        if (srPath) {
            const pi = new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.info, this.id, 1, "JS file: " + file.storageRelativePath);
            await file.loadContent(false);
            const fileContent = file.content;
            if (fileContent && typeof fileContent === "string") {
                const eslint = new eslint_1.ESLint({
                    useEslintrc: false,
                    overrideConfig: {
                        extends: ["eslint:recommended"],
                        parserOptions: {
                            sourceType: "module",
                            ecmaVersion: "latest",
                        },
                        env: {
                            es2022: true,
                            node: true,
                        },
                    },
                });
                const results = await eslint.lintText(fileContent);
                for (const result of results) {
                    for (const message of result.messages) {
                        items.push(new ProjectInfoItem_1.default(IInfoItemData_1.InfoItemType.error, this.id, 100, message.message, undefined, undefined, undefined, JSON.stringify(message)));
                        foundErrors = true;
                    }
                }
            }
            items.push(pi);
        }
        return foundErrors;
    }
    addSubTags(pi, suffix, rootTag) {
        if (!rootTag) {
            return;
        }
        for (const obj in rootTag) {
            if (obj) {
                pi.incrementFeature(obj + " " + suffix);
            }
        }
    }
}
exports.default = JsEslintInfoGenerator;

//# sourceMappingURL=../maps/local/JsEslintInfoGenerator.js.map
