import { isNode as D, isCollection as O, isScalar as K, isSeq as P } from "./accelbyte-widgets711.mjs";
import { Scalar as j } from "./accelbyte-widgets651.mjs";
import { stringify as C } from "./accelbyte-widgets740.mjs";
import { lineComment as F, indentComment as V } from "./accelbyte-widgets741.mjs";
function z({ key: l, value: i }, e, c, h) {
  const { allNullValues: S, doc: W, indent: L, indentStep: E, options: { commentString: d, indentSeq: q, simpleKeys: a } } = e;
  let t = D(l) && l.comment || null;
  if (a) {
    if (t)
      throw new Error("With simple keys, key nodes cannot have comments");
    if (O(l)) {
      const r = "With simple keys, collection cannot be used as a key value";
      throw new Error(r);
    }
  }
  let s = !a && (!l || t && i == null && !e.inFlow || O(l) || (K(l) ? l.type === j.BLOCK_FOLDED || l.type === j.BLOCK_LITERAL : typeof l == "object"));
  e = Object.assign({}, e, {
    allNullValues: !1,
    implicitKey: !s && (a || !S),
    indent: L + E
  });
  let g = !1, p = !1, n = C(l, e, () => g = !0, () => p = !0);
  if (!s && !e.inFlow && n.length > 1024) {
    if (a)
      throw new Error("With simple keys, single line scalar must not span more than 1024 characters");
    s = !0;
  }
  if (e.inFlow) {
    if (S || i == null)
      return g && c && c(), n === "" ? "?" : s ? `? ${n}` : n;
  } else if (S && !a || i == null && s)
    return n = `? ${n}`, t && !g ? n += F(n, e.indent, d(t)) : p && h && h(), n;
  g && (t = null), s ? (t && (n += F(n, e.indent, d(t))), n = `? ${n}
${L}:`) : (n = `${n}:`, t && (n += F(n, e.indent, d(t))));
  let u, w, y;
  D(i) ? (u = !!i.spaceBefore, w = i.commentBefore, y = i.comment) : (u = !1, w = null, y = null, i && typeof i == "object" && (i = W.createNode(i))), e.implicitKey = !1, !s && !t && K(i) && (e.indentAtStart = n.length + 1), p = !1, !q && E.length >= 2 && !e.inFlow && !s && P(i) && !i.flow && !i.tag && !i.anchor && (e.indent = e.indent.substring(2));
  let $ = !1;
  const f = C(i, e, () => $ = !0, () => p = !0);
  let o = " ";
  if (t || u || w) {
    if (o = u ? `
` : "", w) {
      const r = d(w);
      o += `
${V(r, e.indent)}`;
    }
    f === "" && !e.inFlow ? o === `
` && (o = `

`) : o += `
${e.indent}`;
  } else if (!s && O(i)) {
    const r = f[0], b = f.indexOf(`
`), N = b !== -1, A = e.inFlow ?? i.flow ?? i.items.length === 0;
    if (N || !A) {
      let B = !1;
      if (N && (r === "&" || r === "!")) {
        let m = f.indexOf(" ");
        r === "&" && m !== -1 && m < b && f[m + 1] === "!" && (m = f.indexOf(" ", m + 1)), (m === -1 || b < m) && (B = !0);
      }
      B || (o = `
${e.indent}`);
    }
  } else
    (f === "" || f[0] === `
`) && (o = "");
  return n += o + f, e.inFlow ? $ && c && c() : y && !$ ? n += F(n, e.indent, d(y)) : p && h && h(), n;
}
export {
  z as stringifyPair
};
