"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.jsonConstraints = exports.ref = exports.oneOf = exports.when = exports.test = exports.nullable = exports.maxSize = exports.unsupportedFormat = exports.supportedFormat = exports.length = exports.moreThan = exports.lessThan = exports.integer = exports.negative = exports.positive = exports.max = exports.min = exports.matches = exports.uuid = exports.email = exports.url = exports.required = void 0;

var yup = _interopRequireWildcard(require("yup"));

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var _required = function required() {
  var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "Value required";
  return function (r) {
    return r.required(message);
  };
}; //string


exports.required = _required;

var _url = function url() {
  var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "That is not a valid url";
  return function (r) {
    return r.url(message);
  };
};

exports.url = _url;

var _email = function email() {
  var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "That is not a valid email";
  return function (r) {
    return r.email(message);
  };
};

exports.email = _email;

var _uuid = function uuid() {
  var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "That is not a valid uuid";
  return function (r) {
    return r.uuid(message);
  };
};

exports.uuid = _uuid;

var _matches = function matches() {
  var regexp = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : /.*/;
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "This field does not match the pattern";
  return function (r) {
    return r.matches(regexp, {
      message: message
    });
  };
}; //string & number


exports.matches = _matches;

var _min = function min(ref) {
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "Min value is required";
  return function (r) {
    return r.min(maybeRef(ref), message);
  };
};

exports.min = _min;

var _max = function max(ref) {
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "Max value is required";
  return function (r) {
    return r.max(maybeRef(ref), message);
  };
}; //number


exports.max = _max;

var _positive = function positive() {
  var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "Positive value is required";
  return function (r) {
    return r.positive(message);
  };
};

exports.positive = _positive;

var _negative = function negative() {
  var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "Negative value is required";
  return function (r) {
    return r.negative(message);
  };
};

exports.negative = _negative;

var _integer = function integer() {
  var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "an integer please";
  return function (r) {
    return r.integer(message);
  };
};

exports.integer = _integer;

var _lessThan = function lessThan(ref) {
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "This field must be less than ".concat(ref);
  return function (r, key, dependencies) {
    if (typeof ref !== 'number') {
      dependencies.push([key, ref]);
    }

    return r.lessThan(maybeRef(ref), message);
  };
};

exports.lessThan = _lessThan;

var _moreThan = function moreThan(ref) {
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "This field must be more than ".concat(ref);
  return function (r, key, dependencies) {
    if (typeof ref !== 'number') {
      dependencies.push([key, ref]);
    }

    return r.moreThan(maybeRef(ref), message);
  };
}; //array


exports.moreThan = _moreThan;

var _length = function length(ref) {
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "The size of this collection must be ".concat(ref);
  return function (r) {
    return r.length(maybeRef(ref), message);
  };
}; //file


exports.length = _length;

var _supportedFormat = function supportedFormat(arrayOfValues) {
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Unsupported File Format';
  return function (r) {
    var SUPPORTED_FORMATS = arrayOfValues.map(function (f) {
      return f.toLowerCase();
    });
    return r.test('supportedFormat', message, function (value) {
      if (!value.length) return true;
      return SUPPORTED_FORMATS.some(function (format) {
        return value[0].type.toLowerCase().includes(format);
      });
    });
  };
};

exports.supportedFormat = _supportedFormat;

var _unsupportedFormat = function unsupportedFormat(arrayOfValues) {
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Unsupported File Format';
  return function (r) {
    var UNSUPPORTED_FORMATS = arrayOfValues.map(function (f) {
      return f.toLowerCase();
    });
    return r.test('unsupportedFormat', message, function (value) {
      if (!value.length) return true;
      return UNSUPPORTED_FORMATS.some(function (format) {
        return value[0].type.toLowerCase().includes(format);
      });
    });
  };
};

exports.unsupportedFormat = _unsupportedFormat;

var _maxSize = function maxSize(ref) {
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "size is excedeed ".concat(ref);
  return function (r) {
    return r.test('fileSize', message, function (value) {
      if (!value.length) return true;
      return value[0].size <= maybeRef(ref);
    });
  };
}; //mixed


exports.maxSize = _maxSize;

var _nullable = function nullable() {
  return function (r) {
    return r.nullable();
  };
};

exports.nullable = _nullable;

var _test = function test(name) {
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'Test failed';
  var test = arguments.length > 2 ? arguments[2] : undefined;
  return function (r) {
    return r.test(name, message, test);
  };
};

exports.test = _test;

var _when = function when(ref, test) {
  var then = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  var otherwise = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
  return function (r, key, dependencies) {
    // dependencies.push([key, ref])
    var thenReducer = then.reduce(function (resolver, constraint) {
      return constraint(resolver, key, dependencies);
    }, r);
    var otherWiseReducer = otherwise.reduce(function (resolver, constraint) {
      return constraint(resolver, key, dependencies);
    }, r);
    return r.when(ref, {
      is: test,
      then: thenReducer,
      otherwise: otherWiseReducer
    });
  };
};

exports.when = _when;

var _oneOf = function oneOf(arrayOfValues) {
  var message = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "This value must be one of ".concat(arrayOfValues.join(', '));
  return function (r) {
    return r.oneOf(arrayOfValues.map(maybeRef), message);
  };
};

