import { classifyCharacter } from "../../micromark-util-classify-character/index.js";
import { push, splice } from "../../micromark-util-chunked/index.js";
import { resolveAll } from "../../micromark-util-resolve-all/index.js";
const attention = {
	name: "attention",
	resolveAll: resolveAllAttention,
	tokenize: tokenizeAttention
};
function resolveAllAttention(e, i) {
	let a = -1, o, c, l, u, d, f, p, m;
	for (; ++a < e.length;) if (e[a][0] === "enter" && e[a][1].type === "attentionSequence" && e[a][1]._close) {
		for (o = a; o--;) if (e[o][0] === "exit" && e[o][1].type === "attentionSequence" && e[o][1]._open && i.sliceSerialize(e[o][1]).charCodeAt(0) === i.sliceSerialize(e[a][1]).charCodeAt(0)) {
			if ((e[o][1]._close || e[a][1]._open) && (e[a][1].end.offset - e[a][1].start.offset) % 3 && !((e[o][1].end.offset - e[o][1].start.offset + e[a][1].end.offset - e[a][1].start.offset) % 3)) continue;
			f = e[o][1].end.offset - e[o][1].start.offset > 1 && e[a][1].end.offset - e[a][1].start.offset > 1 ? 2 : 1;
			let h = { ...e[o][1].end }, g = { ...e[a][1].start };
			movePoint(h, -f), movePoint(g, f), u = {
				type: f > 1 ? "strongSequence" : "emphasisSequence",
				start: h,
				end: { ...e[o][1].end }
			}, d = {
				type: f > 1 ? "strongSequence" : "emphasisSequence",
				start: { ...e[a][1].start },
				end: g
			}, l = {
				type: f > 1 ? "strongText" : "emphasisText",
				start: { ...e[o][1].end },
				end: { ...e[a][1].start }
			}, c = {
				type: f > 1 ? "strong" : "emphasis",
				start: { ...u.start },
				end: { ...d.end }
			}, e[o][1].end = { ...u.start }, e[a][1].start = { ...d.end }, p = [], e[o][1].end.offset - e[o][1].start.offset && (p = push(p, [[
				"enter",
				e[o][1],
				i
			], [
				"exit",
				e[o][1],
				i
			]])), p = push(p, [
				[
					"enter",
					c,
					i
				],
				[
					"enter",
					u,
					i
				],
				[
					"exit",
					u,
					i
				],
				[
					"enter",
					l,
					i
				]
			]), p = push(p, resolveAll(i.parser.constructs.insideSpan.null, e.slice(o + 1, a), i)), p = push(p, [
				[
					"exit",
					l,
					i
				],
				[
					"enter",
					d,
					i
				],
				[
					"exit",
					d,
					i
				],
				[
					"exit",
					c,
					i
				]
			]), e[a][1].end.offset - e[a][1].start.offset ? (m = 2, p = push(p, [[
				"enter",
				e[a][1],
				i
			], [
				"exit",
				e[a][1],
				i
			]])) : m = 0, splice(e, o - 1, a - o + 3, p), a = o + p.length - m - 2;
			break;
		}
	}
	for (a = -1; ++a < e.length;) e[a][1].type === "attentionSequence" && (e[a][1].type = "data");
	return e;
}
function tokenizeAttention(t, n) {
	let r = this.parser.constructs.attentionMarkers.null, i = this.previous, a = classifyCharacter(i), o;
	return s;
	function s(e) {
		return o = e, t.enter("attentionSequence"), c(e);
	}
	function c(s) {
		if (s === o) return t.consume(s), c;
		let l = t.exit("attentionSequence"), u = classifyCharacter(s), d = !u || u === 2 && a || r.includes(s), f = !a || a === 2 && u || r.includes(i);
		return l._open = !!(o === 42 ? d : d && (a || !f)), l._close = !!(o === 42 ? f : f && (u || !d)), n(s);
	}
}
function movePoint(e, t) {
	e.column += t, e.offset += t, e._bufferIndex += t;
}
export { attention };
