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 { h } from '@stencil/core';
import { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import { createEnterKeyHandlerToTriggerClick } from '../../../../../utils/accessibility';
import { getCalendarEventSlotName } from '../../../utils';
import ChangeManager from '../../../../../helpers/change-manager/change-manager';
import DomNodeSubjectFactory from '../../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { VegaInternalCalendarEventPreviewUpdate } from '../../../../../helpers/event-manager/event-id/vega-internal-event-id';
/** The `VegaCalendarEventItemRenderer` class in TypeScript React is used for rendering calendar event items with specific layout and styling properties. */
export class VegaCalendarEventItemRenderer extends VegaSlimmer {
  /**
   * render event component
   *
   * @param {CalendarEvent} item the calendar event object
   * @param {Record<string,boolean>} classMap the class name map
   * @param {CalendarDate} targetCalendarViewDate the target calendar view date
   * @returns {HTMLVegaCalendarEventElement} calendar event element
   */
  render(item, classMap, targetCalendarViewDate, isGroupEvent = false) {
    return (h("vega-calendar-event", { "data-calendar-ref": this.host, key: `${item.eventKey}-${item.startTime.toString()}-${item.endTime.toString()}`, calendarEvent: item, class: classMap, onKeyDown: createEnterKeyHandlerToTriggerClick(), 
      // eslint-disable-next-line react/jsx-no-bind
      onClick: () => {
        this.handleEventClick(item);
        this.enabledEventPreview &&
          this.setupPreviewContainer(item, targetCalendarViewDate, isGroupEvent);
      } }, !item.isRepeatEvent && h("slot", { name: getCalendarEventSlotName(item.event.key) })));
  }
  /**
   * emit vega click event
   *
   * @param {CalendarEvent} eventItem the event item
   */
  handleEventClick(eventItem) {
    this.eventClickEmitter.emit({ rawEvent: eventItem.event });
  }
  setupPreviewContainer(event, targetCalendarViewDate, isGroupEvent) {
    const previewContainer = this.eventPreviewPopoverController.getEventPreviewContainer(event, targetCalendarViewDate, isGroupEvent);
    if (previewContainer) {
      const preview = this.renderer.getEventPreviewRef();
      if (isGroupEvent) {
        preview.classList.remove('single-event-preview');
        preview.classList.add('group-event-preview');
      }
      else {
        preview.classList.remove('group-event-preview');
        preview.classList.add('single-event-preview');
      }
      // Step 1: notify preview component to update data
      ChangeManager.notify(DomNodeSubjectFactory.getSubject(preview, VegaInternalCalendarEventPreviewUpdate), {
        event,
        targetCalendarViewDate,
        timeFormat: this.timeFormat,
      });
      // Step 2: append preview additional fields slot to preview component.
      const additionalFieldsSlot = this.host.querySelector('[slot=event-preview-additional-fields]');
      additionalFieldsSlot && preview.append(additionalFieldsSlot);
      // Step 3: append event slot to preview component.
      const eventSlotName = getCalendarEventSlotName(event.event.key);
      const eventSlot = this.host.querySelector(`[slot=${eventSlotName}]`);
      const previewEventSlot = preview.querySelector(`[slot=${eventSlotName}]`);
      // Since event slot is used by vega-calendar-event, we should clone it to avoid remove from original position.
      eventSlot && !previewEventSlot && preview.append(eventSlot.cloneNode(true));
      // Step 4: append preview component to popover container to display.
      previewContainer.append(preview);
    }
  }
}
VegaCalendarEventItemRenderer.backgroundColorModeEventHeight = 18;
__decorate([
  MapToComponentField()
], VegaCalendarEventItemRenderer.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventItemRenderer.prototype, "calendarView", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventItemRenderer.prototype, "eventClickEmitter", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventItemRenderer.prototype, "timeFormat", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventItemRenderer.prototype, "enabledEventPreview", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventItemRenderer.prototype, "renderer", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventItemRenderer.prototype, "eventPreviewPopoverController", void 0);
