import { convert as e } from "../../unist-util-is/lib/index.js";
import { visitParents as t } from "../../unist-util-visit-parents/lib/index.js";
import n from "../node_modules/escape-string-regexp/index.js";
//#region ../../node_modules/mdast-util-find-and-replace/lib/index.js
function r(n, r, a) {
	let o = e((a || {}).ignore || []), s = i(r), c = -1;
	for (; ++c < s.length;) t(n, "text", l);
	function l(e, t) {
		let n = -1, r;
		for (; ++n < t.length;) {
			let e = t[n], i = r ? r.children : void 0;
			if (o(e, i ? i.indexOf(e) : void 0, r)) return;
			r = e;
		}
		if (r) return u(e, t);
	}
	function u(e, t) {
		let n = t[t.length - 1], r = s[c][0], i = s[c][1], a = 0, o = n.children.indexOf(e), l = !1, u = [];
		r.lastIndex = 0;
		let d = r.exec(e.value);
		for (; d;) {
			let n = d.index, o = {
				index: d.index,
				input: d.input,
				stack: [...t, e]
			}, s = i(...d, o);
			if (typeof s == "string" && (s = s.length > 0 ? {
				type: "text",
				value: s
			} : void 0), s === !1 ? r.lastIndex = n + 1 : (a !== n && u.push({
				type: "text",
				value: e.value.slice(a, n)
			}), Array.isArray(s) ? u.push(...s) : s && u.push(s), a = n + d[0].length, l = !0), !r.global) break;
			d = r.exec(e.value);
		}
		return l ? (a < e.value.length && u.push({
			type: "text",
			value: e.value.slice(a)
		}), n.children.splice(o, 1, ...u)) : u = [e], o + u.length;
	}
}
function i(e) {
	let t = [];
	if (!Array.isArray(e)) throw TypeError("Expected find and replace tuple or list of tuples");
	let n = !e[0] || Array.isArray(e[0]) ? e : [e], r = -1;
	for (; ++r < n.length;) {
		let e = n[r];
		t.push([a(e[0]), o(e[1])]);
	}
	return t;
}
function a(e) {
	return typeof e == "string" ? new RegExp(n(e), "g") : e;
}
function o(e) {
	return typeof e == "function" ? e : function() {
		return e;
	};
}
//#endregion
export { r as findAndReplace };
