"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.register = register;
const zod_1 = require("zod");
const defaults_1 = require("../../../lib/defaults");
function register(server, docMgr, codeMgr) {
    const docGraph = docMgr.graph;
    const codeGraph = codeMgr.graph;
    server.registerTool('docs_cross_references', {
        description: 'Find all references to a symbol across both code and documentation graphs. ' +
            'Returns: definitions (from CodeGraph — where the symbol is defined), ' +
            'documentation (text sections in docs that contain examples using the symbol), ' +
            'and examples (code blocks in docs that contain the symbol). ' +
            'This is the most comprehensive way to understand a symbol — combining source code, docs, and examples.',
        inputSchema: {
            symbol: zod_1.z.string().min(1).max(defaults_1.MAX_SEARCH_QUERY_LEN).describe('Symbol name to look up, e.g. "createUser", "AuthService"'),
        },
    }, async ({ symbol }) => {
        // 1. Search CodeGraph for definitions
        const definitions = [];
        const symbolLower = symbol.toLowerCase();
        codeGraph.forEachNode((id, attrs) => {
            if (attrs.name === symbol || attrs.name.toLowerCase() === symbolLower) {
                definitions.push({
                    id,
                    fileId: attrs.fileId,
                    kind: attrs.kind,
                    name: attrs.name,
                    signature: attrs.signature,
                    docComment: attrs.docComment,
                    startLine: attrs.startLine,
                    endLine: attrs.endLine,
                });
            }
        });
        // 2. Search DocGraph for code blocks containing the symbol
        const examples = [];
        const documentation = [];
        const seenDocs = new Set();
        docGraph.forEachNode((id, attrs) => {
            if (attrs.symbols.length === 0)
                return;
            if (!attrs.symbols.some(s => s === symbol || s.toLowerCase() === symbolLower))
                return;
            examples.push({
                id,
                fileId: attrs.fileId,
                language: attrs.language,
                symbols: attrs.symbols,
                content: attrs.content,
            });
            // Find parent text section for documentation context
            for (const neighbor of docGraph.inNeighbors(id)) {
                if (seenDocs.has(neighbor))
                    continue;
                const nAttrs = docGraph.getNodeAttributes(neighbor);
                if (nAttrs.fileId === attrs.fileId && nAttrs.language === undefined && nAttrs.level < attrs.level) {
                    seenDocs.add(neighbor);
                    documentation.push({
                        id: neighbor,
                        fileId: nAttrs.fileId,
                        title: nAttrs.title,
                        content: nAttrs.content,
                    });
                }
            }
        });
        if (definitions.length === 0 && examples.length === 0) {
            return { content: [{ type: 'text', text: `No references found for symbol: ${symbol}` }] };
        }
        return {
            content: [{
                    type: 'text',
                    text: JSON.stringify({ definitions, documentation, examples }, null, 2),
                }],
        };
    });
}
