"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.REQUEST_ID_HEADER = exports.CORRELATION_ID_HEADER = void 0;
exports.generateCorrelationId = generateCorrelationId;
exports.generateRequestId = generateRequestId;
exports.createCorrelationContext = createCorrelationContext;
exports.correlationHeaders = correlationHeaders;
const crypto_1 = require("crypto");
exports.CORRELATION_ID_HEADER = 'x-correlation-id';
exports.REQUEST_ID_HEADER = 'x-request-id';
function generateCorrelationId() {
    return (0, crypto_1.randomUUID)();
}
function generateRequestId() {
    return `req-${(0, crypto_1.randomUUID)()}`;
}
function orGenerated(value, fallback) {
    const v = value?.trim();
    return v ? v : fallback();
}
function createCorrelationContext(partial) {
    return {
        correlationId: orGenerated(partial?.correlationId, generateCorrelationId),
        requestId: orGenerated(partial?.requestId, generateRequestId),
        tenantId: partial?.tenantId,
        entityId: partial?.entityId,
    };
}
function correlationHeaders(ctx) {
    const headers = {
        [exports.CORRELATION_ID_HEADER]: ctx.correlationId,
        [exports.REQUEST_ID_HEADER]: ctx.requestId,
    };
    if (ctx.tenantId)
        headers['x-tenant-id'] = ctx.tenantId;
    if (ctx.entityId)
        headers['x-entity-id'] = ctx.entityId;
    return headers;
}
