import { convertElement as p } from "../../../../hast-util-is-element@3.0.0/node_modules/hast-util-is-element/lib/index.es.js";
import { findAfter as f } from "../../../../unist-util-find-after@5.0.0/node_modules/unist-util-find-after/lib/index.es.js";
const d = /\n/g, g = /[\t ]+/g, h = p("br"), m = p(N), y = p("p"), x = p("tr"), C = p([
  // List from: <https://html.spec.whatwg.org/multipage/rendering.html#hidden-elements>
  "datalist",
  "head",
  "noembed",
  "noframes",
  "noscript",
  // Act as if we support scripting.
  "rp",
  "script",
  "style",
  "template",
  "title",
  // Hidden attribute.
  E,
  // From: <https://html.spec.whatwg.org/multipage/rendering.html#flow-content-3>
  S
]), b = p([
  "address",
  // Flow content
  "article",
  // Sections and headings
  "aside",
  // Sections and headings
  "blockquote",
  // Flow content
  "body",
  // Page
  "caption",
  // `table-caption`
  "center",
  // Flow content (legacy)
  "dd",
  // Lists
  "dialog",
  // Flow content
  "dir",
  // Lists (legacy)
  "dl",
  // Lists
  "dt",
  // Lists
  "div",
  // Flow content
  "figure",
  // Flow content
  "figcaption",
  // Flow content
  "footer",
  // Flow content
  "form,",
  // Flow content
  "h1",
  // Sections and headings
  "h2",
  // Sections and headings
  "h3",
  // Sections and headings
  "h4",
  // Sections and headings
  "h5",
  // Sections and headings
  "h6",
  // Sections and headings
  "header",
  // Flow content
  "hgroup",
  // Sections and headings
  "hr",
  // Flow content
  "html",
  // Page
  "legend",
  // Flow content
  "li",
  // Lists (as `display: list-item`)
  "listing",
  // Flow content (legacy)
  "main",
  // Flow content
  "menu",
  // Lists
  "nav",
  // Sections and headings
  "ol",
  // Lists
  "p",
  // Flow content
  "plaintext",
  // Flow content (legacy)
  "pre",
  // Flow content
  "section",
  // Sections and headings
  "ul",
  // Lists
  "xmp"
  // Flow content (legacy)
]);
function F(e, a) {
  const l = a || {}, n = "children" in e ? e.children : [], r = b(e), i = A(e, {
    whitespace: l.whitespace || "normal"
  }), t = [];
  (e.type === "text" || e.type === "comment") && t.push(
    ...k(e, {
      breakBefore: !0,
      breakAfter: !0
    })
  );
  let s = -1;
  for (; ++s < n.length; )
    t.push(
      ...w(
        n[s],
        // @ts-expect-error: `tree` is a parent if we’re here.
        e,
        {
          whitespace: i,
          breakBefore: s ? void 0 : r,
          breakAfter: s < n.length - 1 ? h(n[s + 1]) : r
        }
      )
    );
  const c = [];
  let o;
  for (s = -1; ++s < t.length; ) {
    const u = t[s];
    typeof u == "number" ? o !== void 0 && u > o && (o = u) : u && (o !== void 0 && o > -1 && c.push(`
`.repeat(o) || " "), o = -1, c.push(u));
  }
  return c.join("");
}
function w(e, a, l) {
  return e.type === "element" ? T(e, a, l) : e.type === "text" ? l.whitespace === "normal" ? k(e, l) : B(e) : [];
}
function T(e, a, l) {
  const n = A(e, l), r = e.children || [];
  let i = -1, t = [];
  if (C(e))
    return t;
  let s, c;
  for (h(e) || x(e) && // @ts-expect-error: something up with types of parents.
  f(a, e, x) ? c = `
` : y(e) ? (s = 2, c = 2) : b(e) && (s = 1, c = 1); ++i < r.length; )
    t = t.concat(
      w(r[i], e, {
        whitespace: n,
        breakBefore: i ? void 0 : s,
        breakAfter: i < r.length - 1 ? h(r[i + 1]) : c
      })
    );
  return m(e) && // @ts-expect-error: something up with types of parents.
  f(a, e, m) && t.push("	"), s && t.unshift(s), c && t.push(c), t;
}
function k(e, a) {
  const l = String(e.value), n = [], r = [];
  let i = 0;
  for (; i <= l.length; ) {
    d.lastIndex = i;
    const c = d.exec(l), o = c && "index" in c ? c.index : l.length;
    n.push(
      // Any sequence of collapsible spaces and tabs immediately preceding or
      // following a segment break is removed.
      v(
        // […] ignoring bidi formatting characters (characters with the
        // Bidi_Control property [UAX9]: ALM, LTR, RTL, LRE-RLO, LRI-PDI) as if
        // they were not there.
        l.slice(i, o).replace(/[\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069]/g, ""),
        i === 0 ? a.breakBefore : !0,
        o === l.length ? a.breakAfter : !0
      )
    ), i = o + 1;
  }
  let t = -1, s;
  for (; ++t < n.length; )
    n[t].charCodeAt(n[t].length - 1) === 8203 || t < n.length - 1 && n[t + 1].charCodeAt(0) === 8203 ? (r.push(n[t]), s = void 0) : n[t] ? (typeof s == "number" && r.push(s), r.push(n[t]), s = 0) : (t === 0 || t === n.length - 1) && r.push(0);
  return r;
}
function B(e) {
  return [String(e.value)];
}
function v(e, a, l) {
  const n = [];
  let r = 0, i;
  for (; r < e.length; ) {
    g.lastIndex = r;
    const t = g.exec(e);
    i = t ? t.index : e.length, !r && !i && t && !a && n.push(""), r !== i && n.push(e.slice(r, i)), r = t ? i + t[0].length : i;
  }
  return r !== i && !l && n.push(""), n.join(" ");
}
function A(e, a) {
  if (e.type === "element") {
    const l = e.properties || {};
    switch (e.tagName) {
      case "listing":
      case "plaintext":
      case "xmp":
        return "pre";
      case "nobr":
        return "nowrap";
      case "pre":
        return l.wrap ? "pre-wrap" : "pre";
      case "td":
      case "th":
        return l.noWrap ? "nowrap" : a.whitespace;
      case "textarea":
        return "pre-wrap";
    }
  }
  return a.whitespace;
}
function E(e) {
  return !!(e.properties || {}).hidden;
}
function N(e) {
  return e.tagName === "td" || e.tagName === "th";
}
function S(e) {
  return e.tagName === "dialog" && !(e.properties || {}).open;
}
export {
  F as toText
};
//# sourceMappingURL=index.es.js.map
