//#region src/gateway/server-methods/attachment-normalize.ts
function normalizeAttachmentContent(content) {
	if (typeof content === "string") return content;
	if (ArrayBuffer.isView(content)) return Buffer.from(content.buffer, content.byteOffset, content.byteLength).toString("base64");
	if (content instanceof ArrayBuffer) return Buffer.from(content).toString("base64");
}
function normalizeRpcAttachmentsToChatAttachments(attachments) {
	return attachments?.map((a) => {
		const sourceRecord = a?.source && typeof a.source === "object" ? a.source : void 0;
		const sourceType = typeof sourceRecord?.type === "string" ? sourceRecord.type : void 0;
		const sourceMimeType = typeof sourceRecord?.media_type === "string" ? sourceRecord.media_type : void 0;
		const sourceContent = sourceType === "base64" ? normalizeAttachmentContent(sourceRecord?.data) : void 0;
		const mediaRef = typeof a?.mediaRef === "string" ? a.mediaRef : void 0;
		const sizeBytes = typeof a?.sizeBytes === "number" && Number.isFinite(a.sizeBytes) && a.sizeBytes >= 0 ? a.sizeBytes : void 0;
		return {
			type: typeof a?.type === "string" ? a.type : void 0,
			mimeType: typeof a?.mimeType === "string" ? a.mimeType : sourceMimeType,
			fileName: typeof a?.fileName === "string" ? a.fileName : void 0,
			content: normalizeAttachmentContent(a?.content) ?? sourceContent,
			...mediaRef ? { mediaRef } : {},
			...sizeBytes !== void 0 ? { sizeBytes } : {}
		};
	}).filter((a) => a.content || a.mediaRef) ?? [];
}
//#endregion
export { normalizeRpcAttachmentsToChatAttachments as t };
