"use strict";

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

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

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

var _moment = require("moment");

var _isNil = _interopRequireDefault(require("lodash/isNil"));

var _utils = require("../../utils");

var _ValidationResult = _interopRequireDefault(require("../ValidationResult"));

var _localeContext = require("../../locale-context");

var _math = _interopRequireDefault(require("../../math"));

var isUnderflow = function isUnderflow(value, min, range) {
  if (range) {
    return (0, _utils.toRangeValue)(value, range).some(function (item) {
      return !(0, _utils.isEmpty)(item) && ((0, _moment.isMoment)(item) ? item.isBefore(min) : _math["default"].lt(item, min));
    });
  }

  if ((0, _moment.isMoment)(value)) {
    return value.isBefore(min);
  }

  return _math["default"].lt(value, min);
};

function rangeUnderflow(value, validatorBaseProps, getProp) {
  if (!(0, _utils.isEmpty)(value)) {
    var min = getProp('min');

    if (!(0, _isNil["default"])(min)) {
      var range = getProp('range');

      if (isUnderflow(value, min, range)) {
        var format = getProp('format');
        var label = getProp('label');
        var injectionOptions = {
          min: (0, _moment.isMoment)(min) ? min.format(format) : min,
          label: label
        };
        var ruleName = 'rangeUnderflow';

        var _ref = getProp('defaultValidationMessages') || {},
            _ref$ruleName = _ref[ruleName],
            validationMessage = _ref$ruleName === void 0 ? (0, _localeContext.$l)('Validator', 'range_underflow') : _ref$ruleName;

        return new _ValidationResult["default"]({
          validationProps: (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, validatorBaseProps), {}, {
            min: min,
            range: range
          }),
          validationMessage: validationMessage,
          injectionOptions: injectionOptions,
          value: value,
          ruleName: ruleName
        });
      }
    }
  }

  return true;
}
//# sourceMappingURL=rangeUnderflow.js.map
