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 { VegaCalendarFullDayEventLayoutGenerator } from '../layout/full-day-event-layout-generator';
import { VegaCalendarEventSorter } from '../helpers/calendar-event-sorter';
import { VegaCalendarEventItemRenderer } from './vega-calendar-event-item-renderer';
/** This code snippet defines a TypeScript class `VegaCalendarFullDayEventLayoutRenderer` that extends `VegaSlimmer`. Here's a breakdown of what the class and its methods are doing: */
export class VegaCalendarFullDayEventLayoutRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    this.longEventLayoutGenerator = new VegaCalendarFullDayEventLayoutGenerator();
    this.calendarEventSorter = new VegaCalendarEventSorter();
  }
  /**
   * The function `sortLongEvents` sorts calendar events based on a fixed layout generated by `longEventLayoutGenerator`.
   *
   * @param {CalendarEvent[][]} calendarEvents - calendar current row all events
   * @returns {Nullable<CalendarEvent>[][]} the events after sorter
   */
  sortLongEvents(calendarEvents) {
    this.fullDayEventLayout = this.longEventLayoutGenerator.generate(calendarEvents);
    return calendarEvents.map((items, index) => {
      return this.calendarEventSorter.sort(items, index, this.fullDayEventLayout);
    });
  }
  /**
   * render the month view or week view long events
   *
   * @param {Nullable<CalendarEvent>[]} calendarEvents current events
   * @param {currentViewIndex} currentViewIndex the current index of the row
   * @returns {HTMLElement[]} event elements
   */
  render(calendarEvents, currentViewIndex, targetCalendarViewDate) {
    return calendarEvents.map((item, index) => {
      if (item) {
        return (h("div", { key: item.eventKey, class: this.viewMode === 'month' ? 'month-event-block' : 'day-event-block', style: this.getEventStyle(item, currentViewIndex, index, this.viewMode === 'month'
            ? VegaCalendarFullDayEventLayoutRenderer.monthViewEventOffsetTop
            : 0) }, this.eventPreviewPopoverRenderer.render(item, targetCalendarViewDate, {
          'month-event': this.viewMode === 'month',
          'day-event': this.viewMode !== 'month',
        })));
      }
    });
  }
  /*
   * The below method is e2e-test covered in
   * @see{module:vega-calendar-render-full-day-event-correct}
   */
  /* istanbul ignore next */
  /**
   * Get the current event layout style from the params
   *
   * @param {CalendarEvent} calendarEvent current event item
   * @param {number} currentViewIndex the current date index of the row
   * @param {number} itemIndex the current event index of the event array
   * @param {number} offsetTop the default offset top for the event
   * @returns {VegaCalendarEventStyle} layout style object
   */
  getEventStyle(calendarEvent, currentViewIndex, itemIndex, offsetTop) {
    const cellCount = this.calendarView.weekdays.length;
    const start = currentViewIndex;
    let end = currentViewIndex + 1;
    const top = itemIndex;
    let visible = true;
    const order = this.fullDayEventLayout.getEventRowsIndex(calendarEvent);
    if (order >= 0) {
      const startIndex = this.fullDayEventLayout.eventLayoutCells[order].getEventStartIndex(calendarEvent);
      const endIndex = this.fullDayEventLayout.eventLayoutCells[order].getEventEndIndex(calendarEvent);
      if (startIndex === currentViewIndex) {
        end = endIndex + 1;
      }
      else {
        visible = false;
      }
    }
    return {
      position: 'absolute',
      left: `${(100 / cellCount) * start}%`,
      top: `${top * VegaCalendarEventItemRenderer.backgroundColorModeEventHeight + offsetTop}px`,
      width: `${(100 / cellCount) * (end - start)}%`,
      opacity: visible ? '1' : '0',
    };
  }
}
VegaCalendarFullDayEventLayoutRenderer.monthViewEventOffsetTop = 26;
__decorate([
  MapToComponentField()
], VegaCalendarFullDayEventLayoutRenderer.prototype, "eventItemRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarFullDayEventLayoutRenderer.prototype, "viewMode", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarFullDayEventLayoutRenderer.prototype, "calendarView", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarFullDayEventLayoutRenderer.prototype, "eventPreviewPopoverRenderer", void 0);
