import { VegaInternalTranslation } from '../../../../../../helpers/translation/internal-translation-controller';
/** The above code defines an abstract class `CalendarEventBaseRepeatPattern` in TypeScript that represents a repeat pattern for calendar events. Here is a summary of what the code is doing: */
export class CalendarEventBaseRepeatPattern {
  /**
   * The constructor initializes the current date for a given date range and interval.
   *
   * @param {CalendarDate} repeatStartDate - The `repeatStartDate` parameter represents the starting date of a
   * calendar event or interval. It is of type `CalendarDate`, which likely contains information such as
   * the year, month, and day of the event.
   * @param {number} interval - The `interval` parameter represents the frequency at which the event
   * occurs. For example, if the interval is set to 2, it means the event occurs every 2 units of time
   * (e.g., every 2 days, weeks, months, etc.).
   * @param {Nullable<number>} count - The `count` parameter specifies the number of occurrences of the event. If `count`
   * is provided, the event will repeat `count` times within the specified interval. If `count` is
   * `null`, the event will repeat indefinitely until the `until` date is reached.
   * @param {Nullable<CalendarDate>} until - The `until` parameter in the constructor represents a date until which the recurring
   * event will occur. If this date is reached, the recurring event will stop.
   */
  constructor(repeatStartDate, interval, count, until) {
    this.repeatStartDate = repeatStartDate;
    this.interval = interval;
    this.count = count;
    this.until = until;
  }
  /**
   * The function `getRepeatSeries` retrieves a series of repeated events within a specified calendar period.
   *
   * @param {CalendarPeriod} period - The `period` parameter in the `getRepeatSeries` method represents
   * a specific time period in the form of a `CalendarPeriod` object. This object typically contains a
   * `startDate` and an `endDate`, which define the range of dates for which you want to generate repeat series.
   * @param {number} eventSpansDays - the current repeat series cross days, default value is one
   * @returns {CalendarDate[]}  repeat start date array
   */
  getRepeatSeries(period, eventSpansDays = 1) {
    this.generateRepeatDateCacheUntilEndDate(period.endDate);
    return this.cache.filter((item) => {
      return this.isSeriesInPeriod(item, eventSpansDays, period);
    });
  }
  /**
   * The function `isSeriesInPeriod` checks if a series of dates falls within a specified date range.
   *
   * @param {CalendarDate} series - The `series` parameter represents a specific date in the form of a
   * `CalendarDate` object.
   * @param {number} seriesSpansDays - The `seriesSpansDays` parameter represents the number of days
   * that the series spans. It is used to calculate the end date of the series based on the start date.
   * @param {CalendarPeriod} period - Date period
   * @returns {boolean} a boolean value indicating whether the given series falls within the specified date
   * range.
   */
  isSeriesInPeriod(series, seriesSpansDays, period) {
    const seriesStart = series.toDate();
    const seriesEnd = new Date(series.year, series.month - 1, series.date + seriesSpansDays - 1);
    const { startDate, endDate } = period;
    const rangeStart = startDate.toDate();
    const rangeEnd = endDate.toDate();
    return ((seriesStart >= rangeStart && seriesStart <= rangeEnd) ||
      (seriesEnd >= rangeStart && seriesEnd <= rangeEnd) ||
      (seriesStart <= rangeStart && seriesEnd >= rangeEnd));
  }
  /**
   * The function generates a string representation of the end condition for a repeating event,
   * 	either by a specific date or after a certain number of occurrences.
   *
   * @returns {string} a string representing the end condition of a repeating event.
   */
  generateRepeatEndsString() {
    if (this.until) {
      return ` ${VegaInternalTranslation.t('until {0}', `${this.until.getMonthName()} ${this.until.date}, ${this.until.year}`)}`;
    }
    else if (this.count) {
      return ` ${VegaInternalTranslation.t('after {0} occurrences', this.count)}`;
    }
    return '';
  }
}
CalendarEventBaseRepeatPattern.daySeconds = 24 * 60 * 60 * 1000;
