import { randomUUID } from 'crypto';
export const MAX_CORRELATION_ID_LENGTH = 128;
export function resolveCorrelationId(header) {
    const raw = header;
    const headerId = Array.isArray(raw) ? raw[0] : raw;
    const trimmed = typeof headerId === 'string' ? headerId.trim() : '';
    if (trimmed.length > 0 &&
        trimmed.length <= MAX_CORRELATION_ID_LENGTH) {
        return trimmed;
    }
    return randomUUID();
}
export async function addCorrelationId(request, reply) {
    const correlationId = resolveCorrelationId(request.headers['x-correlation-id']);
    request.correlationId = correlationId;
    reply.header('X-Correlation-ID', correlationId);
}
export function getCorrelationId(request) {
    return request.correlationId;
}
//# sourceMappingURL=correlationId.js.map