import { convert } from "../../unist-util-is/lib/index.js";
import { color } from "./color.js";
var empty = [];
const CONTINUE = !0, EXIT = !1, SKIP = "skip";
function visitParents(i, a, o, s) {
	let c;
	typeof a == "function" && typeof o != "function" ? (s = o, o = a) : c = a;
	let l = convert(c), u = s ? -1 : 1;
	d(i, void 0, [])();
	function d(e, i, c) {
		let f = e && typeof e == "object" ? e : {};
		if (typeof f.type == "string") {
			let r = typeof f.tagName == "string" ? f.tagName : typeof f.name == "string" ? f.name : void 0;
			Object.defineProperty(p, "name", { value: "node (" + color(e.type + (r ? "<" + r + ">" : "")) + ")" });
		}
		return p;
		function p() {
			let n = empty, f, p, m;
			if ((!a || l(e, i, c[c.length - 1] || void 0)) && (n = toResult(o(e, c)), n[0] === !1)) return n;
			if ("children" in e && e.children) {
				let r = e;
				if (r.children && n[0] !== "skip") for (p = (s ? r.children.length : -1) + u, m = c.concat(r); p > -1 && p < r.children.length;) {
					let e = r.children[p];
					if (f = d(e, p, m)(), f[0] === !1) return f;
					p = typeof f[1] == "number" ? f[1] : p + u;
				}
			}
			return n;
		}
	}
}
function toResult(e) {
	return Array.isArray(e) ? e : typeof e == "number" ? [!0, e] : e == null ? empty : [e];
}
export { CONTINUE, EXIT, SKIP, visitParents };
