"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports["default"] = void 0;var _integrations = require("@sentry/integrations");
var _nodeMachineId = require("node-machine-id");
var _axios = _interopRequireDefault(require("axios"));
var _os = _interopRequireDefault(require("os"));
var _path = _interopRequireDefault(require("path"));

var _config = _interopRequireDefault(require("../configManager/config"));
var _package = _interopRequireDefault(require("../../../package.json"));
var _constants = require("../constants");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { "default": obj };}function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {try {var info = gen[key](arg);var value = info.value;} catch (error) {reject(error);return;}if (info.done) {resolve(value);} else {Promise.resolve(value).then(_next, _throw);}}function _asyncToGenerator(fn) {return function () {var self = this,args = arguments;return new Promise(function (resolve, reject) {var gen = fn.apply(self, args);function _next(value) {asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);}function _throw(err) {asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);}_next(undefined);});};}function ownKeys(object, enumerableOnly) {var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {var symbols = Object.getOwnPropertySymbols(object);enumerableOnly && (symbols = symbols.filter(function (sym) {return Object.getOwnPropertyDescriptor(object, sym).enumerable;})), keys.push.apply(keys, symbols);}return keys;}function _objectSpread(target) {for (var i = 1; i < arguments.length; i++) {var source = null != arguments[i] ? arguments[i] : {};i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {_defineProperty(target, key, source[key]);}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));});}return target;}function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return obj;}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}function _createClass(Constructor, protoProps, staticProps) {if (protoProps) _defineProperties(Constructor.prototype, protoProps);if (staticProps) _defineProperties(Constructor, staticProps);Object.defineProperty(Constructor, "prototype", { writable: false });return Constructor;}

// deal with useless duplicate errors on sentry because of different error texts
var sanitizeError = function sanitizeError(err) {
  if (err) {
    if (err.includes('file if your SDK path is correct.')) {
      return err.toLowerCase().split('. check your ')[0];
    }
    if (err.includes('AppConfig error - ')) {
      return err.split(' - ')[0];
    }
  }

  return err;
};var

Redash = /*#__PURE__*/function () {function Redash() {_classCallCheck(this, Redash);}_createClass(Redash, [{ key: "captureEvent", value:
    function captureEvent(e) {
      var defaultProps = {
        fingerprint: (0, _nodeMachineId.machineIdSync)(),
        os: _os["default"].platform(),
        rnvVersion: _package["default"].version };

      return _axios["default"].
      post(
      _constants.REDASH_URL, _objectSpread(_objectSpread({},
      e), defaultProps),
      {
        headers: {
          'x-api-key': _constants.REDASH_KEY } })["catch"](



      function () {return true;});
    } }]);return Redash;}();var


Analytics = /*#__PURE__*/function () {
  function Analytics() {_classCallCheck(this, Analytics);
    this.errorFixer = null;
    this.knowItAll = null;
  }_createClass(Analytics, [{ key: "initialize", value:

    function initialize() {
      if (_config["default"].isAnalyticsEnabled) {
        // ERROR HANDLING
        // eslint-disable-next-line global-require
        this.errorFixer = require('@sentry/node');

        this.errorFixer.init({
          dsn:
          _constants.SENTRY_ENDPOINT,
          release: "rnv@".concat(_package["default"].version),
          integrations: [
          new _integrations.RewriteFrames({
            root: '/',
            iteratee: function iteratee(frame) {
              if (
              frame.filename.includes("rnv".concat(
              _path["default"].sep, "dist").concat(_path["default"].sep)) ||

              frame.filename.includes("rnv".concat(
              _path["default"].sep, "src").concat(_path["default"].sep)))

              {
                if (
                frame.filename.includes("rnv".concat(
                _path["default"].sep, "dist").concat(_path["default"].sep)))

                {
                  frame.filename = frame.filename.split("rnv".concat(
                  _path["default"].sep, "dist").concat(_path["default"].sep))[
                  1];
                } else {
                  frame.filename = frame.filename.split("rnv".concat(
                  _path["default"].sep, "src").concat(_path["default"].sep))[
                  1];
                }
              } else if (
              frame.filename.includes("".concat(
              _path["default"].sep, "node_modules").concat(_path["default"].sep)))

              {
                frame.filename = "node_modules/".concat(
                frame.filename.split("".concat(
                _path["default"].sep, "node_modules").concat(_path["default"].sep))[
                1]);

              }
              return frame;
            } })] });




        // EVENT HANDLING
        this.knowItAll = new Redash();
      }
    } }, { key: "captureException", value:

    function captureException(e) {var _this = this;var context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
      if (_config["default"].isAnalyticsEnabled && this.errorFixer) {
        this.errorFixer.withScope(function (scope) {
          var _context$extra = context.extra,extra = _context$extra === void 0 ? {} : _context$extra,_context$tags = context.tags,tags = _context$tags === void 0 ? {} : _context$tags;
          scope.setTags(_objectSpread(_objectSpread({}, tags), {}, { os: _os["default"].platform() }));
          scope.setExtras(_objectSpread(_objectSpread({}, extra), {}, { fingerprint: (0, _nodeMachineId.machineIdSync)() }));
          if (e instanceof Error) {
            _this.errorFixer.captureException(e);
          } else {
            _this.errorFixer.captureException(
            new Error(sanitizeError(e)));

          }
        });
      }
    } }, { key: "captureEvent", value: function () {var _captureEvent = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(

      function _callee(e) {return regeneratorRuntime.wrap(function _callee$(_context) {while (1) {switch (_context.prev = _context.next) {case 0:if (!(
                _config["default"].isAnalyticsEnabled && this.knowItAll)) {_context.next = 2;break;}return _context.abrupt("return",
                this.knowItAll.captureEvent(e));case 2:return _context.abrupt("return",

                true);case 3:case "end":return _context.stop();}}}, _callee, this);}));function captureEvent(_x) {return _captureEvent.apply(this, arguments);}return captureEvent;}() }, { key: "teardown", value:


    function teardown() {var _this2 = this;
      return new Promise(function (resolve) {
        if (_config["default"].isAnalyticsEnabled && _this2.errorFixer) {
          var client = _this2.errorFixer.getCurrentHub().getClient();
          if (client) {
            return client.close(2000).then(resolve);
          }
          return resolve();
        }
        return resolve();
      });
    } }]);return Analytics;}();var _default =


new Analytics();exports["default"] = _default;
//# sourceMappingURL=analytics.js.map