import * as Sentry from '@sentry/browser';
import { first } from 'lodash';
const isProd = (process.env.NODE_ENV === 'production');

const isMathJaxUrl = /mathjax/;

const isMathjax = (crumb) => ('xhr' === crumb.category && isMathJaxUrl.test(crumb.data.url));

const RavenErrorLogging = {

  boot() {
    if (!isProd) { return; }
    Sentry.init({
      debug: false,
      dsn: 'https://10044345c10446119e980e2ba2f5fe14@sentry.cnx.org/10',
      environment: first(window.location.host.split('.')),
      beforeBreadcrumb(breadcrumb) {
        if (isMathjax(breadcrumb)) { return null; }
        return breadcrumb;
      },
    });
  },

  setUser(user) {
    Sentry.configureScope(scope => {
      scope.setUser({ id: user.account_uuid });
    });
  },

  captureException(error, xtra) {
    if (!isProd) {
      console.warn(error); // eslint-disable-line no-console
    }
    Sentry.captureException(error, xtra);
  },

  log(msg) {
    if (!isProd) { console.info(msg); } // eslint-disable-line no-console
    Sentry.captureMessage(msg);
  },

};


export default RavenErrorLogging;

@@ -50,16 +50,41 @@
  %7B first
+, isEmpty, isObject, each
  %7D from 
@@ -274,24 +274,383 @@
 ata.url));%0A%0A
+const sendWithXtras = (method, arg, xtra) =%3E %7B%0A  if (isEmpty(xtra)) %7B%0A    Sentry%5Bmethod%5D(arg);%0A  %7D else %7B%0A    Sentry.withScope((scope) =%3E %7B%0A      if (isObject(xtra)) %7B%0A        each(xtra, (value, key) =%3E %7B%0A          scope.setExtra(key, value);%0A        %7D);%0A      %7D else %7B%0A        scope.setExtra('info', xtra);%0A      %7D%0A      Sentry%5Bmethod%5D(arg);%0A    %7D);%0A  %7D%0A%7D;%0A%0A
 const RavenE
@@ -1252,31 +1252,39 @@
 e%0A    %7D%0A    
-Sentry.
+sendWithXtras('
 captureExcep
@@ -1279,33 +1279,35 @@
 captureException
-(
+', 
 error, xtra);%0A  
@@ -1323,97 +1323,37 @@
 (msg
-) %7B%0A    if (!isProd) %7B console.info(msg); %7D // eslint-disable-line no-console%0A    Sentry.
+, xtra) %7B%0A    sendWithXtras('
 capt
@@ -1362,20 +1362,28 @@
 eMessage
-(msg
+', msg, xtra
 );%0A  %7D,%0A

use new withScope method of setting xtra info