import "../chunk-PZ5AY32C.js";

// src/adapters/otel.ts
var OTEL_STATUS_UNSET = 0;
var OTEL_STATUS_OK = 1;
var OTEL_STATUS_ERROR = 2;
function hrTimeToUnixNano(hr) {
  const [seconds, nanos] = hr;
  return seconds * 1e9 + nanos;
}
function statusCodeName(code) {
  if (code === OTEL_STATUS_OK) return "OK";
  if (code === OTEL_STATUS_ERROR) return "ERROR";
  return "UNSET";
}
function cleanAttributes(attrs) {
  const out = {};
  if (!attrs) return out;
  for (const [k, v] of Object.entries(attrs)) {
    if (v === null || v === void 0) continue;
    if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") {
      out[k] = v;
      continue;
    }
    if (Array.isArray(v)) {
      out[k] = JSON.stringify(v);
    }
  }
  return out;
}
function readPivotString(attrs, key) {
  const v = attrs[key];
  return typeof v === "string" ? v : void 0;
}
function readPivotNumber(attrs, key) {
  const v = attrs[key];
  return typeof v === "number" ? v : void 0;
}
function resolveParentSpanId(span) {
  if (span.parentSpanId) return span.parentSpanId;
  const ctx = span.parentSpanContext?.();
  return ctx?.spanId;
}
function createOtelBridge(opts) {
  const batchSize = opts.batchSize ?? 200;
  const onError = opts.onError ?? ((err) => {
    console.warn("[otel-bridge] ingest batch failed:", err);
  });
  function convert(span) {
    const ctx = span.spanContext();
    const attributes = cleanAttributes(span.attributes);
    const runId = readPivotString(attributes, "tangle.runId") ?? opts.defaultRunId;
    const scenarioId = readPivotString(attributes, "tangle.scenarioId");
    const cellId = readPivotString(attributes, "tangle.cellId");
    const generation = readPivotNumber(attributes, "tangle.generation");
    if (runId && !attributes["tangle.runId"]) {
      attributes["tangle.runId"] = runId;
    }
    const event = {
      traceId: ctx.traceId,
      spanId: ctx.spanId,
      name: span.name,
      startTimeUnixNano: hrTimeToUnixNano(span.startTime),
      endTimeUnixNano: hrTimeToUnixNano(span.endTime),
      attributes
    };
    const parentSpanId = resolveParentSpanId(span);
    if (parentSpanId) event.parentSpanId = parentSpanId;
    if (span.events && span.events.length > 0) {
      event.events = span.events.map((e) => {
        const eventAttrs = cleanAttributes(e.attributes);
        const node = {
          timeUnixNano: hrTimeToUnixNano(e.time),
          name: e.name
        };
        if (Object.keys(eventAttrs).length > 0) node.attributes = eventAttrs;
        return node;
      });
    }
    if (span.status) {
      event.status = { code: statusCodeName(span.status.code), message: span.status.message };
    }
    if (runId) event["tangle.runId"] = runId;
    if (scenarioId) event["tangle.scenarioId"] = scenarioId;
    if (cellId) event["tangle.cellId"] = cellId;
    if (generation !== void 0) event["tangle.generation"] = generation;
    return event;
  }
  async function ingest(spans) {
    if (spans.length === 0) return;
    const events = spans.map(convert);
    for (let i = 0; i < events.length; i += batchSize) {
      const batch = events.slice(i, i + batchSize);
      try {
        await opts.client.ingestTraces(batch);
      } catch (err) {
        await onError(err, batch);
      }
    }
  }
  return { ingest, spanToEvent: convert };
}
export {
  OTEL_STATUS_ERROR,
  OTEL_STATUS_OK,
  OTEL_STATUS_UNSET,
  createOtelBridge,
  hrTimeToUnixNano
};
//# sourceMappingURL=otel.js.map