import { FeatureFlag } from '../../helpers/feature-control/feature-flag-controller';
import { VegaLoader } from '../../helpers/loading-indicator/vega-loader-controller';
import { VegaNotify } from '../../helpers/notify/vega-notify-controller';
import { VegaThemeManager } from '../../helpers/theme/theme-manager';
import { setVegaLogLevel } from '../../utils/log';
import { VegaDialog } from '../../helpers/dialog/vega-dialog-controller';
import { GlobalSlimmersRegistry } from 'vega-slimmer/core';
import { VegaWatchMethodCancelSlimmer } from '../../helpers/slimmers/global/vega-watch-method-cancel-slimmer';
import VegaEventManager from '../../helpers/event-manager/vega-event-manager';
import { TypographyUrls } from '../../constants/tokens/typography';
import { VegaIconManager } from '../../helpers/icon/icon-manager';
import { default as VegaTranslationResourceEN } from '../../helpers/translation/locales/en';
import VegaZIndexManager from '../../helpers/zindex/vega-z-index-manager';
import { ValidCreditCardNumberRule } from '../../helpers/validator/rules/valid-credit-card-number-rule';
import vegaEnvManager from '../../helpers/vega-telemetry-helper/vega-env-manager';
import { waitForVega } from '../../helpers/common/wait-for-vega-handler';
import { VegaSkeletonLoader } from '../../helpers/skeleton-loader/vega-skeleton-loader-controller';
import { VegaTranslation } from '../../helpers/translation/translation';
import { VegaTranslateObserverSlimmer } from '../../helpers/slimmers/global/vega-translate-observer-slimmer';
import { VegaRTEContent } from '../../components/vega-rich-text-editor/dto/content-state';
import { setVegaNonce, vegaNonceManager } from '../../helpers/csp/vega-nonce-manager';
import { VegaNonceInjectorSlimmer } from '../../helpers/slimmers/global/vega-nonce-injector-slimmer';
import { VegaRTETokenExtension } from '../../components/vega-rich-text-editor/extensions/tokens/token-extension';
import { VegaRTELanguageExtension } from '../../components/vega-rich-text-editor/extensions/languages/language-extension';
import { VegaRTEPresetToolbarItems } from '../../components/vega-rich-text-editor/constants/constant';
import { VegaRTEFunctionExtension } from '../../components/vega-rich-text-editor/extensions/functions/function-extension';
import { VegaRTETableExtension } from '../../components/vega-rich-text-editor/extensions/table/table-extension';
/**
 * Default export function that will be used by stencil global script,
 * which will be invoked before library loaded.
 */
export default function () {
  disableFeatureFlagFromClientGlobal();
  enableFeatureFlagFromClientGlobal();
  injectVegaLoaderIntoGlobal();
  injectVegaNotifyIntoGlobal();
  injectVegaThemeManagerIntoGlobal();
  injectVegaLogLevelIntoGlobal();
  injectVegaDialogIntoGlobal();
  registerGlobalVegaSlimmers();
  injectSetEventSemanticsIntoGlobal();
  injectVegaTypography();
  injectVegaIconManager();
  injectVegaZIndexManagerIntoGlobal();
  injectValidCreditCardNumberRuleIntoGlobal();
  initRuntimeMetricsTelemetryCollection();
  injectWaitForVegaIntoGlobal();
  injectVegaSkeletonLoaderIntoGlobal();
  injectVegaTranslationIntoGlobal();
  injectVegaRichTextEditorClassIntoGlobal();
  injectVegaPublicValidationRules();
  applyBrandingFromClientGlobal();
  injectSetNonceIntoGlobal();
  patchHeadInsertBeforeWithNonce();
}
/**
 * Disable feature flags from the client global defined lists.
 */
function disableFeatureFlagFromClientGlobal() {
  if (globalThis['VegaDisabledFeatureKeys']) {
    FeatureFlag.disable(...globalThis['VegaDisabledFeatureKeys']);
  }
}
/**
 * This function enables feature flags based on a global variable in TypeScript.
 */
function enableFeatureFlagFromClientGlobal() {
  if (globalThis['VegaEnabledFeatureKeys']) {
    FeatureFlag.enable(...globalThis['VegaEnabledFeatureKeys']);
  }
}
/**
 * Inject VegaLoader to global as a public api
 */
function injectVegaLoaderIntoGlobal() {
  globalThis['VegaLoader'] = VegaLoader;
}
/**
 * Inject VegaNotify to global as a public api
 */
function injectVegaNotifyIntoGlobal() {
  globalThis.VegaNotify = VegaNotify;
}
/**
 * Inject VegaThemeManager to global as a public api
 */
function injectVegaThemeManagerIntoGlobal() {
  globalThis.VegaThemeManager = VegaThemeManager;
}
/**
 * Inject VegaLogLevel to global as a public api
 */
function injectVegaLogLevelIntoGlobal() {
  globalThis.setVegaLogLevel = setVegaLogLevel;
}
/**
 * Inject VegaDialog to global as a public api
 */
