import { findAfter } from "../../unist-util-find-after/lib/index.js";
import { convertElement } from "../../hast-util-is-element/lib/index.js";
var searchLineFeeds = /\n/g, searchTabOrSpaces = /[\t ]+/g, br = convertElement("br"), cell = convertElement(isCell), p = convertElement("p"), row = convertElement("tr"), notRendered = convertElement([
	"datalist",
	"head",
	"noembed",
	"noframes",
	"noscript",
	"rp",
	"script",
	"style",
	"template",
	"title",
	hidden,
	closedDialog
]), blockOrCaption = convertElement(/* @__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 toText(e, o) {
	let s = o || {}, c = "children" in e ? e.children : [], u = blockOrCaption(e), d = inferWhitespace(e, {
		whitespace: s.whitespace || "normal",
		breakBefore: !1,
		breakAfter: !1
	}), f = [];
	(e.type === "text" || e.type === "comment") && f.push(...collectText(e, {
		whitespace: d,
		breakBefore: !0,
		breakAfter: !0
	}));
	let m = -1;
	for (; ++m < c.length;) f.push(...renderedTextCollection(c[m], e, {
		whitespace: d,
		breakBefore: m ? void 0 : u,
		breakAfter: m < c.length - 1 ? br(c[m + 1]) : u
	}));
	let h = [], g;
	for (m = -1; ++m < f.length;) {
		let e = f[m];
		typeof e == "number" ? g !== void 0 && e > g && (g = e) : e && (g !== void 0 && g > -1 && h.push("\n".repeat(g) || " "), g = -1, h.push(e));
	}
	return h.join("");
}
function renderedTextCollection(e, o, s) {
	return e.type === "element" ? collectElement(e, o, s) : e.type === "text" ? s.whitespace === "normal" ? collectText(e, s) : collectPreText(e) : [];
}
function collectElement(o, s, c) {
	let h = inferWhitespace(o, c), g = o.children || [], _ = -1, v = [];
	if (notRendered(o)) return v;
	let y, b;
	for (br(o) || row(o) && findAfter(s, o, row) ? b = "\n" : p(o) ? (y = 2, b = 2) : blockOrCaption(o) && (y = 1, b = 1); ++_ < g.length;) v = v.concat(renderedTextCollection(g[_], o, {
		whitespace: h,
		breakBefore: _ ? void 0 : y,
		breakAfter: _ < g.length - 1 ? br(g[_ + 1]) : b
	}));
	return cell(o) && findAfter(s, o, cell) && v.push("	"), y && v.unshift(y), b && v.push(b), v;
}
function collectText(e, o) {
	let c = String(e.value), l = [], u = [], d = 0;
	for (; d <= c.length;) {
		searchLineFeeds.lastIndex = d;
		let e = searchLineFeeds.exec(c), u = e && "index" in e ? e.index : c.length;
		l.push(trimAndCollapseSpacesAndTabs(c.slice(d, u).replace(/[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g, ""), d === 0 ? o.breakBefore : !0, u === c.length ? o.breakAfter : !0)), d = u + 1;
	}
	let f = -1, m;
	for (; ++f < l.length;) l[f].charCodeAt(l[f].length - 1) === 8203 || f < l.length - 1 && l[f + 1].charCodeAt(0) === 8203 ? (u.push(l[f]), m = void 0) : l[f] ? (typeof m == "number" && u.push(m), u.push(l[f]), m = 0) : (f === 0 || f === l.length - 1) && u.push(0);
	return u;
}
function collectPreText(e) {
	return [String(e.value)];
}
function trimAndCollapseSpacesAndTabs(e, o, s) {
	let l = [], u = 0, d;
	for (; u < e.length;) {
		searchTabOrSpaces.lastIndex = u;
		let s = searchTabOrSpaces.exec(e);
		d = s ? s.index : e.length, !u && !d && s && !o && l.push(""), u !== d && l.push(e.slice(u, d)), u = s ? d + s[0].length : d;
	}
	return u !== d && !s && l.push(""), l.join(" ");
}
function inferWhitespace(e, o) {
	if (e.type === "element") {
		let s = e.properties || {};
		switch (e.tagName) {
			case "listing":
			case "plaintext":
			case "xmp": return "pre";
			case "nobr": return "nowrap";
			case "pre": return s.wrap ? "pre-wrap" : "pre";
			case "td":
			case "th": return s.noWrap ? "nowrap" : o.whitespace;
			case "textarea": return "pre-wrap";
			default:
		}
	}
	return o.whitespace;
}
function hidden(e) {
	return !!(e.properties || {}).hidden;
}
function isCell(e) {
	return e.tagName === "td" || e.tagName === "th";
}
function closedDialog(e) {
	return e.tagName === "dialog" && !(e.properties || {}).open;
}
export { toText };
