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 { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
/** The controller will check the calendar render mode when component will mode and copy the slot element to calendar event if the calendar event is a repeat event or the event displayed in popover */
export class VegaCalendarEventSlotController extends VegaSlimmer {
  /**
   * Find the current calendar event slot element, if the slot element is exists, set the calendar even slotMode to true, and store the current slot element.
   */
  findTheCalendarEventSlot() {
    if (this.calendarEvent) {
      const { event: rawEvent } = this.calendarEvent;
      const calendar = this.getParentCalendarElement();
      if (calendar) {
        const matchSlot = calendar.querySelector(`vega-calendar-event-slot[event-key='${rawEvent.key}']`);
        if (matchSlot) {
          this.slotMode = true;
          this.matchSlotElement = matchSlot;
        }
      }
    }
  }
  /**
   * Append the current slot content to slot container if the calendar event is a repeat event or the calendar event render in a popover.
   */
  appendTheRepeatEventSlotToContainer() {
    const slotContainerRef = this.renderer.getSlotContainerRef();
    if (this.slotMode && slotContainerRef && this.matchSlotElement) {
      if (this.calendarEvent.isRepeatEvent || this.isPopoverContent()) {
        const contentElement = this.matchSlotElement.cloneNode(true);
        contentElement.removeAttribute('slot');
        const existSlot = slotContainerRef.querySelector('vega-calendar-event-slot');
        if (!existSlot) {
          slotContainerRef.appendChild(contentElement);
        }
      }
    }
  }
  /**
   * This method is responsible for finding and returning the parent `vega-calendar` element of the current component instance
   *
   * @returns {HTMLVegaCalendarElement} The vega calendar element
   */
  getParentCalendarElement() {
    return this.host['data-calendar-ref'];
  }
  /**
   * Check the calendar is in popover
   *
   * @returns {boolean} true or false
   */
  isPopoverContent() {
    return this.host.classList.contains('calendar-event-in-popover');
  }
}
__decorate([
  MapToComponentField()
], VegaCalendarEventSlotController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventSlotController.prototype, "calendarEvent", void 0);
__decorate([
  MapToComponentField({ writable: true })
], VegaCalendarEventSlotController.prototype, "slotMode", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventSlotController.prototype, "renderer", void 0);
__decorate([
  MapToComponentMethod('componentWillLoad')
], VegaCalendarEventSlotController.prototype, "findTheCalendarEventSlot", null);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaCalendarEventSlotController.prototype, "appendTheRepeatEventSlotToContainer", null);
