/**
 * olam mcp login <service> — Start an OAuth flow for an MCP service.
 */
import { printError, printInfo, printSuccess } from '../../output.js';
import { getMcpAuthClient } from './client.js';
import { spawn } from 'node:child_process';
function openBrowser(url) {
    const cmd = process.platform === 'darwin' ? 'open' :
        process.platform === 'win32' ? 'start' : 'xdg-open';
    try {
        const child = spawn(cmd, [url], { detached: true, stdio: 'ignore' });
        child.unref();
    }
    catch { /* ignore */ }
}
export function registerMcpLogin(cmd) {
    cmd
        .command('login <service>')
        .description('Start OAuth login flow for an MCP service (linear, slack)')
        .option('--label <label>', 'Label for this credential')
        .action(async (service, opts) => {
        const client = getMcpAuthClient();
        const result = await client.oauthStart({ service, label: opts.label });
        if (!result.ok && result.error) {
            printError(result.error);
            process.exitCode = 1;
            return;
        }
        if (result.loginUrl) {
            printInfo('Login URL', result.loginUrl);
            openBrowser(result.loginUrl);
            printInfo('State', result.state ?? '');
            printSuccess(`OAuth flow started for ${service}. Complete in your browser, then run:`);
            console.log(`  olam mcp complete ${service} --state ${result.state ?? '<state>'} --token <access_token>`);
        }
    });
}
//# sourceMappingURL=login.js.map