"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('skills_update', {
        description: 'Update an existing skill. Only provided fields are changed. ' +
            'Re-embeds automatically when title or description changes. ' +
            'Pass expectedVersion to enable optimistic locking.',
        inputSchema: {
            skillId: zod_1.z.string().min(1).max(500).describe('Skill ID to update'),
            title: zod_1.z.string().max(defaults_1.MAX_TITLE_LEN).optional().describe('New title'),
            description: zod_1.z.string().max(defaults_1.MAX_DESCRIPTION_LEN).optional().describe('New description'),
            steps: zod_1.z.array(zod_1.z.string().max(defaults_1.MAX_SKILL_STEP_LEN)).max(defaults_1.MAX_SKILL_STEPS_COUNT).optional().describe('Replace steps array'),
            triggers: zod_1.z.array(zod_1.z.string().max(defaults_1.MAX_SKILL_TRIGGER_LEN)).max(defaults_1.MAX_SKILL_TRIGGERS_COUNT).optional().describe('Replace triggers array'),
            inputHints: zod_1.z.array(zod_1.z.string().max(500)).max(100).optional().describe('Replace inputHints array'),
            filePatterns: zod_1.z.array(zod_1.z.string().max(500)).max(100).optional().describe('Replace filePatterns array'),
            tags: zod_1.z.array(zod_1.z.string().max(defaults_1.MAX_TAG_LEN)).max(defaults_1.MAX_TAGS_COUNT).optional().describe('Replace tags array'),
            source: zod_1.z.enum(['user', 'learned']).optional().describe('New source: "user" or "learned"'),
            confidence: zod_1.z.number().min(0).max(1).optional().describe('New confidence score 0–1'),
            expectedVersion: zod_1.z.number().int().positive().optional().describe('Current version for optimistic locking — request fails with version_conflict if the skill has been updated since'),
        },
    }, async ({ skillId, title, description, steps, triggers, inputHints, filePatterns, tags, source, confidence, expectedVersion }) => {
        const patch = {};
        if (title !== undefined)
            patch.title = title;
        if (description !== undefined)
            patch.description = description;
        if (steps !== undefined)
            patch.steps = steps;
        if (triggers !== undefined)
            patch.triggers = triggers;
        if (inputHints !== undefined)
            patch.inputHints = inputHints;
        if (filePatterns !== undefined)
            patch.filePatterns = filePatterns;
        if (tags !== undefined)
            patch.tags = tags;
        if (source !== undefined)
            patch.source = source;
        if (confidence !== undefined)
            patch.confidence = confidence;
        try {
            const updated = await mgr.updateSkill(skillId, patch, expectedVersion);
            if (!updated) {
                return { content: [{ type: 'text', text: 'Skill not found' }], isError: true };
            }
            return { content: [{ type: 'text', text: JSON.stringify({ skillId, 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;
        }
    });
}
