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 { MapToComponentField, MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { vegaNonceManager } from '../csp/vega-nonce-manager';
import { htmlSanitizer, styleSanitizeStrategy } from '../../utils/sanitize';
/**
 * Component global style slimmer
 */
export class ComponentGlobalStyleSlimmer extends 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 = htmlSanitizer.sanitize(this.style, styleSanitizeStrategy);
      if (vegaNonceManager.nonce) {
        styleElement.setAttribute('nonce', 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([
  MapToComponentField()
], ComponentGlobalStyleSlimmer.prototype, "host", void 0);
__decorate([
  MapToComponentMethod('connectedCallback')
], ComponentGlobalStyleSlimmer.prototype, "connectedCallback", null);
__decorate([
  MapToComponentMethod('disconnectedCallback')
], ComponentGlobalStyleSlimmer.prototype, "disconnectedCallback", null);
__decorate([
  MapToComponentMethod('componentWillLoad')
], ComponentGlobalStyleSlimmer.prototype, "componentWillLoad", null);
