"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, mgr) {
    const graph = mgr.graph;
    server.registerTool('docs_list_snippets', {
        description: 'List code snippets extracted from documentation files. ' +
            'Returns code block nodes with language, symbols, and a content preview. ' +
            'Supports filtering by file, language, and content substring. ' +
            'Use this to discover what code examples exist in the docs.',
        inputSchema: {
            fileId: zod_1.z.string().max(500).optional().describe('Filter by file, e.g. "docs/auth.md"'),
            filter: zod_1.z.string().max(500).optional().describe('Case-insensitive substring match on content'),
            language: zod_1.z.string().max(100).optional().describe('Filter by language, e.g. "typescript"'),
            limit: zod_1.z.number().int().min(1).max(1000).optional().describe('Max results to return (default 10)'),
        },
    }, async ({ fileId, filter, language, limit = defaults_1.LIST_LIMIT_SMALL }) => {
        const lowerFilter = filter?.toLowerCase();
        const lowerLang = language?.toLowerCase();
        const results = [];
        graph.forEachNode((id, attrs) => {
            if (results.length >= limit)
                return;
            if (attrs.language === undefined)
                return; // skip text chunks
            if (fileId && attrs.fileId !== fileId)
                return;
            if (lowerLang && attrs.language !== lowerLang)
                return;
            if (lowerFilter && !attrs.content.toLowerCase().includes(lowerFilter))
                return;
            results.push({
                id,
                fileId: attrs.fileId,
                language: attrs.language,
                symbols: attrs.symbols,
                preview: attrs.content.length > 200 ? attrs.content.slice(0, 200) + '…' : attrs.content,
            });
        });
        if (results.length === 0) {
            return { content: [{ type: 'text', text: 'No code snippets found matching the criteria.' }] };
        }
        const clean = (_k, v) => (Array.isArray(v) && v.length === 0 ? undefined : v);
        return { content: [{ type: 'text', text: JSON.stringify(results, clean, 2) }] };
    });
}
