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 } from '../../../../../helpers/calendar';
/**
 * Controller for managing the post-operation date in the Vega Calendar.
 */
export class VegaCalendarPostOperationDateController extends VegaSlimmer {
  /**
   * Set new current date
   *
   * @param {CalendarDate} currentDate new value
   */
  setPostOperationDate(currentDate) {
    this.postOperationDate = currentDate;
  }
  /**
   * Set post operation Date when the current period changes
   *
   * @param {CalendarPeriod} newValue the new calendar period
   * @param {CalendarPeriod} oldValue the prev calendar period
   */
  setDate(newValue, oldValue) {
    if (newValue.current !== this.postOperationDate) {
      if (this.shouldSetCurrentDateToFirstDayOfMonth(newValue, oldValue)) {
        const currentDate = newValue.current.toDate();
        if (currentDate) {
          currentDate.setDate(1);
          this.setPostOperationDate(CalendarDate.from(currentDate));
        }
      }
      else {
        this.setPostOperationDate(CalendarDate.from(newValue.current.toDate()));
      }
    }
  }
  /**
   * Handle timezone changes by updating postOperationDate
   *
   */
  updateAfterTimezoneChange() {
    this.postOperationDate = CalendarDate.from(new Date(), this.timezone);
  }
  /**
   * Check if the first date should be set
   * Both the new and old values are of the month type.
   * The new date is not equal to the current date.
   *
   * @param {CalendarPeriod} newValue The new calendar period
   * @param {CalendarPeriod} oldValue The old calendar period
   * @returns {boolean} True if the first date should be set, false otherwise
   */
  shouldSetCurrentDateToFirstDayOfMonth(newValue, oldValue) {
    return (newValue.type === 'month' &&
      (oldValue === null || oldValue === void 0 ? void 0 : oldValue.type) === 'month' &&
      !newValue.current.equal(CalendarDate.from(new Date(), this.timezone)));
  }
}
__decorate([
  MapToComponentField({ writable: true })
], VegaCalendarPostOperationDateController.prototype, "postOperationDate", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarPostOperationDateController.prototype, "timezone", void 0);
__decorate([
  MapToComponentMethod('watchCurrentPeriodChange')
], VegaCalendarPostOperationDateController.prototype, "setDate", null);
__decorate([
  MapToComponentMethod('watchTimezoneChange')
], VegaCalendarPostOperationDateController.prototype, "updateAfterTimezoneChange", null);
