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

//#region src/renderers/google-chat.ts
/**
* Google Chat renderer — outputs Card V2 JSON for Google Chat API.
* Cards can be sent via webhooks, Chat API, or Apps Script.
*/
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 nodeToWidgets(node) {
	const p = node.props;
	const widgets = [];
	switch (node.type) {
		case "document":
		case "page":
		case "section":
		case "row":
		case "column":
			for (const child of node.children) if (typeof child !== "string") widgets.push(...nodeToWidgets(child));
			break;
		case "heading": {
			const text = getTextContent(node.children);
			widgets.push({ decoratedText: {
				topLabel: "",
				text: `<b>${text}</b>`,
				wrapText: true
			} });
			break;
		}
		case "text": {
			let text = getTextContent(node.children);
			if (p.bold) text = `<b>${text}</b>`;
			if (p.italic) text = `<i>${text}</i>`;
			if (p.strikethrough) text = `<s>${text}</s>`;
			widgets.push({ textParagraph: { text } });
			break;
		}
		case "link": {
			const href = sanitizeHref(p.href);
			const text = getTextContent(node.children);
			widgets.push({ textParagraph: { text: `<a href="${href}">${text}</a>` } });
			break;
		}
		case "image": {
			const src = sanitizeImageSrc(p.src);
			if (src.startsWith("http")) widgets.push({ image: {
				imageUrl: src,
				altText: p.alt ?? "Image"
			} });
			break;
		}
		case "table": {
			const columns = (p.columns ?? []).map(resolveColumn);
			const rows = p.rows ?? [];
			const header = columns.map((c) => `<b>${c.header}</b>`).join(" | ");
			const body = rows.map((row) => row.map((c) => String(c ?? "")).join(" | ")).join("\n");
			widgets.push({ textParagraph: { text: `${header}\n${body}` } });
			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");
			widgets.push({ textParagraph: { text: items } });
			break;
		}
		case "code": {
			const text = getTextContent(node.children);
			widgets.push({ textParagraph: { text: `<font color="#333333"><code>${text}</code></font>` } });
			break;
		}
		case "divider":
		case "page-break":
			widgets.push({ divider: {} });
			break;
		case "spacer": break;
		case "button": {
			const href = sanitizeHref(p.href);
			const text = getTextContent(node.children);
			widgets.push({ buttonList: { buttons: [{
				text,
				onClick: { openLink: { url: href } },
				color: {
					red: .31,
					green: .27,
					blue: .89,
					alpha: 1
				}
			}] } });
			break;
		}
		case "quote": {
			const text = getTextContent(node.children);
			widgets.push({ textParagraph: { text: `<i>"${text}"</i>` } });
			break;
		}
	}
	return widgets;
}
const googleChatRenderer = { async render(node, _options) {
	const widgets = nodeToWidgets(node);
	let title = node.props.title ?? "";
	if (!title) {
		const firstHeading = node.children.find((c) => typeof c !== "string" && c.type === "heading");
		if (firstHeading) title = getTextContent(firstHeading.children);
	}
	const card = { cardsV2: [{
		cardId: "document",
		card: {
			header: title ? {
				title,
				subtitle: node.props.subject ?? void 0
			} : void 0,
			sections: [{ widgets }]
		}
	}] };
	return JSON.stringify(card, null, 2);
} };

//#endregion
export { googleChatRenderer };
//# sourceMappingURL=google-chat-CkKCBUWC.js.map