import { normalizeIdentifier as e } from "../../micromark-util-normalize-identifier/index.js";
import { toString as t } from "../../mdast-util-to-string/lib/index.js";
import { decodeNamedCharacterReference as n } from "../../decode-named-character-reference/index.dom.js";
import { decodeNumericCharacterReference as r } from "../../micromark-util-decode-numeric-character-reference/index.js";
import { decodeString as i } from "../../micromark-util-decode-string/index.js";
import { stringifyPosition as a } from "../../unist-util-stringify-position/lib/index.js";
import { parse as o } from "../../micromark/lib/parse.js";
import { postprocess as s } from "../../micromark/lib/postprocess.js";
import { preprocess as c } from "../../micromark/lib/preprocess.js";
//#region ../../node_modules/mdast-util-from-markdown/lib/index.js
var l = {}.hasOwnProperty;
function u(e, t, n) {
	return t && typeof t == "object" && (n = t, t = void 0), d(n)(s(o(n).document().write(c()(e, t, !0))));
}
function d(o) {
	let s = {
		transforms: [],
		canContainEols: [
			"emphasis",
			"fragment",
			"heading",
			"paragraph",
			"strong"
		],
		enter: {
			autolink: m(Z),
			autolinkProtocol: D,
			autolinkEmail: D,
			atxHeading: m(J),
			blockQuote: m(le),
			characterEscape: D,
			characterReference: D,
			codeFenced: m(q),
			codeFencedFenceInfo: g,
			codeFencedFenceMeta: g,
			codeIndented: m(q, g),
			codeText: m(ue, g),
			codeTextData: D,
			data: D,
			codeFlowValue: D,
			definition: m(de),
			definitionDestinationString: g,
			definitionLabelString: g,
			definitionTitleString: g,
			emphasis: m(fe),
			hardBreakEscape: m(Y),
			hardBreakTrailing: m(Y),
			htmlFlow: m(X, g),
			htmlFlowData: D,
			htmlText: m(X, g),
			htmlTextData: D,
			image: m(pe),
			label: g,
			link: m(Z),
			listItem: m(me),
			listItemValue: S,
			listOrdered: m(Q, x),
			listUnordered: m(Q),
			paragraph: m(he),
			reference: V,
			referenceString: g,
			resourceDestinationString: g,
			resourceTitleString: g,
			setextHeading: m(J),
			strong: m(ge),
			thematicBreak: m(_e)
		},
		exit: {
			atxHeading: v(),
			atxHeadingSequence: C,
			autolink: v(),
			autolinkEmail: ce,
			autolinkProtocol: K,
			blockQuote: v(),
			characterEscapeValue: O,
			characterReferenceMarkerHexadecimal: U,
			characterReferenceMarkerNumeric: U,
			characterReferenceValue: W,
			characterReference: G,
			codeFenced: v(re),
			codeFencedFence: ne,
			codeFencedFenceInfo: ee,
			codeFencedFenceMeta: te,
			codeFlowValue: O,
			codeIndented: v(ie),
			codeText: v(N),
			codeTextData: O,
			data: O,
			definition: v(),
			definitionDestinationString: se,
			definitionLabelString: ae,
			definitionTitleString: oe,
			emphasis: v(),
			hardBreakEscape: v(A),
			hardBreakTrailing: v(A),
			htmlFlow: v(j),
			htmlFlowData: O,
			htmlText: v(M),
			htmlTextData: O,
			image: v(F),
			label: L,
			labelText: I,
			lineEnding: k,
			link: v(P),
			listItem: v(),
			listOrdered: v(),
			listUnordered: v(),
			paragraph: v(),
			referenceString: H,
			resourceDestinationString: R,
			resourceTitleString: z,
			resource: B,
			setextHeading: v(E),
			setextHeadingLineSequence: T,
			setextHeadingText: w,
			strong: v(),
			thematicBreak: v()
		}
	};
	p(s, (o || {}).mdastExtensions || []);
	let c = {};
	return u;
	function u(e) {
		let t = {
			type: "root",
			children: []
		}, n = {
			stack: [t],
			tokenStack: [],
			config: s,
			enter: _,
			exit: y,
			buffer: g,
			resume: b,
			data: c
		}, r = [], i = -1;
		for (; ++i < e.length;) (e[i][1].type === "listOrdered" || e[i][1].type === "listUnordered") && (e[i][0] === "enter" ? r.push(i) : i = d(e, r.pop(), i));
		for (i = -1; ++i < e.length;) {
			let t = s[e[i][0]];
			l.call(t, e[i][1].type) && t[e[i][1].type].call(Object.assign({ sliceSerialize: e[i][2].sliceSerialize }, n), e[i][1]);
		}
		if (n.tokenStack.length > 0) {
			let e = n.tokenStack[n.tokenStack.length - 1];
			(e[1] || h).call(n, void 0, e[0]);
		}
		for (t.position = {
			start: f(e.length > 0 ? e[0][1].start : {
				line: 1,
				column: 1,
				offset: 0
			}),
			end: f(e.length > 0 ? e[e.length - 2][1].end : {
				line: 1,
				column: 1,
				offset: 0
			})
		}, i = -1; ++i < s.transforms.length;) t = s.transforms[i](t) || t;
		return t;
	}
	function d(e, t, n) {
		let r = t - 1, i = -1, a = !1, o, s, c, l;
		for (; ++r <= n;) {
			let t = e[r];
			switch (t[1].type) {
				case "listUnordered":
				case "listOrdered":
				case "blockQuote":
					t[0] === "enter" ? i++ : i--, l = void 0;
					break;
				case "lineEndingBlank":
					t[0] === "enter" && (o && !l && !i && !c && (c = r), l = void 0);
					break;
				case "linePrefix":
				case "listItemValue":
				case "listItemMarker":
				case "listItemPrefix":
				case "listItemPrefixWhitespace": break;
				default: l = void 0;
			}
			if (!i && t[0] === "enter" && t[1].type === "listItemPrefix" || i === -1 && t[0] === "exit" && (t[1].type === "listUnordered" || t[1].type === "listOrdered")) {
				if (o) {
					let i = r;
					for (s = void 0; i--;) {
						let t = e[i];
						if (t[1].type === "lineEnding" || t[1].type === "lineEndingBlank") {
							if (t[0] === "exit") continue;
							s && (e[s][1].type = "lineEndingBlank", a = !0), t[1].type = "lineEnding", s = i;
						} else if (!(t[1].type === "linePrefix" || t[1].type === "blockQuotePrefix" || t[1].type === "blockQuotePrefixWhitespace" || t[1].type === "blockQuoteMarker" || t[1].type === "listItemIndent")) break;
					}
					c && (!s || c < s) && (o._spread = !0), o.end = Object.assign({}, s ? e[s][1].start : t[1].end), e.splice(s || r, 0, [
						"exit",
						o,
						t[2]
					]), r++, n++;
				}
				if (t[1].type === "listItemPrefix") {
					let i = {
						type: "listItem",
						_spread: !1,
						start: Object.assign({}, t[1].start),
						end: void 0
					};
					o = i, e.splice(r, 0, [
						"enter",
						i,
						t[2]
					]), r++, n++, c = void 0, l = !0;
				}
			}
		}
		return e[t][1]._spread = a, n;
	}
	function m(e, t) {
		return n;
		function n(n) {
			_.call(this, e(n), n), t && t.call(this, n);
		}
	}
	function g() {
		this.stack.push({
			type: "fragment",
			children: []
		});
	}
	function _(e, t, n) {
		this.stack[this.stack.length - 1].children.push(e), this.stack.push(e), this.tokenStack.push([t, n || void 0]), e.position = {
			start: f(t.start),
			end: void 0
		};
	}
	function v(e) {
		return t;
		function t(t) {
			e && e.call(this, t), y.call(this, t);
		}
	}
	function y(e, t) {
		let n = this.stack.pop(), r = this.tokenStack.pop();
		if (r) r[0].type !== e.type && (t ? t.call(this, e, r[0]) : (r[1] || h).call(this, e, r[0]));
		else throw Error("Cannot close `" + e.type + "` (" + a({
			start: e.start,
			end: e.end
		}) + "): it’s not open");
		n.position.end = f(e.end);
	}
	function b() {
		return t(this.stack.pop());
	}
	function x() {
		this.data.expectingFirstListItemValue = !0;
	}
	function S(e) {
		if (this.data.expectingFirstListItemValue) {
			let t = this.stack[this.stack.length - 2];
			t.start = Number.parseInt(this.sliceSerialize(e), 10), this.data.expectingFirstListItemValue = void 0;
		}
	}
	function ee() {
		let e = this.resume(), t = this.stack[this.stack.length - 1];
		t.lang = e;
	}
	function te() {
		let e = this.resume(), t = this.stack[this.stack.length - 1];
		t.meta = e;
	}
	function ne() {
		this.data.flowCodeInside || (this.buffer(), this.data.flowCodeInside = !0);
	}
	function re() {
		let e = this.resume(), t = this.stack[this.stack.length - 1];
		t.value = e.replace(/^(\r?\n|\r)|(\r?\n|\r)$/g, ""), this.data.flowCodeInside = void 0;
	}
	function ie() {
		let e = this.resume(), t = this.stack[this.stack.length - 1];
		t.value = e.replace(/(\r?\n|\r)$/g, "");
	}
	function ae(t) {
		let n = this.resume(), r = this.stack[this.stack.length - 1];
		r.label = n, r.identifier = e(this.sliceSerialize(t)).toLowerCase();
	}
	function oe() {
		let e = this.resume(), t = this.stack[this.stack.length - 1];
		t.title = e;
	}
	function se() {
		let e = this.resume(), t = this.stack[this.stack.length - 1];
		t.url = e;
	}
	function C(e) {
		let t = this.stack[this.stack.length - 1];
		t.depth ||= this.sliceSerialize(e).length;
	}
	function w() {
		this.data.setextHeadingSlurpLineEnding = !0;
	}
	function T(e) {
		let t = this.stack[this.stack.length - 1];
		t.depth = this.sliceSerialize(e).codePointAt(0) === 61 ? 1 : 2;
	}
	function E() {
		this.data.setextHeadingSlurpLineEnding = void 0;
	}
	function D(e) {
		let t = this.stack[this.stack.length - 1].children, n = t[t.length - 1];
		(!n || n.type !== "text") && (n = $(), n.position = {
			start: f(e.start),
			end: void 0
		}, t.push(n)), this.stack.push(n);
	}
	function O(e) {
		let t = this.stack.pop();
		t.value += this.sliceSerialize(e), t.position.end = f(e.end);
	}
	function k(e) {
		let t = this.stack[this.stack.length - 1];
		if (this.data.atHardBreak) {
			let n = t.children[t.children.length - 1];
			n.position.end = f(e.end), this.data.atHardBreak = void 0;
			return;
		}
		!this.data.setextHeadingSlurpLineEnding && s.canContainEols.includes(t.type) && (D.call(this, e), O.call(this, e));
	}
	function A() {
		this.data.atHardBreak = !0;
	}
	function j() {
		let e = this.resume(), t = this.stack[this.stack.length - 1];
		t.value = e;
	}
	function M() {
		let e = this.resume(), t = this.stack[this.stack.length - 1];
		t.value = e;
	}
	function N() {
		let e = this.resume(), t = this.stack[this.stack.length - 1];
		t.value = e;
	}
	function P() {
		let e = this.stack[this.stack.length - 1];
		if (this.data.inReference) {
			let t = this.data.referenceType || "shortcut";
			e.type += "Reference", e.referenceType = t, delete e.url, delete e.title;
		} else delete e.identifier, delete e.label;
		this.data.referenceType = void 0;
	}
	function F() {
		let e = this.stack[this.stack.length - 1];
		if (this.data.inReference) {
			let t = this.data.referenceType || "shortcut";
			e.type += "Reference", e.referenceType = t, delete e.url, delete e.title;
		} else delete e.identifier, delete e.label;
		this.data.referenceType = void 0;
	}
	function I(t) {
		let n = this.sliceSerialize(t), r = this.stack[this.stack.length - 2];
		r.label = i(n), r.identifier = e(n).toLowerCase();
	}
	function L() {
		let e = this.stack[this.stack.length - 1], t = this.resume(), n = this.stack[this.stack.length - 1];
		this.data.inReference = !0, n.type === "link" ? n.children = e.children : n.alt = t;
	}
	function R() {
		let e = this.resume(), t = this.stack[this.stack.length - 1];
		t.url = e;
	}
	function z() {
		let e = this.resume(), t = this.stack[this.stack.length - 1];
		t.title = e;
	}
	function B() {
		this.data.inReference = void 0;
	}
	function V() {
		this.data.referenceType = "collapsed";
	}
	function H(t) {
		let n = this.resume(), r = this.stack[this.stack.length - 1];
		r.label = n, r.identifier = e(this.sliceSerialize(t)).toLowerCase(), this.data.referenceType = "full";
	}
	function U(e) {
		this.data.characterReferenceType = e.type;
	}
	function W(e) {
		let t = this.sliceSerialize(e), i = this.data.characterReferenceType, a;
		i ? (a = r(t, i === "characterReferenceMarkerNumeric" ? 10 : 16), this.data.characterReferenceType = void 0) : a = n(t);
		let o = this.stack[this.stack.length - 1];
		o.value += a;
	}
	function G(e) {
		let t = this.stack.pop();
		t.position.end = f(e.end);
	}
	function K(e) {
		O.call(this, e);
		let t = this.stack[this.stack.length - 1];
		t.url = this.sliceSerialize(e);
	}
	function ce(e) {
		O.call(this, e);
		let t = this.stack[this.stack.length - 1];
		t.url = "mailto:" + this.sliceSerialize(e);
	}
	function le() {
		return {
			type: "blockquote",
			children: []
		};
	}
	function q() {
		return {
			type: "code",
			lang: null,
			meta: null,
			value: ""
		};
	}
	function ue() {
		return {
			type: "inlineCode",
			value: ""
		};
	}
	function de() {
		return {
			type: "definition",
			identifier: "",
			label: null,
			title: null,
			url: ""
		};
	}
	function fe() {
		return {
			type: "emphasis",
			children: []
		};
	}
	function J() {
		return {
			type: "heading",
			depth: 0,
			children: []
		};
	}
	function Y() {
		return { type: "break" };
	}
	function X() {
		return {
			type: "html",
			value: ""
		};
	}
	function pe() {
		return {
			type: "image",
			title: null,
			url: "",
			alt: null
		};
	}
	function Z() {
		return {
			type: "link",
			title: null,
			url: "",
			children: []
		};
	}
	function Q(e) {
		return {
			type: "list",
			ordered: e.type === "listOrdered",
			start: null,
			spread: e._spread,
			children: []
		};
	}
	function me(e) {
		return {
			type: "listItem",
			spread: e._spread,
			checked: null,
			children: []
		};
	}
	function he() {
		return {
			type: "paragraph",
			children: []
		};
	}
	function ge() {
		return {
			type: "strong",
			children: []
		};
	}
	function $() {
		return {
			type: "text",
			value: ""
		};
	}
	function _e() {
		return { type: "thematicBreak" };
	}
}
function f(e) {
	return {
		line: e.line,
		column: e.column,
		offset: e.offset
	};
}
function p(e, t) {
	let n = -1;
	for (; ++n < t.length;) {
		let r = t[n];
		Array.isArray(r) ? p(e, r) : m(e, r);
	}
}
function m(e, t) {
	let n;
	for (n in t) if (l.call(t, n)) switch (n) {
		case "canContainEols": {
			let r = t[n];
			r && e[n].push(...r);
			break;
		}
		case "transforms": {
			let r = t[n];
			r && e[n].push(...r);
			break;
		}
		case "enter":
		case "exit": {
			let r = t[n];
			r && Object.assign(e[n], r);
			break;
		}
	}
}
function h(e, t) {
	throw Error(e ? "Cannot close `" + e.type + "` (" + a({
		start: e.start,
		end: e.end
	}) + "): a different token (`" + t.type + "`, " + a({
		start: t.start,
		end: t.end
	}) + ") is open" : "Cannot close document, a token (`" + t.type + "`, " + a({
		start: t.start,
		end: t.end
	}) + ") is still open");
}
//#endregion
export { u as fromMarkdown };
