import './index.css';
import { REFERRAL_CODE_KEY, sanitizeReferralCode } from './lib/referral';
import queryClient from './query-client.tsx';
import Router from './router.tsx';
import * as Sentry from '@sentry/react';
import { QueryClientProvider } from '@tanstack/react-query';
import ReactDOM from 'react-dom/client';

if (import.meta.env.VITE_SENTRY_DSN) {
  Sentry.init({
    dsn: import.meta.env.VITE_SENTRY_DSN,
    integrations: [
      Sentry.browserTracingIntegration(),
      Sentry.replayIntegration({
        maskAllText: true,
        blockAllMedia: true,
      }),
    ],
    // Performance Monitoring
    tracesSampleRate: 1.0, //  Capture 100% of the transactions
    // Set 'tracePropagationTargets' to control for which URLs distributed tracing should be enabled
    tracePropagationTargets: ['localhost'],
    // Session Replay
    replaysSessionSampleRate: 0.1, // This sets the sample rate at 10%. You may want to change it to 100% while in development and then sample at a lower rate in production.
    replaysOnErrorSampleRate: 1.0, // If you're not already sampling the entire session, change the sample rate to 100% when sampling sessions where errors occur.
  });
}

// Store referral code from URL if present
// See AnalyticsProvider for more details
const urlParams = new URLSearchParams(window.location.search);
const referralCode = sanitizeReferralCode(urlParams.get(REFERRAL_CODE_KEY));
if (referralCode) {
  localStorage.setItem(REFERRAL_CODE_KEY, referralCode);
}

ReactDOM.createRoot(document.getElementById('root')!).render(
  <QueryClientProvider client={queryClient}>
    <Router />
  </QueryClientProvider>,
);
