"use strict";

exports.__esModule = true;
exports.validationRules = exports.showError = exports.hasValidationErrors = exports.hasMultipleFieldValidationErrors = void 0;
var _propertyValidator = require("./propertyValidator");
var _constants = require("../constants");
var _lodash = require("lodash");
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var validationRules = exports.validationRules = _defineProperty(_defineProperty(_defineProperty(_defineProperty({}, _constants.validationErrorTypes.fieldIsRequired, function (value) {
  if (value === null || value === undefined) return false;
  if ((0, _propertyValidator.isString)(value) && (0, _propertyValidator.isStringNullOrWhitespace)(value)) return false;
  return true;
}), _constants.validationErrorTypes.fieldMustBeValidEmail, function (value) {
  if (!value) return true;
  var regexRule = new RegExp("^(([\\w-]+[\\.\\+])+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|(([a-zA-Z0-9][a-zA-Z0-9\\.\\-]+[a-zA-Z0-9])|([a-zA-Z0-9]{1,}))\\.[a-zA-Z]{2,12})$");
  return regexRule.test(value);
}), _constants.validationErrorTypes.fieldMustBeValidPhoneNumber, function (value) {
  if (!value) return true;
  var regexRule = new RegExp("^(\\+?\\-?[ 0-9])*$");
  return regexRule.test(value);
}), _constants.validationErrorTypes.fieldMustBeValidPhoneExtension, function (value) {
  if (!value) return true;
  var regexRule = new RegExp("^(\\-?\\*?\\#?[ 0-9])*$");
  return regexRule.test(value);
});
var showError = exports.showError = function showError(field) {
  if (field == null) return false;
  return field.error != null;
};
var hasValidationErrors = exports.hasValidationErrors = function hasValidationErrors(editState, validationMap) {
  if ((0, _lodash.isEmpty)(editState)) return true;
  var fields = Object.keys(editState);
  var hasAnyValidationErrors = false;
  fields.forEach(function (field) {
    var isValid = editState[field].isValid ? editState[field].isValid() : true;
    if (isValid === false) {
      hasAnyValidationErrors = true;
    }
  });
  if (validationMap) {
    var mapKeys = Object.keys(validationMap);
    mapKeys.forEach(function (mapKey) {
      validationMap[mapKey].map.forEach(function (node) {
        if (node.elements) {
          node.elements.forEach(function (element) {
            var fullElementPath = [].concat(node.path, element.path);
            var errorTypes = [];
            if (validationMap[mapKey].errorTypes) {
              errorTypes.push.apply(errorTypes, validationMap[mapKey].errorTypes);
            }
            if (node.errorTypes) {
              errorTypes.push.apply(errorTypes, node.errorTypes);
            }
            if (element.errorTypes) {
              errorTypes.push.apply(errorTypes, element.errorTypes);
            }
            var isValid = editState[mapKey].isValid(null, fullElementPath, errorTypes, element.dependencies);
            if (isValid === false) {
              hasAnyValidationErrors = true;
            }
          });
        } else {
          var errorTypes = [];
          if (validationMap[mapKey].errorTypes) {
            errorTypes.push(validationMap[mapKey].errorTypes);
          }
          if (node.errorTypes) {
            errorTypes.push(node.errorTypes);
          }
          var isValid = editState[mapKey].isValid(null, node.path, errorTypes);
          if (isValid === false) {
            hasAnyValidationErrors = true;
          }
        }
      });
    });
  }
  return hasAnyValidationErrors;
};
var hasMultipleFieldValidationErrors = exports.hasMultipleFieldValidationErrors = function hasMultipleFieldValidationErrors(editStates) {
  if (editStates) {
    for (var _i = 0, _Object$keys = Object.keys(editStates); _i < _Object$keys.length; _i++) {
      var id = _Object$keys[_i];
      var groupedStates = editStates[id];
      if (hasValidationErrors(groupedStates) !== false) {
        return true;
      }
    }
  }
  return false;
};
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(validationRules, "validationRules", "/home/vsts/work/1/s/src/utils/modelValidationHelper.js");
  reactHotLoader.register(showError, "showError", "/home/vsts/work/1/s/src/utils/modelValidationHelper.js");
  reactHotLoader.register(hasValidationErrors, "hasValidationErrors", "/home/vsts/work/1/s/src/utils/modelValidationHelper.js");
  reactHotLoader.register(hasMultipleFieldValidationErrors, "hasMultipleFieldValidationErrors", "/home/vsts/work/1/s/src/utils/modelValidationHelper.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(validationRules, "validationRules", "/home/vsts/work/1/s/src/utils/modelValidationHelper.js");
  reactHotLoader.register(showError, "showError", "/home/vsts/work/1/s/src/utils/modelValidationHelper.js");
  reactHotLoader.register(hasValidationErrors, "hasValidationErrors", "/home/vsts/work/1/s/src/utils/modelValidationHelper.js");
  reactHotLoader.register(hasMultipleFieldValidationErrors, "hasMultipleFieldValidationErrors", "/home/vsts/work/1/s/src/utils/modelValidationHelper.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();