/**
 * Lightweight Sentry wrapper for client-side error tracking.
 *
 * No-op when VITE_SENTRY_DSN_PUBLIC is not set.
 * When @sentry/react is installed, it will be loaded via the Sentry CDN loader
 * pattern or direct import. This file provides a consistent API regardless.
 *
 * To enable: install @sentry/react and set VITE_SENTRY_DSN_PUBLIC in .env.
 */

const sentryDsn = import.meta.env.VITE_SENTRY_DSN_PUBLIC as string | undefined;

interface SentryLike {
  captureException: (error: unknown, context?: Record<string, unknown>) => void;
  captureMessage: (message: string) => void;
}

function getSentry(): SentryLike | null {
  const win = window as unknown as Record<string, unknown>;
  if (win.__SENTRY__ || win.Sentry) {
    return (win.Sentry ?? win.__SENTRY__) as SentryLike;
  }
  return null;
}

export async function initSentry(): Promise<void> {
  if (!sentryDsn) return;

  // If @sentry/react is installed, it will be initialized by the app's
  // build-time configuration. This function is a no-op placeholder for
  // when the package is installed and configured via vite.config.ts.
}

export function captureException(error: unknown, context?: Record<string, unknown>): void {
  if (!sentryDsn) return;
  getSentry()?.captureException(error, context);
}

export function captureMessage(message: string): void {
  if (!sentryDsn) return;
  getSentry()?.captureMessage(message);
}
