import { markdownLineEnding as e, markdownSpace as t } from "../../micromark-util-character/index.js";
import { factorySpace as n } from "../../micromark-factory-space/index.js";
//#region ../../node_modules/micromark-core-commonmark/lib/setext-underline.js
var r = {
	name: "setextUnderline",
	resolveTo: i,
	tokenize: a
};
function i(e, t) {
	let n = e.length, r, i, a;
	for (; n--;) if (e[n][0] === "enter") {
		if (e[n][1].type === "content") {
			r = n;
			break;
		}
		e[n][1].type === "paragraph" && (i = n);
	} else e[n][1].type === "content" && e.splice(n, 1), !a && e[n][1].type === "definition" && (a = n);
	let o = {
		type: "setextHeading",
		start: { ...e[r][1].start },
		end: { ...e[e.length - 1][1].end }
	};
	return e[i][1].type = "setextHeadingText", a ? (e.splice(i, 0, [
		"enter",
		o,
		t
	]), e.splice(a + 1, 0, [
		"exit",
		e[r][1],
		t
	]), e[r][1].end = { ...e[a][1].end }) : e[r][1] = o, e.push([
		"exit",
		o,
		t
	]), e;
}
function a(r, i, a) {
	let o = this, s;
	return c;
	function c(e) {
		let t = o.events.length, n;
		for (; t--;) if (o.events[t][1].type !== "lineEnding" && o.events[t][1].type !== "linePrefix" && o.events[t][1].type !== "content") {
			n = o.events[t][1].type === "paragraph";
			break;
		}
		return !o.parser.lazy[o.now().line] && (o.interrupt || n) ? (r.enter("setextHeadingLine"), s = e, l(e)) : a(e);
	}
	function l(e) {
		return r.enter("setextHeadingLineSequence"), u(e);
	}
	function u(e) {
		return e === s ? (r.consume(e), u) : (r.exit("setextHeadingLineSequence"), t(e) ? n(r, d, "lineSuffix")(e) : d(e));
	}
	function d(t) {
		return t === null || e(t) ? (r.exit("setextHeadingLine"), i(t)) : a(t);
	}
}
//#endregion
export { r as setextUnderline };
