'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 time is empty or not
 *
 * @augments {InternalFormFieldValidationRule<string[]>}
 */
class TimeRangeRequiredRule extends internalFormFieldValidationRule_abstract.InternalFormFieldValidationRule {
  /**
   * It creates a new instance of the class with the given config.
   *
   * @param {{ timeFormat: TimeFormat }} config - config including the timeFormat for time value
   * @param {TimeFormat} config.timeFormat - The timeFormat for time value
   */
  constructor(config) {
    super();
    this.config = config;
  }
  /**
   * It checks if the input is a valid time string.
   *
   * @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 time value ${input} for range mode`);
    }
    const isValid = Array.isArray(input) &&
      input.length === 2 &&
      input.every((item) => typeGuard.isTimeStr(item, this.config.timeFormat));
    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);
  }
}

/**
 * A rule that checks if time is empty or not
 *
 * @augments {InternalFormFieldValidationRule<string>}
 */
class TimeRequiredRule extends internalFormFieldValidationRule_abstract.InternalFormFieldValidationRule {
  /**
   * It creates a new instance of the class with the given config.
   *
   * @param {{ timeFormat: TimeFormat }} config - config including the timeFormat for time value
   * @param {TimeFormat} config.timeFormat - The timeFormat for time value
   */
  constructor(config) {
    super();
    this.config = config;
  }
  /**
   * It checks if the input is a valid time string.
   *
   * @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 time value ${input}`);
    }
    const isValid = typeGuard.isTimeStr(input, this.config.timeFormat);
    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 typeof input === 'string';
  }
}

exports.TimeRangeRequiredRule = TimeRangeRequiredRule;
exports.TimeRequiredRule = TimeRequiredRule;
