/**
 * olam mcp install — Add olam to Claude Code's MCP servers via `claude mcp add`.
 *
 * Auto-detects whether `olam` is on PATH: prefers globally-installed olam
 * (no npx cold-start cost) and falls back to `npx -y @pleri/olam-cli mcp serve`.
 *
 * Symmetric with `olam mcp uninstall` (B2 of this plan).
 */
import { printError, printSuccess } from '../../output.js';
import { DEFAULT_CLAUDE_SHELL_DEPS, REMEDY_CLAUDE_MISSING, isOnPath, normaliseScope, } from './install-shared.js';
const NPM_PACKAGE_NAME = '@pleri/olam-cli';
export function buildClaudeMcpAddArgs(scope, useGlobal) {
    const target = useGlobal
        ? { cmd: 'olam', args: ['mcp', 'serve'] }
        : { cmd: 'npx', args: ['-y', NPM_PACKAGE_NAME, 'mcp', 'serve'] };
    return {
        command: 'claude',
        args: ['mcp', 'add', 'olam', '--scope', scope, '--', target.cmd, ...target.args],
    };
}
export async function runInstall(opts, deps = DEFAULT_CLAUDE_SHELL_DEPS) {
    if (!isOnPath(deps, 'claude')) {
        printError(REMEDY_CLAUDE_MISSING);
        return 1;
    }
    const useGlobal = isOnPath(deps, 'olam');
    const { command, args } = buildClaudeMcpAddArgs(opts.scope, useGlobal);
    deps.log(`Wiring: ${command} ${args.join(' ')}`);
    const result = deps.spawn(command, args, {
        encoding: 'utf8',
        stdio: ['ignore', 'pipe', 'pipe'],
    });
    if (result.status !== 0) {
        const detail = result.stderr?.trim() || result.stdout?.trim() || '(no output)';
        printError(`claude mcp add failed (rc=${result.status ?? 'unknown'}): ${detail}`);
        return result.status ?? 1;
    }
    printSuccess(`olam MCP registered with claude (--scope ${opts.scope}; command: ${useGlobal ? 'olam' : 'npx'}).`);
    return 0;
}
export function registerMcpInstall(cmd) {
    cmd
        .command('install')
        .description('Add olam to Claude Code as an MCP server (auto-detects whether `olam` is on PATH; shells to `claude mcp add`)')
        .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 runInstall({ scope });
        if (rc !== 0) {
            process.exitCode = rc;
        }
    });
}
//# sourceMappingURL=install.js.map