import * as Sentry from '@sentry/react';

// Vite inlines these at build time, so the bundle has to be built with the values already
// in .env. Setting them on the server after `vite build` has run does nothing.
const dsn: string | undefined = import.meta.env.VITE_SENTRY_DSN;
const configuredSampleRate = Number.parseFloat(
  import.meta.env.VITE_SENTRY_TRACES_SAMPLE_RATE ?? ''
);

// An empty DSN means reporting is deliberately off — dev machines build without one — so
// skip init entirely instead of letting the SDK collect events it can never deliver.
if (dsn) {
  Sentry.init({
    dsn,
    environment: import.meta.env.VITE_SENTRY_ENVIRONMENT || import.meta.env.MODE,
    integrations: [Sentry.browserTracingIntegration()],
    tracesSampleRate: Number.isFinite(configuredSampleRate) ? configuredSampleRate : 0.2,

    // tracePropagationTargets is left at its default, which already covers same-origin
    // requests. That's every Inertia visit and axios call, so a browser span stitches onto
    // the Laravel transaction it triggers — both sides report to the same project.
  });
}
