import { markdownLineEnding, markdownSpace } from "../../micromark-util-character/index.js";
import { factorySpace } from "../../micromark-factory-space/index.js";
var nonLazyContinuation = {
	partial: !0,
	tokenize: tokenizeNonLazyContinuation
};
const codeFenced = {
	concrete: !0,
	name: "codeFenced",
	tokenize: tokenizeCodeFenced
};
function tokenizeCodeFenced(i, a, o) {
	let s = this, c = {
		partial: !0,
		tokenize: T
	}, l = 0, u = 0, d;
	return f;
	function f(e) {
		return p(e);
	}
	function p(e) {
		let t = s.events[s.events.length - 1];
		return l = t && t[1].type === "linePrefix" ? t[2].sliceSerialize(t[1], !0).length : 0, d = e, i.enter("codeFenced"), i.enter("codeFencedFence"), i.enter("codeFencedFenceSequence"), m(e);
	}
	function m(e) {
		return e === d ? (u++, i.consume(e), m) : u < 3 ? o(e) : (i.exit("codeFencedFenceSequence"), markdownSpace(e) ? factorySpace(i, h, "whitespace")(e) : h(e));
	}
	function h(t) {
		return t === null || markdownLineEnding(t) ? (i.exit("codeFencedFence"), s.interrupt ? a(t) : i.check(nonLazyContinuation, y, w)(t)) : (i.enter("codeFencedFenceInfo"), i.enter("chunkString", { contentType: "string" }), g(t));
	}
	function g(r) {
		return r === null || markdownLineEnding(r) ? (i.exit("chunkString"), i.exit("codeFencedFenceInfo"), h(r)) : markdownSpace(r) ? (i.exit("chunkString"), i.exit("codeFencedFenceInfo"), factorySpace(i, _, "whitespace")(r)) : r === 96 && r === d ? o(r) : (i.consume(r), g);
	}
	function _(t) {
		return t === null || markdownLineEnding(t) ? h(t) : (i.enter("codeFencedFenceMeta"), i.enter("chunkString", { contentType: "string" }), v(t));
	}
	function v(t) {
		return t === null || markdownLineEnding(t) ? (i.exit("chunkString"), i.exit("codeFencedFenceMeta"), h(t)) : t === 96 && t === d ? o(t) : (i.consume(t), v);
	}
	function y(e) {
		return i.attempt(c, w, b)(e);
	}
	function b(e) {
		return i.enter("lineEnding"), i.consume(e), i.exit("lineEnding"), x;
	}
	function x(e) {
		return l > 0 && markdownSpace(e) ? factorySpace(i, S, "linePrefix", l + 1)(e) : S(e);
	}
	function S(t) {
		return t === null || markdownLineEnding(t) ? i.check(nonLazyContinuation, y, w)(t) : (i.enter("codeFlowValue"), C(t));
	}
	function C(t) {
		return t === null || markdownLineEnding(t) ? (i.exit("codeFlowValue"), S(t)) : (i.consume(t), C);
	}
	function w(e) {
		return i.exit("codeFenced"), a(e);
	}
	function T(r, i, a) {
		let o = 0;
		return c;
		function c(e) {
			return r.enter("lineEnding"), r.consume(e), r.exit("lineEnding"), l;
		}
		function l(e) {
			return r.enter("codeFencedFence"), markdownSpace(e) ? factorySpace(r, f, "linePrefix", s.parser.constructs.disable.null.includes("codeIndented") ? void 0 : 4)(e) : f(e);
		}
		function f(e) {
			return e === d ? (r.enter("codeFencedFenceSequence"), p(e)) : a(e);
		}
		function p(e) {
			return e === d ? (o++, r.consume(e), p) : o >= u ? (r.exit("codeFencedFenceSequence"), markdownSpace(e) ? factorySpace(r, m, "whitespace")(e) : m(e)) : a(e);
		}
		function m(t) {
			return t === null || markdownLineEnding(t) ? (r.exit("codeFencedFence"), i(t)) : a(t);
		}
	}
}
function tokenizeNonLazyContinuation(e, t, n) {
	let r = this;
	return i;
	function i(t) {
		return t === null ? n(t) : (e.enter("lineEnding"), e.consume(t), e.exit("lineEnding"), a);
	}
	function a(e) {
		return r.parser.lazy[r.now().line] ? n(e) : t(e);
	}
}
export { codeFenced };
