import { markdownLineEnding as e } from "../../../micromark-util-character/index.js";
import { splice as t } from "../../../micromark-util-chunked/index.js";
import { factorySpace as n } from "../../../micromark-factory-space/index.js";
//#region ../../node_modules/micromark/lib/initialize/document.js
var r = { tokenize: a }, i = { tokenize: o };
function a(n) {
	let r = this, a = [], o = 0, s, c, l;
	return u;
	function u(e) {
		if (o < a.length) {
			let t = a[o];
			return r.containerState = t[1], n.attempt(t[0].continuation, d, f)(e);
		}
		return f(e);
	}
	function d(e) {
		if (o++, r.containerState._closeFlow) {
			r.containerState._closeFlow = void 0, s && x();
			let n = r.events.length, i = n, a;
			for (; i--;) if (r.events[i][0] === "exit" && r.events[i][1].type === "chunkFlow") {
				a = r.events[i][1].end;
				break;
			}
			b(o);
			let c = n;
			for (; c < r.events.length;) r.events[c][1].end = { ...a }, c++;
			return t(r.events, i + 1, 0, r.events.slice(n)), r.events.length = c, f(e);
		}
		return u(e);
	}
	function f(e) {
		if (o === a.length) {
			if (!s) return h(e);
			if (s.currentConstruct && s.currentConstruct.concrete) return _(e);
			r.interrupt = !!(s.currentConstruct && !s._gfmTableDynamicInterruptHack);
		}
		return r.containerState = {}, n.check(i, p, m)(e);
	}
	function p(e) {
		return s && x(), b(o), h(e);
	}
	function m(e) {
		return r.parser.lazy[r.now().line] = o !== a.length, l = r.now().offset, _(e);
	}
	function h(e) {
		return r.containerState = {}, n.attempt(i, g, _)(e);
	}
	function g(e) {
		return o++, a.push([r.currentConstruct, r.containerState]), h(e);
	}
	function _(e) {
		if (e === null) {
			s && x(), b(0), n.consume(e);
			return;
		}
		return s ||= r.parser.flow(r.now()), n.enter("chunkFlow", {
			_tokenizer: s,
			contentType: "flow",
			previous: c
		}), v(e);
	}
	function v(t) {
		if (t === null) {
			y(n.exit("chunkFlow"), !0), b(0), n.consume(t);
			return;
		}
		return e(t) ? (n.consume(t), y(n.exit("chunkFlow")), o = 0, r.interrupt = void 0, u) : (n.consume(t), v);
	}
	function y(e, n) {
		let i = r.sliceStream(e);
		if (n && i.push(null), e.previous = c, c && (c.next = e), c = e, s.defineSkip(e.start), s.write(i), r.parser.lazy[e.start.line]) {
			let e = s.events.length;
			for (; e--;) if (s.events[e][1].start.offset < l && (!s.events[e][1].end || s.events[e][1].end.offset > l)) return;
			let n = r.events.length, i = n, a, c;
			for (; i--;) if (r.events[i][0] === "exit" && r.events[i][1].type === "chunkFlow") {
				if (a) {
					c = r.events[i][1].end;
					break;
				}
				a = !0;
			}
			for (b(o), e = n; e < r.events.length;) r.events[e][1].end = { ...c }, e++;
			t(r.events, i + 1, 0, r.events.slice(n)), r.events.length = e;
		}
	}
	function b(e) {
		let t = a.length;
		for (; t-- > e;) {
			let e = a[t];
			r.containerState = e[1], e[0].exit.call(r, n);
		}
		a.length = e;
	}
	function x() {
		s.write([null]), c = void 0, s = void 0, r.containerState._closeFlow = void 0;
	}
}
function o(e, t, r) {
	return n(e, e.attempt(this.parser.constructs.document, t, r), "linePrefix", this.parser.constructs.disable.null.includes("codeIndented") ? void 0 : 4);
}
//#endregion
export { r as document };
