import { markdownLineEnding as e } from "../../micromark-util-character/index.js";
import { push as t, splice as n } from "../../micromark-util-chunked/index.js";
import { resolveAll as r } from "../../micromark-util-resolve-all/index.js";
//#region ../../node_modules/micromark/lib/create-tokenizer.js
function i(i, s, c) {
	let l = {
		_bufferIndex: -1,
		_index: 0,
		line: c && c.line || 1,
		column: c && c.column || 1,
		offset: c && c.offset || 0
	}, u = {}, d = [], f = [], p = [], m = {
		attempt: k(D),
		check: k(O),
		consume: w,
		enter: T,
		exit: E,
		interrupt: k(O, { interrupt: !0 })
	}, h = {
		code: null,
		containerState: {},
		defineSkip: x,
		events: [],
		now: b,
		parser: i,
		previous: null,
		sliceSerialize: v,
		sliceStream: y,
		write: _
	}, g = s.tokenize.call(h, m);
	return s.resolveAll && d.push(s), h;
	function _(e) {
		return f = t(f, e), S(), f[f.length - 1] === null ? (A(s, 0), h.events = r(d, h.events, h), h.events) : [];
	}
	function v(e, t) {
		return o(y(e), t);
	}
	function y(e) {
		return a(f, e);
	}
	function b() {
		let { _bufferIndex: e, _index: t, line: n, column: r, offset: i } = l;
		return {
			_bufferIndex: e,
			_index: t,
			line: n,
			column: r,
			offset: i
		};
	}
	function x(e) {
		u[e.line] = e.column, M();
	}
	function S() {
		let e;
		for (; l._index < f.length;) {
			let t = f[l._index];
			if (typeof t == "string") for (e = l._index, l._bufferIndex < 0 && (l._bufferIndex = 0); l._index === e && l._bufferIndex < t.length;) C(t.charCodeAt(l._bufferIndex));
			else C(t);
		}
	}
	function C(e) {
		g = g(e);
	}
	function w(t) {
		e(t) ? (l.line++, l.column = 1, l.offset += t === -3 ? 2 : 1, M()) : t !== -1 && (l.column++, l.offset++), l._bufferIndex < 0 ? l._index++ : (l._bufferIndex++, l._bufferIndex === f[l._index].length && (l._bufferIndex = -1, l._index++)), h.previous = t;
	}
	function T(e, t) {
		let n = t || {};
		return n.type = e, n.start = b(), h.events.push([
			"enter",
			n,
			h
		]), p.push(n), n;
	}
	function E(e) {
		let t = p.pop();
		return t.end = b(), h.events.push([
			"exit",
			t,
			h
		]), t;
	}
	function D(e, t) {
		A(e, t.from);
	}
	function O(e, t) {
		t.restore();
	}
	function k(e, t) {
		return n;
		function n(n, r, i) {
			let a, o, s, c;
			return Array.isArray(n) ? u(n) : "tokenize" in n ? u([n]) : l(n);
			function l(e) {
				return t;
				function t(t) {
					let n = t !== null && e[t], r = t !== null && e.null;
					return u([...Array.isArray(n) ? n : n ? [n] : [], ...Array.isArray(r) ? r : r ? [r] : []])(t);
				}
			}
			function u(e) {
				return a = e, o = 0, e.length === 0 ? i : d(e[o]);
			}
			function d(e) {
				return n;
				function n(n) {
					return c = j(), s = e, e.partial || (h.currentConstruct = e), e.name && h.parser.constructs.disable.null.includes(e.name) ? p(n) : e.tokenize.call(t ? Object.assign(Object.create(h), t) : h, m, f, p)(n);
				}
			}
			function f(t) {
				return e(s, c), r;
			}
			function p(e) {
				return c.restore(), ++o < a.length ? d(a[o]) : i;
			}
		}
	}
	function A(e, t) {
		e.resolveAll && !d.includes(e) && d.push(e), e.resolve && n(h.events, t, h.events.length - t, e.resolve(h.events.slice(t), h)), e.resolveTo && (h.events = e.resolveTo(h.events, h));
	}
	function j() {
		let e = b(), t = h.previous, n = h.currentConstruct, r = h.events.length, i = Array.from(p);
		return {
			from: r,
			restore: a
		};
		function a() {
			l = e, h.previous = t, h.currentConstruct = n, h.events.length = r, p = i, M();
		}
	}
	function M() {
		l.line in u && l.column < 2 && (l.column = u[l.line], l.offset += u[l.line] - 1);
	}
}
function a(e, t) {
	let n = t.start._index, r = t.start._bufferIndex, i = t.end._index, a = t.end._bufferIndex, o;
	if (n === i) o = [e[n].slice(r, a)];
	else {
		if (o = e.slice(n, i), r > -1) {
			let e = o[0];
			typeof e == "string" ? o[0] = e.slice(r) : o.shift();
		}
		a > 0 && o.push(e[i].slice(0, a));
	}
	return o;
}
function o(e, t) {
	let n = -1, r = [], i;
	for (; ++n < e.length;) {
		let a = e[n], o;
		if (typeof a == "string") o = a;
		else switch (a) {
			case -5:
				o = "\r";
				break;
			case -4:
				o = "\n";
				break;
			case -3:
				o = "\r\n";
				break;
			case -2:
				o = t ? " " : "	";
				break;
			case -1:
				if (!t && i) continue;
				o = " ";
				break;
			default: o = String.fromCharCode(a);
		}
		i = a === -2, r.push(o);
	}
	return r.join("");
}
//#endregion
export { i as createTokenizer };
