"use strict";

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

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

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

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

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

var _enum = require("../../data-set/enum");

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

var isBadInput = function isBadInput(value, range) {
  if (range) {
    return (0, _utils.toRangeValue)(value, range).some(function (item) {
      return !(0, _utils.isEmpty)(item) && _math["default"].isNaN(item);
    });
  }

  return _math["default"].isNaN(value);
};

function badInput(value, _, getProp) {
  if (!(0, _utils.isEmpty)(value)) {
    var type = getProp('type');

    if (type === _enum.FieldType.number || type === _enum.FieldType.currency) {
      var range = getProp('range');

      if (isBadInput(value, range)) {
        var ruleName = 'badInput';

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

        return new _ValidationResult["default"]({
          validationProps: {
            type: type,
            range: range
          },
          validationMessage: validationMessage,
          value: value,
          ruleName: ruleName
        });
      }
    }
  }

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