"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.prettyPrint = void 0;
const colors_1 = __importDefault(require("colors"));
const stepTag = (step) => colors_1.default.bold(colors_1.default.cyan(step));
const timestampTag = (timestamp) => colors_1.default.gray(timestamp);
const traceIdTag = (traceId) => colors_1.default.gray(traceId);
const levelTags = {
    error: colors_1.default.red('[ERROR]'),
    info: colors_1.default.blue('[INFO]'),
    warn: colors_1.default.yellow('[WARN]'),
    debug: colors_1.default.gray('[DEBUG]'),
    trace: colors_1.default.gray('[TRACE]'),
};
const numericTag = (value) => colors_1.default.green(value);
const stringTag = (value) => colors_1.default.cyan(value);
const booleanTag = (value) => colors_1.default.blue(value);
const arrayBrackets = ['[', ']'].map((s) => colors_1.default.gray(s));
const objectBrackets = ['{', '}'].map((s) => colors_1.default.gray(s));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const prettyPrintObject = (obj, depth = 0, parentIsLast = false, prefix = '') => {
    const tab = prefix + (depth === 0 ? '' : parentIsLast ? '│ ' : '│ ');
    if (depth > 2) {
        return `${tab} └ ${colors_1.default.gray('[...]')}`;
    }
    const entries = Object.entries(obj);
    return entries
        .map(([key, value], index) => {
        const isLast = index === entries.length - 1;
        const isObject = typeof value === 'object' && value !== null;
        const prefix = isLast && !isObject ? '└' : '├';
        if (isObject) {
            const subObject = prettyPrintObject(value, depth + 1, isLast, tab);
            const [start, end] = Array.isArray(value) ? arrayBrackets : objectBrackets;
            return `${tab}${prefix} ${key}: ${start}\n${subObject}\n${tab}${isLast ? '└' : '│'} ${end}`;
        }
        let printedValue = value;
        if (typeof value === 'number') {
            printedValue = numericTag(String(value));
        }
        else if (typeof value === 'boolean') {
            printedValue = booleanTag(String(value));
        }
        else if (typeof value === 'string') {
            printedValue = stringTag(value);
        }
        return `${tab}${prefix} ${key}: ${printedValue}`;
    })
        .join('\n');
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const prettyPrint = (json, excludeDetails = false) => {
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    const { time, traceId, msg, flows, level, step, ...details } = json;
    const levelTag = levelTags[level];
    const timestamp = timestampTag(`[${new Date(time).toLocaleTimeString()}]`);
    const objectHasKeys = Object.keys(details).length > 0;
    console.log(`${timestamp} ${traceIdTag(traceId)} ${levelTag} ${stepTag(step)} ${msg}`);
    if (objectHasKeys && !excludeDetails) {
        console.log(prettyPrintObject(details));
    }
};
exports.prettyPrint = prettyPrint;
