import * as Sentry from "@sentry/node";
import chalk from "chalk";
import ora from "ora";
import { MAX_WAIT_PENDING_TIME_MS } from "./constants.js";
const indent = `    ${chalk.gray("|")} `;
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 async function printCriticalFailureToConsoleAndExit(message, spinner) {
    if (spinner) {
        spinner?.fail(message);
    }
    else {
        console.error(chalk.bold.red(message));
    }
    await Sentry.close(MAX_WAIT_PENDING_TIME_MS).then(() => {
        process.exit(1);
    });
}
export function printResultToConsole(message) {
    console.log(chalk.bold.green(message));
}
export function printTableToConsole(table) {
    console.table(table);
}
export async function printResultToConsoleAndExitGracefully(message, spinner) {
    if (spinner) {
        spinner?.succeed(message);
    }
    else {
        printResultToConsole(message);
    }
    await Sentry.close(MAX_WAIT_PENDING_TIME_MS).then(() => {
        process.exit(0);
    });
}
export async function printTableToConsoleAndExitGracefully(table) {
    printTableToConsole(table);
    await Sentry.close(MAX_WAIT_PENDING_TIME_MS).then(() => {
        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