import { a4 as getWeekdays, a5 as WEEKDAYS_SHORT, $ as CalendarDate } from './type-guard-5e7bf13e.js';

/**
 * Calendar view generator abstract class.
 */
class CalendarViewGenerator {
  constructor() {
    this.weekdays = getWeekdays();
  }
  /**
   * Get weekday names.
   *
   * @returns {string[]} - The weekday strings
   */
  getWeekdayNames() {
    return WEEKDAYS_SHORT;
  }
  /**
   * Generate calendar dates
   *
   * @param {CalendarPeriod} period - The calendar period
   * @returns {Date[]} - The date result
   */
  generateDates(period) {
    const cursor = period.startDate.toDate();
    const dates = [];
    for (let i = 0; i < period.count; i++) {
      dates.push(new Date(cursor));
      cursor.setDate(cursor.getDate() + 1);
    }
    return dates;
  }
  /**
   * Generate calendar head
   *
   * @returns {string[]} - The data of week head
   */
  generateWeekHead() {
    return this.weekdays.map((day) => this.getWeekdayNames()[day]);
  }
}

/**
 * Calendar view
 */
class CalendarView {
  constructor(weekdays, dateItems) {
    this._weekdays = weekdays;
    this._dateItems = dateItems;
  }
  /**
   * The week head of the calendar view
   *
   * @type {string[]}
   */
  get weekdays() {
    return this._weekdays;
  }
  /**
   * The date items of the calendar view
   *
   * @type {CalendarDate[][]}
   */
  get dateItems() {
    return this._dateItems;
  }
}

/**
 * Calendar month view generator
 */
class MonthViewGenerator extends CalendarViewGenerator {
  /**
   * @inheritDoc
   */
  generate(period) {
    const showWeekends = period.showWeekends;
    let calendarDates = [];
    let week = [];
    const dates = this.generateDates(period);
    dates.forEach((date, i) => {
      if (!(i % 7)) {
        calendarDates[i / 7] = week = [];
      }
      const item = CalendarDate.from(date);
      week.push(item);
    });
    let weekDays = this.generateWeekHead();
    if (showWeekends === false) {
      weekDays = weekDays.slice(1, 6);
      calendarDates = calendarDates.map((items) => items.slice(1, 6));
    }
    return new CalendarView(weekDays, calendarDates);
  }
}

export { CalendarViewGenerator as C, MonthViewGenerator as M, CalendarView as a };
