"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ycmdGoTo = ycmdGoTo;
const serverManager_1 = require("../serverManager");
const getLocations_1 = require("./getLocations");
async function ycmdGoTo(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;
        }
        if (!line || !column) {
            return {
                success: false,
                message: "Either line/column or searchString must be provided",
            };
        }
        if (!params.filepath) {
            return {
                success: false,
                message: "filepath is required",
            };
        }
        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;
        let response;
        switch (params.command) {
            case "GoToDeclaration":
                response = await client.goToDeclaration(resolvedFilePath, line, column, contents, filetypes);
                break;
            case "GoToReferences":
                response = await client.goToReferences(resolvedFilePath, line, column, contents, filetypes);
                break;
            case "GoToDefinition":
            case "GoTo":
            default:
                response = await client.goToDefinition(resolvedFilePath, line, column, contents, filetypes);
                break;
        }
        let locations;
        if (Array.isArray(response)) {
            locations = response.map((loc) => ({
                filepath: loc.filepath,
                line: loc.line_num,
                column: loc.column_num,
                description: loc.description,
            }));
        }
        else if (response && response.filepath) {
            locations = [
                {
                    filepath: response.filepath,
                    line: response.line_num,
                    column: response.column_num,
                    description: response.description,
                },
            ];
        }
        else {
            locations = [];
        }
        if (locations.length === 0) {
            return {
                success: true,
                locations: [],
                message: `No locations found for ${params.command}`,
            };
        }
        return {
            success: true,
            locations,
            message: `Found ${locations.length} locations for ${params.command}${locations.length === 1 ? "" : "s"}`,
        };
    }
    catch (error) {
        return {
            success: false,
            message: `Failed to get ${params.command}: ${error.message}`,
        };
    }
}
//# sourceMappingURL=goto.js.map