import { vegaNonceManager } from '../../csp/vega-nonce-manager';
/**
 * StyleController Abstract class control the theme style controller
 */
export class StyleControllerAbstract {
  /* eslint-disable no-restricted-globals */
  /**
   * It creates a style tag in the head of the document if it doesn't exist, and returns it.
   *
   * @param {string} themeStyleContainerKey - custom dark mode color
   * @returns {Element} The style element is being returned.
   */
  getStyleContainer(themeStyleContainerKey) {
    let vegaThemeStyleContainer = document.head.querySelector(`#${themeStyleContainerKey}`);
    if (!vegaThemeStyleContainer) {
      vegaThemeStyleContainer = document.createElement('style');
      vegaThemeStyleContainer.id = themeStyleContainerKey;
      if (vegaNonceManager.nonce) {
        vegaThemeStyleContainer.setAttribute('nonce', vegaNonceManager.nonce);
      }
      document.head.append(vegaThemeStyleContainer);
    }
    return vegaThemeStyleContainer;
  }
  /* eslint-enable no-restricted-globals */
  /**
   * Remove the style element.
   *
   * @param {string} themeStyleContainerKey - theme style container key.
   */
  reset(themeStyleContainerKey) {
    // eslint-disable-next-line no-restricted-globals
    const vegaThemeStyleContainer = document.head.querySelector(`#${themeStyleContainerKey}`);
    if (vegaThemeStyleContainer) {
      vegaThemeStyleContainer.remove();
    }
  }
}
