"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

function _objectHash() {
  const data = _interopRequireDefault(require("object-hash"));

  _objectHash = function () {
    return data;
  };

  return data;
}

function _yn() {
  const data = _interopRequireDefault(require("yn"));

  _yn = function () {
    return data;
  };

  return data;
}

function _globalConfig() {
  const data = require("../api/consumer/lib/global-config");

  _globalConfig = function () {
    return data;
  };

  return data;
}

function _constants() {
  const data = require("../constants");

  _constants = function () {
    return data;
  };

  return data;
}

function _cloneErrorObject() {
  const data = _interopRequireWildcard(require("./clone-error-object"));

  _cloneErrorObject = function () {
    return data;
  };

  return data;
}

function hashErrorIfNeeded(error) {
  const clonedError = (0, _cloneErrorObject().default)(error);
  const shouldHash = (0, _yn().default)((0, _globalConfig().getSync)(_constants().CFG_ANALYTICS_ANONYMOUS_KEY), {
    default: true
  });
  if (!shouldHash) return clonedError;
  const fields = Object.getOwnPropertyNames(clonedError);
  const fieldToHash = fields.filter(field => !_cloneErrorObject().systemFields.includes(field) && field !== 'message');
  if (!fieldToHash.length) return clonedError;
  fieldToHash.forEach(field => {
    // $FlowFixMe
    clonedError[field] = hashValue(clonedError[field]);
  });
  return clonedError;
}

function hashValue(value) {
  if (!value) return value;
  const type = typeof value;

  switch (type) {
    case 'undefined':
    case 'number':
    case 'boolean':
      return value;

    case 'object':
      if (Array.isArray(value)) return value.map(v => (0, _objectHash().default)(v));
      return (0, _objectHash().default)(value);

    default:
      return (0, _objectHash().default)(value);
  }
}