import { tinycolor } from '../../../polyfill/tinycolor/tinycolor-polyfill';
import { OverridableColorTokens } from '../../../constants/tokens/overridable-color-tokens';
import { OverridableBorderTokens } from '../../../constants/tokens/overridable-border-tokens';
import { OverridableRingTokens } from '../../../constants/tokens/overridable-ring-tokens';
import { vegaNonceManager } from '../../csp/vega-nonce-manager';
import { htmlSanitizer, styleSanitizeStrategy } from '../../../utils/sanitize';
/**
 * Vega components color override controller
 */
class ColorSchemaOverridingController {
  /**
   * It takes a color and generates a bunch of other colors based on that color
   *
   * @param {VegaThemeOverrideColors} overrideColors - OverrideColors - This is the object that is passed in from the user.
   * @returns {OverrideColor[]} Parsed action colors
   */
  generateOverrideThemes(overrideColors) {
    return this.getOverridableTokens().map((item) => {
      return {
        name: item.name,
        value: this.parseCssVarValue(item, overrideColors),
      };
    });
  }
  /**
   * Set the custom override theme colors to customOverrideThemeStyleContainer
   *
   * @param {OverrideColor[]} overrideThemes Parsed custom override colors
   */
  applyTheme(overrideThemes) {
    const styleString = `
		:root {
		\t\t${overrideThemes
      .filter((item) => {
      return !!item.value;
    })
      .map((item) => `--v-${item.name}: ${item.value};`)
      .join('\n\t\t')}
		}
						`;
    this.getOverrideThemeContainer().innerHTML = htmlSanitizer.sanitize(styleString, styleSanitizeStrategy);
  }
  /* eslint-disable no-restricted-globals */
  /**
   * It creates a customOverrideTheme style tag in the head of the document if it doesn't exist, and returns it
   *
   * @returns {Element} The customOverrideThemeStyleContainer is being returned.
   */
  getOverrideThemeContainer() {
    let customOverrideThemeStyleContainer = document.head.querySelector(`#vegaCustomOverrideTheme`);
    if (!customOverrideThemeStyleContainer) {
      customOverrideThemeStyleContainer = document.createElement('style');
      customOverrideThemeStyleContainer.id = 'vegaCustomOverrideTheme';
      if (vegaNonceManager.nonce) {
        customOverrideThemeStyleContainer.setAttribute('nonce', vegaNonceManager.nonce);
      }
      document.head.append(customOverrideThemeStyleContainer);
    }
    return customOverrideThemeStyleContainer;
  }
  /* eslint-enable no-restricted-globals */
  /**
   * Get the overridable color tokens from design token
   *
   * @returns {OverridableToken[]} overridable action color token array
   */
  getOverridableTokens() {
    return [...OverridableColorTokens, ...OverridableBorderTokens, ...OverridableRingTokens];
  }
  /**
   * Calculate and parse color to css var value
   *
   * @param {OverridableToken} overrideColorToken The token will be overridden
   * @param {VegaThemeOverrideColors} overrideColors the payload of override colors
   * @returns {Nullable<string>} The css var value
   */
  parseCssVarValue(overrideColorToken, overrideColors) {
    var _a;
    return (_a = overrideColorToken.overrideGetter) === null || _a === void 0 ? void 0 : _a.call(overrideColorToken, overrideColors, tinycolor);
  }
}
export const VegaColorSchemaOverridingController = new ColorSchemaOverridingController();
