var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { MapToComponentField, VegaSlimmer, MapToComponentMethod } from 'vega-slimmer/core';
import { isArrayEqual } from '../../../../utils/array';
import { LogUtility } from '../../../../utils/log';
import { FORM_ELEMENT_VALUE_CHANGE } from '../../../../helpers/change-manager/subject/static-subject/static-subject-title';
import ChangeManager from '../../../../helpers/change-manager/change-manager';
import { CalendarDate } from '../../../../helpers/calendar';
/** The date picker value controller slimmer */
export class VegaDatePickerValueController extends VegaSlimmer {
  /*
   * call this method to open the date picker dropdown
   * The below method is e2e-test covered in
   * @see{module:vega-date-picker-render-ui-with-single}
   */
  /** Set default value if value is not set when component will load */
  componentWillLoad() {
    if (!this.value || !this.valueGuard(this.value)) {
      this.setDefaultValueByMode();
    }
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-date-picker-single-date-change}
   * @see{module:vega-date-picker-single-mode-auto-validate-value-change}
   * @see{module:vega-date-picker-range-mode-auto-validate-value-change}
   * @see{module:should-receive-the-value-from-vegaChange-event-in-single-mode}
   * @see{module:should-receive-the-value-from-vegaChange-event-in-range-mode}
   */
  /**
   * Update value when property of value changed.
   *
   * @param {DatePickerValueType<DatePickerModeType>} newValue - The new value of date picker.
   * @param {DatePickerValueType<DatePickerModeType>} oldValue - The old value of date picker.
   */
  watchValue(newValue, oldValue) {
    /**
     * - single mode: @Watch will not be trigger if the value not change.
     * - range mode: the value will be updated to the dropdown calendar,
     *      avoid the value changed again by `onDatePickerSelected` in range mode.
     */
    if (this.mode === 'range' &&
      isArrayEqual(newValue, oldValue)) {
      return; // avoid the value changed again by `onDatePickerSelected` in range mode
    }
    if (!this.valueGuard(newValue)) {
      this.setDefaultValueByMode();
    }
    this.changeEventEmitter.emit(this.value);
    ChangeManager.notify(FORM_ELEMENT_VALUE_CHANGE, {
      host: this.host,
      detail: this.value,
    });
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-date-picker-mode-change}
   */
  /* istanbul ignore next */
  /** Update datePicker settings property of mode is changed */
  watchMode() {
    this.setDefaultValueByMode();
  }
  /**
   * The function `getValue` returns the value of a DatePicker component, with type checking based on the specified DatePickerModeType.
   *
   * @typedef T generic type
   * @returns {DatePickerValueType<T>} The method is returning the value of type `DatePickerValueType<T>`.
   */
  getValue() {
    this.valueGuard(this.value);
    return this.value;
  }
  /**
   * The function sets the value of a date picker if it passes a type guard check.
   *
   * @param {DatePickerValueType} value - The `value` parameter is of type `DatePickerValueType<T>`. This means that it can
   * accept a value of any type `T` that extends `DatePickerModeType`.
   */
  setValue(value) {
    if (this.valueGuard(value)) {
      this.value = value;
    }
  }
  /**
   * The function sets default values based on the mode specified.
   */
  setDefaultValueByMode() {
    if (this.mode === 'range') {
      this.setValue(['', '']);
    }
    if (this.mode === 'single') {
      this.setValue('');
    }
  }
  /**
   * The function `valueGuard` checks if the provided value is valid for the specified mode of a date picker component.
   *
   * @param {DatePickerValueType<DatePickerModeType>} value - The `value` parameter is of type `DatePickerValueType<DatePickerModeType>`. It
   * represents the value of the date picker, which can be a single date or a range of dates depending on
   * the mode of the date picker.
   * @returns {boolean} a boolean value.
   */
  valueGuard(value) {
    let isValid;
    switch (this.mode) {
      case 'single':
        isValid =
          (value && typeof value === 'string' && !!CalendarDate.fromString(value, this.format)) ||
            value === '';
        break;
      case 'range':
        isValid =
          Array.isArray(value) &&
            value.length === 2 &&
            value.every((item) => !!CalendarDate.fromString(item, this.format) || item === '');
        break;
    }
    if (!isValid) {
      LogUtility.warn(`Invalid date picker value ${value} for ${this.mode} mode`);
    }
    return isValid;
  }
}
__decorate([
  MapToComponentField({ writable: true })
], VegaDatePickerValueController.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerValueController.prototype, "mode", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerValueController.prototype, "format", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerValueController.prototype, "changeEventEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerValueController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerValueController.prototype, "isCalendarShow", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerValueController.prototype, "vegaDatePickerCalendarRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerValueController.prototype, "calendarOpenStateController", void 0);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaDatePickerValueController.prototype, "componentWillLoad", null);
__decorate([
  MapToComponentMethod('watchValue')
], VegaDatePickerValueController.prototype, "watchValue", null);
__decorate([
  MapToComponentMethod('watchMode')
], VegaDatePickerValueController.prototype, "watchMode", null);
