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 { MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
/** Event preview popover controller */
export class VegaCalendarEventPreviewPopoverController extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.singleEventPreviewContainerMap = new Map();
    this.groupEventsPreviewContainerMap = new Map();
  }
  /**
   * Set the event preview container for the given events
   *
   * @param {Nullable<CalendarEvent>[] | CalendarEvent} events - The calendar event(s)
   * @param {CalendarDate} viewDate - The current date
   * @param {HTMLElement} container - The HTML element to be used as the preview container
   */
  setEventPreviewContainerMap(events, viewDate, container) {
    const map = Array.isArray(events)
      ? this.groupEventsPreviewContainerMap
      : this.singleEventPreviewContainerMap;
    map.set(this.generateMapKey(events, viewDate), container);
  }
  /**
   * Delete the event preview container for the given events
   *
   * @param {Nullable<CalendarEvent>[] | CalendarEvent} events - The calendar event(s)
   * @param {CalendarDate} viewDate - The current date
   */
  deleteEventPreviewContainer(events, viewDate) {
    const map = Array.isArray(events)
      ? this.groupEventsPreviewContainerMap
      : this.singleEventPreviewContainerMap;
    map.delete(this.generateMapKey(events, viewDate));
  }
  /**
   * Get the event preview container for the given event
   *
   * @param {CalendarEvent} event - The calendar event
   * @param {CalendarDate} viewDate - The current date
   * @param {boolean} isGroup - Whether the event is part of a group
   * @returns {Nullable<HTMLElement>} - The HTML element used as the preview container, or null if not found
   */
  getEventPreviewContainer(event, viewDate, isGroup) {
    const map = isGroup
      ? this.groupEventsPreviewContainerMap
      : this.singleEventPreviewContainerMap;
    const singleKey = this.generateMapKey(event, viewDate);
    if (map.has(singleKey)) {
      return map.get(singleKey);
    }
    else {
      const dateString = this.getDateString(viewDate);
      const groupKey = Array.from(map.keys()).find((key) => key.includes(event.eventKey) && key.endsWith(dateString));
      if (groupKey) {
        return map.get(groupKey);
      }
    }
  }
  /**
   *  clear all preview container maps when timezone changes
   *
   */
  clearAllPreviewContainers() {
    this.singleEventPreviewContainerMap.clear();
    this.groupEventsPreviewContainerMap.clear();
  }
  /**
   * Generate a unique key for the given events
   *
   * @param {Nullable<CalendarEvent>[] | CalendarEvent} events - The calendar event(s)
   * @param {CalendarDate} viewDate - The current date
   * @returns {string} - A unique string key representing the event(s)
   */
  generateMapKey(events, viewDate) {
    const dateString = this.getDateString(viewDate);
    return Array.isArray(events)
      ? events
        .filter((event) => event)
        .map((event) => event.eventKey)
        .join('-') + dateString
      : events.eventKey + dateString;
  }
  /**
   * Get a date string in the format "-YYYY-M-D" for the given date
   *
   * @param {CalendarDate} viewDate - The date object
   * @returns {string} - The formatted date string, or an empty string if the date is null
   */
  getDateString(viewDate) {
    return `-${viewDate.year}-${viewDate.month}-${viewDate.date}`;
  }
}
__decorate([
  MapToComponentMethod('watchTimezoneChange')
], VegaCalendarEventPreviewPopoverController.prototype, "clearAllPreviewContainers", null);
