const t = "flow", M = "block", g = "quoted";
function E(f, c, l = "flow", { indentAtStart: d, lineWidth: i = 80, minContentWidth: b = 20, onFold: F, onOverflow: O } = {}) {
  if (!i || i < 0)
    return f;
  const h = Math.max(1 + b, 1 + i - c.length);
  if (f.length <= h)
    return f;
  const r = [], w = {};
  let a = i - c.length;
  typeof d == "number" && (d > i - Math.max(2, b) ? r.push(0) : a = i - d);
  let o, u, L = !1, e = -1, $ = -1, k = -1;
  l === M && (e = _(f, e), e !== -1 && (a = e + h));
  for (let n; n = f[e += 1]; ) {
    if (l === g && n === "\\") {
      switch ($ = e, f[e + 1]) {
        case "x":
          e += 3;
          break;
        case "u":
          e += 5;
          break;
        case "U":
          e += 9;
          break;
        default:
          e += 1;
      }
      k = e;
    }
    if (n === `
`)
      l === M && (e = _(f, e)), a = e + h, o = void 0;
    else {
      if (n === " " && u && u !== " " && u !== `
` && u !== "	") {
        const s = f[e + 1];
        s && s !== " " && s !== `
` && s !== "	" && (o = e);
      }
      if (e >= a)
        if (o)
          r.push(o), a = o + h, o = void 0;
        else if (l === g) {
          for (; u === " " || u === "	"; )
            u = n, n = f[e += 1], L = !0;
          const s = e > k + 1 ? e - 2 : $ - 1;
          if (w[s])
            return f;
          r.push(s), w[s] = !0, a = s + h, o = void 0;
        } else
          L = !0;
    }
    u = n;
  }
  if (L && O && O(), r.length === 0)
    return f;
  F && F();
  let p = f.slice(0, r[0]);
  for (let n = 0; n < r.length; ++n) {
    const s = r[n], D = r[n + 1] || f.length;
    s === 0 ? p = `
${c}${f.slice(0, D)}` : (l === g && w[s] && (p += `${f[s]}\\`), p += `
${c}${f.slice(s + 1, D)}`);
  }
  return p;
}
function _(f, c) {
  let l = f[c + 1];
  for (; l === " " || l === "	"; ) {
    do
      l = f[c += 1];
    while (l && l !== `
`);
    l = f[c + 1];
  }
  return c;
}
export {
  M as FOLD_BLOCK,
  t as FOLD_FLOW,
  g as FOLD_QUOTED,
  E as foldFlowLines
};
