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

//#region src/renderers/confluence.ts
/**
* Atlassian Document Format (ADF) renderer — for Jira and Confluence.
* ADF is the JSON format used by Atlassian's Document API.
* Can be posted to Confluence pages, Jira issue descriptions, and comments.
*/
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 textNode(text, marks) {
	return {
		type: "text",
		text,
		...marks && marks.length > 0 ? { marks } : {}
	};
}
function nodeToAdf(node) {
	const p = node.props;
	const result = [];
	switch (node.type) {
		case "document":
		case "page":
		case "section":
		case "row":
		case "column":
			for (const child of node.children) if (typeof child !== "string") result.push(...nodeToAdf(child));
			break;
		case "heading": {
			const level = Math.min(Math.max(p.level ?? 1, 1), 6);
			const text = getTextContent(node.children);
			result.push({
				type: "heading",
				attrs: { level },
				content: [textNode(text, [{ type: "strong" }])]
			});
			break;
		}
		case "text": {
			const text = getTextContent(node.children);
			const marks = [];
			if (p.bold) marks.push({ type: "strong" });
			if (p.italic) marks.push({ type: "em" });
			if (p.underline) marks.push({ type: "underline" });
			if (p.strikethrough) marks.push({ type: "strike" });
			if (p.color) marks.push({
				type: "textColor",
				attrs: { color: p.color }
			});
			result.push({
				type: "paragraph",
				content: [textNode(text, marks)]
			});
			break;
		}
		case "link": {
			const href = sanitizeHref(p.href);
			const text = getTextContent(node.children);
			result.push({
				type: "paragraph",
				content: [textNode(text, [{
					type: "link",
					attrs: { href }
				}])]
			});
			break;
		}
		case "image": {
			const src = sanitizeImageSrc(p.src);
			if (src.startsWith("http")) result.push({
				type: "mediaSingle",
				attrs: { layout: "center" },
				content: [{
					type: "media",
					attrs: {
						type: "external",
						url: src,
						width: p.width ?? void 0,
						height: p.height ?? void 0
					}
				}]
			});
			break;
		}
		case "table": {
			const columns = (p.columns ?? []).map(resolveColumn);
			const rows = p.rows ?? [];
			const headerRow = {
				type: "tableRow",
				content: columns.map((col) => ({
					type: "tableHeader",
					content: [{
						type: "paragraph",
						content: [textNode(col.header, [{ type: "strong" }])]
					}]
				}))
			};
			const dataRows = rows.map((row) => ({
				type: "tableRow",
				content: columns.map((_, i) => ({
					type: "tableCell",
					content: [{
						type: "paragraph",
						content: [textNode(String(row[i] ?? ""))]
					}]
				}))
			}));
			result.push({
				type: "table",
				attrs: {
					isNumberColumnEnabled: false,
					layout: "default"
				},
				content: [headerRow, ...dataRows]
			});
			break;
		}
		case "list": {
			const type = p.ordered ? "orderedList" : "bulletList";
			const items = node.children.filter((c) => typeof c !== "string").map((item) => ({
				type: "listItem",
				content: [{
					type: "paragraph",
					content: [textNode(getTextContent(item.children))]
				}]
			}));
			result.push({
				type,
				content: items
			});
			break;
		}
		case "code": {
			const text = getTextContent(node.children);
			const lang = p.language ?? null;
			result.push({
				type: "codeBlock",
				attrs: { language: lang },
				content: [textNode(text)]
			});
			break;
		}
		case "divider":
		case "page-break":
			result.push({ type: "rule" });
			break;
		case "spacer":
			result.push({
				type: "paragraph",
				content: []
			});
			break;
		case "button": {
			const href = sanitizeHref(p.href);
			const text = getTextContent(node.children);
			result.push({
				type: "paragraph",
				content: [textNode(text, [{
					type: "link",
					attrs: { href }
				}, { type: "strong" }])]
			});
			break;
		}
		case "quote": {
			const text = getTextContent(node.children);
			result.push({
				type: "blockquote",
				content: [{
					type: "paragraph",
					content: [textNode(text)]
				}]
			});
			break;
		}
	}
	return result;
}
const confluenceRenderer = { async render(node, _options) {
	const adf = {
		version: 1,
		type: "doc",
		content: nodeToAdf(node)
	};
	return JSON.stringify(adf, null, 2);
} };

//#endregion
export { confluenceRenderer };
//# sourceMappingURL=confluence-Bd3ua1Ut.js.map