"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ycmdDiagnostics = ycmdDiagnostics;
const serverManager_1 = require("../serverManager");
const getLocations_1 = require("./getLocations");
async function ycmdDiagnostics(params) {
    try {
        const setupResult = await serverManager_1.ycmdServerManager.setupClientAndNotifyFile({
            filepath: params.filepath,
            fileContents: params.fileContents,
        });
        if (!setupResult.success) {
            return {
                success: false,
                message: setupResult.message,
            };
        }
        const { client, resolvedFilePath, contents, filetypes } = setupResult;
        let line_num = params.line || 1;
        let column_num = params.column || 1;
        if (params.searchString) {
            const location = await (0, getLocations_1.resolveStringToLocation)(params.filepath, params.searchString, contents, params.matchType || "exact");
            if (location) {
                line_num = location.line;
                column_num = location.column;
            }
        }
        const response = await client.getDiagnostics(resolvedFilePath, contents, filetypes, line_num, column_num);
        console.log("Diagnostics response:", response);
        if (!response || !Array.isArray(response)) {
            return {
                success: true,
                diagnostics: [],
                message: "No diagnostic errors found for file",
            };
        }
        const diagnostics = response.map((diag) => ({
            kind: diag.kind,
            text: diag.text,
            location: {
                line: diag.location.line_num,
                column: diag.location.column_num,
            },
            location_extent: diag.location_extent
                ? {
                    start: {
                        line: diag.location_extent.start.line_num,
                        column: diag.location_extent.start.column_num,
                    },
                    end: {
                        line: diag.location_extent.end.line_num,
                        column: diag.location_extent.end.column_num,
                    },
                }
                : undefined,
            ranges: diag.ranges?.map((range) => ({
                start: {
                    line: range.start.line_num,
                    column: range.start.column_num,
                },
                end: {
                    line: range.end.line_num,
                    column: range.end.column_num,
                },
            })),
            fixit_available: diag.fixit_available,
        }));
        const errorCount = diagnostics.filter((d) => d.kind === "ERROR").length;
        const warningCount = diagnostics.filter((d) => d.kind === "WARNING").length;
        const infoCount = diagnostics.filter((d) => d.kind === "INFO").length;
        let message = "No diagnostics found";
        if (diagnostics.length > 0) {
            const parts = [];
            if (errorCount > 0)
                parts.push(`${errorCount} error${errorCount === 1 ? "" : "s"}`);
            if (warningCount > 0)
                parts.push(`${warningCount} warning${warningCount === 1 ? "" : "s"}`);
            if (infoCount > 0)
                parts.push(`${infoCount} info message${infoCount === 1 ? "" : "s"}`);
            message = `Found ${parts.join(", ")}`;
        }
        return {
            success: true,
            diagnostics,
            message,
        };
    }
    catch (error) {
        return {
            success: false,
            message: `Failed to get diagnostics: ${error.message}`,
        };
    }
}
//# sourceMappingURL=diagnostics.js.map