'use strict';

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

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');
}

var _cloneErrorObject;

function _load_cloneErrorObject() {
  return _cloneErrorObject = _interopRequireDefault(require('./clone-error-object'));
}

var _cloneErrorObject2;

function _load_cloneErrorObject2() {
  return _cloneErrorObject2 = require('./clone-error-object');
}

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

function hashErrorIfNeeded(error) {
  var clonedError = (0, (_cloneErrorObject || _load_cloneErrorObject()).default)(error);
  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 !(_cloneErrorObject2 || _load_cloneErrorObject2()).systemFields.includes(field) && field !== 'message';
  });
  if (!fieldToHash.length) return clonedError;
  fieldToHash.forEach(function (field) {
    // $FlowFixMe
    clonedError[field] = hashValue(clonedError[field]);
  });
  return clonedError;
}

function hashValue(value) {
  if (!value) return value;
  var type = typeof value;
  switch (type) {
    case 'undefined':
    case 'number':
    case 'boolean':
      return value;
    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);
  }
}