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 ChangeManager from '../../../../../helpers/change-manager/change-manager';
import { VegaInternalCalendarEventPreviewHide } from '../../../../../helpers/event-manager/event-id/vega-internal-event-id';
import { Observer } from '../../../../../helpers/change-manager/observer/observer';
import DomNodeSubjectFactory from '../../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
export class VegaCalendarEventPreviewPopoverRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.popoverHideObserver = null;
  }
  render(event, targetCalendarViewDate, classMap) {
    return this.enabledEventPreview
      ? this.renderPreviewPopover(event, classMap, targetCalendarViewDate)
      : this.eventItemRenderer.render(event, classMap, targetCalendarViewDate);
  }
  renderPreviewPopover(event, classMap, targetCalendarViewDate) {
    return (h("vega-popover", { isScreenPosition: true, placement: this.viewMode === 'day' ? 'top' : 'right', alignment: "center", class: {
        'event-preview-popover': true,
        'block': true,
      }, padding: "0", size: VegaCalendarEventPreviewPopoverRenderer.SINGLE_EVENT_PREVIEW_WIDTH, ref: (el) => {
        this.setPopoverHideObserver(el);
      } },
      h("div", { slot: "popover-content", class: "event-preview-popover-content" }, this.eventItemRenderer.render(event, classMap, targetCalendarViewDate)),
      h("div", { slot: "popover" },
        h("div", { class: "preview-container", ref: (el) => {
            el
              ? this.eventPreviewPopoverController.setEventPreviewContainerMap(event, targetCalendarViewDate, el)
              : this.eventPreviewPopoverController.deleteEventPreviewContainer(event, targetCalendarViewDate);
          } }))));
  }
  setPopoverHideObserver(popover) {
    if (popover) {
      this.popoverHideObserver = new Observer((_payload) => true, async () => await popover.hide());
      ChangeManager.register(DomNodeSubjectFactory.getSubject(this.host, VegaInternalCalendarEventPreviewHide), this.popoverHideObserver);
    }
    else if (this.popoverHideObserver) {
      ChangeManager.unregister(DomNodeSubjectFactory.getSubject(this.host, VegaInternalCalendarEventPreviewHide), this.popoverHideObserver);
    }
  }
}
VegaCalendarEventPreviewPopoverRenderer.SINGLE_EVENT_PREVIEW_WIDTH = 400;
__decorate([
  MapToComponentField()
], VegaCalendarEventPreviewPopoverRenderer.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventPreviewPopoverRenderer.prototype, "renderer", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventPreviewPopoverRenderer.prototype, "viewMode", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventPreviewPopoverRenderer.prototype, "timeFormat", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventPreviewPopoverRenderer.prototype, "eventItemRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventPreviewPopoverRenderer.prototype, "enabledEventPreview", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventPreviewPopoverRenderer.prototype, "eventPreviewPopoverController", void 0);
