// This file configures the initialization of Sentry on the client.
// The config you add here will be used whenever a users loads a page in their browser.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#create-initialization-config-files
import * as Sentry from '@sentry/nextjs';

// We vendor this integration to fix a bug re: mismatched wasm file paths
import { wasmIntegration } from '@/sentry-wasm-integration';

Sentry.init({
  dsn: 'https://6bbb841ff7b785e4f69e9a32335f8b91@o1183403.ingest.sentry.io/4506389154562048',

  // Adjust this value in production, or use tracesSampler for greater control
  // this is used to sample performance traces: https://github.com/getsentry/sentry-javascript/issues/9784#issuecomment-1845866736
  sampleRate: 0.002,
  replaysOnErrorSampleRate: 0.0001,

  // Setting this option to true will print useful information to the console while you're setting up Sentry.
  debug: false,

  // turn off general replays & traces and only track errors
  replaysSessionSampleRate: 0,
  tracesSampleRate: 0,
  // turn off client reports since most of them are due to sampling rate. might want to enable and just filter these out in the future
  // https://develop.sentry.dev/sdk/telemetry/client-reports/
  sendClientReports: false,

  integrations: (defaultIntegrations) => [
    // disable session tracking because we don't use the release health feature
    // https://docs.sentry.io/platforms/javascript/configuration/releases/#sessions
    ...defaultIntegrations.filter(
      (integration) => integration.name !== 'BrowserSession'
    ),
    wasmIntegration(),
    Sentry.replayIntegration({
      maskAllText: true,
      blockAllMedia: true,
    }),
  ],

  beforeSendTransaction(event) {
    // matches something like /@username/ or /@username/?some_param=some_value
    const profileMatch = event.transaction?.match(/\/@(.*)[?\/]*(.*)/);
    // matches something like /s/sharecode
    const shareMatch = event.transaction?.match(/\/s\/(.*)/);
    if (profileMatch) {
      // convert to /profile/:username so it gets grouped as /profile/*
      event.transaction = `/profile/*`;
    } else if (shareMatch) {
      // group as /s/*
      event.transaction = `/s/*`;
    }
    return event;
  },
});

export const onRouterTransitionStart = Sentry.captureRouterTransitionStart;
