"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.DotCompletionProvider = exports.CompletionProvider = exports.CompletionItemType = void 0;
const vscode = __importStar(require("vscode"));
const apiParser_1 = require("./apiParser");
// 自动完成项类型枚举
var CompletionItemType;
(function (CompletionItemType) {
    CompletionItemType["Function"] = "function";
    CompletionItemType["Variable"] = "variable";
    CompletionItemType["Keyword"] = "keyword";
    CompletionItemType["Constant"] = "constant";
    CompletionItemType["Class"] = "class";
    CompletionItemType["Property"] = "property";
    CompletionItemType["Method"] = "method";
})(CompletionItemType || (exports.CompletionItemType = CompletionItemType = {}));
// 基础关键字列表
const KEYWORDS = [
    'if', 'else', 'while', 'for', 'foreach', 'do', 'switch', 'case', 'default',
    'break', 'continue', 'return', 'yield', 'try', 'catch', 'throw', 'resume',
    'function', 'local', 'let', 'const', 'static', 'enum', 'class', 'extends',
    'constructor', 'typeof', 'instanceof', 'in', 'delete', 'delegate', 'vargc',
    'vargv', 'tailcall', 'clone', 'weakref', 'null', 'true', 'false'
];
// 常量列表
const CONSTANTS = [
    'PI', 'E', 'RAND_MAX', 'CHAR_BIT', 'CHAR_MAX', 'CHAR_MIN', 'SHRT_MAX',
    'SHRT_MIN', 'INT_MAX', 'INT_MIN', 'LONG_MAX', 'LONG_MIN', 'FLT_MAX',
    'FLT_MIN', 'DBL_MAX', 'DBL_MIN'
];
// 基础自动完成提供者类
class CompletionProvider {
    constructor(cacheManager) {
        this.cacheManager = cacheManager;
        this.apiParser = apiParser_1.ApiParser.getInstance();
    }
    // 提供自动完成项目列表
    async provideCompletionItems(document, position, token, context) {
        const completions = [];
        // 添加关键字完成项
        completions.push(...this.getKeywordCompletions());
        // 添加API函数完成项
        completions.push(...this.getApiFunctionCompletions());
        // 添加API类完成项
        completions.push(...this.getApiClassCompletions());
        // 添加API常量完成项
        completions.push(...this.getApiConstantCompletions());
        // 添加常量完成项
        completions.push(...this.getConstantCompletions());
        // 添加跨文件函数完成项
        completions.push(...await this.getCrossFileFunctionCompletions());
        // 添加当前文档中的变量完成项
        completions.push(...this.getDocumentVariableCompletions(document, position));
        return completions;
    }
    // 获取关键字完成项
    getKeywordCompletions() {
        return KEYWORDS.map(keyword => {
            const item = new vscode.CompletionItem(keyword, vscode.CompletionItemKind.Keyword);
            item.detail = 'Squirrel 关键字';
            return item;
        });
    }
    // 获取API函数完成项
    getApiFunctionCompletions() {
        const completions = [];
        // 添加普通函数（不自动填写第一个参数）
        const functions = this.apiParser.getFunctions();
        functions.forEach(func => {
            const item = new vscode.CompletionItem(func.name, vscode.CompletionItemKind.Function);
            item.detail = '内置函数';
            item.documentation = new vscode.MarkdownString(`\`\`\`squirrel\n${this.apiParser.generateFunctionSignature(func)}\n\`\`\`\n${func.description}`);
            // 为函数创建带参数的插入文本
            if (func.params.length > 0) {
                // 普通函数不自动填写第一个参数，用户需要手动输入
                if (func.params.length > 1) {
                    // 从第二个参数开始添加占位符
                    const remainingParams = func.params.slice(1);
                    const paramText = remainingParams.map((param, index) => `\${${index + 1}:${param.name}}`).join(', ');
                    item.insertText = new vscode.SnippetString(`${func.name}(${paramText})`);
                }
                else {
                    // 只有一个参数或无参数
                    item.insertText = new vscode.SnippetString(`${func.name}()`);
                }
            }
            else {
                item.insertText = new vscode.SnippetString(`${func.name}()`);
            }
            completions.push(item);
        });
        // 添加扩展函数（自动填写所有参数）
        const functionEx = this.apiParser.getFunctionEx();
        functionEx.forEach(func => {
            const item = new vscode.CompletionItem(func.name, vscode.CompletionItemKind.Function);
            item.detail = '扩展函数';
            item.documentation = new vscode.MarkdownString(`\`\`\`squirrel\n${this.apiParser.generateFunctionSignature(func)}\n\`\`\`\n${func.description}`);
            // 为函数创建带参数的插入文本
            if (func.params.length > 0) {
                // 扩展函数自动填写所有参数
                const paramText = func.params.map((param, index) => `\${${index + 1}:${param.name}}`).join(', ');
                item.insertText = new vscode.SnippetString(`${func.name}(${paramText})`);
            }
            else {
                item.insertText = new vscode.SnippetString(`${func.name}()`);
            }
            completions.push(item);
        });
        return completions;
    }
    // 获取API类完成项
    getApiClassCompletions() {
        const classes = this.apiParser.getClasses();
        return classes.map(cls => {
            const item = new vscode.CompletionItem(cls.name, vscode.CompletionItemKind.Class);
            item.detail = '内置类';
            item.documentation = new vscode.MarkdownString(`\`\`\`squirrel\n${this.apiParser.generateClassSignature(cls)}\n\`\`\`\n${cls.description}`);
            item.insertText = cls.name;
            return item;
        });
    }
    // 获取API常量完成项
    getApiConstantCompletions() {
        const constants = this.apiParser.getConstants();
        return constants.map(constant => {
            const item = new vscode.CompletionItem(constant.name, vscode.CompletionItemKind.Constant);
            item.detail = `常量 (${constant.category})`;
            item.documentation = new vscode.MarkdownString(`\`\`\`squirrel\n${constant.name} = ${constant.value}\n\`\`\`\n${constant.description}`);
            item.insertText = constant.name;
            return item;
        });
    }
    // 获取常量完成项
    getConstantCompletions() {
        return CONSTANTS.map(constant => {
            const item = new vscode.CompletionItem(constant, vscode.CompletionItemKind.Constant);
            item.detail = 'Squirrel 常量';
            return item;
        });
    }
    // 获取跨文件函数完成项
    async getCrossFileFunctionCompletions() {
        const completions = [];
        const allFunctions = this.cacheManager.getAllFunctions();
        // 按函数名分组，处理重复函数名的情况
        const functionMap = new Map();
        allFunctions.forEach(func => {
            if (!functionMap.has(func.name)) {
                functionMap.set(func.name, []);
            }
            functionMap.get(func.name).push(func);
        });
        // 为每个函数名创建完成项
        for (const [functionName, functions] of functionMap.entries()) {
            if (functions.length === 1) {
                // 单个函数
                const func = functions[0];
                const item = new vscode.CompletionItem(functionName, vscode.CompletionItemKind.Function);
                item.detail = `函数 - ${func.filePath}`;
                item.documentation = new vscode.MarkdownString(`\`\`\`squirrel\n${func.signature}\n\`\`\``);
                // 为函数创建带参数的插入文本
                if (func.parameters.length > 0) {
                    const paramText = func.parameters.map((param, index) => `\${${index + 1}:${param}}`).join(', ');
                    item.insertText = new vscode.SnippetString(`${functionName}(${paramText})`);
                }
                else {
                    item.insertText = new vscode.SnippetString(`${functionName}()`);
                }
                completions.push(item);
            }
            else {
                // 多个同名函数，创建带文件路径信息的完成项
                functions.forEach(func => {
                    const item = new vscode.CompletionItem(`${functionName} (${func.filePath})`, vscode.CompletionItemKind.Function);
                    item.detail = `函数 - ${func.filePath}`;
                    item.documentation = new vscode.MarkdownString(`\`\`\`squirrel\n${func.signature}\n\`\`\``);
                    // 为函数创建带参数的插入文本
                    if (func.parameters.length > 0) {
                        const paramText = func.parameters.map((param, index) => `\${${index + 1}:${param}}`).join(', ');
                        item.insertText = new vscode.SnippetString(`${functionName}(${paramText})`);
                    }
                    else {
                        item.insertText = new vscode.SnippetString(`${functionName}()`);
                    }
                    item.filterText = functionName;
                    completions.push(item);
                });
            }
        }
        return completions;
    }
    // 获取文档中的变量完成项
    getDocumentVariableCompletions(document, position) {
        const completions = [];
        const variableRegex = /(local|static)?\s?(\w+)\s*(?:=|<-)/g;
        const text = document.getText(new vscode.Range(new vscode.Position(0, 0), position));
        let match;
        const variables = new Set(); // 使用Set避免重复
        while ((match = variableRegex.exec(text)) !== null) {
            const variableName = match[2];
            if (variableName && !variables.has(variableName)) {
                variables.add(variableName);
                const item = new vscode.CompletionItem(variableName, vscode.CompletionItemKind.Variable);
                item.detail = '局部变量';
                completions.push(item);
            }
        }
        return completions;
    }
}
exports.CompletionProvider = CompletionProvider;
// 点号触发的自动完成提供者类（用于对象属性和方法）
class DotCompletionProvider {
    async provideCompletionItems(document, position, token, context) {
        // 查找最后一个点号的位置
        const line = document.lineAt(position.line);
        const dotIdx = line.text.lastIndexOf('.', position.character);
        if (dotIdx === -1) {
            return [];
        }
        // 检查是否在注释中，如果在注释中则不提供自动完成
        const commentIndex = line.text.indexOf('//');
        if (commentIndex >= 0 && position.character > commentIndex) {
            return [];
        }
        // 获取点号前的单词（对象名）
        const range = document.getWordRangeAtPosition(position.with(position.line, position.character - 1));
        if (!range) {
            return [];
        }
        const word = document.getText(range);
        const completions = [];
        // 这里应该根据对象类型提供相应的属性和方法
        // 由于这是一个简化实现，我们提供一些常见的对象方法
        if (word === 'string' || word === 'String') {
            completions.push(...this.getStringMethodCompletions());
        }
        else if (word === 'array' || word === 'Array') {
            completions.push(...this.getArrayMethodCompletions());
        }
        else if (word === 'table' || word === 'Table') {
            completions.push(...this.getTableMethodCompletions());
        }
        return completions;
    }
    // 获取字符串方法完成项
    getStringMethodCompletions() {
        const methods = [
            { name: 'len', signature: 'function len()', description: '返回字符串长度' },
            { name: 'tointeger', signature: 'function tointeger()', description: '将字符串转换为整数' },
            { name: 'tofloat', signature: 'function tofloat()', description: '将字符串转换为浮点数' },
            { name: 'tostring', signature: 'function tostring()', description: '返回字符串本身' },
            { name: 'slice', signature: 'function slice(start, end)', description: '返回字符串的子串' },
            { name: 'find', signature: 'function find(substr)', description: '查找子串在字符串中的位置' },
            { name: 'tolower', signature: 'function tolower()', description: '转换为小写' },
            { name: 'toupper', signature: 'function toupper()', description: '转换为大写' }
        ];
        return methods.map(method => {
            const item = new vscode.CompletionItem(method.name, vscode.CompletionItemKind.Method);
            item.detail = '字符串方法';
            item.documentation = new vscode.MarkdownString(`\`\`\`squirrel\n${method.signature}\n\`\`\`\n${method.description}`);
            item.insertText = method.name;
            return item;
        });
    }
    // 获取数组方法完成项
    getArrayMethodCompletions() {
        const methods = [
            { name: 'len', signature: 'function len()', description: '返回数组长度' },
            { name: 'append', signature: 'function append(value)', description: '向数组末尾添加元素' },
            { name: 'push', signature: 'function push(value)', description: '向数组末尾添加元素' },
            { name: 'pop', signature: 'function pop()', description: '移除并返回数组最后一个元素' },
            { name: 'resize', signature: 'function resize(newsize)', description: '调整数组大小' },
            { name: 'sort', signature: 'function sort()', description: '对数组进行排序' },
            { name: 'reverse', signature: 'function reverse()', description: '反转数组元素顺序' }
        ];
        return methods.map(method => {
            const item = new vscode.CompletionItem(method.name, vscode.CompletionItemKind.Method);
            item.detail = '数组方法';
            item.documentation = new vscode.MarkdownString(`\`\`\`squirrel\n${method.signature}\n\`\`\`\n${method.description}`);
            item.insertText = method.name;
            return item;
        });
    }
    // 获取表方法完成项
    getTableMethodCompletions() {
        const methods = [
            { name: 'len', signature: 'function len()', description: '返回表中键值对的数量' },
            { name: 'rawget', signature: 'function rawget(key)', description: '获取指定键的值' },
            { name: 'rawset', signature: 'function rawset(key, value)', description: '设置指定键的值' },
            { name: 'rawdelete', signature: 'function rawdelete(key)', description: '删除指定键' },
            { name: 'setdelegate', signature: 'function setdelegate(delegate)', description: '设置委托表' },
            { name: 'getdelegate', signature: 'function getdelegate()', description: '获取委托表' }
        ];
        return methods.map(method => {
            const item = new vscode.CompletionItem(method.name, vscode.CompletionItemKind.Method);
            item.detail = '表方法';
            item.documentation = new vscode.MarkdownString(`\`\`\`squirrel\n${method.signature}\n\`\`\`\n${method.description}`);
            item.insertText = method.name;
            return item;
        });
    }
}
exports.DotCompletionProvider = DotCompletionProvider;
//# sourceMappingURL=completionProvider.js.map