/**
 * OAuth login for CLI machine ownership (called from interactive `spectral login`).
 *
 * Thin wrapper around the shared `runOAuthFlow()` in auth-helper.ts, adding
 * interactive messaging specific to the `spectral login` command.
 */
import pc from "picocolors";
import { getConfigFile, readConfig } from "../config.js";
import { runOAuthFlow } from "../auth-helper.js";
export async function runLoginOAuth() {
    process.stdout.write(pc.bold("Spectral login (OAuth)\n"));
    process.stdout.write(pc.dim(`Opens a browser to authorize the CLI. The JWT is stored at ${getConfigFile()}.\n\n`));
    // Load existing config to pass apiUrl
    const existingCfg = await readConfig();
    const apiUrl = existingCfg?.apiUrl ?? undefined;
    process.stdout.write(pc.dim("Waiting for authorization...\n"));
    const result = await runOAuthFlow(apiUrl);
    if (!result.ok) {
        process.stderr.write(pc.red(`✗ ${result.reason}\n`));
        process.exit(1);
    }
    process.stdout.write(pc.green("✓ CLI authorized.\n"));
    process.stdout.write(pc.dim(`Saved user JWT to ${getConfigFile()}\n`));
    process.stdout.write(pc.dim("Run `spectral serve` to register your machine with owner info.\n"));
}
