"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 utils_exports = {};
__export(utils_exports, {
  mergeWithDefaults: () => mergeWithDefaults,
  migrateLegacyOptions: () => migrateLegacyOptions,
  migrateTurnHandling: () => migrateTurnHandling,
  resolveEndpointing: () => resolveEndpointing,
  stripUndefined: () => stripUndefined
});
module.exports = __toCommonJS(utils_exports);
var import_base = require("../../inference/eot/base.cjs");
var import_log = require("../../log.cjs");
var import_agent_session = require("../agent_session.cjs");
var import_endpointing = require("./endpointing.cjs");
var import_interruption = require("./interruption.cjs");
var import_preemptive_generation = require("./preemptive_generation.cjs");
var import_turn_handling = require("./turn_handling.cjs");
var import_user_turn_limit = require("./user_turn_limit.cjs");
const defaultSessionOptions = {
  maxToolSteps: 3,
  userAwayTimeout: 15,
  aecWarmupDuration: 3e3,
  ttsReadIdleTimeout: 1e4,
  forwardAudioIdleTimeout: 1e4,
  turnHandling: {},
  useTtsAlignedTranscript: true,
  ttsTextTransforms: ["filter_markdown", "filter_emoji"]
};
const defaultLegacyVoiceOptions = {
  minEndpointingDelay: import_turn_handling.defaultTurnHandlingOptions.endpointing.minDelay,
  maxEndpointingDelay: import_turn_handling.defaultTurnHandlingOptions.endpointing.maxDelay,
  maxToolSteps: defaultSessionOptions.maxToolSteps
};
function migrateLegacyOptions(legacyOptions) {
  var _a, _b, _c, _d, _e;
  const logger = (0, import_log.log)();
  const {
    voiceOptions,
    turnDetection,
    stt,
    vad,
    llm,
    tts,
    userData,
    connOptions,
    ...sessionOptions
  } = legacyOptions;
  if (voiceOptions !== void 0) {
    logger.warn(
      "voiceOptions is deprecated, use top-level SessionOptions fields on AgentSessionOptions instead"
    );
  }
  const turnHandling = {
    interruption: {
      discardAudioIfUninterruptible: voiceOptions == null ? void 0 : voiceOptions.discardAudioIfUninterruptible,
      minDuration: voiceOptions == null ? void 0 : voiceOptions.minInterruptionDuration,
      minWords: voiceOptions == null ? void 0 : voiceOptions.minInterruptionWords,
      ...(_a = sessionOptions.turnHandling) == null ? void 0 : _a.interruption
    },
    endpointing: {
      minDelay: voiceOptions == null ? void 0 : voiceOptions.minEndpointingDelay,
      maxDelay: voiceOptions == null ? void 0 : voiceOptions.maxEndpointingDelay,
      ...(_b = sessionOptions.turnHandling) == null ? void 0 : _b.endpointing
    },
    preemptiveGeneration: {
      ...(_c = sessionOptions.turnHandling) == null ? void 0 : _c.preemptiveGeneration
    },
    userTurnLimit: {
      ...(_d = sessionOptions.turnHandling) == null ? void 0 : _d.userTurnLimit
    },
    // Preserve an explicit `null` (opt-out) distinctly from `undefined` (not
    // given). `??` would collapse both, so only fall back to the deprecated
    // top-level `turnDetection` when `turnHandling.turnDetection` is absent.
    turnDetection: ((_e = sessionOptions == null ? void 0 : sessionOptions.turnHandling) == null ? void 0 : _e.turnDetection) !== void 0 ? sessionOptions.turnHandling.turnDetection : turnDetection
  };
  if ((voiceOptions == null ? void 0 : voiceOptions.allowInterruptions) === false && turnHandling.interruption.enabled === void 0) {
    turnHandling.interruption.enabled = false;
  }
  const migratedVoiceOptions = {};
  if ((voiceOptions == null ? void 0 : voiceOptions.maxToolSteps) !== void 0) {
    migratedVoiceOptions.maxToolSteps = voiceOptions.maxToolSteps;
  }
  if ((voiceOptions == null ? void 0 : voiceOptions.userAwayTimeout) !== void 0) {
    migratedVoiceOptions.userAwayTimeout = voiceOptions.userAwayTimeout;
  }
  const deprecatedPreemptiveGen = legacyOptions.preemptiveGeneration ?? (voiceOptions == null ? void 0 : voiceOptions.preemptiveGeneration);
  if (deprecatedPreemptiveGen !== void 0) {
    logger.warn(
      "preemptiveGeneration as a top-level option is deprecated, use turnHandling.preemptiveGeneration instead"
    );
    if (turnHandling.preemptiveGeneration.enabled === void 0) {
      turnHandling.preemptiveGeneration = {
        ...turnHandling.preemptiveGeneration,
        enabled: deprecatedPreemptiveGen
      };
    }
  }
  const legacyVoiceOptions = { ...defaultLegacyVoiceOptions, ...voiceOptions };
  const agentSessionOptions = {
    stt,
    vad,
    llm,
    tts,
    userData,
    connOptions,
    ...defaultSessionOptions,
    ...migratedVoiceOptions,
    ...sessionOptions,
    turnHandling: mergeWithDefaults(turnHandling),
    // repopulate the deprecated voice options with migrated options for backwards compatibility
    voiceOptions: legacyVoiceOptions
  };
  return { agentSessionOptions, legacyVoiceOptions };
}
function stripUndefined(obj) {
  return Object.fromEntries(Object.entries(obj).filter(([, v]) => v !== void 0));
}
function resolveEndpointing(overrides, turnDetection) {
  const base = turnDetection instanceof import_base.BaseStreamingTurnDetector ? import_endpointing.streamingEndpointingOptions : import_endpointing.defaultEndpointingOptions;
  return { ...base, ...stripUndefined(overrides) };
}
function mergeWithDefaults(config) {
  const endpointingOverrides = stripUndefined(config.endpointing);
  return {
    // Keep an explicit `null` (opt-out) — only an absent value falls back to
    // the default, so the constructor can tell opt-out from not-given.
    turnDetection: config.turnDetection === void 0 ? import_turn_handling.defaultTurnHandlingOptions.turnDetection : config.turnDetection,
    // Resolve endpointing defaults against the effective detector: an omitted
    // `turnDetection` auto-provisions the streaming TurnDetector (→ tighter
    // streaming defaults), while `null` opt-out or a non-streaming mode keeps
    // the legacy defaults. Activities re-resolve per-detector from `endpointingOverrides`.
    endpointing: {
      ...config.turnDetection === void 0 || config.turnDetection instanceof import_base.BaseStreamingTurnDetector ? import_endpointing.streamingEndpointingOptions : import_endpointing.defaultEndpointingOptions,
      ...endpointingOverrides
    },
    endpointingOverrides,
    interruption: { ...import_interruption.defaultInterruptionOptions, ...stripUndefined(config.interruption) },
    preemptiveGeneration: {
      ...import_preemptive_generation.defaultPreemptiveGenerationOptions,
      ...stripUndefined(config.preemptiveGeneration ?? {})
    },
    userTurnLimit: {
      ...import_user_turn_limit.defaultUserTurnLimitOptions,
      ...stripUndefined(config.userTurnLimit ?? {})
    }
  };
}
function migrateTurnHandling(opts) {
  if (opts.turnHandling !== void 0) {
    return opts.turnHandling;
  }
  const migrated = {};
  const endpointing = {};
  if (opts.minEndpointingDelay !== void 0) {
    endpointing.minDelay = opts.minEndpointingDelay;
  }
  if (opts.maxEndpointingDelay !== void 0) {
    endpointing.maxDelay = opts.maxEndpointingDelay;
  }
  if (Object.keys(endpointing).length > 0) {
    migrated.endpointing = endpointing;
  }
  const interruption = {};
  if (opts.allowInterruptions === false) {
    interruption.enabled = false;
  }
  if (Object.keys(interruption).length > 0) {
    migrated.interruption = interruption;
  }
  if (opts.turnDetection !== void 0) {
    migrated.turnDetection = opts.turnDetection;
  }
  if (opts.preemptiveGeneration !== void 0) {
    migrated.preemptiveGeneration = { enabled: opts.preemptiveGeneration };
  }
  return {
    ...migrated.endpointing ? { endpointing: migrated.endpointing } : {},
    ...migrated.interruption ? { interruption: migrated.interruption } : {},
    ...migrated.turnDetection !== void 0 ? { turnDetection: migrated.turnDetection } : {},
    ...migrated.preemptiveGeneration ? { preemptiveGeneration: migrated.preemptiveGeneration } : {}
  };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  mergeWithDefaults,
  migrateLegacyOptions,
  migrateTurnHandling,
  resolveEndpointing,
  stripUndefined
});
//# sourceMappingURL=utils.cjs.map