"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) {
    server.registerTool('notes_find_linked', {
        description: 'Find all notes in the knowledge graph that link to a specific node in the docs, code, files, or tasks graph. ' +
            'This is a reverse lookup — given a target (e.g. a file, a code symbol, or a doc section), ' +
            'returns all notes that reference it via cross-graph relations. ' +
            'Returns an array of { noteId, title, kind, tags }. ' +
            'Use notes_get to fetch full content of a returned note.',
        inputSchema: {
            targetId: zod_1.z.string().min(1).max(defaults_1.MAX_TARGET_NODE_ID_LEN).describe('Target node ID in the external graph (e.g. "src/config.ts", "src/auth.ts::login", "docs/api.md::Setup")'),
            targetGraph: zod_1.z.enum(['docs', 'code', 'files', 'tasks', 'skills']).describe('Which graph the target belongs to: "docs", "code", "files", "tasks", or "skills"'),
            kind: zod_1.z.string().max(defaults_1.MAX_LINK_KIND_LEN).optional().describe('Filter by relation kind (e.g. "references", "depends_on"). If omitted, returns all relations.'),
            projectId: zod_1.z.string().max(defaults_1.MAX_PROJECT_ID_LEN).optional().describe('Project ID that the target node belongs to. Defaults to the current project.'),
        },
    }, async ({ targetId, targetGraph, kind, projectId }) => {
        const results = mgr.findLinkedNotes(targetGraph, targetId, kind, projectId);
        if (results.length === 0) {
            return { content: [{ type: 'text', text: `No notes linked to ${targetGraph}::${targetId}` }] };
        }
        const clean = (_k, v) => (Array.isArray(v) && v.length === 0 ? undefined : v);
        return { content: [{ type: 'text', text: JSON.stringify(results, clean, 2) }] };
    });
}
