import { findAfter as e } from "../../unist-util-find-after/lib/index.js";
import { convertElement as t } from "../../hast-util-is-element/lib/index.js";
//#region ../../node_modules/hast-util-to-text/lib/index.js
var n = /\n/g, r = /[\t ]+/g, i = t("br"), a = t(v), o = t("p"), s = t("tr"), c = t([
	"datalist",
	"head",
	"noembed",
	"noframes",
	"noscript",
	"rp",
	"script",
	"style",
	"template",
	"title",
	_,
	y
]), l = t(/* @__PURE__ */ "address.article.aside.blockquote.body.caption.center.dd.dialog.dir.dl.dt.div.figure.figcaption.footer.form,.h1.h2.h3.h4.h5.h6.header.hgroup.hr.html.legend.li.listing.main.menu.nav.ol.p.plaintext.pre.section.ul.xmp".split("."));
function u(e, t) {
	let n = t || {}, r = "children" in e ? e.children : [], a = l(e), o = g(e, {
		whitespace: n.whitespace || "normal",
		breakBefore: !1,
		breakAfter: !1
	}), s = [];
	(e.type === "text" || e.type === "comment") && s.push(...p(e, {
		whitespace: o,
		breakBefore: !0,
		breakAfter: !0
	}));
	let c = -1;
	for (; ++c < r.length;) s.push(...d(r[c], e, {
		whitespace: o,
		breakBefore: c ? void 0 : a,
		breakAfter: c < r.length - 1 ? i(r[c + 1]) : a
	}));
	let u = [], f;
	for (c = -1; ++c < s.length;) {
		let e = s[c];
		typeof e == "number" ? f !== void 0 && e > f && (f = e) : e && (f !== void 0 && f > -1 && u.push("\n".repeat(f) || " "), f = -1, u.push(e));
	}
	return u.join("");
}
function d(e, t, n) {
	return e.type === "element" ? f(e, t, n) : e.type === "text" ? n.whitespace === "normal" ? p(e, n) : m(e) : [];
}
function f(t, n, r) {
	let u = g(t, r), f = t.children || [], p = -1, m = [];
	if (c(t)) return m;
	let h, _;
	for (i(t) || s(t) && e(n, t, s) ? _ = "\n" : o(t) ? (h = 2, _ = 2) : l(t) && (h = 1, _ = 1); ++p < f.length;) m = m.concat(d(f[p], t, {
		whitespace: u,
		breakBefore: p ? void 0 : h,
		breakAfter: p < f.length - 1 ? i(f[p + 1]) : _
	}));
	return a(t) && e(n, t, a) && m.push("	"), h && m.unshift(h), _ && m.push(_), m;
}
function p(e, t) {
	let r = String(e.value), i = [], a = [], o = 0;
	for (; o <= r.length;) {
		n.lastIndex = o;
		let e = n.exec(r), a = e && "index" in e ? e.index : r.length;
		i.push(h(r.slice(o, a).replace(/[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g, ""), o === 0 ? t.breakBefore : !0, a === r.length ? t.breakAfter : !0)), o = a + 1;
	}
	let s = -1, c;
	for (; ++s < i.length;) i[s].charCodeAt(i[s].length - 1) === 8203 || s < i.length - 1 && i[s + 1].charCodeAt(0) === 8203 ? (a.push(i[s]), c = void 0) : i[s] ? (typeof c == "number" && a.push(c), a.push(i[s]), c = 0) : (s === 0 || s === i.length - 1) && a.push(0);
	return a;
}
function m(e) {
	return [String(e.value)];
}
function h(e, t, n) {
	let i = [], a = 0, o;
	for (; a < e.length;) {
		r.lastIndex = a;
		let n = r.exec(e);
		o = n ? n.index : e.length, !a && !o && n && !t && i.push(""), a !== o && i.push(e.slice(a, o)), a = n ? o + n[0].length : o;
	}
	return a !== o && !n && i.push(""), i.join(" ");
}
function g(e, t) {
	if (e.type === "element") {
		let n = e.properties || {};
		switch (e.tagName) {
			case "listing":
			case "plaintext":
			case "xmp": return "pre";
			case "nobr": return "nowrap";
			case "pre": return n.wrap ? "pre-wrap" : "pre";
			case "td":
			case "th": return n.noWrap ? "nowrap" : t.whitespace;
			case "textarea": return "pre-wrap";
			default:
		}
	}
	return t.whitespace;
}
function _(e) {
	return !!(e.properties || {}).hidden;
}
function v(e) {
	return e.tagName === "td" || e.tagName === "th";
}
function y(e) {
	return e.tagName === "dialog" && !(e.properties || {}).open;
}
//#endregion
export { u as toText };
