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