"use strict";

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

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

function _validationError() {
  const data = _interopRequireDefault(require("../error/validation-error"));

  _validationError = function () {
    return data;
  };

  return data;
}

function _generalError() {
  const data = _interopRequireDefault(require("../error/general-error"));

  _generalError = function () {
    return data;
  };

  return data;
}

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) {
  let type = typeof value;
  if ((expectedType === 'array' || expectedType === 'object') && Array.isArray(value)) type = 'array';

  if (type !== expectedType) {
    const errorMessage = `${message}, expected ${fieldName} to be ${expectedType}, got ${type}`;
    if (isUserInput) throw new (_generalError().default)(errorMessage);
    throw new (_validationError().default)(`${message}, expected ${fieldName} to be ${expectedType}, got ${type}`);
  }
}