import { CalendarPeriod } from './calendar-period.abstract';
import { CalendarDate } from '../calendar-date';
import { getWeekdays } from '../utils';
/**
 * Period of month
 */
export class MonthPeriod extends CalendarPeriod {
  constructor(current, showWeekends, timezone) {
    super(MonthPeriod.DISPLAY_DATE_COUNT, current, timezone);
    this._type = 'month';
    this._showWeekends = true;
    this._showWeekends = showWeekends === false ? false : true;
  }
  /**
   * The function `getMonthFirstDay` returns the first day of a given month in a calendar.
   *
   * @param {CalendarDate} [calendarDate] - The `calendarDate` parameter is an optional parameter of
   * type `CalendarDate`. It represents a specific date in the calendar. If a `calendarDate` is
   * provided, the function will use that date to determine the first day of the month. If no
   * `calendarDate` is provided, the function will use the current date considering timezone.
   * @param {string} [timezone] - The timezone to use when no calendarDate is provided
   * @returns {CalendarDate} A new `CalendarDate` object representing the first day of the month from the provided
   * `calendarDate` parameter or the current date if no parameter is provided.
   */
  static getMonthFirstDay(calendarDate, timezone) {
    const date = calendarDate || CalendarDate.from(new Date(), timezone);
    return new CalendarDate(date.year, date.month, 1);
  }
  /**
   * Calculate the period start date and end date.
   *
   * @param {CalendarDate} date - The date to calculate the period from.
   */
  calculatePeriod(date) {
    const firstDay = MonthPeriod.getMonthFirstDay(date);
    const cursor = firstDay.toDate();
    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);
  }
  /**
   * @inheritDoc
   */
  next() {
    const current = this.current.toDate();
    const year = current.getFullYear();
    const month = current.getMonth() + 1;
    const day = current.getDate();
    // Get the last day of the next month
    const lastDayOfNextMonth = new Date(year, month + 1, 0).getDate();
    const nextDay = Math.min(day, lastDayOfNextMonth);
    const next = new Date(year, month, nextDay);
    return this.createBy(CalendarDate.from(next));
  }
  /**
   * @inheritDoc
   */
  previous() {
    const current = this.current.toDate();
    const year = current.getFullYear();
    const month = current.getMonth() - 1;
    const day = current.getDate();
    // Get the last day of the previous month
    const lastDayOfPreviousMonth = new Date(year, month + 1, 0).getDate();
    const previousDay = Math.min(day, lastDayOfPreviousMonth);
    const previous = new Date(year, month, previousDay);
    return this.createBy(CalendarDate.from(previous));
  }
  /**
   * @inheritDoc
   */
  createBy(current) {
    return new MonthPeriod(current, this._showWeekends, this.timezone);
  }
}
MonthPeriod.DISPLAY_DATE_COUNT = 6 * 7;
