"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkspaceLiaison = void 0;
exports.toExtensionsToLanguageMap = toExtensionsToLanguageMap;
const node_path_1 = __importDefault(require("node:path"));
// noinspection JSMismatchedCollectionQueryUpdate (IntelliJ is confused about how I am setting the private values, suppressing warnings)
class WorkspaceLiaison {
    workspace;
    selectedLanguages;
    extensionToLanguageMap;
    relevantLanguageToFilesMap;
    constructor(workspace, selectedLanguages, extensionToLanguageMap) {
        this.workspace = workspace;
        this.selectedLanguages = new Set(selectedLanguages);
        this.extensionToLanguageMap = extensionToLanguageMap;
    }
    getWorkspace() {
        return this.workspace;
    }
    async getRelevantLanguages() {
        return [...(await this.getRelevantLanguageToFilesMap()).keys()].sort();
    }
    async getRelevantLanguageToFilesMap() {
        if (this.relevantLanguageToFilesMap) {
            return this.relevantLanguageToFilesMap;
        }
        if (!this.workspace) {
            this.relevantLanguageToFilesMap = new Map([...this.selectedLanguages].map(lang => [lang, []]));
            return this.relevantLanguageToFilesMap;
        }
        const files = await this.workspace.getTargetedFiles();
        this.relevantLanguageToFilesMap = new Map();
        for (const file of files) {
            const fileExt = node_path_1.default.extname(file).toLowerCase();
            const lang = this.extensionToLanguageMap.get(fileExt);
            if (!lang || !this.selectedLanguages.has(lang)) {
                continue;
            }
            if (!this.relevantLanguageToFilesMap.has(lang)) {
                this.relevantLanguageToFilesMap.set(lang, []);
            }
            this.relevantLanguageToFilesMap.get(lang).push(file);
        }
        return this.relevantLanguageToFilesMap;
    }
}
exports.WorkspaceLiaison = WorkspaceLiaison;
// Converts our file_extensions map which associates languages to file extensions to a map that associates
// file extensions to languages. This will speed up processing later, which is why we do this conversion.
function toExtensionsToLanguageMap(langToFileExtsMap) {
    const extensionsToLanguageMap = new Map();
    for (const language of Object.keys(langToFileExtsMap)) {
        for (const fileExt of langToFileExtsMap[language]) {
            extensionsToLanguageMap.set(fileExt, language);
        }
    }
    return extensionsToLanguageMap;
}
//# sourceMappingURL=utils.js.map