'use strict';

const misc = require('./misc-3d30df91.js');
const ui = require('./ui-fe7a2158.js');
const log = require('./global-slimmer-registry-7bbdb1f2.js');
const createPublicApiRuntimeMetricsSlimmer = require('./create-public-api-runtime-metrics-slimmer-1b8ecfb4.js');
require('./dynamic-slimmer-0050c489.js');

const VegaLoaderRuntimeMetricsPayloadDefinition = [
  {
    payloadKey: 'IS_LOAD_METHOD_CALLED',
    payloadDescription: 'Indicate if the method load of public API VegaLoaderController is called',
    /**
     * Indicate whether the method is collected when the method been called .
     *
     * @param {string} scope - The class name of public api
     * @param {string} methodName - The method name called by the public api
     * @returns {boolean} A boolean value
     */
    canAccept: (scope, methodName) => scope === 'VegaLoaderController' && methodName === 'load',
    /**
     * The function checks if the method name is load.
     *
     * @returns {boolean} A boolean value
     */
    payloadValue: () => {
      return true;
    },
  },
  {
    payloadKey: 'IS_CLOSE_METHOD_CALLED',
    payloadDescription: 'Indicate if the method close of public API VegaLoaderController is called',
    /**
     * Indicate whether the method is collected when the method been called .
     *
     * @param {string} scope - The class name of public api
     * @param {string} methodName - The method name called by the public api
     * @returns {boolean} A boolean value
     */
    canAccept: (scope, methodName) => scope === 'VegaLoaderController' && methodName === 'close',
    /**
     * The function checks if the method name is close.
     *
     * @returns {boolean} A boolean value
     */
    payloadValue: () => {
      return true;
    },
  },
];

var __decorate = (undefined && undefined.__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;
};
/**
 * Vega loader controller to control the loading indicator
 */
class VegaLoaderController {
  constructor() {
    this.vegaPublicAPIRuntimeMetricsSlimmer = createPublicApiRuntimeMetricsSlimmer.createPublicAPIRuntimeMetricsSlimmer(VegaLoaderController, ['load', 'close'], VegaLoaderRuntimeMetricsPayloadDefinition);
  }
  /**
   * Create a loading indicator and append it to the container
   *
   * @param {VegaLoaderIndicatorOptions} options - VegaLoaderOptions = {} options of the method
   * @returns {Nullable<string>} The uuid of the loader.
   * @vegaVersion 1.16.0
   */
  load(options = {}) {
    let containerSelector = options.containerSelector || 'body';
    let containerElement = document.querySelector(containerSelector);
    if (containerElement &&
      VegaLoaderController.SPECIAL_ELEMENT_TO_WRAP_FULL_BODY.includes(containerElement.tagName)) {
      containerSelector = 'body';
      containerElement = document.querySelector('body');
    }
    if (!containerElement) {
      log.LogUtility.error('Container is not found by selector: ', containerSelector);
      return null;
    }
    const loadingIndicator = this.createLoadingIndicator(options.indicatorOptions);
    const loader = this.createLoader(containerSelector);
    loader.append(loadingIndicator);
    return this.appendLoader(loader, containerElement);
  }
  /**
   * Close the loader by id
   *
   * @param {string} id - The id of the loader.
   * @vegaVersion 1.16.0
   */
  close(id) {
    const loader = this.findLoader(id);
    if (loader) {
      if (loader.dataset.container) {
        const container = document.querySelector(loader.dataset.container);
        if (container) {
          if (container.dataset['vegaLoaderOriginPosition']) {
            container.style.position = container.dataset['vegaLoaderOriginPosition'];
          }
        }
      }
      loader.remove();
    }
  }
  /**
   * Find loader by id, if the id include '>>>' it will find inside shadowRoot
   *
   * @param {string} id - The id of the loader.
   * @returns {Nullable<HTMLElement>} The loader found by id.
   */
  findLoader(id) {
    var _a, _b;
    let loader;
    if (id.includes('>>>')) {
      const [containerSelector, loaderSelector] = id.split('>>>');
      loader = (_b = (_a = document.querySelector(containerSelector)) === null || _a === void 0 ? void 0 : _a.shadowRoot) === null || _b === void 0 ? void 0 : _b.getElementById(loaderSelector);
    }
    else {
      loader = document.getElementById(id);
    }
    return loader;
  }
  /**
   * Create a loader for loading indicator
   *
   * @param {string} containerSelector The container which you want to append
   * @returns {HTMLElement} a wrapper for loader
   */
  createLoader(containerSelector) {
    const loader = document.createElement('vega-loader-wrapper');
    loader.className = ui.arrayToClassString([
      containerSelector === 'body' ? 'fixed-wrapper' : 'absolute-wrapper',
    ]);
    loader.dataset['container'] = containerSelector;
    loader.id = `vega-loader-${misc.generateUUID()}`;
    return loader;
  }
  /**
   * Create a loading indicator
   *
   * @param {VegaLoaderIndicatorOptions} indicatorOptions the props of vega loading indicator
   * @returns {HTMLVegaLoadingIndicatorElement} vega-loading-indicator component template
   */
  createLoadingIndicator(indicatorOptions = {}) {
    const loadingIndicator = document.createElement('vega-loading-indicator');
    Object.assign(loadingIndicator, indicatorOptions);
    return loadingIndicator;
  }
  /**
   * Append the loader to the container
   *
   * @param {HTMLElement} loader A loader with loading indicator
   * @param {string} containerElement The container which you want to append the loader to
   * @returns {string} The uuid of the loader.
   */
  appendLoader(loader, containerElement) {
    containerElement.dataset['vegaLoaderOriginPosition'] = containerElement.style.position;
    containerElement.style.position = 'relative';
    if (containerElement.shadowRoot) {
      containerElement.shadowRoot.append(loader);
      return `${loader.dataset['container']}>>>${loader.id}`;
    }
    else {
      containerElement.append(loader);
      return loader.id;
    }
  }
}
VegaLoaderController.SPECIAL_ELEMENT_TO_WRAP_FULL_BODY = ['VEGA-MODAL'];
__decorate([
  createPublicApiRuntimeMetricsSlimmer.InjectVegaTelemetrySlimmer()
], VegaLoaderController.prototype, "vegaPublicAPIRuntimeMetricsSlimmer", void 0);
const VegaLoader = new VegaLoaderController();

exports.VegaLoader = VegaLoader;
