export var ValidatorNames;
(function (ValidatorNames) {
  ValidatorNames["max"] = "max";
  ValidatorNames["maxLength"] = "maxLength";
  ValidatorNames["min"] = "min";
  ValidatorNames["minLength"] = "minLength";
  ValidatorNames["pattern"] = "pattern";
  ValidatorNames["required"] = "required";
})(ValidatorNames || (ValidatorNames = {}));
const defaultErrorMessagesConfig = {
  required: 'This entry is required',
};
/**  reducer callback: iterates through validators and returns the first one that fails it's validate function  */
export function 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 function composeValidatorList(componentClass) {
  const vList = Object.values(ValidatorNames)
    .filter((vname) => componentClass[vname] != null)
    .map((validatorEntry) => ({ name: validatorEntry, options: componentClass[validatorEntry] }));
  return getValidator(vList);
}
export const defaultValidator = () => ({
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  validate: (_value) => true,
});
export const getValidator = (list) => (list || [])
  .map((validator) => validatorFactory(validator.name))
  .reduce(combineValidators, defaultValidator());
export const isRequiredValidator = () => ({
  errorMessage: defaultErrorMessagesConfig.required,
  validate: (inputElement) => inputElement && 'validity' in inputElement ? !inputElement.validity.valueMissing : true,
});
export const validatorFactory = (name) => {
  switch (name) {
    case ValidatorNames.required:
      return isRequiredValidator();
    default:
      return defaultValidator();
  }
};
//# sourceMappingURL=validator.js.map