"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('tasks_find_linked', {
        description: 'Find all tasks that link to a specific node in the docs, code, files, or knowledge graph. ' +
            'This is a reverse lookup — given a target, returns all tasks that reference it. ' +
            'Returns an array of { taskId, title, kind, status, priority, tags }. ' +
            'Use tasks_get to fetch full content of a returned task.',
        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/auth.ts" or "guide.md::Setup"'),
            targetGraph: zod_1.z.enum(['docs', 'code', 'files', 'knowledge', 'skills'])
                .describe('Which graph the target belongs to: "docs", "code", "files", "knowledge", or "skills"'),
            kind: zod_1.z.string().max(defaults_1.MAX_LINK_KIND_LEN).optional().describe('Filter by relation kind. 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.findLinkedTasks(targetGraph, targetId, kind, projectId);
        if (results.length === 0) {
            return { content: [{ type: 'text', text: `No tasks 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) }] };
    });
}
