/**
 * Shared pre-flight check: every authenticated `spectral` subcommand (and the
 * fall-through to spectral) must verify that credentials exist in
 * `~/.spectral/config.json` before proceeding.
 *
 * Extracted from `cli.ts` so `serve` (and any future subcommand) reuses the
 * exact same UX — message wording, exit code, stderr formatting.
 *
 * `requireLogin()` writes to stderr and calls `process.exit(1)` on failure;
 * `checkLogin()` is the testable variant that just reports the result so we
 * can assert on it without killing the test runner.
 */
import { getConfigFile, readConfig } from "./config.js";
// Plain ANSI red. Mirrors the helper in cli.ts; we intentionally keep this
// dependency-light so pre-flight stays cheap on cold start.
function red(s) {
    return process.stdout.isTTY || process.stderr.isTTY
        ? `\x1b[31m${s}\x1b[0m`
        : s;
}
/** Inspect login state without side effects. Safe to call from tests. */
export async function checkLogin() {
    const cfg = await readConfig();
    return { ok: cfg !== null, config: cfg, configFile: getConfigFile() };
}
/**
 * Enforce login: returns the config on success, prints a friendly error and
 * exits 1 on failure. Matches the wording used by the main spawn path.
 */
export async function requireLogin() {
    const result = await checkLogin();
    if (!result.ok || !result.config) {
        process.stderr.write(red(`✗ Not logged in. Run: spectral login\n`));
        process.stderr.write(`  (expected credentials at ${result.configFile})\n`);
        process.exit(1);
    }
    return result.config;
}
