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 InternalVegaZIndexManager from '../zindex/internal-vega-z-index-manager';
import { generateUUID } from '../../utils/misc';
import { getDeviceType } from '../../utils/ui';
import { DeviceType } from '../../types/ui.type';
import { InjectVegaSlimmer, InjectVegaTelemetrySlimmer, createDynamicSlimmer, methodPlaceholder, } from 'vega-slimmer/core';
import { PageResizeObserverSlimmer } from '../slimmers/page-resize-observer-slimmer';
import { createPublicAPIRuntimeMetricsSlimmer } from '../slimmers/runtime-metrics/create-public-api-runtime-metrics-slimmer';
import { VegaNotifyRuntimeMetricsPayloadDefinition } from '../../constants/runtime-metrics-payload-definition/public-api/VegaNotifyController/runtime-metrics-payload-definition';
/**
 * The page notification controller
 */
class VegaNotifyController {
  constructor() {
    this.resizeObserverSlimmer = createDynamicSlimmer(VegaNotifyController, PageResizeObserverSlimmer, [], [{ mapFrom: 'connectResizeObserver', mapTo: 'connectResizeObserver' }], [
      {
        // eslint-disable-next-line jsdoc/require-jsdoc
        callback: (entries) => {
          var _a;
          const currentVisible = entries[0].contentRect.height > 0;
          if (!currentVisible && ((_a = this.container) === null || _a === void 0 ? void 0 : _a.classList.contains('hydrated'))) {
            InternalVegaZIndexManager.removeZIndex(this.container, 'top');
            this.container.remove();
            this.container = null;
          }
        },
        // eslint-disable-next-line jsdoc/require-jsdoc
        target: () => this.container,
      },
    ]);
    this.vegaPublicAPIRuntimeMetricsSlimmer = createPublicAPIRuntimeMetricsSlimmer(VegaNotifyController, ['open', 'close', 'closeAll'], VegaNotifyRuntimeMetricsPayloadDefinition);
  }
  /**
   * It creates a new notification element, adds it to the container, and returns the notification's ID
   *
   * @param {VegaNotifyOption} options - VegaNotifyProps
   * @returns {Promise<string>} The ID of the notification.
   * @vegaVersion 1.17.0
   */
  async open(options) {
    const pageNotification = this.createPageNotification(options);
    this.getContainer().append(pageNotification);
    return pageNotification.id;
  }
  /**
   * It closes all notifications
   *
   * @vegaVersion 1.17.0
   */
  async closeAll() {
    this.container &&
      Array.from(this.container.children).forEach((item) => {
        void item.close();
      });
  }
  /**
   * It closes the notification with the given id
   *
   * @param {string} id - The id of the notification to close.
   * @vegaVersion 1.17.0
   */
  async close(id) {
    // eslint-disable-next-line no-restricted-globals
    await document.getElementById(id).close();
  }
  /**
   * The function "connectResizeObserver" is used to connect a resize observer.
   *
   * @vegaVersion 1.17.0
   */
  connectResizeObserver() {
    methodPlaceholder();
  }
  /**
   * It creates a new page notification element, sets its properties, and returns it
   *
   * @param {VegaNotifyOption} props - VegaNotifyProps - the properties of the notification
   * @returns {HTMLVegaPageNotificationElement} A function that takes in a props object and returns a pageNotification element.
   */
  createPageNotification(props) {
    const pageNotification = 
    // eslint-disable-next-line no-restricted-globals
    document.createElement('vega-page-notification');
    Object.keys(props).forEach((propName) => {
      props[propName] !== undefined && (pageNotification[propName] = props[propName]);
    });
    pageNotification.style.marginBottom = getDeviceType() === DeviceType.MOBILE ? '12px' : '16px';
    pageNotification.id = `vega-page-notification-${generateUUID()}`;
    return pageNotification;
  }
  /**
   * It creates a div element, sets its z-index to be on top of everything else, adds a class to it, and
   * then adds it to the body of the page
   *
   * @returns {HTMLVegaPageNotificationListElement} The container is being returned.
   */
  getContainer() {
    if (this.container) {
      return this.container;
    }
    // eslint-disable-next-line no-restricted-globals
    this.container = document.createElement('vega-page-notification-list');
    InternalVegaZIndexManager.setZIndex(this.container, 'top');
    if (getDeviceType() === DeviceType.MOBILE) {
      this.container.classList.add('vega-page-notification-list-bottom');
    }
    else {
      this.container.classList.add('vega-page-notification-list-topRight');
    }
    // eslint-disable-next-line no-restricted-globals
    document.body.append(this.container);
    this.connectResizeObserver();
    return this.container;
  }
}
__decorate([
  InjectVegaSlimmer()
], VegaNotifyController.prototype, "resizeObserverSlimmer", void 0);
__decorate([
  InjectVegaTelemetrySlimmer()
], VegaNotifyController.prototype, "vegaPublicAPIRuntimeMetricsSlimmer", void 0);
export const VegaNotify = new VegaNotifyController();
