//#region src/renderers/csv.ts
function resolveColumn(col) {
	return typeof col === "string" ? { header: col } : col;
}
function escapeCsv(value) {
	if (value.includes(",") || value.includes("\"") || value.includes("\n")) return `"${value.replace(/"/g, "\"\"")}"`;
	return value;
}
function findTables(node) {
	const tables = [];
	if (node.type === "table") tables.push(node);
	for (const child of node.children) if (typeof child !== "string") tables.push(...findTables(child));
	return tables;
}
function tableToCsv(node) {
	const columns = (node.props.columns ?? []).map(resolveColumn);
	const rows = node.props.rows ?? [];
	const lines = [];
	if (node.props.caption) lines.push(`# ${node.props.caption}`);
	lines.push(columns.map((c) => escapeCsv(c.header)).join(","));
	for (const row of rows) lines.push(row.map((cell) => escapeCsv(String(cell ?? ""))).join(","));
	return lines.join("\n");
}
const csvRenderer = { async render(node, _options) {
	const tables = findTables(node);
	if (tables.length === 0) return "# No tables found in document\n";
	return `${tables.map(tableToCsv).join("\n\n")}\n`;
} };

//#endregion
export { csvRenderer };
//# sourceMappingURL=csv-COrS4qdy.js.map