import { CalendarDate } from '../calendar-date';
import { getSystemTimezone, getWeekdays } from '../utils';
/**
 * CalendarPeriod is an abstract class that represents a period of date for calendar.
 */
export class CalendarPeriod {
  constructor(count, current, timezone) {
    this._count = count;
    this._timezone = timezone || getSystemTimezone();
    this._current = current ? current : CalendarDate.from(new Date(), this._timezone);
    this.calculatePeriod(this._current);
  }
  /**
   * The current date of the period.
   *
   * @type {CalendarDate}
   */
  get current() {
    return this._current;
  }
  /**
   * The start date of the period
   *
   * @type {CalendarDate}
   */
  get startDate() {
    return this._startDate;
  }
  /**
   * The end date of the period
   *
   * @type {CalendarDate}
   */
  get endDate() {
    return this._endDate;
  }
  /**
   * The count of the period
   *
   * @type {number}
   */
  get count() {
    return this._count;
  }
  /**
   * This function returns the type of the object.
   *
   * @type {string}
   */
  get type() {
    return this._type;
  }
  /**
   * This function returns the showWeekends of the object.
   *
   * @type {boolean}
   */
  get showWeekends() {
    return this._showWeekends;
  }
  /**
   * The timezone of the period.
   *
   * @type {string | undefined}
   */
  get timezone() {
    return this._timezone;
  }
  /**
   * Go to a specific date.
   *
   * @param {CalendarDate} date - The date to go to.
   * @returns {this} - The current period.
   */
  goTo(date) {
    return this.createBy(date);
  }
  /**
   * Go to now.
   *
   * @returns {this} - The current period.
   */
  now() {
    return this.createBy(CalendarDate.from(new Date(), this._timezone));
  }
  /**
   * Check the give period is equal to current period
   *
   * @param {CalendarPeriod} [period] - Give period to compare
   * @returns {boolean} compare result
   */
  equal(period) {
    return this.startDate.equal(period.startDate) && this.endDate.equal(period.endDate);
  }
  /**
   * Calculate the period start date and end date.
   *
   * @param {CalendarDate} date - The date to calculate the period from.
   */
  calculatePeriod(date) {
    const cursor = date.toDate();
    if (this.count > 1) {
      cursor.setDate(cursor.getDate() - getWeekdays().indexOf(cursor.getDay()));
    }
    this._startDate = CalendarDate.from(cursor);
    cursor.setDate(cursor.getDate() + this.count - 1);
    this._endDate = CalendarDate.from(cursor);
  }
}
