import { mkdirSync } from "node:fs";
import path from "node:path";
import pino from "pino";
import pretty from "pino-pretty";
import { resolvePaseoHome } from "./paseo-home.js";
const LOG_LEVEL_PRIORITIES = {
    trace: 10,
    debug: 20,
    info: 30,
    warn: 40,
    error: 50,
    fatal: 60,
};
const DEFAULT_CONSOLE_LEVEL = "info";
const DEFAULT_CONSOLE_FORMAT = "json";
const DEFAULT_FILE_LEVEL = "info";
const DEFAULT_DAEMON_LOG_FILENAME = "daemon.log";
const REDACT_PATHS = [
    "authorization",
    "Authorization",
    "headers.authorization",
    "headers.Authorization",
    "req.headers.authorization",
    "req.headers.Authorization",
    '["sec-websocket-protocol"]',
    "Sec-WebSocket-Protocol",
    'headers["sec-websocket-protocol"]',
    "headers.Sec-WebSocket-Protocol",
    'req.headers["sec-websocket-protocol"]',
    "req.headers.Sec-WebSocket-Protocol",
];
function resolveFilePath(paseoHome, configuredPath) {
    const fallback = path.join(paseoHome, DEFAULT_DAEMON_LOG_FILENAME);
    if (!configuredPath) {
        return fallback;
    }
    if (path.isAbsolute(configuredPath)) {
        return configuredPath;
    }
    return path.resolve(paseoHome, configuredPath);
}
function minLogLevel(levels) {
    let minLevel = levels[0];
    for (const level of levels) {
        if (LOG_LEVEL_PRIORITIES[level] < LOG_LEVEL_PRIORITIES[minLevel]) {
            minLevel = level;
        }
    }
    return minLevel;
}
function resolveConfiguredPaseoHome(options) {
    if (options?.paseoHome) {
        return options.paseoHome;
    }
    return resolvePaseoHome();
}
function normalizeLoggerConfigInput(config) {
    if (!config) {
        return undefined;
    }
    if ("log" in config) {
        return config;
    }
    if ("level" in config || "format" in config) {
        const legacy = config;
        return {
            log: {
                ...(legacy.level ? { level: legacy.level } : {}),
                ...(legacy.format ? { format: legacy.format } : {}),
            },
        };
    }
    return config;
}
function resolveLogLevelsAndFormat(persistedLog) {
    const persistedGlobalLevel = persistedLog?.level;
    const consoleLevel = persistedLog?.console?.level ?? persistedGlobalLevel ?? DEFAULT_CONSOLE_LEVEL;
    const fileLevel = persistedLog?.file
        ? (persistedLog.file.level ?? persistedGlobalLevel ?? DEFAULT_FILE_LEVEL)
        : undefined;
    const consoleFormat = persistedLog?.console?.format ?? persistedLog?.format ?? DEFAULT_CONSOLE_FORMAT;
    return { consoleLevel, fileLevel, consoleFormat };
}
export function resolveLogConfig(configInput, options) {
    const persistedConfig = normalizeLoggerConfigInput(configInput);
    const paseoHome = resolveConfiguredPaseoHome(options);
    const persistedLog = persistedConfig?.log;
    const { consoleLevel, fileLevel, consoleFormat } = resolveLogLevelsAndFormat(persistedLog);
    const file = options?.file !== false && persistedLog?.file
        ? {
            level: fileLevel ?? DEFAULT_FILE_LEVEL,
            path: resolveFilePath(paseoHome, persistedLog.file.path),
        }
        : undefined;
    return {
        level: minLogLevel(file ? [consoleLevel, file.level] : [consoleLevel]),
        console: {
            level: consoleLevel,
            format: consoleFormat,
        },
        ...(file ? { file } : {}),
    };
}
export function createRootLogger(configInput, options) {
    const config = resolveLogConfig(configInput, options);
    if (config.file) {
        mkdirSync(path.dirname(config.file.path), { recursive: true });
    }
    const stream = config.console.format === "pretty"
        ? pretty({
            colorize: true,
            singleLine: true,
            ignore: "pid,hostname",
            destination: config.file?.path ?? 1,
        })
        : pino.destination({ dest: config.file?.path ?? 1, sync: false });
    return pino({
        level: config.file?.level ?? config.console.level,
        redact: { paths: REDACT_PATHS, remove: true },
    }, stream);
}
export function createChildLogger(parent, name) {
    return parent.child({ name });
}
//# sourceMappingURL=logger.js.map