'use strict';

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

var _validationError;

function _load_validationError() {
  return _validationError = _interopRequireDefault(require('../error/validation-error'));
}

var _generalError;

function _load_generalError() {
  return _generalError = _interopRequireDefault(require('../error/general-error'));
}

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

function validateType(message, value, fieldName, expectedType) {
  validate(message, value, fieldName, expectedType, false);
}

function validateUserInputType(message, value, fieldName, expectedType) {
  validate(message, value, fieldName, expectedType, true);
}

function validate(message, value, fieldName, expectedType, isUserInput) {
  var type = typeof value;
  if ((expectedType === 'array' || expectedType === 'object') && Array.isArray(value)) type = 'array';
  if (type !== expectedType) {
    var errorMessage = `${message}, expected ${fieldName} to be ${expectedType}, got ${type}`;
    if (isUserInput) throw new (_generalError || _load_generalError()).default(errorMessage);
    throw new (_validationError || _load_validationError()).default(`${message}, expected ${fieldName} to be ${expectedType}, got ${type}`);
  }
}