import { __toESM as e } from "../../../_virtual/_rolldown/runtime.js";
import "../../devlop/lib/default.js";
import { bail as t } from "../../bail/index.js";
import { require_extend as n } from "../../extend/index.js";
import r from "../node_modules/is-plain-obj/index.js";
import { trough as i } from "../../trough/lib/index.js";
import { VFile as a } from "../../vfile/lib/index.js";
import { CallableInstance as o } from "./callable-instance.js";
//#region ../../node_modules/unified/lib/index.js
var s = /* @__PURE__ */ e(n(), 1), c = {}.hasOwnProperty, l = new class e extends o {
	constructor() {
		super("copy"), this.Compiler = void 0, this.Parser = void 0, this.attachers = [], this.compiler = void 0, this.freezeIndex = -1, this.frozen = void 0, this.namespace = {}, this.parser = void 0, this.transformers = i();
	}
	copy() {
		let t = new e(), n = -1;
		for (; ++n < this.attachers.length;) {
			let e = this.attachers[n];
			t.use(...e);
		}
		return t.data((0, s.default)(!0, {}, this.namespace)), t;
	}
	data(e, t) {
		return typeof e == "string" ? arguments.length === 2 ? (f("data", this.frozen), this.namespace[e] = t, this) : c.call(this.namespace, e) && this.namespace[e] || void 0 : e ? (f("data", this.frozen), this.namespace = e, this) : this.namespace;
	}
	freeze() {
		if (this.frozen) return this;
		let e = this;
		for (; ++this.freezeIndex < this.attachers.length;) {
			let [t, ...n] = this.attachers[this.freezeIndex];
			if (n[0] === !1) continue;
			n[0] === !0 && (n[0] = void 0);
			let r = t.call(e, ...n);
			typeof r == "function" && this.transformers.use(r);
		}
		return this.frozen = !0, this.freezeIndex = Infinity, this;
	}
	parse(e) {
		this.freeze();
		let t = h(e), n = this.parser || this.Parser;
		return u("parse", n), n(String(t), t);
	}
	process(e, t) {
		let n = this;
		return this.freeze(), u("process", this.parser || this.Parser), d("process", this.compiler || this.Compiler), t ? r(void 0, t) : new Promise(r);
		function r(r, i) {
			let a = h(e), o = n.parse(a);
			n.run(o, a, function(e, t, r) {
				if (e || !t || !r) return s(e);
				let i = t, a = n.stringify(i, r);
				_(a) ? r.value = a : r.result = a, s(e, r);
			});
			function s(e, n) {
				e || !n ? i(e) : r ? r(n) : t(void 0, n);
			}
		}
	}
	processSync(e) {
		let n = !1, r;
		return this.freeze(), u("processSync", this.parser || this.Parser), d("processSync", this.compiler || this.Compiler), this.process(e, i), m("processSync", "process", n), r;
		function i(e, i) {
			n = !0, t(e), r = i;
		}
	}
	run(e, t, n) {
		p(e), this.freeze();
		let r = this.transformers;
		return !n && typeof t == "function" && (n = t, t = void 0), n ? i(void 0, n) : new Promise(i);
		function i(i, a) {
			let o = h(t);
			r.run(e, o, s);
			function s(t, r, o) {
				let s = r || e;
				t ? a(t) : i ? i(s) : n(void 0, s, o);
			}
		}
	}
	runSync(e, n) {
		let r = !1, i;
		return this.run(e, n, a), m("runSync", "run", r), i;
		function a(e, n) {
			t(e), i = n, r = !0;
		}
	}
	stringify(e, t) {
		this.freeze();
		let n = h(t), r = this.compiler || this.Compiler;
		return d("stringify", r), p(e), r(e, n);
	}
	use(e, ...t) {
		let n = this.attachers, i = this.namespace;
		if (f("use", this.frozen), e != null) if (typeof e == "function") l(e, t);
		else if (typeof e == "object") Array.isArray(e) ? c(e) : o(e);
		else throw TypeError("Expected usable value, not `" + e + "`");
		return this;
		function a(e) {
			if (typeof e == "function") l(e, []);
			else if (typeof e == "object") if (Array.isArray(e)) {
				let [t, ...n] = e;
				l(t, n);
			} else o(e);
			else throw TypeError("Expected usable value, not `" + e + "`");
		}
		function o(e) {
			if (!("plugins" in e) && !("settings" in e)) throw Error("Expected usable value but received an empty preset, which is probably a mistake: presets typically come with `plugins` and sometimes with `settings`, but this has neither");
			c(e.plugins), e.settings && (i.settings = (0, s.default)(!0, i.settings, e.settings));
		}
		function c(e) {
			let t = -1;
			if (e != null) if (Array.isArray(e)) for (; ++t < e.length;) {
				let n = e[t];
				a(n);
			}
			else throw TypeError("Expected a list of plugins, not `" + e + "`");
		}
		function l(e, t) {
			let i = -1, a = -1;
			for (; ++i < n.length;) if (n[i][0] === e) {
				a = i;
				break;
			}
			if (a === -1) n.push([e, ...t]);
			else if (t.length > 0) {
				let [i, ...o] = t, c = n[a][1];
				r(c) && r(i) && (i = (0, s.default)(!0, c, i)), n[a] = [
					e,
					i,
					...o
				];
			}
		}
	}
}().freeze();
function u(e, t) {
	if (typeof t != "function") throw TypeError("Cannot `" + e + "` without `parser`");
}
function d(e, t) {
	if (typeof t != "function") throw TypeError("Cannot `" + e + "` without `compiler`");
}
function f(e, t) {
	if (t) throw Error("Cannot call `" + e + "` on a frozen processor.\nCreate a new processor first, by calling it: use `processor()` instead of `processor`.");
}
function p(e) {
	if (!r(e) || typeof e.type != "string") throw TypeError("Expected node, got `" + e + "`");
}
function m(e, t, n) {
	if (!n) throw Error("`" + e + "` finished async. Use `" + t + "` instead");
}
function h(e) {
	return g(e) ? e : new a(e);
}
function g(e) {
	return !!(e && typeof e == "object" && "message" in e && "messages" in e);
}
function _(e) {
	return typeof e == "string" || v(e);
}
function v(e) {
	return !!(e && typeof e == "object" && "byteLength" in e && "byteOffset" in e);
}
//#endregion
export { l as unified };
