'use strict';

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

/**
 * A rule that checks if date is empty or not
 *
 * @augments {InternalFormFieldValidationRule<string | string[]>}
 */
class DateRequiredRule extends internalFormFieldValidationRule_abstract.InternalFormFieldValidationRule {
  /**
   * It creates a new instance of the class with the given config.
   *
   * @param {{ mode: 'single' | 'range' }} config - config including the mode for date value
   * @param {'single' | 'range'} config.mode - The mode for date value
   */
  constructor(config) {
    super();
    this.config = config;
  }
  /**
   * If the input is not valid for the current mode, throw an error.
   * Otherwise, return an object with a boolean indicating whether the input is valid and a message to display to the user
   *
   * @param {string | 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 date value ${input} for mode ${this.config.mode}`);
    }
    let isValid;
    switch (this.config.mode) {
      case 'single':
        isValid = typeGuard.isDateStr(input);
        break;
      case 'range':
        isValid = Array.isArray(input) && input.length === 2 && input.every(typeGuard.isDateStr);
        break;
    }
    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, if the mode is 'single', return true if
   * the input is a string. Otherwise, if the mode is 'range', return true if the input is an array. Otherwise, return false.
   *
   * @param {unknown} input - The input value that the user has entered.
   * @returns {boolean} A boolean value.
   */
  isAcceptedInputType(input) {
    if (input === undefined || input === null)
      return true;
    if (!this.config.mode) {
      throw new Error(`Unexpected type of mode: ${this.config.mode}`);
    }
    switch (this.config.mode) {
      case 'single':
        return typeof input === 'string';
      case 'range':
        return Array.isArray(input);
      default:
        return false;
    }
  }
}

exports.DateRequiredRule = DateRequiredRule;
