import './global-slimmer-registry-0a7aa308.js';
import { I as InjectVegaTelemetrySlimmer, c as createPublicAPIRuntimeMetricsSlimmer } from './create-public-api-runtime-metrics-slimmer-407de188.js';
import { g as generateUUID } from './misc-9fdbcfbb.js';
import './dynamic-slimmer-60d5da5a.js';

const VegaSkeletonRuntimeMetricsPayloadDefinition = [
  {
    payloadKey: 'IS_LOAD_METHOD_CALLED',
    payloadDescription: 'Indicate if the method load of public API VegaSkeletonLoaderController 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 === 'VegaSkeletonLoaderController' && methodName === 'load',
    /**
     * The function checks if the method name is load.
     *
     * @returns {boolean} A boolean value
     */
    payloadValue: () => {
      return true;
    },
  },
  {
    payloadKey: 'IS_UNLOAD_METHOD_CALLED',
    payloadDescription: 'Indicate if the method unLoad of public API VegaSkeletonLoaderController 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 === 'VegaSkeletonLoaderController' && methodName === 'unLoad',
    /**
     * The function checks if the method name is unLoad.
     *
     * @returns {boolean} A boolean value
     */
    payloadValue: () => {
      return true;
    },
  },
  {
    payloadKey: 'IS_UNLOADALL_METHOD_CALLED',
    payloadDescription: 'Indicate if the method unLoadAll of public API VegaSkeletonLoaderController 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 === 'VegaSkeletonLoaderController' && methodName === 'unLoadAll',
    /**
     * The function checks if the method name is unLoadAll.
     *
     * @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 Skeleton Loader Controller to show skeletons in the target elements
 */
class VegaSkeletonLoaderController {
  constructor() {
    this.vegaPublicAPIRuntimeMetricsSlimmer = createPublicAPIRuntimeMetricsSlimmer(VegaSkeletonLoaderController, ['load', 'unLoad', 'unLoadAll'], VegaSkeletonRuntimeMetricsPayloadDefinition);
    this.loaderMap = new Map();
  }
  /**
   * Load the skeletons in the target elements
   *
   * @param { VegaSkeletonLoaderConfig } config - Configuration for the loading function
   * @param {string} config.target - Target elements to show the skeletons.
   * @param {VegaSkeletonLoaderConfig.type} [config.type = 'text'] - Type of the skeleton.
   * @param {VegaSkeletonLoaderConfig.width} config.width - Width of the skeleton.
   * @param {VegaSkeletonLoaderConfig.height} config.height - Height of the skeleton.
   * @param {VegaSkeletonLoaderConfig.items} config.items - Configuration for multiple skeleton items.
   * @param {VegaSkeletonLoaderConfig.layout} config.layout - Layout of the skeleton items.
   * @returns { string } - Key of the loaded skeleton items.
   * @vegaVersion 2.16.0
   */
  load({ target, type = 'text', width, height, items, layout }) {
    // eslint-disable-next-line no-restricted-globals
    const targets = Array.from(document.body.querySelectorAll(target));
    const key = `vega-skeleton-loader-${generateUUID()}`;
    const loaderItems = [];
    targets.forEach((target) => {
      const skeletonsLoaders = !items
        ? [this.createSkeleton({ type, width, height })]
        : items.map(this.createSkeleton);
      const itemsWithKey = (layout ? [this.wrapLayout(skeletonsLoaders, layout)] : skeletonsLoaders).map((item) => {
        item.dataset[VegaSkeletonLoaderController.LOADER_DATA_SET_KEY] = key;
        return item;
      });
      loaderItems.push(...itemsWithKey);
      target.append(...itemsWithKey);
    });
    this.loaderMap.set(key, loaderItems);
    return key;
  }
  /**
   * Remove the skeletons from the dom.
   *
   * @param {string} key - The key returned from {@link VegaSkeletonLoaderController.load}.
   * @vegaVersion 2.16.0
   */
  unLoad(key) {
    if (this.loaderMap.has(key)) {
      this.loaderMap.get(key).forEach((item) => item.remove());
      this.loaderMap.delete(key);
    }
  }
  /**
   * Remove all the skeletons from the dom.
   *
   * @vegaVersion 2.16.0
   */
  unLoadAll() {
    for (const items of this.loaderMap.values()) {
      items.forEach((item) => item.remove());
    }
    this.loaderMap.clear();
  }
  /**
   * Create a skeleton item. The skeleton animated always be true
   *
   * @param {VegaSkeletonLoaderItem} config - Configuration for the skeleton item.
   * @returns {HTMLVegaSkeletonElement} - Skeleton item.
   */
  createSkeleton(config) {
    const { type, width, height } = config;
    // eslint-disable-next-line no-restricted-globals
    const skeleton = document.createElement('vega-skeleton');
    Object.assign(skeleton, { animated: true, type, width, height });
    return skeleton;
  }
  /**
   * Wrap the flex layout for the skeletons.
   *
   * @param {HTMLElement[]} skeletons - Skeleton elements of the loader.
   * @param {VegaSkeletonLoaderLayout} layout - Flex properties for the layout.
   * @returns {HTMLVegaFlexElement} - Container wrapping the skeletons in the specified flex layout.
   */
  wrapLayout(skeletons, layout) {
    // eslint-disable-next-line no-restricted-globals
    const skeletonContainer = document.createElement('vega-flex');
    Object.assign(skeletonContainer, Object.assign({ className: 'vega-skeleton-loader-template-container' }, layout.props));
    Object.assign(skeletonContainer.style, layout.style);
    skeletonContainer.append(...skeletons);
    return skeletonContainer;
  }
}
VegaSkeletonLoaderController.LOADER_DATA_SET_KEY = 'vegaSkeletonLoaderKey';
__decorate([
  InjectVegaTelemetrySlimmer()
], VegaSkeletonLoaderController.prototype, "vegaPublicAPIRuntimeMetricsSlimmer", void 0);
const VegaSkeletonLoader = new VegaSkeletonLoaderController();

export { VegaSkeletonLoader };
