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