"use strict";

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

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard")["default"];

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.colorRgbaReg = exports.colorHexReg = void 0;
exports["default"] = typeMismatch;
exports.urlReg = exports.emailReg = void 0;

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

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _moment = require("moment");

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

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

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

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

var _mobxHelper = require("../../mobx-helper");

var _types;

/* eslint-disable */
var emailReg = /^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/;
exports.emailReg = emailReg;
var urlReg = /^(?:(?:https?|HTTPS?|ftp|FTP):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-zA-Z\u00a1-\uffff0-9]-*)*[a-zA-Z\u00a1-\uffff0-9]+)(?:\.(?:[a-zA-Z\u00a1-\uffff0-9]-*)*[a-zA-Z\u00a1-\uffff0-9]+)*)(?::\d{2,5})?(?:[\/?#]\S*)?$/;
exports.urlReg = urlReg;
var colorRgbaReg = /^[rR][gG][Bb][Aa]?\((\s*(2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)\s*,){2}\s*(2[0-4][0-9]|25[0-5]|[01]?[0-9][0-9]?)\s*,?\s*(0?\.\d{1,2}|1|0)?\s*\){1}$/;
exports.colorRgbaReg = colorRgbaReg;
var colorHexReg = /^#([0-9a-fA-F]{6}|[0-9a-fA-F]{3}|[0-9a-fA-F]{8})$/;
/* eslint-enable */

exports.colorHexReg = colorHexReg;

var isNotDateIf = function isNotDateIf(value, range) {
  if (range) {
    return (0, _utils.toRangeValue)(value, range).some(function (item) {
      return !(0, _utils.isEmpty)(item) && (0, _moment.isMoment)(item) && !item.isValid();
    });
  }

  return (0, _moment.isMoment)(value) && !value.isValid();
};

var isNotDate = function isNotDate(value, range) {
  if (range) {
    return (0, _utils.toRangeValue)(value, range).some(function (item) {
      return !(0, _utils.isEmpty)(item) && (!(0, _moment.isMoment)(item) || !item.isValid());
    });
  }

  return !(0, _moment.isMoment)(value) || !value.isValid();
};

var types = (_types = {}, (0, _defineProperty2["default"])(_types, _enum.FieldType.email, [function (value) {
  return !emailReg.test(value);
}, 'EmailField', 'type_mismatch_email']), (0, _defineProperty2["default"])(_types, _enum.FieldType.url, [function (value) {
  return !urlReg.test(value);
}, 'UrlField', 'type_mismatch_url']), (0, _defineProperty2["default"])(_types, _enum.FieldType.color, [function (value) {
  return !(colorRgbaReg.test(value) || colorHexReg.test(value));
}, 'ColorPicker', 'type_mismatch_color']), (0, _defineProperty2["default"])(_types, _enum.FieldType.date, [isNotDate, 'DatePicker', 'type_mismatch_date']), (0, _defineProperty2["default"])(_types, _enum.FieldType.dateTime, [isNotDate, 'DatePicker', 'type_mismatch_date']), (0, _defineProperty2["default"])(_types, _enum.FieldType.week, [isNotDate, 'DatePicker', 'type_mismatch_date']), (0, _defineProperty2["default"])(_types, _enum.FieldType.month, [isNotDate, 'DatePicker', 'type_mismatch_date']), (0, _defineProperty2["default"])(_types, _enum.FieldType.year, [isNotDate, 'DatePicker', 'type_mismatch_date']), (0, _defineProperty2["default"])(_types, _enum.FieldType.time, [isNotDate, 'DatePicker', 'type_mismatch_date']), (0, _defineProperty2["default"])(_types, _enum.FieldType.auto, [isNotDateIf, 'DatePicker', 'type_mismatch_date']), _types);

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

    if (type) {
      var validateType = types[type];

      if (validateType) {
        var _validateType = (0, _slicedToArray2["default"])(validateType, 3),
            validate = _validateType[0],
            component = _validateType[1],
            validatorKey = _validateType[2];

        var range = getProp('range');

        if (validate(value, range)) {
          var ruleName = 'typeMismatch';

          var locale = _localeContext["default"].getCmp(component);

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

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

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