function injectVegaDialogIntoGlobal() {
  globalThis.VegaDialog = VegaDialog;
}
/**
 * register global vega slimmers
 */
function registerGlobalVegaSlimmers() {
  GlobalSlimmersRegistry.register([
    VegaWatchMethodCancelSlimmer,
    VegaTranslateObserverSlimmer,
    VegaNonceInjectorSlimmer,
  ]);
}
/**
 * Inject setEventSemantics to global as a public api
 */
function injectSetEventSemanticsIntoGlobal() {
  globalThis.VegaEventManager = VegaEventManager;
}
/**
 * Inject Vega typography stylesheet
 */
function injectVegaTypography() {
  Object.keys(TypographyUrls).forEach((key) => {
    const { url } = TypographyUrls[key];
    const typographyLinkId = `vega-typography-${key}`;
    // eslint-disable-next-line no-restricted-globals
    if (!document.querySelector(`#${typographyLinkId}`)) {
      // eslint-disable-next-line no-restricted-globals
      const typographyLink = document.createElement('link');
      typographyLink.id = typographyLinkId;
      typographyLink.href = url;
      typographyLink.rel = 'stylesheet';
      // eslint-disable-next-line no-restricted-globals
      document.head.append(typographyLink);
    }
  });
}
/**
 * Inject VegaIconManager to global as a public api
 */
function injectVegaIconManager() {
  globalThis.VegaIconManager = VegaIconManager;
}
/**
 * Inject VegaZIndexManager to global as a public api
 */
function injectVegaZIndexManagerIntoGlobal() {
  globalThis.VegaZIndexManager = VegaZIndexManager;
}
/**
 * Inject ValidCreditCardNumberRule to global as a public api.
 */
function injectValidCreditCardNumberRuleIntoGlobal() {
  globalThis.ValidCreditCardNumberRule = ValidCreditCardNumberRule;
}
/**
 * init telemetry runtime metrics.
 * 1. Inject VegaEnvInfo to global
 * 2. Register runtime metrics add page hide listener method
 */
function initRuntimeMetricsTelemetryCollection() {
  globalThis.VegaEnvInfo = vegaEnvManager.getVegaEnv();
  // eslint-disable-next-line spellcheck/spell-checker
  void import('@heartlandone/vega-sandbox-pr-2966-09f901157cfc9e276b2b0dc4870e9a03e9a0f2b7-telemetry-runtime-metrics/index').then((
  // eslint-disable-next-line spellcheck/spell-checker
  module) => {
    module['default']['init'](FeatureFlag);
  });
}
/**
 * Inject waitForVega to global as a public api
 */
function injectWaitForVegaIntoGlobal() {
  globalThis['waitForVega'] = waitForVega;
}
/**
 * Inject VegaSkeletonLoader to global as a public api
 */
function injectVegaSkeletonLoaderIntoGlobal() {
  globalThis.VegaSkeletonLoader = VegaSkeletonLoader;
}
/**
 * Inject VegaTranslation to global as a public api
 */
function injectVegaTranslationIntoGlobal() {
  globalThis.VegaTranslation = VegaTranslation;
  globalThis.VegaTranslationResourceEN = VegaTranslationResourceEN;
}
/**
 * Inject VegaRTEContent to global as a public api
 * Inject VegaRTETokenExtension to global as a public api
 * Inject VegaRTELanguageExtension to global as a public api
 * Inject VegaRTEPresetToolbarItems to global as a variable
 */
function injectVegaRichTextEditorClassIntoGlobal() {
  globalThis.VegaRTEContent = VegaRTEContent;
  globalThis.VegaRTETokenExtension = VegaRTETokenExtension;
  globalThis.VegaRTELanguageExtension = VegaRTELanguageExtension;
  globalThis.VegaRTEPresetToolbarItems = VegaRTEPresetToolbarItems;
  globalThis.VegaRTEFunctionExtension = VegaRTEFunctionExtension;
  globalThis.VegaRTETableExtension = VegaRTETableExtension;
}
/**
 * Inject VegaPublicValidationRules to global as a public api
 */
function injectVegaPublicValidationRules() {
  void import('../../helpers/validator/public-rules').then((module) => {
    Object.entries(module).forEach(([name, value]) => {
      globalThis[name] = value;
    });
  });
}
/**
 * Switch the Vega theme to a new brand theme from the client global defined options.
 */
function applyBrandingFromClientGlobal() {
  if (globalThis['VegaThemeManagerApplyBranding']) {
    VegaThemeManager.applyBranding(globalThis['VegaThemeManagerApplyBranding']);
  }
}
/**
 * Injects the `setVegaNonce` function into the global scope as `setVegaNonce`.
 */
function injectSetNonceIntoGlobal() {
  globalThis.setVegaNonce = setVegaNonce;
}
/**
 * Patch document.head.insertBefore to ensure nonce is set on injected elements.
 */
function patchHeadInsertBeforeWithNonce() {
  vegaNonceManager.patchHeadInsertBeforeWithNonceToStencil();
}
