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

//#region src/renderers/notion.ts
/**
* Notion renderer — outputs Notion Block JSON for the Notion API.
* Blocks can be appended to a page via `notion.blocks.children.append()`.
*/
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("");
}
function textToRichText(text, annotations) {
	return [{
		type: "text",
		text: { content: text },
		...annotations ? { annotations } : {}
	}];
}
function nodeToBlocks(node) {
	const p = node.props;
	const blocks = [];
	switch (node.type) {
		case "document":
		case "page":
		case "section":
		case "row":
		case "column":
			for (const child of node.children) if (typeof child !== "string") blocks.push(...nodeToBlocks(child));
			break;
		case "heading": {
			const level = p.level ?? 1;
			const text = getTextContent(node.children);
			const type = level <= 1 ? "heading_1" : level === 2 ? "heading_2" : "heading_3";
			blocks.push({
				object: "block",
				type,
				[type]: { rich_text: textToRichText(text) }
			});
			break;
		}
		case "text": {
			const text = getTextContent(node.children);
			const annotations = {};
			if (p.bold) annotations.bold = true;
			if (p.italic) annotations.italic = true;
			if (p.strikethrough) annotations.strikethrough = true;
			if (p.underline) annotations.underline = true;
			blocks.push({
				object: "block",
				type: "paragraph",
				paragraph: { rich_text: textToRichText(text, Object.keys(annotations).length > 0 ? annotations : void 0) }
			});
			break;
		}
		case "link": {
			const href = sanitizeHref(p.href);
			const text = getTextContent(node.children);
			blocks.push({
				object: "block",
				type: "paragraph",
				paragraph: { rich_text: [{
					type: "text",
					text: {
						content: text,
						link: { url: href }
					}
				}] }
			});
			break;
		}
		case "image": {
			const src = sanitizeImageSrc(p.src);
			if (src.startsWith("http")) blocks.push({
				object: "block",
				type: "image",
				image: {
					type: "external",
					external: { url: src },
					...p.caption ? { caption: textToRichText(p.caption) } : {}
				}
			});
			break;
		}
		case "table": {
			const columns = (p.columns ?? []).map(resolveColumn);
			const rows = p.rows ?? [];
			const tableRows = [];
			tableRows.push({
				object: "block",
				type: "table_row",
				table_row: { cells: columns.map((col) => textToRichText(col.header, { bold: true })) }
			});
			for (const row of rows) tableRows.push({
				object: "block",
				type: "table_row",
				table_row: { cells: columns.map((_, i) => textToRichText(String(row[i] ?? ""))) }
			});
			blocks.push({
				object: "block",
				type: "table",
				table: {
					table_width: columns.length,
					has_column_header: true,
					children: tableRows
				}
			});
			break;
		}
		case "list": {
			const ordered = p.ordered;
			const items = node.children.filter((c) => typeof c !== "string");
			for (const item of items) {
				const text = getTextContent(item.children);
				const type = ordered ? "numbered_list_item" : "bulleted_list_item";
				blocks.push({
					object: "block",
					type,
					[type]: { rich_text: textToRichText(text) }
				});
			}
			break;
		}
		case "code": {
			const text = getTextContent(node.children);
			const lang = p.language ?? "plain text";
			blocks.push({
				object: "block",
				type: "code",
				code: {
					rich_text: textToRichText(text),
					language: lang
				}
			});
			break;
		}
		case "divider":
		case "page-break":
			blocks.push({
				object: "block",
				type: "divider",
				divider: {}
			});
			break;
		case "spacer":
			blocks.push({
				object: "block",
				type: "paragraph",
				paragraph: { rich_text: [] }
			});
			break;
		case "button": {
			const href = sanitizeHref(p.href);
			const text = getTextContent(node.children);
			blocks.push({
				object: "block",
				type: "paragraph",
				paragraph: { rich_text: [{
					type: "text",
					text: {
						content: text,
						link: { url: href }
					},
					annotations: { bold: true }
				}] }
			});
			break;
		}
		case "quote": {
			const text = getTextContent(node.children);
			blocks.push({
				object: "block",
				type: "quote",
				quote: { rich_text: textToRichText(text) }
			});
			break;
		}
	}
	return blocks;
}
const notionRenderer = { async render(node, _options) {
	const blocks = nodeToBlocks(node);
	return JSON.stringify({ children: blocks }, null, 2);
} };

//#endregion
export { notionRenderer };
//# sourceMappingURL=notion-iG2C5bEY.js.map