import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
import * as Function from "effect/Function";
/**
 * Logs a cause with a message.
 *
 * @param cause - The cause to log.
 * @param message - The message to log.
 */
export const logCauseWith = Function.dual(2, (cause, message) => Effect.logError(message, prettyCause(cause)));
/**
 * Pretty prints a cause.
 *
 * @param cause - The cause to pretty print.
 * @returns The pretty printed cause.
 */
export const prettyCause = (cause) => {
    if (Cause.isInterruptedOnly(cause)) {
        return "All fibers interrupted without errors.";
    }
    const stack = Cause.prettyErrors(cause)
        .flatMap((error) => {
        const output = (error.stack ?? "").split("\n").filter((line) => !line.trim().startsWith("at ")) ?? [];
        return error.cause ? [output, ...renderCause(error.cause)] : [output];
    })
        .filter((lines) => lines.length > 0);
    if (stack.length <= 1) {
        return stack[0]?.join("\n") ?? "";
    }
    return stack
        .map((lines, index, array) => {
        const prefix = index === 0 ? "┌ " : index === array.length - 1 && lines.length === 1 ? "└ " : "├ ";
        const output = lines.map((line, index) => `${index === 0 ? prefix : "│ "}${line}`).join("\n");
        return index === array.length - 1 ? output : `${output}\n│`;
    })
        .join("\n");
};
/**
 * Renders a cause.
 *
 * @param cause - The cause to render.
 * @returns The rendered cause.
 */
const renderCause = (cause) => {
    const output = (cause.stack ?? "").split("\n").filter((line) => !line.trim().startsWith("at ")) ?? [];
    return cause.cause ? [output, ...renderCause(cause.cause)] : [output];
};
//# sourceMappingURL=Utils.js.map