import { markdownLineEnding as e, markdownLineEndingOrSpace as t, markdownSpace as n } from "../../micromark-util-character/index.js";
import { factorySpace as r } from "../../micromark-factory-space/index.js";
import { EditMap as i } from "./edit-map.js";
import { gfmTableAlign as a } from "./infer.js";
//#region ../../node_modules/micromark-extension-gfm-table/lib/syntax.js
function o() {
	return { flow: { null: {
		name: "table",
		tokenize: s,
		resolveAll: c
	} } };
}
function s(i, a, o) {
	let s = this, c = 0, l = 0, u;
	return d;
	function d(e) {
		let t = s.events.length - 1;
		for (; t > -1;) {
			let e = s.events[t][1].type;
			if (e === "lineEnding" || e === "linePrefix") t--;
			else break;
		}
		let n = t > -1 ? s.events[t][1].type : null, r = n === "tableHead" || n === "tableRow" ? E : f;
		return r === E && s.parser.lazy[s.now().line] ? o(e) : r(e);
	}
	function f(e) {
		return i.enter("tableHead"), i.enter("tableRow"), p(e);
	}
	function p(e) {
		return e === 124 ? m(e) : (u = !0, l += 1, m(e));
	}
	function m(t) {
		return t === null ? o(t) : e(t) ? l > 1 ? (l = 0, s.interrupt = !0, i.exit("tableRow"), i.enter("lineEnding"), i.consume(t), i.exit("lineEnding"), _) : o(t) : n(t) ? r(i, m, "whitespace")(t) : (l += 1, u && (u = !1, c += 1), t === 124 ? (i.enter("tableCellDivider"), i.consume(t), i.exit("tableCellDivider"), u = !0, m) : (i.enter("data"), h(t)));
	}
	function h(e) {
		return e === null || e === 124 || t(e) ? (i.exit("data"), m(e)) : (i.consume(e), e === 92 ? g : h);
	}
	function g(e) {
		return e === 92 || e === 124 ? (i.consume(e), h) : h(e);
	}
	function _(e) {
		return s.interrupt = !1, s.parser.lazy[s.now().line] ? o(e) : (i.enter("tableDelimiterRow"), u = !1, n(e) ? r(i, v, "linePrefix", s.parser.constructs.disable.null.includes("codeIndented") ? void 0 : 4)(e) : v(e));
	}
	function v(e) {
		return e === 45 || e === 58 ? b(e) : e === 124 ? (u = !0, i.enter("tableCellDivider"), i.consume(e), i.exit("tableCellDivider"), y) : T(e);
	}
	function y(e) {
		return n(e) ? r(i, b, "whitespace")(e) : b(e);
	}
	function b(t) {
		return t === 58 ? (l += 1, u = !0, i.enter("tableDelimiterMarker"), i.consume(t), i.exit("tableDelimiterMarker"), x) : t === 45 ? (l += 1, x(t)) : t === null || e(t) ? w(t) : T(t);
	}
	function x(e) {
		return e === 45 ? (i.enter("tableDelimiterFiller"), S(e)) : T(e);
	}
	function S(e) {
		return e === 45 ? (i.consume(e), S) : e === 58 ? (u = !0, i.exit("tableDelimiterFiller"), i.enter("tableDelimiterMarker"), i.consume(e), i.exit("tableDelimiterMarker"), C) : (i.exit("tableDelimiterFiller"), C(e));
	}
	function C(e) {
		return n(e) ? r(i, w, "whitespace")(e) : w(e);
	}
	function w(t) {
		return t === 124 ? v(t) : t === null || e(t) ? !u || c !== l ? T(t) : (i.exit("tableDelimiterRow"), i.exit("tableHead"), a(t)) : T(t);
	}
	function T(e) {
		return o(e);
	}
	function E(e) {
		return i.enter("tableRow"), D(e);
	}
	function D(t) {
		return t === 124 ? (i.enter("tableCellDivider"), i.consume(t), i.exit("tableCellDivider"), D) : t === null || e(t) ? (i.exit("tableRow"), a(t)) : n(t) ? r(i, D, "whitespace")(t) : (i.enter("data"), O(t));
	}
	function O(e) {
		return e === null || e === 124 || t(e) ? (i.exit("data"), D(e)) : (i.consume(e), e === 92 ? k : O);
	}
	function k(e) {
		return e === 92 || e === 124 ? (i.consume(e), O) : O(e);
	}
}
function c(e, t) {
	let n = -1, r = !0, o = 0, s = [
		0,
		0,
		0,
		0
	], c = [
		0,
		0,
		0,
		0
	], d = !1, f = 0, p, m, h, g = new i();
	for (; ++n < e.length;) {
		let i = e[n], a = i[1];
		i[0] === "enter" ? a.type === "tableHead" ? (d = !1, f !== 0 && (u(g, t, f, p, m), m = void 0, f = 0), p = {
			type: "table",
			start: Object.assign({}, a.start),
			end: Object.assign({}, a.end)
		}, g.add(n, 0, [[
			"enter",
			p,
			t
		]])) : a.type === "tableRow" || a.type === "tableDelimiterRow" ? (r = !0, h = void 0, s = [
			0,
			0,
			0,
			0
		], c = [
			0,
			n + 1,
			0,
			0
		], d && (d = !1, m = {
			type: "tableBody",
			start: Object.assign({}, a.start),
			end: Object.assign({}, a.end)
		}, g.add(n, 0, [[
			"enter",
			m,
			t
		]])), o = a.type === "tableDelimiterRow" ? 2 : m ? 3 : 1) : o && (a.type === "data" || a.type === "tableDelimiterMarker" || a.type === "tableDelimiterFiller") ? (r = !1, c[2] === 0 && (s[1] !== 0 && (c[0] = c[1], h = l(g, t, s, o, void 0, h), s = [
			0,
			0,
			0,
			0
		]), c[2] = n)) : a.type === "tableCellDivider" && (r ? r = !1 : (s[1] !== 0 && (c[0] = c[1], h = l(g, t, s, o, void 0, h)), s = c, c = [
			s[1],
			n,
			0,
			0
		])) : a.type === "tableHead" ? (d = !0, f = n) : a.type === "tableRow" || a.type === "tableDelimiterRow" ? (f = n, s[1] === 0 ? c[1] !== 0 && (h = l(g, t, c, o, n, h)) : (c[0] = c[1], h = l(g, t, s, o, n, h)), o = 0) : o && (a.type === "data" || a.type === "tableDelimiterMarker" || a.type === "tableDelimiterFiller") && (c[3] = n);
	}
	for (f !== 0 && u(g, t, f, p, m), g.consume(t.events), n = -1; ++n < t.events.length;) {
		let e = t.events[n];
		e[0] === "enter" && e[1].type === "table" && (e[1]._align = a(t.events, n));
	}
	return e;
}
function l(e, t, n, r, i, a) {
	let o = r === 1 ? "tableHeader" : r === 2 ? "tableDelimiter" : "tableData";
	n[0] !== 0 && (a.end = Object.assign({}, d(t.events, n[0])), e.add(n[0], 0, [[
		"exit",
		a,
		t
	]]));
	let s = d(t.events, n[1]);
	if (a = {
		type: o,
		start: Object.assign({}, s),
		end: Object.assign({}, s)
	}, e.add(n[1], 0, [[
		"enter",
		a,
		t
	]]), n[2] !== 0) {
		let i = d(t.events, n[2]), a = d(t.events, n[3]), o = {
			type: "tableContent",
			start: Object.assign({}, i),
			end: Object.assign({}, a)
		};
		if (e.add(n[2], 0, [[
			"enter",
			o,
			t
		]]), r !== 2) {
			let r = t.events[n[2]], i = t.events[n[3]];
			if (r[1].end = Object.assign({}, i[1].end), r[1].type = "chunkText", r[1].contentType = "text", n[3] > n[2] + 1) {
				let t = n[2] + 1, r = n[3] - n[2] - 1;
				e.add(t, r, []);
			}
		}
		e.add(n[3] + 1, 0, [[
			"exit",
			o,
			t
		]]);
	}
	return i !== void 0 && (a.end = Object.assign({}, d(t.events, i)), e.add(i, 0, [[
		"exit",
		a,
		t
	]]), a = void 0), a;
}
function u(e, t, n, r, i) {
	let a = [], o = d(t.events, n);
	i && (i.end = Object.assign({}, o), a.push([
		"exit",
		i,
		t
	])), r.end = Object.assign({}, o), a.push([
		"exit",
		r,
		t
	]), e.add(n + 1, 0, a);
}
function d(e, t) {
	let n = e[t], r = n[0] === "enter" ? "start" : "end";
	return n[1][r];
}
//#endregion
export { o as gfmTable };
