"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.auditLoggerMiddleware = void 0;
const crypto_1 = __importDefault(require("crypto"));
const WRITE_METHODS = new Set(['POST', 'PUT', 'PATCH', 'DELETE']);
const auditLoggerMiddleware = (logger) => {
    return (req, res, next) => {
        if (!WRITE_METHODS.has(req.method)) {
            next();
            return;
        }
        const startAt = Date.now();
        const ip = req.ip ?? req.socket.remoteAddress ?? 'unknown';
        const ipHash = crypto_1.default.createHash('sha256').update(ip).digest('hex');
        res.on('finish', () => {
            const durationMs = Date.now() - startAt;
            const walletId = res.locals.walletId ?? req.params.walletId ?? req.body?.walletId ?? null;
            logger.info('Audit log', {
                timestamp: new Date().toISOString(),
                method: req.method,
                path: req.originalUrl ?? req.path,
                ipHash,
                statusCode: res.statusCode,
                walletId,
                success: res.statusCode < 400,
                durationMs,
                requestId: res.locals.requestId ?? req.headers['x-request-id'] ?? null,
            });
        });
        next();
    };
};
exports.auditLoggerMiddleware = auditLoggerMiddleware;
//# sourceMappingURL=audit-logger.middleware.js.map