import { markdownToIR } from "../../../src/markdown/ir.js";
import { renderMarkdownWithMarkers } from "../../../src/markdown/render.js";
import "../../../src/markdown/index.js";
//#region extensions/feishu/src/format.ts
/**
* Feishu / Lark outbound text formatting.
*
* Agent output is often Markdown. Feishu card markdown (`tag: markdown`) expects
* Lark-flavored Markdown (bold/italic/strikethrough, links, code fences, etc.), not
* CommonMark-only constructs like `**bold**` left uninterpreted in plain `text`
* messages — and plain text messages do not render Markdown at all.
*
* Pipeline mirrors Telegram: Markdown → shared IR → channel-specific rendering.
*/
const FEISHU_STYLE_MARKERS = {
	bold: {
		open: "**",
		close: "**"
	},
	italic: {
		open: "*",
		close: "*"
	},
	strikethrough: {
		open: "~~",
		close: "~~"
	},
	code: {
		open: "`",
		close: "`"
	},
	code_block: {
		open: "```\n",
		close: "\n```"
	},
	blockquote: {
		open: "> ",
		close: ""
	}
};
function escapeFeishuMarkdownPlain(text) {
	return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\*/g, "&#42;").replace(/_/g, "&#95;").replace(/~/g, "&#126;").replace(/\[/g, "&#91;").replace(/\]/g, "&#93;").replace(/`/g, "&#96;");
}
function buildFeishuLinkRenderer(_irText) {
	return (link, _text) => {
		const href = link.href.trim();
		if (!href) return null;
		if (!/^(https?|mailto):/i.test(href) && !href.startsWith("#")) return null;
		if (_text.trim() === href) return {
			start: link.start,
			end: link.end,
			open: "<",
			close: `>`
		};
		return {
			start: link.start,
			end: link.end,
			open: "[",
			close: `](${href})`
		};
	};
}
function renderFeishuCardMarkdown(markdown, options = {}) {
	const ir = markdownToIR(markdown, {
		linkify: true,
		enableSpoilers: false,
		tableMode: options.tableMode ?? "bullets"
	});
	return renderMarkdownWithMarkers(ir, {
		styleMarkers: FEISHU_STYLE_MARKERS,
		escapeText: escapeFeishuMarkdownPlain,
		buildLink: buildFeishuLinkRenderer(ir.text)
	});
}
function markdownToFeishuPlainText(markdown) {
	const ir = markdownToIR(markdown, {
		linkify: true,
		enableSpoilers: false,
		tableMode: "bullets"
	});
	let result = ir.text;
	const sortedLinks = [...ir.links].toSorted((a, b) => b.start - a.start);
	for (const link of sortedLinks) {
		const replacement = `${ir.text.slice(link.start, link.end)} (${link.href})`;
		result = result.slice(0, link.start) + replacement + result.slice(link.end);
	}
	return result;
}
function formatFeishuOutboundText(input) {
	const raw = input.text ?? "";
	if (!raw.trim()) return raw;
	if (input.renderMode === "raw") return raw;
	if (input.forCardMarkdown) return renderFeishuCardMarkdown(raw);
	return markdownToFeishuPlainText(raw);
}
//#endregion
export { formatFeishuOutboundText, markdownToFeishuPlainText, renderFeishuCardMarkdown };

//# sourceMappingURL=format.js.map