export class ErrorFormatterHelper {
  static formatError(value: any, minValue = null, maxValue = null) {
    if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
      if (value.backend && value.fleio_error) {
        return value.fleio_error;
      }
      if (value.custom && value.message) {
        return value.message;
      }
      if (value.email) {
        return $localize`Enter a valid email.`;
      }
      if (value.invalidTime) {
        if (value.example) {
          return $localize`Invalid format. Valid format example: ${value.example}`;
        } else {
          return $localize`Invalid format.`;
        }
      }
      if (value.doesNotExists) {
        return $localize`Select an existing value`;
      }
      if (value.matDatepickerParse) {
        return $localize`Invalid date format`;
      }
      if (value.minlength) {
        if (value.minlength.requiredLength) {
          return $localize`The value's length is lower than minimum(${value.minlength.requiredLength})`;
        } else {
          return $localize`The value's length is lower than minimum`;
        }
      }
      if (value.maxlength) {
        if (value.maxlength.requiredLength) {
          return $localize`The value's length is greater than maximum(${value.maxlength.requiredLength})`;
        } else {
          return $localize`The value's length is greater than minimum`;
        }
      }
      if (value.min) {
        if (minValue === null) {
          minValue = value.min.min;
        }
        return $localize`The value is lower than minimum(${minValue})`;
      }
      if (value.max) {
        if (maxValue === null) {
          maxValue = value.max.max;
        }
        return $localize`The value is greater than maximum(${maxValue})`;
      }
      if (value.pattern) {
        if (value.pattern.requiredPattern === '^[0-9]*' || value.pattern.requiredPattern === '^[0-9]*$') {
          return $localize`Input should contain only numbers`;
        } else {
          return $localize`Invalid pattern`;
        }
      }
      if (value.invalidObjectSelected) {
        return $localize`Invalid item selected`;
      }
      if (value.objectRequired) {
        return $localize`The value has to be a valid record`;
      }
      if (value.invalidSelection) {
        return $localize`The value must be either empty or a valid selection`;
      }
      if (value.invalidInternationalPhone) {
        return $localize`Number must start with + followed by country code`;
      }
      if (value.numericRequired) {
        // browser may think letters in number input is null so this error
        // must take precedence over 'required' error
        return $localize`Numeric value required`;
      }
      if (value.integerRequired) {
        return $localize`The value has to be a valid integer`;
      }
      if (value.invalidUrl) {
        return $localize`Invalid url`;
      }
      if (value.nonZero) {
        return $localize`The value cannot be 0 (zero)`;
      }
      if (value.required) {
        return $localize`This field is required`;
      }
    }
    return value;
  }
}
