const GLOBAL_KEY = /* @__PURE__ */ Symbol.for("@open-mercato/shared.telemetryRuntime");
const ENABLED_BACKENDS = /* @__PURE__ */ new Set(["console", "signoz", "newrelic", "otlp"]);
function store() {
  const globalStore = globalThis;
  let current = globalStore[GLOBAL_KEY];
  if (!current) {
    current = {};
    globalStore[GLOBAL_KEY] = current;
  }
  return current;
}
function isTelemetryBackendEnabled(raw) {
  const value = raw ?? (typeof process === "undefined" ? void 0 : process.env.TELEMETRY_BACKEND);
  return ENABLED_BACKENDS.has((value ?? "").trim().toLowerCase());
}
function registerTelemetryRuntime(runtime) {
  store().active = runtime;
  return () => {
    const current = store();
    if (current.active === runtime) current.active = void 0;
  };
}
function getTelemetryRuntime() {
  return store().active;
}
function resetTelemetryRuntime() {
  store().active = void 0;
}
const NOOP_SPAN = { setAttributes() {
} };
function withTelemetrySpan(name, fn, options) {
  const runtime = getTelemetryRuntime();
  if (!runtime?.withSpan) return fn(NOOP_SPAN);
  return runtime.withSpan(name, fn, options);
}
function captureTelemetryTrace() {
  const carrier = getTelemetryRuntime()?.captureTraceContext();
  return carrier && Object.keys(carrier).length > 0 ? carrier : void 0;
}
export {
  captureTelemetryTrace,
  getTelemetryRuntime,
  isTelemetryBackendEnabled,
  registerTelemetryRuntime,
  resetTelemetryRuntime,
  withTelemetrySpan
};
//# sourceMappingURL=runtime.js.map
