import { n as htmlToMarkdown } from "./html-BaeOCZKE.js";
//#region src/sync/email-body.ts
/** Decode a Gmail part body (base64url) to a UTF-8 string. */
function decodeBody(data) {
	if (!data) return "";
	return Buffer.from(data, "base64url").toString("utf-8");
}
/**
* Recursively collect the text/plain and text/html bodies from a Gmail message
* payload. Attachment parts (those with a filename) are ignored — only inline
* body parts are considered. The first body of each type wins (the top-level
* alternative), so signatures appended in nested forwards don't clobber it.
*/
function collectBodyParts(payload) {
	const result = {};
	const walk = (part) => {
		if (!part) return;
		const mime = part.mimeType ?? "";
		if (!(Boolean(part.filename) || Boolean(part.body?.attachmentId)) && part.body?.data) {
			if (mime === "text/plain" && result.plain === void 0) result.plain = decodeBody(part.body.data);
			else if (mime === "text/html" && result.html === void 0) result.html = decodeBody(part.body.data);
		}
		for (const child of part.parts ?? []) walk(child);
	};
	walk(payload);
	return result;
}
/**
* Extract the email body as Markdown: prefers the plain-text part verbatim,
* otherwise converts the HTML part via Turndown. Returns an empty string when
* the message carries no inline body (e.g. attachment-only messages).
*/
async function extractEmailBodyMarkdown(payload) {
	const { plain, html } = collectBodyParts(payload);
	if (plain && plain.trim()) return plain.trim();
	if (html && html.trim()) return (await htmlToMarkdown(html)).trim();
	return "";
}
//#endregion
export { extractEmailBodyMarkdown };

//# sourceMappingURL=email-body-BOd7U-D2.js.map