import { VegaColorSchemaOverridingController } from './controllers/color-schema-overriding-controller';
import './controllers/dark-mode-style-controller';
import ChangeManager from '../change-manager/change-manager';
import { DARK_MODE_CHANGE, GP_BRAND_CHANGE, } from '../change-manager/subject/static-subject/static-subject-title';
import './controllers/brand-style-controller';
import { emptySanitizeStrategy, htmlSanitizer } from '../../utils/sanitize';
/**
 * Vega components controller to control the internal theme manager
 */
class InternalThemeManager {
  constructor() {
    this.currentBranding = 'heartland';
  }
  /**
   * It takes the colors you passed in, parses them into the correct format, and then applies them to the theme.
   *
   * When `overrideColors.overrideTokenMap` is provided (typically the `contrastCapPatches`
   * returned by `validateColorAccessibility()`), matching tokens in the generated theme are replaced
   * with the patched values before applying.
   *
   * @param {VegaThemeOverrideColors} overrideColors - OverrideColors - This is the object that you pass to the function.
   */
  overrideColors(overrideColors) {
    const overrideThemes = VegaColorSchemaOverridingController.generateOverrideThemes(overrideColors);
    const finalThemes = overrideColors.overrideTokenMap
      ? this.applyTokenMapPatches(overrideThemes, overrideColors.overrideTokenMap)
      : overrideThemes;
    VegaColorSchemaOverridingController.applyTheme(finalThemes);
  }
  /**
   * It removes all the custom override theme styles from the DOM
   */
  reset() {
    VegaColorSchemaOverridingController.getOverrideThemeContainer().innerHTML = htmlSanitizer.sanitize('', emptySanitizeStrategy);
  }
  /* eslint-disable no-restricted-globals */
  /**
   * This function toggles the dark mode of a webpage by adding or removing a 'dark' class to the HTML element.
   *
   * @param {boolean | undefined} darkMode - A boolean or undefined value that determines whether to enable or disable dark mode.
   * If it is true, dark mode is enabled.
   * If it is false, dark mode is disabled.
   */
  toggleDarkMode(darkMode) {
    if (darkMode) {
      document.documentElement.classList.add('dark');
    }
    else {
      document.documentElement.classList.remove('dark');
    }
    ChangeManager.notify(DARK_MODE_CHANGE, darkMode);
  }
  /* eslint-enable no-restricted-globals */
  /**
   * The function checks if the HTML element has a class of "dark" and returns a boolean value accordingly.
   *
   * @returns {boolean} A boolean value indicating whether the HTML element has a class of "dark", which is
   * typically used to indicate that the website is in dark mode.
   */
  isDarkMode() {
    // eslint-disable-next-line no-restricted-globals
    return document.documentElement.classList.contains('dark');
  }
  /**
   * This function return the new brand name.
   *
   * @returns {BrandOptions} return the new brand options.
   */
  getBranding() {
    return this.currentBranding;
  }
  /**
   * This function switches the Vega theme to a brand theme based on the provided options.
   *
   * @param {BrandOptions} options - The options includes 'gp', 'genius' and 'heartland' options
   */
  applyBranding(options) {
    this.currentBranding = options;
    ChangeManager.notify(GP_BRAND_CHANGE, options);
  }
  /**
   * Validate a full `VegaThemeOverrideColors` payload against WCAG 2.1 AA thresholds
   * without applying any theme changes.
   *
   * Reports each failing token (those fixable by choosing a different `actionColor`) with
   * its actual and required contrast ratios. Also returns `contrastCapPatches` — a map of
   * token-level repairs for tokens whose formula hard-caps contrast below the threshold.
   * Pass the patches back via `overrideColors({ ..., overrideTokenMap: result.contrastCapPatches })`
   * to apply them at override time.
   *
   * @param {VegaThemeOverrideColors} overrideColors The same input accepted by `overrideColors()`
   * @returns {Promise<ActionColorAccessibilityResult>} Per-token contrast report with patches
   */
  async validateColorAccessibility(overrideColors) {
    const validatorModule = await import('./controllers/action-color-accessibility-validator');
    return validatorModule.vegaAccessibilityValidator.validate(overrideColors);
  }
  /**
   * Replace tokens in the generated theme array with values from the provided patch map.
   *
   * @param {OverrideColor[]} themes - Generated override theme entries.
   * @param {Record<string, string>} patchMap - Token name → patched CSS value map.
   * @returns {OverrideColor[]} Theme array with matching tokens patched.
   */
  applyTokenMapPatches(themes, patchMap) {
    if (Object.keys(patchMap).length === 0) {
      return themes;
    }
    return themes.map((theme) => patchMap[theme.name] !== undefined ? { name: theme.name, value: patchMap[theme.name] } : theme);
  }
}
export const VegaInternalThemeManager = new InternalThemeManager();
