import util from 'util';
import path from 'path';

import eventCategories from 'common/analytics/categories';
import eventActions from 'common/analytics/actions';
import eventNames from 'common/analytics/names';

function namespaceOfFile (filename) {
  const app = require('common/electron/app').default;
  const appPath = path.join(app.getAppPath(), 'scripts') + path.sep;

  let name = filename
    .replace(appPath, '')
    .replace(/\\/g, '/')
    .replace('.js', '');

  if (name.startsWith('common/')) {
    name += ':' + process.type;
  }

  // replace slashes with semicolons
  name = name.replace(/\//g, ':');

  return global.manifest.name + ':' + name;
}

function reportToPiwik (namespace, isFatal, err) {
  const piwik = require('common/services/piwik').default.getTracker();
  if (piwik) {
    piwik.trackEvent(
      eventCategories['Logs'],
      eventActions['Exception'],
      isFatal ? eventNames['Fatal Error'] : eventNames['Error'],
      `${namespace}: ${err.name}: ${err.message}`
    );
  }
}

function reportToSentry (namespace, isFatal, err) {
  const sentry = require('common/services/sentry').default;
  if (sentry) {
    console.log('reporting to sentry:', err);
    sentry.captureException(err, {
      level: isFatal ? 'fatal' : 'error',
      extra: {
        trace: new Error().stack
      },
      tags: {
        namespace
      }
    }, function (result) {
      console.log('reported to sentry:', result);
    });
  }
}

export function debugLogger (filename) {
  let logger = null;
  let browserLogger = null;
  return function () {
    if (!logger) {
      const debug = require('common/modules/debug').default;
      logger = debug(namespaceOfFile(filename));
    }
    if (!browserLogger) {
      browserLogger = require('common/utils/logger-browser').default;
    }
    logger.log = browserLogger.printDebug;
    logger(util.format(...arguments));
  };
}

export function errorLogger (filename, isFatal) {
  let namespace = null;
  let browserLogger = null;
  return function (err, skipReporting = false) {
    if (!namespace) {
      namespace = namespaceOfFile(filename);
    }

    if (!(err instanceof Error)) {
      if (global.options.dev) {
        const fnName = isFatal ? 'logFatal' : 'logError';
        throw new Error('the first parameter to ' + fnName + ' must be an Error');
      } else {
        err = new Error(err);
      }
    }

    if (!browserLogger) {
      browserLogger = require('common/utils/logger-browser').default;
    }
    browserLogger.printError(namespace, isFatal, err.stack);

    if (!skipReporting && !global.options.debug) {
      reportToPiwik(namespace, isFatal, err);
      reportToSentry(namespace, isFatal, err);
    }
  };
}

@@ -48,170 +48,8 @@
 ';%0A%0A
-import eventCategories from 'common/analytics/categories';%0Aimport eventActions from 'common/analytics/actions';%0Aimport eventNames from 'common/analytics/names';%0A%0A
 func
@@ -502,360 +502,8 @@
 %0A%7D%0A%0A
-function reportToPiwik (namespace, isFatal, err) %7B%0A  const piwik = require('common/services/piwik').default.getTracker();%0A  if (piwik) %7B%0A    piwik.trackEvent(%0A      eventCategories%5B'Logs'%5D,%0A      eventActions%5B'Exception'%5D,%0A      isFatal ? eventNames%5B'Fatal Error'%5D : eventNames%5B'Error'%5D,%0A      %60$%7Bnamespace%7D: $%7Berr.name%7D: $%7Berr.message%7D%60%0A    );%0A  %7D%0A%7D%0A%0A
 func
@@ -2090,54 +2090,8 @@
 ) %7B%0A
-      reportToPiwik(namespace, isFatal, err);%0A
     

Stop reporting crashes to piwik