"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ycmdSignatureHelp = ycmdSignatureHelp;
const serverManager_1 = require("../serverManager");
const pathUtils_1 = require("../utils/pathUtils");
async function ycmdSignatureHelp(params) {
    try {
        const resolvedFilePath = (0, pathUtils_1.resolveFilePath)(params.filepath);
        if (!params.filepath) {
            return {
                success: false,
                message: "filepath is required",
            };
        }
        if (typeof params.line !== "number" || typeof params.column !== "number") {
            return {
                success: false,
                message: "line and column must be numbers",
            };
        }
        const setupResult = await serverManager_1.ycmdServerManager.setupClientAndNotifyFile({
            filepath: params.filepath,
            fileContents: params.contents,
        });
        if (!setupResult.success) {
            return {
                success: false,
                message: setupResult.message,
            };
        }
        const { client, contents, filetypes } = setupResult;
        const response = await client.getSignatureHelp(resolvedFilePath, params.line, params.column, contents, filetypes);
        let signatureHelp;
        if (response && response.signatures) {
            signatureHelp = {
                signatures: response.signatures.map((sig) => ({
                    label: sig.label,
                    documentation: sig.documentation,
                    parameters: sig.parameters?.map((param) => ({
                        label: param.label,
                        documentation: param.documentation,
                    })),
                })),
                activeSignature: response.activeSignature,
                activeParameter: response.activeParameter,
            };
        }
        else if (response && response.detailed_info) {
            const detailedInfo = response.detailed_info;
            signatureHelp = {
                signatures: [
                    {
                        label: detailedInfo,
                        documentation: response.extra_menu_info,
                    },
                ],
                activeSignature: 0,
                activeParameter: 0,
            };
        }
        else if (response && typeof response === "string") {
            signatureHelp = {
                signatures: [
                    {
                        label: response,
                    },
                ],
                activeSignature: 0,
                activeParameter: 0,
            };
        }
        else {
            return {
                success: true,
                signatureHelp: {
                    signatures: [],
                },
                message: "No signature information available at this location",
            };
        }
        const signatureCount = signatureHelp.signatures.length;
        let message = "No signature information available";
        if (signatureCount > 0) {
            message = `Found ${signatureCount} signature${signatureCount === 1 ? "" : "s"}`;
            if (signatureHelp.activeSignature !== undefined && signatureCount > 1) {
                message += ` (showing signature ${signatureHelp.activeSignature + 1} of ${signatureCount})`;
            }
            if (signatureHelp.activeParameter !== undefined) {
                const activeSignature = signatureHelp.signatures[signatureHelp.activeSignature || 0];
                if (activeSignature?.parameters &&
                    activeSignature.parameters.length > 0) {
                    message += `, parameter ${signatureHelp.activeParameter + 1} of ${activeSignature.parameters.length}`;
                }
            }
        }
        return {
            success: true,
            signatureHelp,
            message,
        };
    }
    catch (error) {
        return {
            success: false,
            message: `Failed to get signature help: ${error.message}`,
        };
    }
}
//# sourceMappingURL=signature.js.map