import { InternalFormFieldValidationRule } from './internal-form-field-validation-rule.abstract';
/**
 * A rule that checks if the toggle switch is required to switch on
 *
 * @augments {InternalFormFieldValidationRule<boolean>}
 */
export class ToggleSwitchRequiredRule extends InternalFormFieldValidationRule {
  /**
   * If the input is a boolean, return a boolean. If the
   * input is anything else, return false
   *
   * @param {unknown} input - The input to be evaluated.
   * @returns {EvaluateResult} - An object with two properties: isValid and message.
   */
  evaluate(input) {
    if (typeof input === 'boolean') {
      return {
        isValid: input,
        message: input ? '' : 'This field is required',
      };
    }
    else {
      return {
        isValid: false,
        message: '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} The return value is a boolean that indicates whether the input can be evaluated.
   */
  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 boolean.
   *
   * @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 === 'boolean';
  }
}
