// tools/auto-update-handler.ts — MCP tool handlers for SPEC-443 auto-update tools.
// Handles: enable_auto_update (free), check_update_status (free), pin_version (pro)
import { checkForUpdates, enableAutoUpdate, pinVersion } from '../engine/auto-updater/index.js';
import { jsonResult, errText } from './response-helpers.js';
// ---------------------------------------------------------------------------
// Handlers
// ---------------------------------------------------------------------------
/**
 * Checks npm registry and local config for update status.
 * Free tool — no license check.
 */
export async function handleCheckUpdateStatus() {
    try {
        const result = await checkForUpdates();
        return jsonResult(result);
    }
    catch (err) {
        return errText(`Failed to check update status: ${String(err)}`);
    }
}
/**
 * Enables auto-update by switching the MCP config to npx @planu/cli@latest.
 * Free tool — always benefits the user.
 */
export async function handleEnableAutoUpdate(args) {
    const confirm = args.confirm ?? false;
    try {
        const result = await enableAutoUpdate(confirm, args.configPath);
        return jsonResult(result);
    }
    catch (err) {
        return errText(`Failed to enable auto-update: ${String(err)}. ` +
            'Check that the MCP config file exists and is readable.');
    }
}
/**
 * Pins Planu to a specific version in the MCP config.
 * Pro tool — requires license.
 */
export async function handlePinVersion(args) {
    if (!args.version || args.version.trim() === '') {
        return errText('version is required. Example: "1.21.0".');
    }
    const versionPattern = /^\d+\.\d+\.\d+/;
    if (!versionPattern.test(args.version.trim())) {
        return errText(`Invalid version format: "${args.version}". Use semver format, e.g. "1.21.0".`);
    }
    const confirm = args.confirm ?? false;
    try {
        const result = await pinVersion(args.version.trim(), confirm);
        return jsonResult(result);
    }
    catch (err) {
        return errText(`Failed to pin version: ${String(err)}. ` +
            'Check that the MCP config file exists and is readable.');
    }
}
//# sourceMappingURL=auto-update-handler.js.map