import chalk from "chalk";
/**
 * Read all content from piped stdin.
 * Returns undefined if stdin is a TTY (interactive terminal).
 */
export async function readPipedStdin() {
    // If stdin is a TTY, we're running interactively - don't read stdin
    if (process.stdin.isTTY) {
        return undefined;
    }
    return new Promise((resolve) => {
        let data = "";
        process.stdin.setEncoding("utf8");
        process.stdin.on("data", (chunk) => {
            data += chunk;
        });
        process.stdin.on("end", () => {
            resolve(data.trim() || undefined);
        });
        process.stdin.resume();
    });
}
export function collectSettingsDiagnostics(settingsManager, context) {
    return settingsManager.drainErrors().map(({ scope, error }) => ({
        type: "warning",
        message: `(${context}, ${scope} settings) ${error.message}`,
    }));
}
export function reportDiagnostics(diagnostics) {
    for (const diagnostic of diagnostics) {
        const color = diagnostic.type === "error" ? chalk.red : diagnostic.type === "warning" ? chalk.yellow : chalk.dim;
        const prefix = diagnostic.type === "error" ? "Error: " : diagnostic.type === "warning" ? "Warning: " : "";
        console.error(color(`${prefix}${diagnostic.message}`));
    }
}
export function isTruthyEnvFlag(value) {
    if (!value)
        return false;
    return value === "1" || value.toLowerCase() === "true" || value.toLowerCase() === "yes";
}
function drainWritable(stream) {
    if (stream.destroyed || stream.writable === false || stream.writableEnded) {
        return Promise.resolve();
    }
    return new Promise((resolve) => {
        let settled = false;
        const settle = () => {
            if (settled) {
                return;
            }
            settled = true;
            stream.removeListener("error", onError);
            resolve();
        };
        const onError = () => settle();
        stream.once("error", onError);
        try {
            stream.write("", settle);
        }
        catch {
            settle();
        }
    });
}
export async function drainProcessStdio() {
    await Promise.all([drainWritable(process.stdout), drainWritable(process.stderr)]);
}
//# sourceMappingURL=main-stdio.js.map