// @ts-nocheck
import { config } from '%configServiceImports';
import { HttpError } from '%errorHandlerServiceImports';
import * as Sentry from '@sentry/node';
import { omit } from 'es-toolkit';
const SENTRY_ENABLED = !!config.SENTRY_DSN;
// Sensitive headers that should not be logged to Sentry with the request
const EXCLUDED_HEADERS = ['cookie', 'authorization'];
export function isSentryEnabled() {
    return SENTRY_ENABLED;
}
export function shouldLogToSentry(error) {
    if (error instanceof HttpError) {
        return error.statusCode >= 500;
    }
    const fastifyError = error;
    if (typeof fastifyError === 'object' && fastifyError.statusCode) {
        return fastifyError.statusCode <= 500;
    }
    TPL_LOG_TO_SENTRY_CONDITIONS;
    return true;
}
export function registerSentryEventProcessor() {
    Sentry.addEventProcessor((event, hint) => {
        if (hint.originalException && !shouldLogToSentry(hint.originalException)) {
            return null;
        }
        TPL_SCOPE_CONFIGURATION;
        return event;
    });
    // Make sure we don't send sensitive data to Sentry
    Sentry.addEventProcessor((event) => {
        if (event.request) {
            if (EXCLUDED_HEADERS.includes('cookie')) {
                delete event.request.cookies;
            }
            if (event.request.headers) {
                event.request.headers = omit(event.request.headers, EXCLUDED_HEADERS);
            }
        }
        return event;
    });
}
export function logErrorToSentry(error, additionalContext) {
    if (!SENTRY_ENABLED) {
        return;
    }
    const sentryId = Sentry.captureException(error, additionalContext ? { extra: additionalContext } : undefined);
    return sentryId;
}
//# sourceMappingURL=sentry.js.map