import chalk from "chalk";
import ora from "ora";
import { captureError, shutdownAnalytics } from "./analytics/lib.js";
const indent = `    ${chalk.gray("|")} `;
async function flushAnalytics() {
    try {
        await shutdownAnalytics();
    }
    catch {
    }
}
function toError(message) {
    return message instanceof Error ? message : new Error(String(message));
}
export function printDiagnosticsToConsole(message, maybeProblem) {
    console.error(chalk.bold.blue(message));
    if (maybeProblem &&
        typeof maybeProblem === "object" &&
        "detail" in maybeProblem &&
        typeof maybeProblem.detail === "string") {
        console.error(indent + chalk.bold.red(JSON.stringify(maybeProblem, null, 2)));
    }
    else if (maybeProblem &&
        typeof maybeProblem === "object" &&
        "title" in maybeProblem &&
        typeof maybeProblem.title === "string") {
        console.error(indent + chalk.bold.red(JSON.stringify(maybeProblem.title, null, 2)));
    }
}
export function printSpinnerToConsole(message) {
    return ora(message).start();
}
export function printWarningToConsole(message) {
    console.error(chalk.yellow(message));
}
export function printCriticalFailureToConsole(message, spinner) {
    if (spinner) {
        spinner?.fail(message);
    }
    else {
        console.error(chalk.bold.red(message));
    }
}
export async function printCriticalFailureToConsoleAndExit(message, spinner) {
    printCriticalFailureToConsole(message, spinner);
    await captureError(toError(message));
    await flushAnalytics();
    process.exit(1);
}
export function printResultToConsole(message) {
    console.log(chalk.bold.green(message));
}
export function printJsonToConsole(value) {
    console.log(JSON.stringify(value, null, 2));
}
export function printTableToConsole(table) {
    console.table(table);
}
export async function printResultToConsoleAndExitGracefully(message, spinner) {
    if (spinner) {
        spinner?.succeed(message);
    }
    else {
        printResultToConsole(message);
    }
    await flushAnalytics();
    process.exit(0);
}
export async function printTableToConsoleAndExitGracefully(table) {
    printTableToConsole(table);
    await flushAnalytics();
    process.exit(0);
}
export async function printCancellationToConsoleAndExitGracefully(message) {
    printDiagnosticsToConsole(message);
    await flushAnalytics();
    process.exit(0);
}
export async function printJsonToConsoleAndExitGracefully(value) {
    printJsonToConsole(value);
    await flushAnalytics();
    process.exit(0);
}
export default function setBlocking() {
    if (typeof process === "undefined")
        return;
    [process.stdout, process.stderr].forEach((_stream) => {
        const stream = _stream;
        if (stream._handle &&
            stream.isTTY &&
            typeof stream._handle.setBlocking === "function") {
            stream._handle.setBlocking(true);
        }
    });
}
export function textOrJson(text) {
    try {
        return JSON.parse(text);
    }
    catch (_e) {
        return text;
    }
}
export class ConsoleOutput {
    child = () => ({
        info: (message) => console.log(message),
        warn: (message) => console.warn(message),
        error: (message) => console.error(message),
    });
}
//# sourceMappingURL=output.js.map