import { markdownLineEndingOrSpace as e } from "../../micromark-util-character/index.js";
import { normalizeIdentifier as t } from "../../micromark-util-normalize-identifier/index.js";
import { push as n, splice as r } from "../../micromark-util-chunked/index.js";
import { resolveAll as i } from "../../micromark-util-resolve-all/index.js";
import { factoryDestination as a } from "../../micromark-factory-destination/index.js";
import { factoryLabel as o } from "../../micromark-factory-label/index.js";
import { factoryTitle as s } from "../../micromark-factory-title/index.js";
import { factoryWhitespace as c } from "../../micromark-factory-whitespace/index.js";
//#region ../../node_modules/micromark-core-commonmark/lib/label-end.js
var l = {
	name: "labelEnd",
	resolveAll: p,
	resolveTo: m,
	tokenize: h
}, u = { tokenize: g }, d = { tokenize: _ }, f = { tokenize: v };
function p(e) {
	let t = -1, n = [];
	for (; ++t < e.length;) {
		let r = e[t][1];
		if (n.push(e[t]), r.type === "labelImage" || r.type === "labelLink" || r.type === "labelEnd") {
			let e = r.type === "labelImage" ? 4 : 2;
			r.type = "data", t += e;
		}
	}
	return e.length !== n.length && r(e, 0, e.length, n), e;
}
function m(e, t) {
	let a = e.length, o = 0, s, c, l, u;
	for (; a--;) if (s = e[a][1], c) {
		if (s.type === "link" || s.type === "labelLink" && s._inactive) break;
		e[a][0] === "enter" && s.type === "labelLink" && (s._inactive = !0);
	} else if (l) {
		if (e[a][0] === "enter" && (s.type === "labelImage" || s.type === "labelLink") && !s._balanced && (c = a, s.type !== "labelLink")) {
			o = 2;
			break;
		}
	} else s.type === "labelEnd" && (l = a);
	let d = {
		type: e[c][1].type === "labelLink" ? "link" : "image",
		start: { ...e[c][1].start },
		end: { ...e[e.length - 1][1].end }
	}, f = {
		type: "label",
		start: { ...e[c][1].start },
		end: { ...e[l][1].end }
	}, p = {
		type: "labelText",
		start: { ...e[c + o + 2][1].end },
		end: { ...e[l - 2][1].start }
	};
	return u = [[
		"enter",
		d,
		t
	], [
		"enter",
		f,
		t
	]], u = n(u, e.slice(c + 1, c + o + 3)), u = n(u, [[
		"enter",
		p,
		t
	]]), u = n(u, i(t.parser.constructs.insideSpan.null, e.slice(c + o + 4, l - 3), t)), u = n(u, [
		[
			"exit",
			p,
			t
		],
		e[l - 2],
		e[l - 1],
		[
			"exit",
			f,
			t
		]
	]), u = n(u, e.slice(l + 1)), u = n(u, [[
		"exit",
		d,
		t
	]]), r(e, c, e.length, u), e;
}
function h(e, n, r) {
	let i = this, a = i.events.length, o, s;
	for (; a--;) if ((i.events[a][1].type === "labelImage" || i.events[a][1].type === "labelLink") && !i.events[a][1]._balanced) {
		o = i.events[a][1];
		break;
	}
	return c;
	function c(n) {
		return o ? o._inactive ? h(n) : (s = i.parser.defined.includes(t(i.sliceSerialize({
			start: o.end,
			end: i.now()
		}))), e.enter("labelEnd"), e.enter("labelMarker"), e.consume(n), e.exit("labelMarker"), e.exit("labelEnd"), l) : r(n);
	}
	function l(t) {
		return t === 40 ? e.attempt(u, m, s ? m : h)(t) : t === 91 ? e.attempt(d, m, s ? p : h)(t) : s ? m(t) : h(t);
	}
	function p(t) {
		return e.attempt(f, m, h)(t);
	}
	function m(e) {
		return n(e);
	}
	function h(e) {
		return o._balanced = !0, r(e);
	}
}
function g(t, n, r) {
	return i;
	function i(e) {
		return t.enter("resource"), t.enter("resourceMarker"), t.consume(e), t.exit("resourceMarker"), o;
	}
	function o(n) {
		return e(n) ? c(t, l)(n) : l(n);
	}
	function l(e) {
		return e === 41 ? m(e) : a(t, u, d, "resourceDestination", "resourceDestinationLiteral", "resourceDestinationLiteralMarker", "resourceDestinationRaw", "resourceDestinationString", 32)(e);
	}
	function u(n) {
		return e(n) ? c(t, f)(n) : m(n);
	}
	function d(e) {
		return r(e);
	}
	function f(e) {
		return e === 34 || e === 39 || e === 40 ? s(t, p, r, "resourceTitle", "resourceTitleMarker", "resourceTitleString")(e) : m(e);
	}
	function p(n) {
		return e(n) ? c(t, m)(n) : m(n);
	}
	function m(e) {
		return e === 41 ? (t.enter("resourceMarker"), t.consume(e), t.exit("resourceMarker"), t.exit("resource"), n) : r(e);
	}
}
function _(e, n, r) {
	let i = this;
	return a;
	function a(t) {
		return o.call(i, e, s, c, "reference", "referenceMarker", "referenceString")(t);
	}
	function s(e) {
		return i.parser.defined.includes(t(i.sliceSerialize(i.events[i.events.length - 1][1]).slice(1, -1))) ? n(e) : r(e);
	}
	function c(e) {
		return r(e);
	}
}
function v(e, t, n) {
	return r;
	function r(t) {
		return e.enter("reference"), e.enter("referenceMarker"), e.consume(t), e.exit("referenceMarker"), i;
	}
	function i(r) {
		return r === 93 ? (e.enter("referenceMarker"), e.consume(r), e.exit("referenceMarker"), e.exit("reference"), t) : n(r);
	}
}
//#endregion
export { l as labelEnd };