exports.oneOf = _oneOf;

var _ref20 = function ref(_ref) {
  return yup.ref(_ref);
};

exports.ref = _ref20;

var maybeRef = function maybeRef(x) {
  return x !== null && x !== void 0 && x.ref ? _ref20(x.ref) : x;
}; //### JSON method ###


var jsonConstraints = {
  required: function required(_ref2) {
    var _ref2$message = _ref2.message,
        message = _ref2$message === void 0 ? "Value required" : _ref2$message;
    return _required(message);
  },
  url: function url(_ref3) {
    var _ref3$message = _ref3.message,
        message = _ref3$message === void 0 ? "That is not a valid url" : _ref3$message;
    return _url(message);
  },
  email: function email(_ref4) {
    var _ref4$message = _ref4.message,
        message = _ref4$message === void 0 ? "That is not a valid email" : _ref4$message;
    return _email(message);
  },
  uuid: function uuid(_ref5) {
    var _ref5$message = _ref5.message,
        message = _ref5$message === void 0 ? "That is not a valid uuid" : _ref5$message;
    return _uuid(message);
  },
  matches: function matches(_ref6) {
    var _ref6$regexp = _ref6.regexp,
        regexp = _ref6$regexp === void 0 ? /.*/ : _ref6$regexp,
        _ref6$message = _ref6.message,
        message = _ref6$message === void 0 ? "This field does not match the pattern" : _ref6$message;
    return _matches(regexp, message);
  },
  min: function min(_ref7) {
    var ref = _ref7.ref,
        _ref7$message = _ref7.message,
        message = _ref7$message === void 0 ? "Min value is required" : _ref7$message;
    return _min(ref, message);
  },
  max: function max(_ref8) {
    var ref = _ref8.ref,
        _ref8$message = _ref8.message,
        message = _ref8$message === void 0 ? "Max value is required" : _ref8$message;
    return _max(ref, message);
  },
  positive: function positive(_ref9) {
    var _ref9$message = _ref9.message,
        message = _ref9$message === void 0 ? "Positive value is required" : _ref9$message;
    return _positive(message);
  },
  negative: function negative(_ref10) {
    var _ref10$message = _ref10.message,
        message = _ref10$message === void 0 ? "Negative value is required" : _ref10$message;
    return _negative(message);
  },
  integer: function integer(_ref11) {
    var _ref11$message = _ref11.message,
        message = _ref11$message === void 0 ? "an integer please" : _ref11$message;
    return _integer(message);
  },
  lessThan: function lessThan(_ref12) {
    var ref = _ref12.ref,
        _ref12$message = _ref12.message,
        message = _ref12$message === void 0 ? "This field must be less than ".concat(ref) : _ref12$message;
    return _lessThan(ref, message);
  },
  moreThan: function moreThan(_ref13) {
    var ref = _ref13.ref,
        _ref13$message = _ref13.message,
        message = _ref13$message === void 0 ? "This field must be more than ".concat(ref) : _ref13$message;
    return _moreThan(ref, message);
  },
  length: function length(_ref14) {
    var ref = _ref14.ref,
        _ref14$message = _ref14.message,
        message = _ref14$message === void 0 ? "The size of this collection must be ".concat(ref) : _ref14$message;
    return _length(ref, message);
  },
  supportedFormat: function supportedFormat(_ref15) {
    var arrayOfValues = _ref15.arrayOfValues,
        _ref15$message = _ref15.message,
        message = _ref15$message === void 0 ? 'Unsupported File Format' : _ref15$message;
    return _supportedFormat(arrayOfValues, message);
  },
  unsupportedFormat: function unsupportedFormat(_ref16) {
    var arrayOfValues = _ref16.arrayOfValues,
        _ref16$message = _ref16.message,
        message = _ref16$message === void 0 ? 'Unsupported File Format' : _ref16$message;
    return _unsupportedFormat(arrayOfValues, message);
  },
  maxSize: function maxSize(_ref17) {
    var ref = _ref17.ref,
        _ref17$message = _ref17.message,
        message = _ref17$message === void 0 ? "size is excedeed ".concat(ref) : _ref17$message;
    return _maxSize(ref, message);
  },
  test: function test(c) {
    return _test(c.name, c.message, c.test);
  },
  when: function when(_ref18) {
    var ref = _ref18.ref,
        test = _ref18.test,
        _ref18$then = _ref18.then,
        then = _ref18$then === void 0 ? [] : _ref18$then,
        _ref18$otherwise = _ref18.otherwise,
        otherwise = _ref18$otherwise === void 0 ? [] : _ref18$otherwise;
    return _when(ref, test, then, otherwise);
  },
  oneOf: function oneOf(_ref19) {
    var arrayOfValues = _ref19.arrayOfValues,
        _ref19$message = _ref19.message,
        message = _ref19$message === void 0 ? "This value must be one of ".concat(arrayOfValues.join(', ')) : _ref19$message;
    return _oneOf(arrayOfValues, message);
  },
  ref: function ref(val) {
    return _ref20(val.ref);
  },
  nullable: function nullable() {
    return _nullable();
  }
};
exports.jsonConstraints = jsonConstraints;