"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var report_exports = {};
__export(report_exports, {
  createSessionReport: () => createSessionReport,
  sessionReportToJSON: () => sessionReportToJSON,
  toSnakeCaseDeep: () => toSnakeCaseDeep
});
module.exports = __toCommonJS(report_exports);
var import_model_usage = require("../metrics/model_usage.cjs");
var import_version = require("../version.cjs");
function createSessionReport(opts) {
  const timestamp = opts.timestamp ?? Date.now();
  const audioRecordingStartedAt = opts.audioRecordingStartedAt;
  return {
    jobId: opts.jobId,
    roomId: opts.roomId,
    room: opts.room,
    options: opts.options,
    events: opts.events,
    chatHistory: opts.chatHistory,
    enableRecording: opts.enableRecording ?? false,
    recordingOptions: opts.recordingOptions ?? {
      audio: false,
      traces: false,
      logs: false,
      transcript: false
    },
    startedAt: opts.startedAt ?? Date.now(),
    timestamp,
    audioRecordingPath: opts.audioRecordingPath,
    audioRecordingStartedAt,
    duration: audioRecordingStartedAt !== void 0 ? timestamp - audioRecordingStartedAt : void 0,
    modelUsage: opts.modelUsage
  };
}
function jsToPythonFieldName(key) {
  if (key === "args") {
    return "arguments";
  }
  return key.replace(/([A-Z]+)/g, (m) => `_${m.toLowerCase()}`);
}
function toSnakeCaseDeep(value) {
  if (value === null || typeof value !== "object") {
    return value;
  }
  if (Array.isArray(value)) {
    return value.map(toSnakeCaseDeep);
  }
  if (typeof value.toJSON === "function") {
    return toSnakeCaseDeep(value.toJSON());
  }
  const out = {};
  for (const [k, v] of Object.entries(value)) {
    if (v === void 0) {
      continue;
    }
    out[jsToPythonFieldName(k)] = k === "extra" ? v : toSnakeCaseDeep(v);
  }
  return out;
}
function serializeEventSource(source) {
  if (source && typeof source === "object") {
    const s = source;
    if ("model" in s || "provider" in s) {
      return { model: s.model ?? null, provider: s.provider ?? null };
    }
  }
  return source === void 0 ? null : String(source);
}
function serializeEventError(error) {
  if (error === null || error === void 0) {
    return null;
  }
  if (error instanceof Error) {
    return error.message;
  }
  if (typeof error === "object") {
    return toSnakeCaseDeep(error);
  }
  return error;
}
function eventToJSON(event) {
  const e = { ...event };
  switch (event.type) {
    case "speech_created":
      delete e.speechHandle;
      break;
    case "overlapping_speech":
      delete e.speechInput;
      break;
    case "error":
      e.source = serializeEventSource(e.source);
      e.error = serializeEventError(e.error);
      break;
    case "close":
      e.error = serializeEventError(e.error);
      break;
  }
  return toSnakeCaseDeep(e);
}
function modelUsageToJSON(usage) {
  const filtered = (0, import_model_usage.filterZeroValues)(usage);
  const out = {};
  for (const [k, v] of Object.entries(filtered)) {
    if (v === void 0) {
      continue;
    }
    if (k === "sessionDurationMs") {
      out.session_duration = v / 1e3;
    } else if (k === "audioDurationMs") {
      out.audio_duration = v / 1e3;
    } else {
      out[jsToPythonFieldName(k)] = v;
    }
  }
  return out;
}
function sessionReportToJSON(report) {
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
  const events = [];
  const options = report.options;
  const interruptionConfig = (_a = options.turnHandling) == null ? void 0 : _a.interruption;
  const endpointingConfig = (_b = options.turnHandling) == null ? void 0 : _b.endpointing;
  const allowInterruptions = (interruptionConfig == null ? void 0 : interruptionConfig.enabled) !== void 0 ? interruptionConfig.enabled : (interruptionConfig == null ? void 0 : interruptionConfig.mode) !== void 0 ? true : options.allowInterruptions ?? ((_c = options.voiceOptions) == null ? void 0 : _c.allowInterruptions);
  const discardAudioIfUninterruptible = (interruptionConfig == null ? void 0 : interruptionConfig.discardAudioIfUninterruptible) ?? options.discardAudioIfUninterruptible ?? ((_d = options.voiceOptions) == null ? void 0 : _d.discardAudioIfUninterruptible);
  const minInterruptionDuration = (interruptionConfig == null ? void 0 : interruptionConfig.minDuration) ?? options.minInterruptionDuration ?? ((_e = options.voiceOptions) == null ? void 0 : _e.minInterruptionDuration);
  const minInterruptionWords = (interruptionConfig == null ? void 0 : interruptionConfig.minWords) ?? options.minInterruptionWords ?? ((_f = options.voiceOptions) == null ? void 0 : _f.minInterruptionWords);
  const minEndpointingDelay = (endpointingConfig == null ? void 0 : endpointingConfig.minDelay) ?? options.minEndpointingDelay ?? ((_g = options.voiceOptions) == null ? void 0 : _g.minEndpointingDelay);
  const maxEndpointingDelay = (endpointingConfig == null ? void 0 : endpointingConfig.maxDelay) ?? options.maxEndpointingDelay ?? ((_h = options.voiceOptions) == null ? void 0 : _h.maxEndpointingDelay);
  for (const event of report.events) {
    if (event.type === "metrics_collected" || event.type === "session_usage_updated") {
      continue;
    }
    events.push(eventToJSON(event));
  }
  return {
    job_id: report.jobId,
    room_id: report.roomId,
    room: report.room,
    events,
    audio_recording_path: report.audioRecordingPath ?? null,
    audio_recording_started_at: report.audioRecordingStartedAt ?? null,
    options: {
      allow_interruptions: allowInterruptions,
      discard_audio_if_uninterruptible: discardAudioIfUninterruptible,
      min_interruption_duration: minInterruptionDuration,
      min_interruption_words: minInterruptionWords,
      min_endpointing_delay: minEndpointingDelay,
      max_endpointing_delay: maxEndpointingDelay,
      max_tool_steps: options.maxToolSteps,
      user_away_timeout: options.userAwayTimeout ?? null,
      preemptive_generation: ((_i = options.turnHandling) == null ? void 0 : _i.preemptiveGeneration) ?? {}
    },
    chat_history: toSnakeCaseDeep(report.chatHistory.toJSON({ excludeTimestamp: false })),
    enable_user_data_training: report.enableRecording,
    timestamp: report.timestamp,
    usage: report.modelUsage ? report.modelUsage.map(modelUsageToJSON) : null,
    sdk_version: import_version.version
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  createSessionReport,
  sessionReportToJSON,
  toSnakeCaseDeep
});
//# sourceMappingURL=report.cjs.map