"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasicValidators = void 0;
const Utilities_1 = require("../core/Utilities");
const StorageUtilities_1 = require("./StorageUtilities");
const obscenity_1 = require("obscenity");
class BasicValidators {
    static contentMatcher = undefined;
    static async isFolderSharingValid(folder, isChildFolder) {
        if (!this.isFolderNameOKForSharing(folder.name)) {
            return folder.name + " is an unsupported folder name.";
        }
        await folder.load();
        if (!isChildFolder && folder.fileCount > 0) {
            return "Folder that contains files at the root.";
        }
        for (const childFileName in folder.files) {
            const childFile = folder.files[childFileName];
            if (childFile) {
                const result = this.isFileNameOKForSharing(childFile.name);
                if (!result) {
                    return childFile.name + " is an unsupported file name.";
                }
                const res = await this.hasStrongLanguageContent(childFile);
                if (res) {
                    return childFile.name + " has unsupported content.";
                }
            }
        }
        for (const childFolderName in folder.folders) {
            const childFolder = folder.folders[childFolderName];
            if (childFolder) {
                const result = await this.isFolderSharingValid(childFolder, true);
                if (result) {
                    return result;
                }
            }
        }
        return undefined;
    }
    static isFileNameOKForSharing(fileName) {
        fileName = fileName.toLowerCase();
        const ext = StorageUtilities_1.default.getTypeFromName(fileName);
        if (ext !== "ts" && ext !== "json" && ext !== "lang") {
            return false;
        }
        if (fileName.startsWith(".") ||
            fileName.startsWith("just.config") ||
            fileName.endsWith(".config.ts") ||
            fileName.endsWith(".config.js") ||
            (fileName.startsWith("manifest") && fileName.endsWith("json")) ||
            (fileName.startsWith("package") && fileName.endsWith("json"))) {
            return false;
        }
        if (!Utilities_1.default.isUsableAsObjectKey(fileName)) {
            return false;
        }
        return true;
    }
    static isFolderNameOKForSharing(folderName) {
        if (folderName.startsWith(".") ||
            folderName === "lib" ||
            folderName === "node_modules" ||
            folderName === ".git" ||
            folderName === "dist" ||
            folderName === "build") {
            return false;
        }
        return true;
    }
    static async hasStrongLanguageContent(file) {
        await file.loadContent();
        if (file.isBinary) {
            return undefined;
        }
        const str = file.content;
        if (!str) {
            return undefined;
        }
        if (typeof str !== "string") {
            return undefined;
        }
        if (str.length < 1) {
            return undefined;
        }
        const content = str.toLowerCase();
        if (this.contentMatcher === undefined) {
            this.contentMatcher = new obscenity_1.RegExpMatcher({
                ...obscenity_1.englishDataset.build(),
                ...obscenity_1.englishRecommendedTransformers,
            });
        }
        if (this.contentMatcher.hasMatch(content)) {
            const matches = this.contentMatcher.getAllMatches(content);
            let strMatches = [];
            const strMatchesSet = new Set();
            for (let i = 0; i < matches.length && i < 100; i++) {
                const match = matches[i];
                if (match) {
                    const result = content.substring(match.startIndex, match.endIndex + 1);
                    if (!strMatchesSet.has(result)) {
                        strMatches.push(result);
                        strMatchesSet.add(result);
                    }
                }
            }
            return strMatches.join(", ");
        }
        return undefined;
    }
}
exports.BasicValidators = BasicValidators;

//# sourceMappingURL=../maps/storage/BasicValidators.js.map
