"use strict";

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

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

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 isOverflow = function isOverflow(value, max, range) {
  if (range) {
    return (0, _utils.toRangeValue)(value, range).some(function (item) {
      return !(0, _utils.isEmpty)(item) && ((0, _moment.isMoment)(item) ? item.isAfter(max) : _math["default"].gt(item, max));
    });
  }

  if ((0, _moment.isMoment)(value)) {
    return value.isAfter(max);
  }

  return _math["default"].gt(value, max);
};

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

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

      if (isOverflow(value, max, range)) {
        var format = getProp('format');
        var label = getProp('label');
        var injectionOptions = {
          max: (0, _moment.isMoment)(max) ? max.format(format) : max,
          label: label
        };
        var ruleName = 'rangeOverflow';

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

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

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