"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDiagnosticsFromContextItems = getDiagnosticsFromContextItems;
const textDocumentManager_1 = require("../../textDocumentManager");
const contextProviderStatistics_1 = require("../contextProviderStatistics");
const contextItemSchemas_1 = require("./contextItemSchemas");
const CONTENT_EXCLUDED_EXPECTATION = 'content_excluded';
async function getDiagnosticsFromContextItems(accessor, completionId, resolvedContextItems) {
    const diagnosticBags = (0, contextItemSchemas_1.filterContextItemsByType)(resolvedContextItems, 'DiagnosticBag');
    // Set expectations for the diagnostics provided.
    for (const item of diagnosticBags) {
        setupExpectationsForDiagnosticBags(accessor, completionId, item.data, item.providerId);
    }
    // Flatten and sort the traits by importance.
    // TODO: once we deprecate the old API, importance should also dictate elision.
    const allUris = new Set();
    const diagnosticBagsWithProviderId = [];
    for (const item of diagnosticBags) {
        for (const diagnosticBag of item.data) {
            allUris.add(diagnosticBag.uri.toString());
            diagnosticBagsWithProviderId.push({ providerId: item.providerId, bag: diagnosticBag });
        }
    }
    if (diagnosticBagsWithProviderId.length === 0) {
        return [];
    }
    const contextProviderStatistics = accessor.get(contextProviderStatistics_1.ICompletionsContextProviderService);
    const tdm = accessor.get(textDocumentManager_1.ICompletionsTextDocumentManagerService);
    const validationMap = new Map();
    await Promise.all(Array.from(allUris).map(async (uri) => {
        validationMap.set(uri, await tdm.getTextDocumentValidation({ uri }));
    }));
    const statistics = contextProviderStatistics.getStatisticsForCompletion(completionId);
    const filteredDiagnosticBags = diagnosticBagsWithProviderId
        .filter(item => {
        const isValid = validationMap.get(item.bag.uri.toString())?.status === 'valid';
        // Set expectations regardless of validity
        if (isValid) {
            statistics.addExpectations(item.providerId, [[item.bag, 'included']]);
        }
        else {
            statistics.addExpectations(item.providerId, [[item.bag, CONTENT_EXCLUDED_EXPECTATION]]);
        }
        return isValid;
    })
        .map(item => item.bag);
    return filteredDiagnosticBags.sort((a, b) => (a.importance ?? 0) - (b.importance ?? 0));
}
function setupExpectationsForDiagnosticBags(accessor, completionId, bags, providerId) {
    const statistics = accessor.get(contextProviderStatistics_1.ICompletionsContextProviderService).getStatisticsForCompletion(completionId);
    bags.forEach(bag => {
        statistics.addExpectations(providerId, [[bag, 'included']]);
    });
}
//# sourceMappingURL=diagnostics.js.map