import { n as sanitizeHref, r as sanitizeImageSrc } from "./sanitize-O_3j1mNJ.js";

//#region src/renderers/discord.ts
/**
* Discord renderer — outputs embed JSON for Discord webhooks/bots.
* Uses Discord's markdown subset and embed structure.
*/
function resolveColumn(col) {
	return typeof col === "string" ? { header: col } : col;
}
function getTextContent(children) {
	return children.map((c) => typeof c === "string" ? c : getTextContent(c.children)).join("");
}
/** Extract the first h1 title and first HTTP image from the tree. */
function extractMeta(node) {
	if (node.type === "heading") {
		if ((node.props.level ?? 1) === 1) return { title: getTextContent(node.children) };
	}
	if (node.type === "image") {
		const src = sanitizeImageSrc(node.props.src);
		if (src.startsWith("http")) return { imageUrl: src };
	}
	for (const child of node.children) if (typeof child !== "string") {
		const result = extractMeta(child);
		if (result.title || result.imageUrl) return result;
	}
	return {};
}
function nodeToMarkdown(node, meta) {
	const p = node.props;
	let content = "";
	const fields = [];
	switch (node.type) {
		case "document":
		case "page":
		case "section":
		case "row":
		case "column":
			for (const child of node.children) if (typeof child !== "string") {
				const result = nodeToMarkdown(child, meta);
				content += result.content;
				fields.push(...result.fields);
			}
			break;
		case "heading": {
			const text = getTextContent(node.children);
			if ((p.level ?? 1) === 1 && text === meta.title) break;
			content += `**${text}**\n\n`;
			break;
		}
		case "text": {
			let text = getTextContent(node.children);
			if (p.bold) text = `**${text}**`;
			if (p.italic) text = `*${text}*`;
			if (p.strikethrough) text = `~~${text}~~`;
			content += `${text}\n\n`;
			break;
		}
		case "link": {
			const href = sanitizeHref(p.href);
			const text = getTextContent(node.children);
			content += `[${text}](${href})\n\n`;
			break;
		}
		case "image": break;
		case "table": {
			const columns = (p.columns ?? []).map(resolveColumn);
			const rows = p.rows ?? [];
			if (columns.length <= 3 && rows.length <= 10) for (const col of columns) {
				const colIdx = columns.indexOf(col);
				const values = rows.map((row) => String(row[colIdx] ?? "")).join("\n");
				fields.push({
					name: col.header,
					value: values || "-",
					inline: true
				});
			}
			else {
				const header = columns.map((c) => c.header).join(" | ");
				const separator = columns.map(() => "---").join(" | ");
				const body = rows.map((row) => row.map((c) => String(c ?? "")).join(" | ")).join("\n");
				content += `\`\`\`\n${header}\n${separator}\n${body}\n\`\`\`\n\n`;
			}
			break;
		}
		case "list": {
			const ordered = p.ordered;
			const items = node.children.filter((c) => typeof c !== "string").map((item, i) => {
				return `${ordered ? `${i + 1}.` : "•"} ${getTextContent(item.children)}`;
			}).join("\n");
			content += `${items}\n\n`;
			break;
		}
		case "code": {
			const lang = p.language ?? "";
			const text = getTextContent(node.children);
			content += `\`\`\`${lang}\n${text}\n\`\`\`\n\n`;
			break;
		}
		case "divider":
		case "page-break":
			content += "───────────\n\n";
			break;
		case "button": {
			const href = sanitizeHref(p.href);
			const text = getTextContent(node.children);
			content += `[**${text}**](${href})\n\n`;
			break;
		}
		case "quote": {
			const text = getTextContent(node.children);
			content += `> ${text}\n\n`;
			break;
		}
	}
	return {
		content,
		fields
	};
}
const discordRenderer = { async render(node, _options) {
	const meta = extractMeta(node);
	const { content, fields } = nodeToMarkdown(node, meta);
	const embed = {
		title: meta.title ?? node.props.title ?? void 0,
		description: content.trim() || void 0,
		color: 5195493
	};
	if (fields.length > 0) embed.fields = fields;
	if (meta.imageUrl) embed.image = { url: meta.imageUrl };
	return JSON.stringify({ embeds: [embed] }, null, 2);
} };

//#endregion
export { discordRenderer };
//# sourceMappingURL=discord-BLUnkEh9.js.map