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 { MapToComponentMethod, VegaSlimmer } from 'vega-slimmer/core';
import { vegaNonceManager } from '../../csp/vega-nonce-manager';
/**
 * Class responsible for injecting nonce into inline style attributes for vega components.
 */
export class VegaNonceInjectorSlimmer extends VegaSlimmer {
  constructor(option) {
    super();
    this.component = option.component;
  }
  /**
   * Component lifecycle - [connectedCallback]
   * Registers an observer to watch for translation updates, which triggers a forced update on the component.
   */
  /**
   * Lifecycle callback that is invoked when the custom element is added to the DOM.
   *
   * This implementation overrides the `insertBefore` method of the element's shadow root
   * to automatically set the `nonce` attribute on any inline `<style>` elements that are inserted.
   * This is typically used to support Content Security Policy (CSP) by ensuring that dynamically
   * injected styles have the correct nonce value.
   */
  connectedCallback() {
    const shadowRoot = this.component['host'].shadowRoot;
    // override element#insertBefore to set nonce attribute for inline style element
    const originalInsertBefore = shadowRoot.insertBefore;
    /**
     * Overrides the shadowRoot's insertBefore method to inject a CSP nonce into any STYLE element being inserted.
     *
     * @template T - The type of Node being inserted.
     * @param {T} newElement - The new node to insert.
     * @param {Node | null} referenceElement - The node before which newElement is inserted, or null to insert at the end.
     * @returns {T} The inserted node.
     */
    shadowRoot.insertBefore = function (newElement, referenceElement) {
      if (newElement.nodeName === 'STYLE') {
        const nonce = vegaNonceManager.nonce;
        if (nonce !== undefined) {
          newElement.setAttribute('nonce', nonce);
        }
      }
      return originalInsertBefore.call(this, newElement, referenceElement);
    };
  }
}
__decorate([
  MapToComponentMethod('connectedCallback')
], VegaNonceInjectorSlimmer.prototype, "connectedCallback", null);
