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

//#region src/renderers/slack.ts
/**
* Slack Block Kit renderer — outputs JSON that can be posted via Slack's API.
* Maps document nodes to Slack blocks (section, header, divider, image, etc.).
*/
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 mrkdwn(text) {
	return {
		type: "mrkdwn",
		text
	};
}
function plainText(text) {
	return {
		type: "plain_text",
		text
	};
}
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":
			blocks.push({
				type: "header",
				text: plainText(getTextContent(node.children))
			});
			break;
		case "text": {
			let text = getTextContent(node.children);
			if (p.bold) text = `*${text}*`;
			if (p.italic) text = `_${text}_`;
			if (p.strikethrough) text = `~${text}~`;
			blocks.push({
				type: "section",
				text: mrkdwn(text)
			});
			break;
		}
		case "link": {
			const href = sanitizeHref(p.href);
			const text = getTextContent(node.children);
			blocks.push({
				type: "section",
				text: mrkdwn(`<${href}|${text}>`)
			});
			break;
		}
		case "image": {
			const src = sanitizeImageSrc(p.src);
			if (src.startsWith("http")) blocks.push({
				type: "image",
				image_url: src,
				alt_text: p.alt ?? "Image",
				...p.caption ? { title: plainText(p.caption) } : {}
			});
			break;
		}
		case "table": {
			const columns = (p.columns ?? []).map(resolveColumn);
			const rows = p.rows ?? [];
			let text = `${columns.map((c) => `*${c.header}*`).join(" | ")}\n${columns.map(() => "---").join(" | ")}\n${rows.map((row) => row.map((cell) => String(cell ?? "")).join(" | ")).join("\n")}`;
			if (p.caption) text = `_${p.caption}_\n${text}`;
			blocks.push({
				type: "section",
				text: mrkdwn(`\`\`\`\n${text}\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");
			blocks.push({
				type: "section",
				text: mrkdwn(items)
			});
			break;
		}
		case "code": {
			const text = getTextContent(node.children);
			const lang = p.language ?? "";
			blocks.push({
				type: "section",
				text: mrkdwn(`\`\`\`${lang}\n${text}\n\`\`\``)
			});
			break;
		}
		case "divider":
		case "page-break":
			blocks.push({ type: "divider" });
			break;
		case "spacer": break;
		case "button": {
			const href = sanitizeHref(p.href);
			const text = getTextContent(node.children);
			blocks.push({
				type: "actions",
				elements: [{
					type: "button",
					text: plainText(text),
					url: href,
					style: "primary"
				}]
			});
			break;
		}
		case "quote": {
			const text = getTextContent(node.children);
			blocks.push({
				type: "section",
				text: mrkdwn(`> ${text}`)
			});
			break;
		}
	}
	return blocks;
}
const slackRenderer = { async render(node, _options) {
	const blocks = nodeToBlocks(node);
	return JSON.stringify({ blocks }, null, 2);
} };

//#endregion
export { slackRenderer };
//# sourceMappingURL=slack-BI3EQwYm.js.map