"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ycmdCompletion = ycmdCompletion;
const serverManager_1 = require("../serverManager");
const getLocations_1 = require("./getLocations");
async function ycmdCompletion(params) {
    try {
        let line = params.line;
        let column = params.column;
        if (params.searchString && (!line || !column)) {
            const location = await (0, getLocations_1.resolveStringToLocation)(params.filepath, params.searchString, params.contents, params.matchType || "exact");
            if (!location) {
                return {
                    success: false,
                    message: `Could not find "${params.searchString}" in file ${params.filepath}`,
                };
            }
            line = location.line;
            column = location.column + params.searchString.length;
        }
        if (!line || !column) {
            return {
                success: false,
                message: "Either line/column or searchString must be provided",
            };
        }
        const setupResult = await serverManager_1.ycmdServerManager.setupClientAndNotifyFile({
            filepath: params.filepath,
            fileContents: params.contents,
        });
        if (!setupResult.success) {
            return {
                success: false,
                message: setupResult.message,
            };
        }
        const { client, resolvedFilePath, contents, filetypes } = setupResult;
        const response = await client.getCompletions({
            filepath: resolvedFilePath,
            line_num: line,
            column_num: column,
            file_data: {
                [resolvedFilePath]: {
                    contents,
                    filetypes,
                },
            },
            force_semantic: params.forceSemantic,
        });
        const completions = response.completions.map((comp) => ({
            text: comp.insertion_text,
            displayText: comp.menu_text,
            detail: comp.extra_menu_info,
            documentation: comp.detailed_info,
            kind: comp.kind,
        }));
        return {
            success: true,
            completions,
            completionStartColumn: response.completion_start_column,
            message: `Found ${completions.length} completions`,
        };
    }
    catch (error) {
        return {
            success: false,
            message: `Failed to get completions: ${error.message}`,
        };
    }
}
//# sourceMappingURL=completion.js.map