/**
As of version 4 the default sort order of enums is the order in which they are defined.
https://github.com/UselessPickles/ts-enum-util/issues/7#issuecomment-475499821
*/
export var ValidatorNames;
(function (ValidatorNames) {
  ValidatorNames["required"] = "required";
  ValidatorNames["max"] = "max";
  ValidatorNames["maxlength"] = "maxlength";
  ValidatorNames["min"] = "min";
  ValidatorNames["minlength"] = "minlength";
  ValidatorNames["pattern"] = "pattern";
})(ValidatorNames || (ValidatorNames = {}));
export const defaultErrorMessagesConfig = {
  required: () => 'This entry is required',
  maxlength: (options) => `Please enter a value that is ${options} characters or less`,
  minlength: (options) => `Please enter a value that is ${options} characters or more`,
  pattern: () => 'Please enter a value in the correct format',
};
/**  reducer callback: iterates through validators and returns the first one that fails it's validate function  */
export const combineValidators = (v1, v2) => {
  const combined = {
    validate: (vParam) => {
      const result1 = v1.validate(vParam);
      const result2 = v2.validate(vParam);
      if (!result1)
        combined.errorMessage = v1.errorMessage;
      else if (!result2)
        combined.errorMessage = v2.errorMessage;
      return result1 && result2;
    },
  };
  return combined;
};
/** creates a list of ValidatorEntry objects from the ValidatorNames enum that exist on the component class
    example: [
      {name: 'required},
      {name: 'min', options: '5'}
    ]
  */
export const composeValidatorList = (componentClass) => Object.values(ValidatorNames)
  .filter((vname) => componentClass[vname] != null)
  .map((validatorEntry) => ({
  name: validatorEntry,
  customErrorMessage: componentClass[`${validatorEntry}Message`],
  options: componentClass[validatorEntry],
}));
export const defaultValidator = () => ({
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  validate: (_value) => true,
});
export const getValidator = (list) => (list || [])
  .map((validatorEntry) => validatorFactory(validatorEntry))
  .reduce(combineValidators, defaultValidator());
export const initializeValidators = (componentClass) => {
  const validatorList = composeValidatorList(componentClass);
  return getValidator(validatorList);
};
export const isRequiredValidator = (customErrorMessage) => ({
  errorMessage: customErrorMessage || defaultErrorMessagesConfig.required(),
  validate: (inputElement) => inputElement && 'validity' in inputElement ? !inputElement.validity.valueMissing : true,
});
export const maxlengthValidator = (customErrorMessage, options) => ({
  errorMessage: customErrorMessage || defaultErrorMessagesConfig.maxlength(options),
  validate: (inputElement) => inputElement && 'validity' in inputElement ? !inputElement.validity.tooLong : true,
});
export const minlengthValidator = (customErrorMessage, options) => ({
  errorMessage: customErrorMessage || defaultErrorMessagesConfig.minlength(options),
  validate: (inputElement) => inputElement && 'validity' in inputElement ? !inputElement.validity.tooShort : true,
});
export const patternValidator = (customErrorMessage) => ({
  errorMessage: customErrorMessage || defaultErrorMessagesConfig.pattern(),
  validate: (inputElement) => inputElement && 'validity' in inputElement ? !inputElement.validity.patternMismatch : true,
});
export const validatorFactory = (validatorEntry) => {
  const { name, customErrorMessage, options } = validatorEntry;
  const { maxlength, minlength, pattern, required } = ValidatorNames;
  switch (name) {
    case maxlength:
      return maxlengthValidator(customErrorMessage, options);
    case minlength:
      return minlengthValidator(customErrorMessage, options);
    case pattern:
      return patternValidator(customErrorMessage);
    case required:
      return isRequiredValidator(customErrorMessage);
    default:
      return defaultValidator();
  }
};
//# sourceMappingURL=validator.js.map