'use strict';

const globalSlimmerRegistry = require('./global-slimmer-registry-b3bce7e0.js');
const vegaNonceManager = require('./vega-nonce-manager-df69d3ed.js');
const index = require('./index-592b4052.js');

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;
};
/**
 * Component global style slimmer
 */
class ComponentGlobalStyleSlimmer extends globalSlimmerRegistry.VegaSlimmer {
  /**
   * The constructor of the slimmer
   *
   * @param {string} styles - The style string that will be appended to the document head.
   */
  constructor(styles) {
    super();
    this.style = styles;
  }
  /**
   * Component lifecycle - [connectedCallback]
   */
  connectedCallback() {
    const componentName = this.host.tagName;
    const connectedComponents = ComponentGlobalStyleSlimmer.connectedComponents.get(componentName);
    if (connectedComponents) {
      connectedComponents.add(this.host);
    }
    else {
      ComponentGlobalStyleSlimmer.connectedComponents.set(componentName, new Set([this.host]));
    }
  }
  /**
   * Component lifecycle - [disconnectedCallback]
   */
  disconnectedCallback() {
    const componentName = this.host.tagName;
    const connectedComponents = ComponentGlobalStyleSlimmer.connectedComponents.get(componentName);
    if (connectedComponents) {
      connectedComponents.delete(this.host);
      if (connectedComponents.size === 0) {
        const styleElement = this.getStyleElement();
        if (styleElement) {
          styleElement.remove();
        }
      }
    }
  }
  /**
   * Component lifecycle - [componentWillLoad]
   */
  componentWillLoad() {
    this.id = `${this.host.tagName}-GLOBAL-STYLE`;
    let styleElement = this.getStyleElement();
    if (!styleElement) {
      // eslint-disable-next-line no-restricted-globals
      styleElement = document.createElement('style');
      styleElement.id = this.id;
      styleElement.innerHTML = index.htmlSanitizer.sanitize(this.style, index.styleSanitizeStrategy);
      if (vegaNonceManager.vegaNonceManager.nonce) {
        styleElement.setAttribute('nonce', vegaNonceManager.vegaNonceManager.nonce);
      }
      // eslint-disable-next-line no-restricted-globals
      document.head.append(styleElement);
    }
  }
  /**
   * @returns {Nullable<HTMLStyleElement>} - The style element
   */
  getStyleElement() {
    // eslint-disable-next-line no-restricted-globals
    return document.head.querySelector(`style#${this.id}`);
  }
}
ComponentGlobalStyleSlimmer.connectedComponents = new Map();
__decorate([
  globalSlimmerRegistry.MapToComponentField()
], ComponentGlobalStyleSlimmer.prototype, "host", void 0);
__decorate([
  globalSlimmerRegistry.MapToComponentMethod('connectedCallback')
], ComponentGlobalStyleSlimmer.prototype, "connectedCallback", null);
__decorate([
  globalSlimmerRegistry.MapToComponentMethod('disconnectedCallback')
], ComponentGlobalStyleSlimmer.prototype, "disconnectedCallback", null);
__decorate([
  globalSlimmerRegistry.MapToComponentMethod('componentWillLoad')
], ComponentGlobalStyleSlimmer.prototype, "componentWillLoad", null);

exports.ComponentGlobalStyleSlimmer = ComponentGlobalStyleSlimmer;
