{"version":3,"file":"index.mjs","sources":["../../../../src/internal/openFeature/index.ts"],"sourcesContent":["import { LocalStorageProvider } from '@openfeature/localstorage-provider';\nimport { OFREPWebProvider } from '@openfeature/ofrep-web-provider';\nimport { OpenFeature, ProviderEvents, NOOP_PROVIDER, type EventDetails, MultiProvider } from '@openfeature/react-sdk';\n\nimport { config } from '../../config';\nimport { logError } from '../../utils/logging';\n\n// Ensure the module augmentation is pulled in\nimport './openfeature-types.gen.d.ts';\n\nfunction checkDefaultProvider(event?: EventDetails) {\n  if (event?.domain) {\n    return;\n  }\n\n  // Warn plugin developers if we've detected OpenFeature's default provider has been changed,\n  //  as plugins should always be using a domain when setting a provider to avoid conflicts.\n  if (OpenFeature.getProvider() !== NOOP_PROVIDER) {\n    const err = new Error(\n      'OpenFeature default domain provider has been unexpectedly changed. This may be caused by a plugin that is incorrectly using the default domain.',\n      { cause: OpenFeature.getProvider() }\n    );\n    console.error(err);\n    logError(err);\n  }\n}\n\n// The domain creates the unique instance of the OpenFeature client for Grafana core,\n// with its own evaluation context and provider.\n// Plugins should not use this client or domain, and instead create their own client\n// with a different domain to avoid conflicts.\n//\n// If changing this, you MUST also update the same constant in packages/grafana-test-utils/src/utilities/featureFlags.ts\n// to ensure tests work correctly.\nconst GRAFANA_CORE_OPEN_FEATURE_DOMAIN = 'internal-grafana-core';\nconst GRAFANA_OPEN_FEATURE_LOCALSTORAGE_PREFIX = 'grafana.openfeature.';\n\n// Allow direct access to a singleton localStorage provider,\n//  to allow the feature control developer UI to override flags via the provider\nlet localStorageProvider: LocalStorageProvider;\nexport function getLocalStorageProvider() {\n  return (localStorageProvider ??= new LocalStorageProvider({\n    prefix: GRAFANA_OPEN_FEATURE_LOCALSTORAGE_PREFIX,\n  }));\n}\n\n// Allow direct access to a singleton OFREP provider,\n//  to allow the feature control developer UI to discover flags from the provider\nlet ofrepWebProvider: OFREPWebProvider;\nexport function getOFREPWebProvider() {\n  const baseUrl = window.__grafanaOFREPRootUrlEnabled\n    ? `${config.appSubUrl || ''}`\n    : `${config.appSubUrl || ''}/apis/features.grafana.app/v0alpha1/namespaces/${config.namespace}`;\n\n  return (ofrepWebProvider ??= new OFREPWebProvider({\n    baseUrl,\n    disableVisibilityRefresh: true, // Do not refresh\n    cacheMode: 'disabled', // Do not write to localStorage\n    timeoutMs: 5_000,\n  }));\n}\n\nexport async function initOpenFeature() {\n  OpenFeature.addHandler(ProviderEvents.Ready, checkDefaultProvider);\n  OpenFeature.addHandler(ProviderEvents.Error, checkDefaultProvider);\n\n  const lsProvider = getLocalStorageProvider();\n  const ofProvider = getOFREPWebProvider();\n\n  await OpenFeature.setProviderAndWait(\n    GRAFANA_CORE_OPEN_FEATURE_DOMAIN,\n    new MultiProvider([{ provider: lsProvider }, { provider: ofProvider }]),\n    {\n      targetingKey: config.namespace,\n      ...config.openFeatureContext,\n    }\n  );\n}\n\n/**\n * Get the OpenFeature client for Grafana core.\n * Prefer to instead use the React hooks for evaluating feature flags instead as they remain up to date with the latest flag values.\n * If you must use this client directly, do not store the evaluation result for later - always call `getFeatureFlagClient().getFooValue()` just\n * in time when you use it to ensure you get the latest value.\n */\nexport function getFeatureFlagClient() {\n  return OpenFeature.getClient(GRAFANA_CORE_OPEN_FEATURE_DOMAIN);\n}\n"],"names":[],"mappings":";;;;;;;;AAUA,SAAS,qBAAqB,KAAA,EAAsB;AAClD,EAAA,IAAI,+BAAO,MAAA,EAAQ;AACjB,IAAA;AAAA,EACF;AAIA,EAAA,IAAI,WAAA,CAAY,WAAA,EAAY,KAAM,aAAA,EAAe;AAC/C,IAAA,MAAM,MAAM,IAAI,KAAA;AAAA,MACd,iJAAA;AAAA,MACA,EAAE,KAAA,EAAO,WAAA,CAAY,WAAA,EAAY;AAAE,KACrC;AACA,IAAA,OAAA,CAAQ,MAAM,GAAG,CAAA;AACjB,IAAA,QAAA,CAAS,GAAG,CAAA;AAAA,EACd;AACF;AASA,MAAM,gCAAA,GAAmC,uBAAA;AACzC,MAAM,wCAAA,GAA2C,s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