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 { MapToComponentMethod, MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
import { getCurrentBreakpoint } from '../../utils/breakpoints';
import { tryGetDocument } from '../../utils/try-get-document';
/**
 * The `PageResizeObserverSlimmer` class is a TypeScript class that extends `VegaSlimmer` and provides functionality for observing page resize events.
 *
 * Note: The slimmer which will extends the `PageResizeObserverSlimmer` should not have same name method `connectedCallbackByResizeSlimmer` , `disconnectedCallbackByResizeSlimmer` and `componentDidLoadByResizeSlimmer`
 */
export class PageResizeObserverSlimmer extends VegaSlimmer {
  /**
   * The constructor function takes a PageResizeObserverEntry object as a parameter.
   *
   * @param {PageResizeObserverEntry} observerEntry - The `observerEntry` parameter is an instance of
   * the `PageResizeObserverEntry` class. It represents an entry in the `PageResizeObserver`'s list of
   * observed elements. It contains information about the observed element's size and position.
   */
  constructor(observerEntry) {
    super();
    this.observerEntry = observerEntry;
  }
  /**
   * create resize observer when connectedCallback
   */
  reconnectResizeObserver() {
    if (this.resizeObserver === null) {
      this.createResizeObserver();
    }
  }
  /**
   * cancel resize observer when disconnectedCallback
   */
  disconnectResizeObserver() {
    var _a;
    (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
    this.resizeObserver = null;
  }
  /**
   * create resize observer when componentDidLoad
   */
  connectResizeObserver() {
    this.createResizeObserver();
  }
  /**
   * The function creates a resize observer that calls a callback function with the current breakpoint whenever the page resize.
   */
  createResizeObserver() {
    var _a;
    const observerEntry = this.observerEntry;
    (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
    const VegaResizeObserver = window['VegaResizeObserver'] || window.ResizeObserver;
    if (VegaResizeObserver) {
      this.resizeObserver = new VegaResizeObserver((entries, observer) => {
        if (observerEntry &&
          typeof observerEntry.shouldTrigger === 'function' &&
          !observerEntry.shouldTrigger()) {
          return;
        }
        if (observerEntry && typeof observerEntry.callback === 'function') {
          /**
           * The function "handle" calls the callback function of an observer entry with the specified
           * arguments.
           */
          const handle = () => {
            observerEntry.callback(entries, observer);
          };
          observerEntry.isDeferred ? setTimeout(handle) : handle();
        }
        else {
          this.currentBreakpoint = getCurrentBreakpoint();
        }
      });
    }
    this.observerResizeObserver();
  }
  /**
   * The function `observerResizeObserver` observes the resize of one or multiple HTML elements using the
   * ResizeObserver API.
   */
  observerResizeObserver() {
    var _a;
    if (this.resizeObserver && tryGetDocument()) {
      const resizeObserver = this.resizeObserver;
      let observers = null;
      if ((_a = this.observerEntry) === null || _a === void 0 ? void 0 : _a.target) {
        observers = this.observerEntry.target();
      }
      if (Array.isArray(observers)) {
        observers.forEach((observer) => {
          resizeObserver.observe(observer);
        });
      }
      else if (observers) {
        resizeObserver.observe(observers);
      }
      else {
        resizeObserver.observe(tryGetDocument().body);
      }
    }
  }
}
__decorate([
  MapToComponentField({ writable: true, isOptional: true })
], PageResizeObserverSlimmer.prototype, "currentBreakpoint", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], PageResizeObserverSlimmer.prototype, "reconnectResizeObserver", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], PageResizeObserverSlimmer.prototype, "disconnectResizeObserver", null);
__decorate([
  MapToComponentMethod('componentDidLoad')
], PageResizeObserverSlimmer.prototype, "connectResizeObserver", null);
