{"version":3,"file":"index.mjs","sources":["../../../../src/internal/openFeature/index.ts"],"sourcesContent":["import { OFREPWebProvider } from '@openfeature/ofrep-web-provider';\nimport { OpenFeature, ProviderEvents, NOOP_PROVIDER, type EventDetails } from '@openfeature/react-sdk';\n\nimport { type FeatureToggles } from '@grafana/data';\n\nimport { config } from '../../config';\nimport { logError } from '../../utils/logging';\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\nexport type FeatureFlagName = keyof FeatureToggles;\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.\nexport const GRAFANA_CORE_OPEN_FEATURE_DOMAIN = 'internal-grafana-core';\n\nexport async function initOpenFeature() {\n  OpenFeature.addHandler(ProviderEvents.Ready, checkDefaultProvider);\n  OpenFeature.addHandler(ProviderEvents.Error, checkDefaultProvider);\n\n  const subPath = config.appSubUrl || '';\n  const baseUrl = `${subPath}/apis/features.grafana.app/v0alpha1/namespaces/${config.namespace}`;\n\n  const ofProvider = new OFREPWebProvider({\n    baseUrl: baseUrl,\n    pollInterval: -1, // disable polling\n    timeoutMs: 5_000,\n  });\n\n  await OpenFeature.setProviderAndWait(GRAFANA_CORE_OPEN_FEATURE_DOMAIN, ofProvider, {\n    targetingKey: config.namespace,\n    ...config.openFeatureContext,\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":";;;;;;AAQA,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;AAWO,MAAM,gCAAA,GAAmC;AAEhD,eAAsB,eAAA,GAAkB;AACtC,EAAA,WAAA,CAAY,UAAA,CAAW,cAAA,CAAe,KAAA,EAAO,oBAAoB,CAAA;AACjE,EAAA,WAAA,CAAY,UAAA,CAAW,cAAA,CAAe,KAAA,EAAO,oBAAoB,CAAA;AAEjE,EAAA,MAAM,OAAA,GAAU,OAAO,SAAA,IAAa,EAAA;AACpC,EAAA,MAAM,OAAA,GAAU,CAAA,EAAG,OAAO,CAAA,+CAAA,EAAkD,OAAO,SAAS,CAAA,CAAA;AAE5F,EAAA,MAAM,UAAA,GAAa,IAAI,gBAAA,CAAiB;AAAA,IACtC,OAAA;AAAA,IACA,YAAA,EAAc,CAAA,CAAA;AAAA;AAAA,IACd,SAAA,EAAW;AAAA,GACZ,CAAA;AAED,EAAA,MAAM,WAAA,CAAY,kBAAA,CAAmB,gCAAA,EAAkC,UAAA,EAAY;AAAA,IACjF,cAAc,MAAA,CAAO,SAAA;AAAA,IACrB,GAAG,MAAA,CAAO;AAAA,GACX,CAAA;AACH;AAQO,SAAS,oBAAA,GAAuB;AACrC,EAAA,OAAO,WAAA,CAAY,UAAU,gCAAgC,CAAA;AAC/D;;;;"}