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",
            integrations: [
                new Sentry.BrowserTracing({
                    tracingOrigins: tracingOrigins,
                    traceFetch: false,
                    traceXHR: false,
                }),
                new Sentry.Replay({
                    maskAllText: false,
                    maskAllInputs: false,
                    blockAllMedia: false,
                    networkDetailAllowUrls: [/\/api\//],
                }),
            ],
            environment: environment,
            tracesSampleRate: 1.0,
            debug: false,
            release: release,
            replaysSessionSampleRate: 1.0,
            replaysOnErrorSampleRate: 1.0,
        })
        console.log("Sentry initialized")
        initialized = true
    }
}

export const currentTransaction = () => Sentry.getCurrentHub().getScope().getTransaction()

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