import * as Sentry from "@sentry/svelte"

let initialized = false

export const init = (dsn: string, tracingOrigins: (string | RegExp)[], environment: string, release: string) => {
    if (typeof window !== "undefined") {
        Sentry.init({
            dsn: dsn,
            tunnel: "/_s",
            tracePropagationTargets: tracingOrigins,
            integrations: [
                Sentry.browserTracingIntegration(),
                Sentry.replayIntegration({
                    maskAllText: false,
                    maskAllInputs: false,
                    blockAllMedia: false,
                    networkDetailAllowUrls: [/\/api\//],
                    mutationLimit: 99999,
                    mutationBreadcrumbLimit: 2000,
                }),
            ],
            environment: environment,
            tracesSampleRate: 1.0,
            debug: false,
            release: release,
            replaysSessionSampleRate: 1.0,
            replaysOnErrorSampleRate: 1.0,
            sendDefaultPii: true,
        })
        console.log("Sentry initialized")
        initialized = true
    }
}

// export const startSpan = (opt: { name: string; op: string }) => {
//     if (typeof window !== "undefined" && initialized) {
//         return Sentry.startInactiveSpan(opt)
//     }
// }

export const currentTransaction = () => {
    const activeSpan = Sentry.getActiveSpan()
    if (activeSpan && activeSpan) {
        return activeSpan
    }
    return null
}
export const setUser = (user: Sentry.User) => {
    if (typeof window !== "undefined" && initialized) {
        user.ip_address = "{{auto}}"
        Sentry.setUser(user)
    }
}
