import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import * as Sentry from '@sentry/angular';
import { APP_TITLE } from '@app_consts';
import { IENVIRONMENT } from '@app_core/injection-tokens';

declare const environment: IENVIRONMENT;
declare const appVersion: string;
declare const appBuild: string;

if (environment.sentry_dsn) {
  Sentry.init({
    dsn: environment.sentry_dsn,
    attachStacktrace: true,
    debug: false,
    release: APP_TITLE + '_' + appVersion,
    integrations: [Sentry.browserTracingIntegration()],
    tracePropagationTargets: ['localhost', environment.apiUrl],
      // new RewriteFrames(),
      // new SentryBrowser.BrowserTracing({
      //   tracingOrigins: ['localhost', environment.apiUrl],
      //   routingInstrumentation: SentryBrowser.routingInstrumentation,
      // }),
    // ],

    // Set tracesSampleRate to 1.0 to capture 100%
    // of transactions for performance monitoring.
    // We recommend adjusting this value in production
    tracesSampleRate: 0.5,
    maxBreadcrumbs: 50,
    // ignoreErrors: ['Non-Error exception captured'],
    beforeSend(event: any, hint) {
      // Note: issue with double entries during http exceptions: https://github.com/getsentry/sentry-javascript/issues/2169
      // Note: issue with a second entry not being set correctly (as a non-error): https://github.com/getsentry/sentry-javascript/issues/2292#issuecomment-554932519
      const isNonErrorException = event.exception?.values[0]?.value?.startsWith('Non-Error exception captured');
      if (isNonErrorException) {
        if (!event.extra.__serialized__) {
          return null;
        }
        let realErrMsg = (event.extra.__serialized__ as any).error ? (event.extra.__serialized__ as any).error.message : null;
        realErrMsg = realErrMsg || (event.extra.__serialized__ as any).message;
        // this is a useless error message that masks the actual error.  Lets try to set it properly
        event.exception.values[0].value = realErrMsg;
        event.message = realErrMsg;
      }
      return event;
    }
  });
  Sentry.setTag('AppName', APP_TITLE);
  Sentry.setTag('AppVersion', appVersion);
  Sentry.setTag('AppBuild', appBuild);
}
// MinimumBreadcrumbLevel=Error
// MinimumEventLevel=Error


if (environment.production) {
  enableProdMode();
}


platformBrowserDynamic().bootstrapModule(AppModule).then(ref => {
    // Ensure Angular destroys itself on hot reloads.
    if (window['ngRef']) window['ngRef'].destroy();
    window['ngRef'] = ref;

    // Otherwise, log the boot error
}).catch(err => console.error(err));
