'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 _abstractError;

function _load_abstractError() {
  return _abstractError = _interopRequireDefault(require('./abstract-error'));
}

var _abstractError2;

function _load_abstractError2() {
  return _abstractError2 = require('./abstract-error');
}

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

function hashErrorIfNeeded(error) {
  var clonedError = error.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 !(_abstractError2 || _load_abstractError2()).systemFields.includes(field);
  });
  if (!fieldToHash.length) return clonedError;
  fieldToHash.forEach(function (field) {
    clonedError[field] = hashValue(clonedError[field]);
  });
  return clonedError;
}

function hashValue(value) {
  if (!value) return 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);
  }
}