import { encodeCharacterReference as e } from "../util/encode-character-reference.js";
import { formatHeadingAsSetext as t } from "../util/format-heading-as-setext.js";
//#region ../../node_modules/mdast-util-to-markdown/lib/handle/heading.js
function n(n, r, i, a) {
	let o = Math.max(Math.min(6, n.depth || 1), 1), s = i.createTracker(a);
	if (t(n, i)) {
		let e = i.enter("headingSetext"), t = i.enter("phrasing"), r = i.containerPhrasing(n, {
			...s.current(),
			before: "\n",
			after: "\n"
		});
		return t(), e(), r + "\n" + (o === 1 ? "=" : "-").repeat(r.length - (Math.max(r.lastIndexOf("\r"), r.lastIndexOf("\n")) + 1));
	}
	let c = "#".repeat(o), l = i.enter("headingAtx"), u = i.enter("phrasing");
	s.move(c + " ");
	let d = i.containerPhrasing(n, {
		before: "# ",
		after: "\n",
		...s.current()
	});
	return /^[\t ]/.test(d) && (d = e(d.charCodeAt(0)) + d.slice(1)), d = d ? c + " " + d : c, i.options.closeAtx && (d += " " + c), u(), l(), d;
}
//#endregion
export { n as heading };
