"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.SignatureHelpProvider = void 0;
const vscode = __importStar(require("vscode"));
const apiParser_1 = require("./apiParser");
// 签名帮助提供者类
class SignatureHelpProvider {
    constructor(cacheManager) {
        this.cacheManager = cacheManager;
        this.apiParser = apiParser_1.ApiParser.getInstance();
    }
    // 提供函数调用时的参数信息和帮助
    async provideSignatureHelp(document, position, token, context) {
        // 获取光标前的文本
        const textBeforeCursor = document.getText(new vscode.Range(new vscode.Position(0, 0), position));
        // 查找最近的未闭合的函数调用
        const functionCall = this.findCurrentFunctionCall(textBeforeCursor);
        if (!functionCall) {
            return undefined;
        }
        // 首先检查是否是API函数
        const apiFunction = this.apiParser.getFunctionByName(functionCall.functionName);
        if (apiFunction) {
            // 创建签名帮助对象
            const signatureHelp = new vscode.SignatureHelp();
            signatureHelp.signatures = [this.createApiSignatureInformation(apiFunction)];
            signatureHelp.activeSignature = 0;
            signatureHelp.activeParameter = functionCall.currentParameterIndex;
            return signatureHelp;
        }
        // 查找自定义函数信息
        const functions = this.cacheManager.findFunctionsByName(functionCall.functionName);
        if (functions.length === 0) {
            return undefined;
        }
        // 创建签名帮助对象
        const signatureHelp = new vscode.SignatureHelp();
        signatureHelp.signatures = functions.map(func => this.createSignatureInformation(func));
        signatureHelp.activeSignature = 0;
        signatureHelp.activeParameter = functionCall.currentParameterIndex;
        return signatureHelp;
    }
    // 查找当前的函数调用
    findCurrentFunctionCall(text) {
        // 简化的实现，实际项目中可能需要更复杂的解析
        // 查找最近的开括号
        let openParenIndex = -1;
        let parenCount = 0;
        for (let i = text.length - 1; i >= 0; i--) {
            const char = text[i];
            if (char === ')') {
                parenCount++;
            }
            else if (char === '(') {
                if (parenCount === 0) {
                    openParenIndex = i;
                    break;
                }
                parenCount--;
            }
        }
        if (openParenIndex === -1) {
            return undefined;
        }
        // 查找函数名
        const beforeParen = text.substring(0, openParenIndex).trim();
        const lastSpaceIndex = beforeParen.lastIndexOf(' ');
        const functionName = lastSpaceIndex === -1 ? beforeParen : beforeParen.substring(lastSpaceIndex + 1);
        // 计算当前参数索引
        let currentParameterIndex = 0;
        let commaCount = 0;
        for (let i = openParenIndex + 1; i < text.length; i++) {
            const char = text[i];
            if (char === ',') {
                commaCount++;
            }
            else if (char === ')') {
                break;
            }
        }
        currentParameterIndex = commaCount;
        return {
            functionName: functionName,
            currentParameterIndex: currentParameterIndex
        };
    }
    // 创建签名信息
    createSignatureInformation(func) {
        const signatureInfo = new vscode.SignatureInformation(func.signature);
        signatureInfo.documentation = new vscode.MarkdownString(`定义于文件: ${func.filePath}`);
        // 解析参数信息
        const paramMatch = func.signature.match(/\(([^)]*)\)/);
        if (paramMatch && paramMatch[1]) {
            const params = paramMatch[1].split(',').map(p => p.trim()).filter(p => p.length > 0);
            signatureInfo.parameters = params.map(param => {
                const paramInfo = new vscode.ParameterInformation(param);
                paramInfo.documentation = `参数: ${param}`;
                return paramInfo;
            });
        }
        return signatureInfo;
    }
    // 创建API签名信息
    createApiSignatureInformation(apiFunc) {
        const signature = this.apiParser.generateFunctionSignature(apiFunc);
        const signatureInfo = new vscode.SignatureInformation(signature);
        signatureInfo.documentation = new vscode.MarkdownString(apiFunc.description);
        // 添加参数信息
        signatureInfo.parameters = apiFunc.params.map(param => {
            const label = param.optional ? `[${param.name}]` : param.name;
            const paramInfo = new vscode.ParameterInformation(label);
            paramInfo.documentation = new vscode.MarkdownString(`${param.type} - ${param.description}`);
            return paramInfo;
        });
        return signatureInfo;
    }
}
exports.SignatureHelpProvider = SignatureHelpProvider;
//# sourceMappingURL=signatureHelpProvider.js.map