'use strict';

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

/**
 * A rule that checks if the input range is empty or not
 *
 * @augments {InternalFormFieldValidationRule<string[]>}
 */
class InputRangeRequiredRule extends internalFormFieldValidationRule_abstract.InternalFormFieldValidationRule {
  /**
   * It checks if the input is a valid value.
   *
   * @param {string[]} input - The value of the input field.
   * @returns {EvaluateResult} An object with two properties: isValid and message.
   */
  evaluate(input) {
    if (!this.isAcceptedInputType(input)) {
      throw new Error(`Invalid value ${input} for input range`);
    }
    const isValid = Array.isArray(input) &&
      input.length === 2 &&
      input.every((item) => !!item || item == '0');
    return {
      isValid,
      message: isValid ? '' : 'This field is required',
    };
  }
  /**
   * 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);
  }
  /**
   * If the input is undefined or null, return true, otherwise return true if the input is a string.
   *
   * @param {unknown} input - The input value to be validated.
   * @returns {boolean}A boolean.
   */
  isAcceptedInputType(input) {
    if (input === undefined || input === null)
      return true;
    return Array.isArray(input);
  }
}

exports.InputRangeRequiredRule = InputRangeRequiredRule;
