'use strict';

const internalFormFieldValidationRule_abstract = require('./internal-form-field-validation-rule.abstract-07cdff46.js');

/**
 * A rule that checks if the length of input is less than or equal to a maximum value
 *
 * @augments {InternalFormFieldValidationRule<string>}
 * @property {{ maxStrLength: number }} config config including the maxStrLength limit for validation
 */
class MaxStringLengthRule extends internalFormFieldValidationRule_abstract.InternalFormFieldValidationRule {
  /**
   * It creates a new instance of the class with the given config.
   *
   * @param {{ maxStrLength: number }} config config including the maxStrLength limit for validation
   * @param {number} config.maxStrLength number of the maxStrLength
   */
  constructor(config) {
    super();
    this.config = config;
  }
  /**
   * It checks if the input is a string and if it's shorter than the maximum string length
   *
   * @param {string} input - string
   * @returns {EvaluateResultTranslation} An object with two properties: isValid and message.
   * @throws {Error} if input is unable to evaluate
   */
  evaluate(input) {
    var _a;
    if (!this.isAcceptedInputType(input)) {
      throw new Error(`Unexpected type of input: ${typeof input}: ${input}`);
    }
    const maxStrLength = ((_a = this.config) === null || _a === void 0 ? void 0 : _a.maxStrLength) || Number.MAX_VALUE;
    return {
      isValid: input.length <= maxStrLength,
      message: input.length <= maxStrLength
        ? ''
        : { key: 'The maximum input length is {0}', args: [maxStrLength] },
    };
  }
  /**
   * It returns true if the input is of an accepted type and the field is not disabled and visible
   *
   * @param {unknown} input - The input to be evaluated.
   * @param {FormFieldStatusMeta} [status] - FormFieldStatusMeta
   * @returns {boolean} A boolean value.
   */
  canEvaluate(input, status) {
    return status
      ? this.isAcceptedInputType(input) && !status.disabled && status.visible
      : this.isAcceptedInputType(input);
  }
  /**
   * If the field is not disabled, visible, and has been touched or modified, then show the error
   *
   * @param {FormFieldStatusMeta} status - FormFieldStatusMeta
   * @returns {boolean} A boolean value.
   */
  shouldShowError(status) {
    return !status.disabled && status.visible && (status.touched || status.modified);
  }
  /**
   * This function returns true if the input is a string, and false otherwise.
   *
   * @param {unknown} input - The input to be validated.
   * @returns {boolean} A boolean value.
   */
  isAcceptedInputType(input) {
    return typeof input === 'string';
  }
}

/**
 * A rule that checks if the length of input is more than or equal to a minimum value
 *
 * @augments {InternalFormFieldValidationRule<string>}
 * @property {{ minStrLength: number }} config config including the minStrLength limit for validation
 */
class MinStringLengthRule extends internalFormFieldValidationRule_abstract.InternalFormFieldValidationRule {
  /**
   * It creates a new instance of the class with the given config.
   *
   * @param {{ minStrLength: number }} config config including the minStrLength limit for validation
   * @param {number} config.minStrLength number of the minStrLength
   */
  constructor(config) {
    super();
    this.config = config;
  }
  /**
   * It checks if the input is a string and if it's longer than the minimum string length
   *
   * @param {string} input - string
   * @returns {EvaluateResultTranslation} An object with two properties: isValid and message.
   * @throws {Error} if input is unable to evaluate
   */
  evaluate(input) {
    var _a;
    if (!this.isAcceptedInputType(input)) {
      throw new Error(`Unexpected type of input: ${typeof input}: ${input}`);
    }
    const minStrLength = ((_a = this.config) === null || _a === void 0 ? void 0 : _a.minStrLength) || 0;
    return {
      isValid: input.length >= minStrLength,
      message: input.length >= minStrLength
        ? ''
        : {
          key: 'The minimum input length is {0}',
          args: [minStrLength],
        },
    };
  }
  /**
   * If the input is accepted and the status is not disabled and visible, then return true
   *
   * @param {unknown} input - The input to be evaluated.
   * @param {FormFieldStatusMeta} [status] - FormFieldStatusMeta
   * @returns {boolean} A boolean value.
   */
  canEvaluate(input, status) {
    return status
      ? this.isAcceptedInputType(input) && !status.disabled && status.visible
      : this.isAcceptedInputType(input);
  }
  /**
   * If the field is not disabled, visible, and has been touched or modified, then show the error
   *
   * @param {FormFieldStatusMeta} status - FormFieldStatusMeta
   * @returns {boolean} A boolean value.
   */
  shouldShowError(status) {
    return !status.disabled && status.visible && (status.touched || status.modified);
  }
  /**
   * This function returns true if the input is a string, and false otherwise.
   *
   * @param {unknown} input - The input value to be validated.
   * @returns {boolean} A boolean value.
   */
  isAcceptedInputType(input) {
    return typeof input === 'string';
  }
}

exports.MaxStringLengthRule = MaxStringLengthRule;
exports.MinStringLengthRule = MinStringLengthRule;
