import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);

// ../../packages/core/dist/chunk-HFSYZHGF.js
import { mkdir } from "node:fs/promises";
import { dirname } from "node:path";
var OtlpJsonFileExporter = class {
  // biome-ignore lint/suspicious/noExplicitAny: serialized span data
  spans = [];
  filePath;
  constructor(filePath) {
    this.filePath = filePath;
  }
  export(spans, resultCallback) {
    for (const span of spans) {
      this.spans.push({
        traceId: span.spanContext().traceId,
        spanId: span.spanContext().spanId,
        parentSpanId: span.parentSpanId || void 0,
        name: span.name,
        kind: span.kind,
        startTimeUnixNano: hrTimeToNanos(span.startTime),
        endTimeUnixNano: hrTimeToNanos(span.endTime),
        attributes: convertAttributes(span.attributes),
        status: span.status,
        events: span.events?.map(
          (e) => ({
            name: e.name,
            timeUnixNano: hrTimeToNanos(e.time),
            attributes: convertAttributes(e.attributes)
          })
        )
      });
    }
    resultCallback({ code: 0 });
  }
  async shutdown() {
    await this.flush();
  }
  async forceFlush() {
    await this.flush();
  }
  async flush() {
    if (this.spans.length === 0) return;
    await mkdir(dirname(this.filePath), { recursive: true });
    const otlpJson = {
      resourceSpans: [
        {
          resource: { attributes: [] },
          scopeSpans: [
            {
              scope: { name: "agentv", version: "1.0.0" },
              spans: this.spans
            }
          ]
        }
      ]
    };
    const { writeFile } = await import("node:fs/promises");
    await writeFile(this.filePath, JSON.stringify(otlpJson, null, 2));
  }
};
function hrTimeToNanos(hrTime) {
  return String(hrTime[0] * 1e9 + hrTime[1]);
}
function convertAttributes(attrs) {
  return Object.entries(attrs || {}).map(([key, value]) => ({
    key,
    value: serializeAttributeValue(value)
  }));
}
function serializeAttributeValue(value) {
  if (typeof value === "string") return { stringValue: value };
  if (typeof value === "number") {
    return Number.isInteger(value) ? { intValue: value } : { doubleValue: value };
  }
  if (typeof value === "boolean") return { boolValue: value };
  if (Array.isArray(value)) return { arrayValue: { values: value.map(serializeAttributeValue) } };
  return { stringValue: String(value) };
}

export {
  OtlpJsonFileExporter
};
//# sourceMappingURL=chunk-C5GOHBQM.js.map