"use strict";

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

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

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

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

var _moment = require("moment");

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

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

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

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

function isStepMismatch(value, step, min, max, range) {
  if (range) {
    var nearStepValues;
    (0, _utils.toRangeValue)(value, range).every(function (item) {
      if (!(0, _utils.isEmpty)(item)) {
        nearStepValues = (0, _moment.isMoment)(item) ? (0, _utils.getNearStepMomentValues)(item, step) : (0, _utils.getNearStepValues)(item, step, min, max);
      }

      return !nearStepValues;
    });
    return nearStepValues;
  }

  return (0, _moment.isMoment)(value) ? (0, _utils.getNearStepMomentValues)(value, step) : (0, _utils.getNearStepValues)(value, step, min, max);
}

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

    if (step !== undefined) {
      var nonStrictStep = getProp('nonStrictStep');

      if (nonStrictStep === false) {
        var min = getProp('min');
        var max = getProp('max');
        var range = getProp('range');
        var nearStepValues = isStepMismatch(value, step, min, max, range);

        if (nearStepValues !== undefined) {
          var format = getProp('format');

          var _nearStepValues = (0, _slicedToArray2["default"])(nearStepValues, 2),
              before = _nearStepValues[0],
              after = _nearStepValues[1];

          var injectionOptions = {
            0: (0, _moment.isMoment)(before) ? before.format(format) : _math["default"].toString(before),
            1: (0, _moment.isMoment)(after) ? after.format(format) : _math["default"].toString(after)
          };
          var ruleName = nearStepValues.length === 2 ? 'stepMismatchBetween' : 'stepMismatch';
          var key = nearStepValues.length === 2 ? 'step_mismatch_between' : 'step_mismatch';

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

          return new _ValidationResult["default"]({
            validationProps: (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, validatorBaseProps), {}, {
              step: step,
              min: min,
              max: max,
              range: range
            }),
            validationMessage: validationMessage,
            injectionOptions: injectionOptions,
            value: (0, _moment.isMoment)(value) ? value.format(format) : value,
            ruleName: ruleName
          });
        }
      }
    }
  }

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