import { asciiDigit, markdownSpace } from "../../micromark-util-character/index.js";
import { factorySpace } from "../../micromark-factory-space/index.js";
import { blankLine } from "./blank-line.js";
import { thematicBreak } from "./thematic-break.js";
const list = {
	continuation: { tokenize: tokenizeListContinuation },
	exit: tokenizeListEnd,
	name: "list",
	tokenize: tokenizeListStart
};
var listItemPrefixWhitespaceConstruct = {
	partial: !0,
	tokenize: tokenizeListItemPrefixWhitespace
}, indentConstruct = {
	partial: !0,
	tokenize: tokenizeIndent
};
function tokenizeListStart(n, a, s) {
	let c = this, l = c.events[c.events.length - 1], u = l && l[1].type === "linePrefix" ? l[2].sliceSerialize(l[1], !0).length : 0, d = 0;
	return f;
	function f(t) {
		let r = c.containerState.type || (t === 42 || t === 43 || t === 45 ? "listUnordered" : "listOrdered");
		if (r === "listUnordered" ? !c.containerState.marker || t === c.containerState.marker : asciiDigit(t)) {
			if (c.containerState.type || (c.containerState.type = r, n.enter(r, { _container: !0 })), r === "listUnordered") return n.enter("listItemPrefix"), t === 42 || t === 45 ? n.check(thematicBreak, s, m)(t) : m(t);
			if (!c.interrupt || t === 49) return n.enter("listItemPrefix"), n.enter("listItemValue"), p(t);
		}
		return s(t);
	}
	function p(t) {
		return asciiDigit(t) && ++d < 10 ? (n.consume(t), p) : (!c.interrupt || d < 2) && (c.containerState.marker ? t === c.containerState.marker : t === 41 || t === 46) ? (n.exit("listItemValue"), m(t)) : s(t);
	}
	function m(e) {
		return n.enter("listItemMarker"), n.consume(e), n.exit("listItemMarker"), c.containerState.marker = c.containerState.marker || e, n.check(blankLine, c.interrupt ? s : h, n.attempt(listItemPrefixWhitespaceConstruct, _, g));
	}
	function h(e) {
		return c.containerState.initialBlankLine = !0, u++, _(e);
	}
	function g(e) {
		return markdownSpace(e) ? (n.enter("listItemPrefixWhitespace"), n.consume(e), n.exit("listItemPrefixWhitespace"), _) : s(e);
	}
	function _(e) {
		return c.containerState.size = u + c.sliceSerialize(n.exit("listItemPrefix"), !0).length, a(e);
	}
}
function tokenizeListContinuation(e, i, o) {
	let c = this;
	return c.containerState._closeFlow = void 0, e.check(blankLine, l, u);
	function l(t) {
		return c.containerState.furtherBlankLines = c.containerState.furtherBlankLines || c.containerState.initialBlankLine, factorySpace(e, i, "listItemIndent", c.containerState.size + 1)(t);
	}
	function u(n) {
		return c.containerState.furtherBlankLines || !markdownSpace(n) ? (c.containerState.furtherBlankLines = void 0, c.containerState.initialBlankLine = void 0, d(n)) : (c.containerState.furtherBlankLines = void 0, c.containerState.initialBlankLine = void 0, e.attempt(indentConstruct, i, d)(n));
	}
	function d(t) {
		return c.containerState._closeFlow = !0, c.interrupt = void 0, factorySpace(e, e.attempt(list, i, o), "linePrefix", c.parser.constructs.disable.null.includes("codeIndented") ? void 0 : 4)(t);
	}
}
function tokenizeIndent(e, t, r) {
	let i = this;
	return factorySpace(e, a, "listItemIndent", i.containerState.size + 1);
	function a(e) {
		let n = i.events[i.events.length - 1];
		return n && n[1].type === "listItemIndent" && n[2].sliceSerialize(n[1], !0).length === i.containerState.size ? t(e) : r(e);
	}
}
function tokenizeListEnd(e) {
	e.exit(this.containerState.type);
}
function tokenizeListItemPrefixWhitespace(e, r, i) {
	let a = this;
	return factorySpace(e, o, "listItemPrefixWhitespace", a.parser.constructs.disable.null.includes("codeIndented") ? void 0 : 5);
	function o(e) {
		let n = a.events[a.events.length - 1];
		return !markdownSpace(e) && n && n[1].type === "listItemPrefixWhitespace" ? r(e) : i(e);
	}
}
export { list };
