// SPDX-License-Identifier: Apache-2.0
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import pino from 'pino';
import pinoPretty from 'pino-pretty';
import { mkdirSync } from 'node:fs';
import { v4 as uuidv4 } from 'uuid';
// eslint-disable-next-line unicorn/import-style
import * as util from 'node:util';
import chalk from 'chalk';
import * as constants from '../constants.js';
import { inject, injectable } from 'tsyringe-neo';
import { patchInject } from '../dependency-injection/container-helper.js';
import { InjectTokens } from '../dependency-injection/inject-tokens.js';
import { PathEx } from '../../business/utils/path-ex.js';
import { SoloError } from '../errors/solo-error.js';
import { MessageLevel } from './message-level.js';
/**
 * Pino-based implementation of the SoloLogger interface.
 *
 * Emits two files under constants.SOLO_LOGS_DIR:
 *  - solo.ndjson : newline-delimited JSON (authoritative)
 *  - solo.log    : pretty human-readable
 */
let SoloPinoLogger = class SoloPinoLogger {
    developmentMode;
    pinoLogger;
    traceId;
    messageGroupMap = new Map();
    MINOR_LINE_SEPARATOR = '-------------------------------------------------------------------------------';
    /**
     * @param logLevel - the log level to use (fatal|error|warn|info|debug|trace)
     * @param developmentMode - if true, show full stack traces in error messages
     */
    constructor(logLevel, developmentMode) {
        this.developmentMode = developmentMode;
        logLevel = patchInject(logLevel, InjectTokens.LogLevel, this.constructor.name) ?? 'info';
        this.developmentMode = patchInject(developmentMode, InjectTokens.DevelopmentMode, this.constructor.name);
        this.nextTraceId();
        // Ensure logs directory exists
        const logsDirectory = constants.SOLO_LOGS_DIR;
        try {
            mkdirSync(logsDirectory, { recursive: true });
        }
        catch {
            // no-op: if this fails, pino will attempt to create the files and error if impossible
        }
        // Configure dual outputs: NDJSON (machine) + pretty (human)
        const ndjsonTarget = {
            target: 'pino/file',
            level: logLevel,
            options: { destination: PathEx.join(logsDirectory, 'solo.ndjson') },
        };
        const prettyTarget = {
            target: 'pino-pretty',
            level: logLevel,
            options: {
                destination: PathEx.join(logsDirectory, 'solo.log'), // write formatted logs to <logsDirectory>/solo.log
                translateTime: 'HH:MM:ss.l', // prepend timestamp as [HH:MM:ss.ms]
                colorize: false, // disable pino-pretty color output (avoid ANSI codes)
                messageKey: 'msg', // use the 'msg' property as the main log message
                messageFormat: '{msg} [traceId="{traceId}"]', // format line: message + traceId suffix
                ignore: 'pid,hostname,traceId', // exclude these fields from printed output
                colorizeObjects: false, // don't colorize objects or nested values
                crlf: false, // use '\n' (Unix newlines) instead of '\r\n' (Windows)
                hideObject: false, // don't hide full object payloads after message
            },
        };
        const baseOptions = {
            level: logLevel,
            // Always include traceId when set via mixin
            mixin: () => (this.traceId ? { traceId: this.traceId } : {}),
            // Redact obvious secrets if they sneak into objects
            redact: {
                paths: ['*.authorization', '*.Authorization', '*.accessToken', '*.privateKey', '*.operatorKey'],
                remove: true,
            },
        };
        if (process.env.CI === 'true') {
            const ndjsonStream = pino.destination({
                dest: PathEx.join(logsDirectory, 'solo.ndjson'),
                sync: true,
            });
            const prettyStream = pinoPretty({
                ...prettyTarget.options,
                destination: pino.destination({
                    dest: PathEx.join(logsDirectory, 'solo.log'),
                    sync: true,
                }),
            });
            this.pinoLogger = pino(baseOptions, pino.multistream([{ stream: ndjsonStream }, { stream: prettyStream }]));
        }
        else {
            this.pinoLogger = pino(baseOptions, pino.transport({ targets: [ndjsonTarget, prettyTarget] }));
        }
    }
    setDevMode(developmentMode) {
        this.debug(`dev mode logging: ${developmentMode}`);
        this.developmentMode = developmentMode;
    }
    nextTraceId() {
        this.traceId = uuidv4();
    }
    prepMeta(meta = {}) {
        if (this.traceId) {
            meta['traceId'] = this.traceId;
        }
        return meta;
    }
    showUser(message, ...arguments_) {
        const formatted = util.format(String(message), ...arguments_.map(String));
        console.log(formatted);
        // Mirror existing behavior: also persist to logs at info level
        this.info(formatted);
    }
    showUserError(error) {
        // Build chain of causes (up to 10 deep)
        const errorObject = error;
        const stack = [
            { message: errorObject?.message ? String(errorObject.message) : String(error), stacktrace: errorObject?.stack },
        ];
        if (errorObject?.cause) {
            let depth = 0;
            let cause = errorObject.cause;
            while (cause && depth < 10) {
                const c = cause;
                if (c.stack) {
                    stack.push({ message: c.message ? String(c.message) : String(c), stacktrace: c.stack });
                }
                cause = c.cause;
                depth += 1;
            }
        }
        console.log(chalk.red('*********************************** ERROR *****************************************'));
        if (this.developmentMode) {
            let prefix = '';
            let indent = '';
            for (const s of stack) {
                console.log(indent + prefix + chalk.yellow(String(s.message)));
                if (s.stacktrace) {
                    // Keep it readable; trim obvious internal noise
                    const formatted = String(s.stacktrace)
                        .split('\n')
                        .filter((l) => !l.includes('node:internal'))
                        .join('\n')
                        .trim();
                    console.log(indent + chalk.gray(formatted) + '\n');
                }
                indent += '  ';
                prefix = 'Caused by: ';
            }
        }
        else {
            const lines = error?.message
                ? String(error.message).split('\n')
                : String(error).split('\n');
            for (const line of lines) {
                console.log(chalk.yellow(line));
            }
        }
        console.log(chalk.red('***********************************************************************************'));
        // Persist the error with structure
        this.toPino('error', error, []);
    }
    error(message, ...arguments_) {
        this.toPino('error', message, arguments_);
    }
    warn(message, ...arguments_) {
        this.toPino('warn', message, arguments_);
    }
    info(message, ...arguments_) {
        this.toPino('info', message, arguments_);
    }
    debug(message, ...arguments_) {
        this.toPino('debug', message, arguments_);
    }
    showList(title, items = []) {
        this.showUser(chalk.green(`\n *** ${title} ***`));
        this.showUser(chalk.green(this.MINOR_LINE_SEPARATOR));
        if (items.length > 0) {
            for (const name of items) {
                this.showUser(chalk.cyan(` - ${name}`));
            }
        }
        else {
            this.showUser(chalk.blue('[ None ]'));
        }
        this.showUser('\n');
        return true;
    }
    showJSON(title, object) {
        this.showUser(chalk.green(`\n *** ${title} ***`));
        this.showUser(chalk.green(this.MINOR_LINE_SEPARATOR));
        console.log(JSON.stringify(object, null, ' '));
    }
    getMessageGroup(key) {
        if (!this.messageGroupMap.has(key)) {
            throw new SoloError(`Message group with key "${key}" does not exist.`);
        }
        return this.messageGroupMap.get(key);
    }
    addMessageGroup(key, title) {
        if (this.messageGroupMap.has(key)) {
            this.warn(`Message group with key "${key}" already exists. Skipping.`);
            return;
        }
        this.messageGroupMap.set(key, [`${title}:`]);
        this.debug(`Added message group "${title}" with key "${key}".`);
    }
    addMessageGroupMessage(key, message) {
        if (!this.messageGroupMap.has(key)) {
            throw new SoloError(`Message group with key "${key}" does not exist.`);
        }
        this.messageGroupMap.get(key).push(message);
        this.debug(`Added message to group "${key}": ${message}`);
    }
    showMessageGroup(key, messageLevel = MessageLevel.INFO) {
        if (!this.messageGroupMap.has(key)) {
            this.warn(`Message group with key "${key}" does not exist.`);
            return;
        }
        let titleColor;
        let textColor;
        switch (messageLevel) {
            case MessageLevel.ERROR: {
                titleColor = chalk.red;
                textColor = chalk.red;
                break;
            }
            case MessageLevel.WARN: {
                titleColor = chalk.yellow;
                textColor = chalk.yellow;
                break;
            }
            default: {
                titleColor = chalk.green;
                textColor = chalk.cyan;
                break;
            }
        }
        const messages = this.messageGroupMap.get(key);
        this.showUser(titleColor(`\n *** ${messages[0]} ***`));
        this.showUser(titleColor(this.MINOR_LINE_SEPARATOR));
        for (let index = 1; index < messages.length; index++) {
            this.showUser(textColor(` - ${messages[index]}`));
        }
        this.showUser(titleColor(this.MINOR_LINE_SEPARATOR));
        this.debug(`Displayed message group "${key}".`);
    }
    getMessageGroupKeys() {
        return [...this.messageGroupMap.keys()];
    }
    showAllMessageGroups() {
        const keys = this.getMessageGroupKeys();
        if (keys.length === 0) {
            this.debug('No message groups available.');
            return;
        }
        for (const key of keys) {
            this.showMessageGroup(key);
        }
    }
    toPino(level, message, arguments_) {
        // Build base object (traceId via mixin already present, but include explicitly for clarity in unit tests)
        let object = {};
        const meta = this.prepMeta({});
        // Prefer structured errors/objects when provided
        if (message instanceof Error) {
            object = { ...object, ...meta, err: message };
            this.pinoLogger[level](object, message.message ?? 'Error');
            return;
        }
        if (message && typeof message === 'object') {
            object = { ...object, ...meta, ...message };
            const message_ = arguments_.length > 0 ? util.format('%s', ...arguments_.map(String)) : undefined;
            if (message_) {
                this.pinoLogger[level](object, message_);
            }
            else {
                this.pinoLogger[level](object);
            }
            return;
        }
        const formatted = util.format(String(message), ...arguments_);
        this.pinoLogger[level](meta, formatted);
    }
};
SoloPinoLogger = __decorate([
    injectable(),
    __param(0, inject(InjectTokens.LogLevel)),
    __param(1, inject(InjectTokens.DevelopmentMode)),
    __metadata("design:paramtypes", [String, Boolean])
], SoloPinoLogger);
export { SoloPinoLogger };
//# sourceMappingURL=solo-pino-logger.js.map