import { BaseIndexer } from './base-indexer.js';
export class RubyIndexer extends BaseIndexer {
    constructor() {
        super(...arguments);
        this.language = 'ruby';
    }
    extractExports(root, filePath) {
        const exports = [];
        const addExport = (name, type, node) => {
            exports.push({
                name,
                type,
                filePath,
                line: node.startPosition.row + 1,
                column: node.startPosition.column + 1,
                parameters: this.extractParameters(node)
            });
        };
        // 1. Methods
        const methodNodes = root.descendantsOfType('method');
        for (const node of methodNodes) {
            const nameNode = node.childForFieldName('name');
            if (nameNode)
                addExport(nameNode.text, 'method', node);
        }
        // 2. Classes
        const classNodes = root.descendantsOfType('class');
        for (const node of classNodes) {
            const nameNode = node.childForFieldName('name');
            if (nameNode)
                addExport(nameNode.text, 'class', node);
        }
        // 3. Singleton methods (def self.func)
        const singletonNodes = root.descendantsOfType('singleton_method');
        for (const node of singletonNodes) {
            const nameNode = node.childForFieldName('name');
            if (nameNode)
                addExport(nameNode.text, 'method', node);
        }
        return exports;
    }
    extractImports(root) {
        const imports = {};
        // require 'library'
        const callNodes = root.descendantsOfType('call');
        for (const node of callNodes) {
            const methodName = node.childForFieldName('method')?.text;
            if (methodName === 'require' || methodName === 'load') {
                const argsNode = node.childForFieldName('arguments');
                if (argsNode) {
                    const firstArg = argsNode.children.find(c => c.type === 'string' || c.type === 'simple_symbol');
                    if (firstArg) {
                        const fullPath = firstArg.text.replace(/['":]/g, '');
                        const parts = fullPath.split('/');
                        const localName = parts[parts.length - 1];
                        if (localName)
                            imports[localName] = fullPath;
                    }
                }
            }
        }
        return imports;
    }
    extractCalls(root) {
        const calls = [];
        const callNodes = root.descendantsOfType('call');
        for (const node of callNodes) {
            const methodNode = node.childForFieldName('method');
            if (methodNode) {
                const name = methodNode.text;
                const argsNode = node.childForFieldName('arguments');
                const argPositions = [];
                if (argsNode) {
                    for (let i = 0; i < argsNode.childCount; i++) {
                        const arg = argsNode.child(i);
                        if (arg && arg.isNamed && arg.text !== '(' && arg.text !== ')') {
                            argPositions.push({
                                row: arg.startPosition.row,
                                column: arg.startPosition.column
                            });
                        }
                    }
                }
                calls.push({ name, argPositions });
            }
        }
        return calls;
    }
    extractParameters(node) {
        const params = [];
        const paramNode = node.childForFieldName('parameters');
        if (paramNode) {
            const identifiers = paramNode.descendantsOfType('identifier');
            for (const id of identifiers) {
                // Ruby parameters can be complicated, but identifiers in parameters are usually the names
                params.push(id.text);
            }
        }
        return params;
    }
}
//# sourceMappingURL=ruby-indexer.js.map