// src/status/normalize.ts
var WORKING_STATUSES = /* @__PURE__ */ new Set([
  "generating",
  "streaming",
  "loading",
  "loading_reference",
  "thinking",
  "active"
]);
var FULL_STATUS_ACTIVE_CHAT_OPTIONS = {
  includeMessages: true,
  includeInputContent: true,
  includeActiveModal: true,
  messageLimit: 60,
  totalBytesLimit: 96 * 1024,
  stringLimit: 4 * 1024,
  fallbackStringLimit: 1024
};
var LIVE_STATUS_ACTIVE_CHAT_OPTIONS = {
  includeMessages: false,
  includeInputContent: false,
  includeActiveModal: false,
  messageLimit: 0,
  totalBytesLimit: 0,
  stringLimit: 512,
  fallbackStringLimit: 256
};
var STATUS_MODAL_MESSAGE_LIMIT = 2 * 1024;
var STATUS_MODAL_BUTTON_LIMIT = 120;
function truncateString(value, maxChars) {
  if (value.length <= maxChars) return value;
  if (maxChars <= 12) return value.slice(0, Math.max(0, maxChars));
  return `${value.slice(0, maxChars - 12)}...[truncated]`;
}
function trimStructuredStrings(value, maxChars) {
  if (typeof value === "string") return truncateString(value, maxChars);
  if (Array.isArray(value)) return value.map((item) => trimStructuredStrings(item, maxChars));
  if (!value || typeof value !== "object") return value;
  return Object.fromEntries(
    Object.entries(value).map(([key, nested]) => [key, trimStructuredStrings(nested, maxChars)])
  );
}
function estimateBytes(value) {
  try {
    return JSON.stringify(value).length;
  } catch {
    return String(value ?? "").length;
  }
}
function trimMessageForStatus(message, stringLimit) {
  if (!message || typeof message !== "object") return message;
  return trimStructuredStrings(message, stringLimit);
}
function normalizeMessageTime(message) {
  if (!message || typeof message !== "object") return message;
  const msg = message;
  if (msg.receivedAt == null) {
    const fallback = msg.timestamp ?? msg.createdAt;
    if (fallback != null) {
      const ts = typeof fallback === "string" ? Date.parse(fallback) : Number(fallback);
      if (Number.isFinite(ts) && ts > 0) msg.receivedAt = ts;
    }
  }
  return msg;
}
function trimMessagesForStatus(messages, options) {
  if (!options.includeMessages || options.messageLimit <= 0 || options.totalBytesLimit <= 0) return [];
  if (!Array.isArray(messages) || messages.length === 0) return [];
  const recent = messages.slice(-options.messageLimit);
  const kept = [];
  let totalBytes = 0;
  for (let i = recent.length - 1; i >= 0; i -= 1) {
    let normalized = normalizeMessageTime(trimMessageForStatus(recent[i], options.stringLimit));
    let size = estimateBytes(normalized);
    if (size > options.totalBytesLimit) {
      normalized = normalizeMessageTime(trimMessageForStatus(recent[i], options.fallbackStringLimit));
      size = estimateBytes(normalized);
    }
    if (kept.length > 0 && totalBytes + size > options.totalBytesLimit) {
      continue;
    }
    kept.push(normalized);
    totalBytes += size;
  }
  return kept.reverse();
}
function hasApprovalButtons(activeModal) {
  return (activeModal?.buttons?.length ?? 0) > 0;
}
function normalizeManagedStatus(status, opts) {
  if (hasApprovalButtons(opts?.activeModal)) return "waiting_approval";
  const normalized = String(status || "idle").trim().toLowerCase();
  if (normalized === "waiting_approval") return "waiting_approval";
  if (WORKING_STATUSES.has(normalized)) return "generating";
  if (normalized === "error") return "error";
  if (normalized === "stopped") return "stopped";
  if (normalized === "starting") return "starting";
  if (normalized === "panel_hidden") return "panel_hidden";
  if (normalized === "not_monitored") return "not_monitored";
  if (normalized === "disconnected") return "disconnected";
  return "idle";
}
function isManagedStatusWorking(status) {
  return normalizeManagedStatus(status) === "generating";
}
function isManagedStatusWaiting(status, opts) {
  return normalizeManagedStatus(status, opts) === "waiting_approval";
}
function normalizeActiveChatData(activeChat, options = FULL_STATUS_ACTIVE_CHAT_OPTIONS) {
  if (!activeChat) return activeChat;
  const resolvedOptions = {
    ...FULL_STATUS_ACTIVE_CHAT_OPTIONS,
    ...options
  };
  return {
    ...activeChat,
    status: normalizeManagedStatus(activeChat.status, { activeModal: activeChat.activeModal }),
    messages: trimMessagesForStatus(activeChat.messages, resolvedOptions),
    activeModal: resolvedOptions.includeActiveModal && activeChat.activeModal ? {
      message: truncateString(activeChat.activeModal.message || "", STATUS_MODAL_MESSAGE_LIMIT),
      buttons: (activeChat.activeModal.buttons || []).map(
        (button) => truncateString(String(button || ""), STATUS_MODAL_BUTTON_LIMIT)
      )
    } : null,
    inputContent: resolvedOptions.includeInputContent && activeChat.inputContent ? truncateString(activeChat.inputContent, 2 * 1024) : void 0
  };
}
export {
  LIVE_STATUS_ACTIVE_CHAT_OPTIONS,
  isManagedStatusWaiting,
  isManagedStatusWorking,
  normalizeActiveChatData,
  normalizeManagedStatus
};
//# sourceMappingURL=normalize.mjs.map