import { longestStreak } from "../../../longest-streak/index.js";
import { formatCodeAsIndented } from "../util/format-code-as-indented.js";
import { checkFence } from "../util/check-fence.js";
function code(i, a, o, s) {
	let c = checkFence(o), l = i.value || "", u = c === "`" ? "GraveAccent" : "Tilde";
	if (formatCodeAsIndented(i, o)) {
		let e = o.enter("codeIndented"), n = o.indentLines(l, map);
		return e(), n;
	}
	let d = o.createTracker(s), f = c.repeat(Math.max(longestStreak(l, c) + 1, 3)), p = o.enter("codeFenced"), m = d.move(f);
	if (i.lang) {
		let e = o.enter(`codeFencedLang${u}`);
		m += d.move(o.safe(i.lang, {
			before: m,
			after: " ",
			encode: ["`"],
			...d.current()
		})), e();
	}
	if (i.lang && i.meta) {
		let e = o.enter(`codeFencedMeta${u}`);
		m += d.move(" "), m += d.move(o.safe(i.meta, {
			before: m,
			after: "\n",
			encode: ["`"],
			...d.current()
		})), e();
	}
	return m += d.move("\n"), l && (m += d.move(l + "\n")), m += d.move(f), p(), m;
}
function map(e, n, r) {
	return (r ? "" : "    ") + e;
}
export { code };
