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 { CalendarDate, DayPeriod, MonthPeriod, WeekPeriod, } from '../../../../../helpers/calendar';
import { getClosestWorkday } from '../../../utils';
/** This TypeScript class `VegaCalendarCurrentPeriodController` updates the current period based on the view mode in a calendar component. */
export class VegaCalendarCurrentPeriodController extends VegaSlimmer {
  /**
   * set new current period
   *
   * @param {CalendarPeriod} newPeriod new value
   */
  setCurrentPeriod(newPeriod) {
    this.currentPeriod = newPeriod;
  }
  /**
   * set new current period by the view mode and the current date
   *
   * @param {VegaCalendarViewModeType} viewMode view mode value
   * @param {CalendarDate} date current date
   */
  updateCalendarPeriod(viewMode, date) {
    switch (viewMode) {
      case 'month':
        this.setCurrentPeriod(new MonthPeriod(date, this.showWeekends, this.timezone));
        break;
      case 'week':
        this.setCurrentPeriod(new WeekPeriod(date, this.showWeekends, this.timezone));
        break;
      case 'day':
        date = this.showWeekends === false ? getClosestWorkday(date) : date;
        this.setCurrentPeriod(new DayPeriod(date, this.showWeekends, this.timezone));
        break;
    }
  }
  /**
   * default current period is month period, re-create period if the default view mode is not month
   * set default calendar view
   */
  initialCurrentPeriod() {
    this.updateCalendarPeriod(this.viewMode, CalendarDate.from(new Date(), this.timezone));
  }
  /**
   * emit period change event
   *
   * @param {CalendarPeriod} newValue the new calendar period
   * @param {CalendarPeriod} oldValue the prev calendar period
   */
  emitChangeEvent(newValue, oldValue) {
    if (newValue &&
      oldValue &&
      newValue.startDate.equal(oldValue.startDate) &&
      newValue.endDate.equal(oldValue.endDate)) {
      return;
    }
    if (newValue && newValue.type !== this.viewMode) {
      this.viewMode = newValue.type;
    }
    this.changeEventEmitter.emit({
      viewMode: newValue.type,
      currentPeriod: this.getCurrentPeriodSummary(),
    });
  }
  /**
   * update the current period when view mode change
   *
   * @param {VegaCalendarViewModeType} viewMode new view mode value
   */
  updateCurrentPeriodByViewMode(viewMode) {
    if (this.currentPeriod && viewMode !== this.currentPeriod.type) {
      this.updateCalendarPeriod(viewMode, this.currentPeriod.current);
    }
  }
  /**
   * The calendar period should update when the property showWeekends change
   */
  updateCurrentPeriodAfterHideWeekEndsChange() {
    this.updateCalendarPeriod(this.viewMode, this.currentPeriod.current);
  }
  /**
   * The function `getStartDateAndEndDate` returns an array of `VegaCalendarDateType` objects based on the `startDate` and `endDate` properties of the `currentPeriod` object.
   *
   * @returns {VegaCalendarChangeEventType['currentPeriod']} An array of objects containing the date, month, and year properties of the currentPeriod's
   * start date and end date.
   */
  getCurrentPeriodSummary() {
    const { current, startDate, endDate } = this.currentPeriod;
    return {
      year: current.year,
      month: current.month,
      startDate: {
        date: startDate.date,
        month: startDate.month,
        year: startDate.year,
      },
      endDate: {
        date: endDate.date,
        month: endDate.month,
        year: endDate.year,
      },
    };
  }
}
__decorate([
  MapToComponentField({ writable: true })
], VegaCalendarCurrentPeriodController.prototype, "currentPeriod", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarCurrentPeriodController.prototype, "changeEventEmitter", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaCalendarCurrentPeriodController.prototype, "viewMode", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarCurrentPeriodController.prototype, "showWeekends", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarCurrentPeriodController.prototype, "timezone", void 0);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaCalendarCurrentPeriodController.prototype, "initialCurrentPeriod", null);
__decorate([
  MapToComponentMethod('watchCurrentPeriodChange')
], VegaCalendarCurrentPeriodController.prototype, "emitChangeEvent", null);
__decorate([
  MapToComponentMethod('watchViewModeChange')
], VegaCalendarCurrentPeriodController.prototype, "updateCurrentPeriodByViewMode", null);
__decorate([
  MapToComponentMethod('watchShowWeekendsChange')
], VegaCalendarCurrentPeriodController.prototype, "updateCurrentPeriodAfterHideWeekEndsChange", null);
