import { Collection as L } from "./accelbyte-widgets650.mjs";
import { isNode as k, isPair as j } from "./accelbyte-widgets711.mjs";
import { stringify as P } from "./accelbyte-widgets740.mjs";
import { lineComment as F, indentComment as b } from "./accelbyte-widgets741.mjs";
function D(c, r, o) {
  return (r.inFlow ?? c.flow ? O : N)(c, r, o);
}
function N({ comment: c, items: r }, o, { blockItemPrefix: u, flowChars: m, itemIndent: p, onChompKeep: d, onComment: y }) {
  const { indent: $, options: { commentString: B } } = o, v = Object.assign({}, o, { indent: p, type: null });
  let e = !1;
  const f = [];
  for (let i = 0; i < r.length; ++i) {
    const l = r[i];
    let a = null;
    if (k(l))
      !e && l.spaceBefore && f.push(""), S(o, f, l.commentBefore, e), l.comment && (a = l.comment);
    else if (j(l)) {
      const n = k(l.key) ? l.key : null;
      n && (!e && n.spaceBefore && f.push(""), S(o, f, n.commentBefore, e));
    }
    e = !1;
    let s = P(l, v, () => a = null, () => e = !0);
    a && (s += F(s, p, B(a))), e && a && (e = !1), f.push(u + s);
  }
  let t;
  if (f.length === 0)
    t = m.start + m.end;
  else {
    t = f[0];
    for (let i = 1; i < f.length; ++i) {
      const l = f[i];
      t += l ? `
${$}${l}` : `
`;
    }
  }
  return c ? (t += `
` + b(B(c), $), y && y()) : e && d && d(), t;
}
function O({ comment: c, items: r }, o, { flowChars: u, itemIndent: m, onComment: p }) {
  const { indent: d, indentStep: y, flowCollectionPadding: $, options: { commentString: B } } = o;
  m += y;
  const v = Object.assign({}, o, {
    indent: m,
    inFlow: !0,
    type: null
  });
  let e = !1, f = 0;
  const t = [];
  for (let s = 0; s < r.length; ++s) {
    const n = r[s];
    let g = null;
    if (k(n))
      n.spaceBefore && t.push(""), S(o, t, n.commentBefore, !1), n.comment && (g = n.comment);
    else if (j(n)) {
      const h = k(n.key) ? n.key : null;
      h && (h.spaceBefore && t.push(""), S(o, t, h.commentBefore, !1), h.comment && (e = !0));
      const C = k(n.value) ? n.value : null;
      C ? (C.comment && (g = C.comment), C.commentBefore && (e = !0)) : n.value == null && h && h.comment && (g = h.comment);
    }
    g && (e = !0);
    let w = P(n, v, () => g = null);
    s < r.length - 1 && (w += ","), g && (w += F(w, m, B(g))), !e && (t.length > f || w.includes(`
`)) && (e = !0), t.push(w), f = t.length;
  }
  let i;
  const { start: l, end: a } = u;
  if (t.length === 0)
    i = l + a;
  else if (e || (e = t.reduce((n, g) => n + g.length + 2, 2) > L.maxFlowStringSingleLineLength), e) {
    i = l;
    for (const s of t)
      i += s ? `
${y}${d}${s}` : `
`;
    i += `
${d}${a}`;
  } else
    i = `${l}${$}${t.join(" ")}${$}${a}`;
  return c && (i += F(i, d, B(c)), p && p()), i;
}
function S({ indent: c, options: { commentString: r } }, o, u, m) {
  if (u && m && (u = u.replace(/^\n+/, "")), u) {
    const p = b(r(u), c);
    o.push(p.trimStart());
  }
}
export {
  D as stringifyCollection
};
