"use strict";

exports.__esModule = true;
exports.hasMultipleFieldValidationErrors = exports.hasValidationErrors = exports.showError = exports.validationRules = void 0;

var _propertyValidator = require("./propertyValidator");

var _constants = require("../constants");

var _lodash = require("lodash");

var _validationRules;

(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(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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 = (_validationRules = {}, _defineProperty(_validationRules, _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;
}), _defineProperty(_validationRules, _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-Z]{2,12})$");
  return regexRule.test(value);
}), _defineProperty(_validationRules, _constants.validationErrorTypes.fieldMustBeValidPhoneNumber, function (value) {
  if (!value) return true;
  var regexRule = new RegExp("^(\\+?\\-?[ 0-9])*$");
  return regexRule.test(value);
}), _defineProperty(_validationRules, _constants.validationErrorTypes.fieldMustBeValidPhoneExtension, function (value) {
  if (!value) return true;
  var regexRule = new RegExp("^(\\-?\\*?\\#?[ 0-9])*$");
  return regexRule.test(value);
}), _validationRules);
exports.validationRules = validationRules;

var showError = function showError(field) {
  if (field == null) return false;
  return field.error != null;
};

exports.showError = showError;

var 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;
};

exports.hasValidationErrors = hasValidationErrors;

var 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;
};

exports.hasMultipleFieldValidationErrors = hasMultipleFieldValidationErrors;
;

(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);
})();