"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__tsfTypeAliases = void 0;
exports.isSentryInstalled = isSentryInstalled;
exports.installSentry = installSentry;
exports.flushSentry = flushSentry;
exports.resetSentryForTests = resetSentryForTests;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-reflection/type-metadata-runtime"));
const node_1 = require("@sentry/node");
const env_1 = require("../env");
const package_1 = require("../helpers/io/package");
const helpers_1 = require("./otel/helpers");
const SentryState = {
    installed: false
};
function isSentryInstalled() {
    return SentryState.installed;
}
function installSentry(options) {
    const { dsn, ...sentryOptions } = options;
    const packageJson = (0, package_1.getPackageJson)();
    const release = packageJson?.name && packageJson?.version ? `${packageJson.name.replace(/^@.+?\//, '')}@${packageJson.version}` : undefined;
    (0, node_1.init)({
        dsn,
        environment: env_1.Env.APP_ENV,
        maxBreadcrumbs: 0,
        release,
        skipOpenTelemetrySetup: true,
        integrations: integrations => integrations.filter(integration => !['Http', 'NodeFetch'].includes(integration.name)),
        ...sentryOptions
    });
    if ((0, helpers_1.isTracingInstalled)()) {
        (0, node_1.addEventProcessor)(event => {
            const spanContext = (0, helpers_1.getTraceContext)();
            if (spanContext) {
                event.contexts = {
                    ...event.contexts,
                    trace: {
                        trace_id: spanContext.traceId,
                        span_id: spanContext.spanId
                    }
                };
            }
            return event;
        });
    }
    SentryState.installed = true;
}
async function flushSentry() {
    if (isSentryInstalled())
        await (0, node_1.flush)(5);
}
function resetSentryForTests() {
    SentryState.installed = false;
}
exports.__tsfTypeAliases = _a.decodeCompactMetadataV1("[1,{\"ISentryOptions\":{\"kind\":18,\"typeName\":\"ISentryOptions\",\"types\":[{\"kind\":20,\"name\":\"dsn\",\"type\":{\"kind\":6},\"optional\":false}],\"implements\":[{\"$tsfAlias\":[0,\"@sentry/node\",\"NodeOptions\",\"NodeOptions\"]}]}}]", [require]);
//# sourceMappingURL=sentry.js.map