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 } from 'vega-slimmer/core';
import { PageResizeObserverSlimmer } from '../../../../../helpers/slimmers/page-resize-observer-slimmer';
import { debounce } from '../../../../../utils/timer';
import { VegaCalendarEventItemRenderer } from '../renderers/vega-calendar-event-item-renderer';
import changeManager from '../../../../../helpers/change-manager/change-manager';
import domNodeSubjectFactory from '../../../../../helpers/change-manager/subject/dom-node-subject/dom-node-subject-factory';
import { VegaInternalCalendarMonthEventsChange } from '../../../../../helpers/event-manager/event-id/vega-internal-event-id';
/** The `VegaCalendarEventResponsiveController` class extends `PageResizeObserverSlimmer` and updates the max display count of month events with debounce in a VegaCalendar component. */
export class VegaCalendarEventResponsiveController extends PageResizeObserverSlimmer {
  /**
   * update the event view when the calendar layout update
   */
  constructor() {
    super({
      // eslint-disable-next-line jsdoc/require-jsdoc
      target: /* istanbul ignore next */ () => this.host,
      // eslint-disable-next-line jsdoc/require-jsdoc
      callback: /* istanbul ignore next */ () => this.updateMaxDisplayCountWithDebounce(),
    });
    this.updateMaxDisplayCountWithDebounce = debounce(this.updateMaxDisplayCount, 500, this);
  }
  /**
   * Update the month event display items.
   */
  updateMaxDisplayCount() {
    // The debounce method will case this method called after component disconnect, then the slimmer will get issue to get viewMode from component class, this issue will catch in spec.ts
    try {
      const eventContainerRef = this.monthEventRenderer.getMonthEventContainerRef();
      if (this.viewMode === 'month' && eventContainerRef && eventContainerRef.parentElement) {
        const maxContainerHeight = eventContainerRef.parentElement.getBoundingClientRect().height;
        const count = Math.floor((maxContainerHeight - VegaCalendarEventResponsiveController.monthDateTitleHeight) /
          VegaCalendarEventItemRenderer.backgroundColorModeEventHeight);
        changeManager.notify(domNodeSubjectFactory.getSubject(this.host, VegaInternalCalendarMonthEventsChange), {
          host: this.host,
          detail: count,
        });
      }
      // eslint-disable-next-line no-empty
    }
    catch (_a) { }
  }
  /**
   * update the max display count when switch view mode
   *
   * @param {VegaCalendarViewModeType} viewMode new view mode value
   */
  updateMaxDisplayCountAfterSwitchToMonthView(viewMode) {
    if (viewMode === 'month') {
      this.updateMaxDisplayCountWithDebounce();
    }
  }
}
VegaCalendarEventResponsiveController.monthDateTitleHeight = 26;
__decorate([
  MapToComponentField()
], VegaCalendarEventResponsiveController.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventResponsiveController.prototype, "monthEventRenderer", void 0);
__decorate([
  MapToComponentField()
], VegaCalendarEventResponsiveController.prototype, "viewMode", void 0);
__decorate([
  MapToComponentMethod('componentDidLoad')
], VegaCalendarEventResponsiveController.prototype, "updateMaxDisplayCount", null);
__decorate([
  MapToComponentMethod('watchViewModeChange')
], VegaCalendarEventResponsiveController.prototype, "updateMaxDisplayCountAfterSwitchToMonthView", null);
