import { visit as e } from "../../unist-util-visit/lib/index.js";
import t from "../../@ungap/structured-clone/esm/index.js";
import { position as n } from "../../unist-util-position/lib/index.js";
import { handlers as r } from "./handlers/index.js";
//#region ../../node_modules/mdast-util-to-hast/lib/state.js
var i = {}.hasOwnProperty, a = {};
function o(n, o) {
	let f = o || a, p = /* @__PURE__ */ new Map(), m = /* @__PURE__ */ new Map(), h = {
		all: _,
		applyData: c,
		definitionById: p,
		footnoteById: m,
		footnoteCounts: /* @__PURE__ */ new Map(),
		footnoteOrder: [],
		handlers: {
			...r,
			...f.handlers
		},
		one: g,
		options: f,
		patch: s,
		wrap: u
	};
	return e(n, function(e) {
		if (e.type === "definition" || e.type === "footnoteDefinition") {
			let t = e.type === "definition" ? p : m, n = String(e.identifier).toUpperCase();
			t.has(n) || t.set(n, e);
		}
	}), h;
	function g(e, n) {
		let r = e.type, a = h.handlers[r];
		if (i.call(h.handlers, r) && a) return a(h, e, n);
		if (h.options.passThrough && h.options.passThrough.includes(r)) {
			if ("children" in e) {
				let { children: n, ...r } = e, i = t(r);
				return i.children = h.all(e), i;
			}
			return t(e);
		}
		return (h.options.unknownHandler || l)(h, e, n);
	}
	function _(e) {
		let t = [];
		if ("children" in e) {
			let n = e.children, r = -1;
			for (; ++r < n.length;) {
				let i = h.one(n[r], e);
				if (i) {
					if (r && n[r - 1].type === "break" && (!Array.isArray(i) && i.type === "text" && (i.value = d(i.value)), !Array.isArray(i) && i.type === "element")) {
						let e = i.children[0];
						e && e.type === "text" && (e.value = d(e.value));
					}
					Array.isArray(i) ? t.push(...i) : t.push(i);
				}
			}
		}
		return t;
	}
}
function s(e, t) {
	e.position && (t.position = n(e));
}
function c(e, n) {
	let r = n;
	if (e && e.data) {
		let n = e.data.hName, i = e.data.hChildren, a = e.data.hProperties;
		typeof n == "string" && (r.type === "element" ? r.tagName = n : r = {
			type: "element",
			tagName: n,
			properties: {},
			children: "children" in r ? r.children : [r]
		}), r.type === "element" && a && Object.assign(r.properties, t(a)), "children" in r && r.children && i != null && (r.children = i);
	}
	return r;
}
function l(e, t) {
	let n = t.data || {}, r = "value" in t && !(i.call(n, "hProperties") || i.call(n, "hChildren")) ? {
		type: "text",
		value: t.value
	} : {
		type: "element",
		tagName: "div",
		properties: {},
		children: e.all(t)
	};
	return e.patch(t, r), e.applyData(t, r);
}
function u(e, t) {
	let n = [], r = -1;
	for (t && n.push({
		type: "text",
		value: "\n"
	}); ++r < e.length;) r && n.push({
		type: "text",
		value: "\n"
	}), n.push(e[r]);
	return t && e.length > 0 && n.push({
		type: "text",
		value: "\n"
	}), n;
}
function d(e) {
	let t = 0, n = e.charCodeAt(t);
	for (; n === 9 || n === 32;) t++, n = e.charCodeAt(t);
	return e.slice(t);
}
//#endregion
export { o as createState };
