'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _objectHash;

function _load_objectHash() {
  return _objectHash = _interopRequireDefault(require('object-hash'));
}

var _yn;

function _load_yn() {
  return _yn = _interopRequireDefault(require('yn'));
}

var _globalConfig;

function _load_globalConfig() {
  return _globalConfig = require('../api/consumer/lib/global-config');
}

var _constants;

function _load_constants() {
  return _constants = require('../constants');
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

function _extendableBuiltin(cls) {
  function ExtendableBuiltin() {
    var instance = Reflect.construct(cls, Array.from(arguments));
    Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
    return instance;
  }

  ExtendableBuiltin.prototype = Object.create(cls.prototype, {
    constructor: {
      value: cls,
      enumerable: false,
      writable: true,
      configurable: true
    }
  });

  if (Object.setPrototypeOf) {
    Object.setPrototypeOf(ExtendableBuiltin, cls);
  } else {
    ExtendableBuiltin.__proto__ = cls;
  }

  return ExtendableBuiltin;
}

var systemFields = ['stack', 'code', 'errno', 'syscall'];

var AbstractError = function (_extendableBuiltin2) {
  _inherits(AbstractError, _extendableBuiltin2);

  function AbstractError() {
    _classCallCheck(this, AbstractError);

    var _this = _possibleConstructorReturn(this, (AbstractError.__proto__ || Object.getPrototypeOf(AbstractError)).call(this));

    _this.name = _this.constructor.name;
    return _this;
  }

  // partially forked from 'utils-copy-error' package


  _createClass(AbstractError, [{
    key: 'clone',
    value: function clone() {
      var error = this;
      // $FlowFixMe
      var err = new error.constructor(error.message);

      systemFields.forEach(function (field) {
        // $FlowFixMe
        if (error[field]) err[field] = error[field];
      });
      Object.keys(error).forEach(function (key) {
        // $FlowFixMe
        err[key] = (_ramda || _load_ramda()).default.clone(error[key]);
      });
      return err;
    }
  }, {
    key: 'makeAnonymous',
    value: function makeAnonymous() {
      var _this2 = this;

      var clonedError = this.clone();
      var shouldHash = (0, (_yn || _load_yn()).default)((0, (_globalConfig || _load_globalConfig()).getSync)((_constants || _load_constants()).CFG_ANALYTICS_ANONYMOUS_KEY), { default: true });
      if (!shouldHash) return clonedError;
      var fields = Object.getOwnPropertyNames(clonedError);
      var fieldToHash = fields.filter(function (field) {
        return !systemFields.includes(field);
      });
      if (!fieldToHash.length) return clonedError;
      fieldToHash.forEach(function (field) {
        clonedError[field] = _this2.hashValue(clonedError[field]);
      });
      return clonedError;
    }
  }, {
    key: 'hashValue',
    value: function hashValue(value) {
      var type = typeof value;
      switch (type) {
        case 'object':
          if (Array.isArray(value)) return value.map(function (v) {
            return (0, (_objectHash || _load_objectHash()).default)(v);
          });
          return (0, (_objectHash || _load_objectHash()).default)(value);
        default:
          return (0, (_objectHash || _load_objectHash()).default)(value);
      }
    }
  }]);

  return AbstractError;
}(_extendableBuiltin(Error));

exports.default = AbstractError;