import { getLogger } from "../../../../logger.js";
import { hasFileParts, makeMessageContentParts, uploadPartsImages } from "./image-upload.js";

//#region src/domains/run/services/blob-storage/image-upload-helpers.ts
const logger = getLogger("image-upload-helpers");
async function buildPersistedMessageContent(text, parts, ctx) {
	if (!hasFileParts(parts)) return { text };
	try {
		const contentParts = makeMessageContentParts(await uploadPartsImages(parts, ctx));
		logger.debug({
			messageId: ctx.messageId,
			originalParts: parts.length,
			uploadedParts: contentParts.length,
			fileParts: contentParts.filter((p) => p.kind === "file").length
		}, "Built persisted message content with uploaded images");
		return {
			text,
			parts: contentParts
		};
	} catch (error) {
		logger.error({
			error: error instanceof Error ? error.message : String(error),
			messageId: ctx.messageId
		}, "Failed to upload images, persisting text only");
		return { text };
	}
}

//#endregion
export { buildPersistedMessageContent };