import { getConversationProperties, getConversationUserProperties } from "@inkeep/agents-core";

//#region src/utils/conversationFormatter.ts
const CONVERSATION_DETAIL_MESSAGE_LIMIT = 200;
function toIsoString(value) {
	if (!value) return (/* @__PURE__ */ new Date(0)).toISOString();
	if (value instanceof Date) return Number.isNaN(value.getTime()) ? (/* @__PURE__ */ new Date(0)).toISOString() : value.toISOString();
	const normalized = /Z$|[+-]\d{2}:?\d{2}$/i.test(value) ? value : `${value.replace(" ", "T")}Z`;
	const d = new Date(normalized);
	return Number.isNaN(d.getTime()) ? (/* @__PURE__ */ new Date(0)).toISOString() : d.toISOString();
}
function formatMessage(m) {
	const content = m.content?.text ?? null;
	return {
		id: m.id,
		role: m.role === "agent" ? "assistant" : "user",
		content,
		createdAt: toIsoString(m.createdAt)
	};
}
function formatConversationDetail(conversation, messages) {
	return {
		id: conversation.id,
		agentId: conversation.agentId ?? null,
		title: conversation.title ?? null,
		userProperties: getConversationUserProperties(conversation),
		properties: getConversationProperties(conversation),
		createdAt: toIsoString(conversation.createdAt),
		updatedAt: toIsoString(conversation.updatedAt),
		messages: messages.slice(-CONVERSATION_DETAIL_MESSAGE_LIMIT).map(formatMessage)
	};
}
function formatFeedback(feedback) {
	const { tenantId: _t, projectId: _p, createdAt, updatedAt, ...rest } = feedback;
	return {
		...rest,
		createdAt: toIsoString(createdAt),
		updatedAt: toIsoString(updatedAt)
	};
}
function formatEvent(event) {
	const { tenantId: _t, projectId: _p, createdAt, updatedAt, ...rest } = event;
	return {
		...rest,
		createdAt: toIsoString(createdAt),
		updatedAt: toIsoString(updatedAt)
	};
}

//#endregion
export { CONVERSATION_DETAIL_MESSAGE_LIMIT, formatConversationDetail, formatEvent, formatFeedback, formatMessage, toIsoString };