"use strict";
/**
 * Structured logger with severity levels.
 *
 * All governance modules use this instead of raw process.stderr.write.
 * Outputs structured JSON to stderr by default, with optional file output.
 *
 * @connexum/ai-governance
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
exports.createLogger = createLogger;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const fs_1 = require("./fs");
const LEVEL_PRIORITY = {
    DEBUG: 0,
    INFO: 1,
    WARN: 2,
    ERROR: 3,
    CRITICAL: 4,
};
class Logger {
    config;
    minPriority;
    constructor(config) {
        this.config = {
            level: 'INFO',
            logFile: '',
            format: 'text',
            ...config,
        };
        this.minPriority = LEVEL_PRIORITY[this.config.level];
    }
    debug(message, data) {
        this.log('DEBUG', message, data);
    }
    info(message, data) {
        this.log('INFO', message, data);
    }
    warn(message, data) {
        this.log('WARN', message, data);
    }
    error(message, data) {
        this.log('ERROR', message, data);
    }
    critical(message, data) {
        this.log('CRITICAL', message, data);
    }
    log(level, message, data) {
        if (LEVEL_PRIORITY[level] < this.minPriority)
            return;
        const entry = {
            timestamp: new Date().toISOString(),
            level,
            module: this.config.module,
            message,
            ...(data ? { data } : {}),
        };
        const output = this.config.format === 'json'
            ? JSON.stringify(entry)
            : `[${entry.timestamp}] [${level}] [${this.config.module}] ${message}${data ? ' ' + JSON.stringify(data) : ''}`;
        process.stderr.write(output + '\n');
        if (this.config.logFile) {
            try {
                (0, fs_1.ensureDir)(path.dirname(this.config.logFile));
                fs.appendFileSync(this.config.logFile, JSON.stringify(entry) + '\n');
            }
            catch {
                // Logger must never throw
            }
        }
    }
}
exports.Logger = Logger;
/**
 * Create a logger for a specific module.
 */
function createLogger(module, options) {
    return new Logger({ module, ...options });
}
//# sourceMappingURL=log.js.map