/**
 * olam memory uninstall — Remove `agentmemory` from the operator's host
 * claude. Symmetric with `install`. Idempotent — "not registered" is
 * success-as-noop.
 *
 * Plan reference: docs/plans/olam-agent-memory-distributed/phase-a-tasks.md A4
 */
import { printError, printSuccess, printWarning } from '../../output.js';
import { DEFAULT_CLAUDE_SHELL_DEPS, REMEDY_CLAUDE_MISSING, isOnPath, looksLikeNotInstalled, normaliseScope, } from '../mcp/install-shared.js';
const MCP_NAME = 'agentmemory';
export function buildClaudeMcpRemoveArgs(scope) {
    return {
        command: 'claude',
        args: ['mcp', 'remove', MCP_NAME, '--scope', scope],
    };
}
export async function runUninstall(opts, deps = DEFAULT_CLAUDE_SHELL_DEPS) {
    if (!isOnPath(deps, 'claude')) {
        printError(REMEDY_CLAUDE_MISSING);
        return 1;
    }
    const { command, args } = buildClaudeMcpRemoveArgs(opts.scope);
    deps.log(`Unwiring: ${command} ${args.join(' ')}`);
    const result = deps.spawn(command, args, {
        encoding: 'utf8',
        stdio: ['ignore', 'pipe', 'pipe'],
    });
    if (result.status === 0) {
        printSuccess(`agentmemory MCP removed from claude (--scope ${opts.scope}).`);
        return 0;
    }
    const stderr = result.stderr?.trim() ?? '';
    const stdout = result.stdout?.trim() ?? '';
    const combined = `${stderr}\n${stdout}`.trim();
    if (looksLikeNotInstalled(combined)) {
        printWarning(`agentmemory MCP not registered with claude (--scope ${opts.scope}); nothing to remove.`);
        return 0;
    }
    printError(`claude mcp remove failed (rc=${result.status ?? 'unknown'}): ${combined || '(no output)'}`);
    return result.status ?? 1;
}
export function registerMemoryUninstall(cmd) {
    cmd
        .command('uninstall')
        .description('Remove agentmemory from the operator\'s host claude (symmetric with `install`; idempotent)')
        .option('--scope <scope>', 'claude mcp scope: user | project | local', 'user')
        .action(async (rawOpts) => {
        const scope = normaliseScope(rawOpts.scope);
        if (scope === null) {
            printError(`--scope must be one of: user, project, local (got: ${rawOpts.scope})`);
            process.exitCode = 1;
            return;
        }
        const rc = await runUninstall({ scope });
        if (rc !== 0) {
            process.exitCode = rc;
        }
    });
}
//# sourceMappingURL=uninstall.js.map