"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.register = register;
const zod_1 = require("zod");
const manager_types_1 = require("../../../graphs/manager-types");
const defaults_1 = require("../../../lib/defaults");
function register(server, mgr) {
    server.registerTool('notes_update', {
        description: 'Update an existing note in the knowledge graph. ' +
            'Only the provided fields are changed; others remain unchanged. ' +
            'Re-embeds automatically if title or content changes. ' +
            'Pass expectedVersion to enable optimistic locking.',
        inputSchema: {
            noteId: zod_1.z.string().min(1).max(500).describe('ID of the note to update'),
            title: zod_1.z.string().max(defaults_1.MAX_TITLE_LEN).optional().describe('New title'),
            content: zod_1.z.string().max(defaults_1.MAX_NOTE_CONTENT_LEN).optional().describe('New content'),
            tags: zod_1.z.array(zod_1.z.string().max(defaults_1.MAX_TAG_LEN)).max(defaults_1.MAX_TAGS_COUNT).optional().describe('New tags (replaces existing)'),
            expectedVersion: zod_1.z.number().int().positive().optional().describe('Current version for optimistic locking — request fails with version_conflict if the note has been updated since'),
        },
    }, async ({ noteId, title, content, tags, expectedVersion }) => {
        try {
            const updated = await mgr.updateNote(noteId, { title, content, tags }, expectedVersion);
            if (!updated) {
                return { content: [{ type: 'text', text: 'Note not found' }], isError: true };
            }
            return { content: [{ type: 'text', text: JSON.stringify({ noteId, updated: true }, null, 2) }] };
        }
        catch (err) {
            if (err instanceof manager_types_1.VersionConflictError) {
                return { content: [{ type: 'text', text: JSON.stringify({ error: 'version_conflict', current: err.current, expected: err.expected }) }], isError: true };
            }
            throw err;
        }
    });
}
