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, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { isCalendarDateType } from '../../../../../types/type-guard';
import { MonthPeriod } from '../../../../../helpers/calendar';
/** Allow repick controller. */
export class VegaDatePickerCalendarAllowRepickController extends VegaSlimmer {
  /**
   * Trigger repick
   *
   * @param {DatePickerCalendarRepickType} type - The type of repick.
   */
  triggerRepick(type) {
    this.repickType = type;
    if (type === 'end') {
      this.setEndDateAsCurrentMonthPeriod();
    }
  }
  /** while selection completed, we should clear repick type */
  watchCalendarSelection() {
    if (this.calendarSelection.isCompleted()) {
      this.repickType = null;
    }
  }
  /**
   * Get triggered repick type.
   *
   * @returns {DatePickerCalendarRepickType} - Repick type.
   */
  getRepickType() {
    return this.repickType;
  }
  /**
   * Set end date as current period.
   *  - while start date and end date in same month of year, we set startDate as current period
   *  - while in range mobile device, since we only render on month, so set the end date at current period
   *  - while in range desktop devide, we set previous month as current period to keep end date is in second month.
   */
  setEndDateAsCurrentMonthPeriod() {
    const startDate = this.calendarSelection.startDate;
    const endDate = this.calendarSelection.endDate;
    if (endDate) {
      const date = isCalendarDateType(startDate) && startDate.compare(endDate).inMonth
        ? startDate
        : this.isMobileDevice
          ? endDate
          : endDate.add(0, -1);
      this.currentPeriodController.updateCalendarPeriod(new MonthPeriod(date));
    }
  }
}
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarAllowRepickController.prototype, "calendarSelection", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarAllowRepickController.prototype, "isMobileDevice", void 0);
__decorate([
  MapToComponentField()
], VegaDatePickerCalendarAllowRepickController.prototype, "currentPeriodController", void 0);
__decorate([
  MapToComponentMethod('triggerRepick')
], VegaDatePickerCalendarAllowRepickController.prototype, "triggerRepick", null);
__decorate([
  MapToComponentMethod('watchCalendarSelection')
], VegaDatePickerCalendarAllowRepickController.prototype, "watchCalendarSelection", null);
