/**
 * olam begin — Friendly alias for `olam host-cp start` (Phase D, D1).
 *
 * Additions over `olam host-cp start`:
 *   - Richer banner ("Olam is live. Host CP at http://...")
 *   - Opens Host CP SPA in default browser (best-effort; headless/CI auto-skipped by openUrl).
 *   - --no-browser: explicit opt-out.
 */
import pc from 'picocolors';
import { startHostCp, readToken, HOST_CP_PORT } from './host-cp.js';
import { openUrl } from '@olam/core/src/util/open-url.js';
/**
 * Core begin logic, injectable for testing.
 * Returns true when the host CP started (or was already running) without error.
 */
export async function doBegin(opts, _startFn = startHostCp, _openFn = openUrl) {
    const prevExitCode = process.exitCode;
    await _startFn({ showToken: opts.showToken });
    const failed = process.exitCode !== undefined && process.exitCode !== 0 && process.exitCode !== prevExitCode;
    if (failed)
        return false;
    if (opts.browser) {
        const token = readToken();
        if (token) {
            const base = `http://127.0.0.1:${HOST_CP_PORT}/`;
            console.log(pc.dim(`Opening ${base}`));
            _openFn(`${base}?token=${token}`);
        }
    }
    return true;
}
export function registerBegin(program) {
    program
        .command('begin')
        .description('Start the Olam host control plane (alias: olam host-cp start)')
        .option('--show-token', 'Print token to stdout')
        .option('--no-browser', 'Skip opening browser')
        .action(async (opts) => {
        await doBegin({
            showToken: opts.showToken === true,
            browser: opts.browser !== false,
        });
    });
}
//# sourceMappingURL=begin.js.map