// ../logger/dist/chunk-57LG5ZUS.js
var FARO_LEVEL = {
  debug: "debug",
  info: "info",
  warn: "warn",
  error: "error",
  fatal: "error"
};
async function createFaroSink(opts) {
  const transport = opts.transport ?? await loadFaroTransport(opts);
  if (!transport) return null;
  return {
    name: "faro",
    log(record) {
      transport.push({ kind: "log", record });
    },
    span(record) {
      transport.push({ kind: "span", record });
    },
    async flush() {
    }
  };
}
async function loadFaroTransport(opts) {
  try {
    const sdkName = "@grafana/faro-web-sdk";
    const tracingName = "@grafana/faro-web-tracing";
    const sdk = await import(
      /* @vite-ignore */
      sdkName
    );
    const tracing = await import(
      /* @vite-ignore */
      tracingName
    );
    const faro = sdk.initializeFaro({
      url: opts.endpoint,
      app: { name: opts.app },
      instrumentations: [
        ...sdk.getWebInstrumentations(),
        new tracing.TracingInstrumentation()
      ]
    });
    return {
      push({ kind, record }) {
        if (kind === "log") {
          const r = record;
          faro.api.pushLog([r.message], {
            level: FARO_LEVEL[r.level],
            context: flatten(r.context)
          });
        } else {
          const r = record;
          faro.api.pushEvent(`span:${r.name}`, {
            durationMs: String(r.durationMs),
            status: r.status,
            ...flatten(r.context)
          });
        }
      }
    };
  } catch {
    return null;
  }
}
function flatten(ctx) {
  const out = {};
  for (const [k, v] of Object.entries(ctx)) {
    out[k] = typeof v === "string" ? v : JSON.stringify(v);
  }
  return out;
}

export { createFaroSink };
//# sourceMappingURL=chunk-XWP763SH.js.map
//# sourceMappingURL=chunk-XWP763SH.js.map