"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Diagnostics = void 0;
const jsx_runtime_1 = require("../../../../prompt/jsx-runtime//jsx-runtime");
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
/** @jsxRuntime automatic */
/** @jsxImportSource ../../../../prompt/jsx-runtime/ */
const vscodeTypesShim_1 = require("../../../../../../../util/common/test/shims/vscodeTypesShim");
const components_1 = require("../../../../prompt/src/components/components");
const prompt_1 = require("../../../../prompt/src/prompt");
const componentsCompletionsPromptFactory_1 = require("../completionsPromptFactory/componentsCompletionsPromptFactory");
function getCode(diagnostic) {
    if (diagnostic.code === undefined) {
        return undefined;
    }
    if (typeof diagnostic.code === 'string') {
        return diagnostic.code;
    }
    if (typeof diagnostic.code === 'number') {
        return diagnostic.code.toString();
    }
    if (typeof diagnostic.code === 'object' && diagnostic.code !== null && diagnostic.code.value) {
        return diagnostic.code.value.toString();
    }
    return undefined;
}
function getRelativePath(tdm, item) {
    return tdm.getRelativePath({ uri: item.uri.toString() }) ?? item.uri.path;
}
const Diagnostics = (props, context) => {
    const [diagnostics, setDiagnostics] = context.useState();
    const [languageId, setLanguageId] = context.useState();
    const [position, setPosition] = context.useState();
    const [document, setDocument] = context.useState();
    context.useData(componentsCompletionsPromptFactory_1.isCompletionRequestData, (data) => {
        if (data.diagnostics !== diagnostics) {
            setDiagnostics(data.diagnostics);
        }
        const normalizedLanguageId = (0, prompt_1.normalizeLanguageId)(data.document.detectedLanguageId);
        if (normalizedLanguageId !== languageId) {
            setLanguageId(normalizedLanguageId);
        }
        if (data.position !== position) {
            setPosition(data.position);
        }
        if (data.document.uri !== document?.uri) {
            setDocument(data.document);
        }
    });
    if (!diagnostics || diagnostics.length === 0 || !languageId) {
        return;
    }
    const validChunks = diagnostics.filter(diagnostic => diagnostic.values.length > 0);
    if (validChunks.length === 0) {
        return;
    }
    // Sort by importance, with the most important first
    validChunks.sort((a, b) => (b.importance ?? 0) - (a.importance ?? 0));
    // Reverse the order so the most important snippet is last. Note, that we don't directly
    // sort in ascending order to handle importance 0 correctly.
    validChunks.reverse();
    return validChunks.map(diagnosticBag => {
        const elements = [];
        elements.push((0, jsx_runtime_1.jsx)(components_1.Text, { source: diagnosticBag, children: `Consider the following ${languageId} diagnostics from ${getRelativePath(props.tdms, diagnosticBag)}:` }, diagnosticBag.id));
        let values = diagnosticBag.values;
        if (document !== undefined && document.uri.toString() === diagnosticBag.uri.toString() && position !== undefined) {
            // Create a copy of the diagnostics to avoid mutating the original array in the context item in case it is used elsewhere.
            values = diagnosticBag.values.slice();
            values.sort((a, b) => {
                const aDist = Math.abs(a.range.start.line - position.line);
                const bDist = Math.abs(b.range.start.line - position.line);
                return aDist - bDist;
            });
        }
        values.forEach(diagnostic => {
            let codeStr = '';
            const code = getCode(diagnostic);
            if (code !== undefined) {
                const source = diagnostic.source ? diagnostic.source.toUpperCase() : '';
                codeStr = ` ${source}${code}`;
            }
            const start = diagnostic.range.start;
            elements.push((0, jsx_runtime_1.jsx)(components_1.Text, { children: `${start.line + 1}:${start.character + 1} - ${vscodeTypesShim_1.DiagnosticSeverity[diagnostic.severity].toLowerCase()}${codeStr}: ${diagnostic.message}` }));
        });
        return (0, jsx_runtime_1.jsx)(components_1.Chunk, { children: elements });
    });
};
exports.Diagnostics = Diagnostics;
//# sourceMappingURL=diagnostics.js.map