import { anchorIsValid as u } from "./accelbyte-widgets719.mjs";
import { isPair as g, isAlias as c, isNode as S, isScalar as a, isCollection as y } from "./accelbyte-widgets711.mjs";
import { stringifyComment as h } from "./accelbyte-widgets741.mjs";
import { stringifyString as p } from "./accelbyte-widgets652.mjs";
function P(n, e) {
  const o = Object.assign({
    blockQuote: !0,
    commentString: h,
    defaultKeyType: null,
    defaultStringType: "PLAIN",
    directives: null,
    doubleQuotedAsJSON: !1,
    doubleQuotedMinMultiLineLength: 40,
    falseStr: "false",
    flowCollectionPadding: !0,
    indentSeq: !0,
    lineWidth: 80,
    minContentWidth: 20,
    nullStr: "null",
    simpleKeys: !1,
    singleQuote: null,
    trueStr: "true",
    verifyAliasOrder: !0
  }, n.schema.toStringOptions, e);
  let r;
  switch (o.collectionStyle) {
    case "block":
      r = !1;
      break;
    case "flow":
      r = !0;
      break;
    default:
      r = null;
  }
  return {
    anchors: /* @__PURE__ */ new Set(),
    doc: n,
    flowCollectionPadding: o.flowCollectionPadding ? " " : "",
    indent: "",
    indentStep: typeof o.indent == "number" ? " ".repeat(o.indent) : "  ",
    inFlow: r,
    options: o
  };
}
function v(n, e) {
  var s;
  if (e.tag) {
    const t = n.filter((i) => i.tag === e.tag);
    if (t.length > 0)
      return t.find((i) => i.format === e.format) ?? t[0];
  }
  let o, r;
  if (a(e)) {
    r = e.value;
    const t = n.filter((i) => {
      var l;
      return (l = i.identify) == null ? void 0 : l.call(i, r);
    });
    o = t.find((i) => i.format === e.format) ?? t.find((i) => !i.format);
  } else
    r = e, o = n.find((t) => t.nodeClass && r instanceof t.nodeClass);
  if (!o) {
    const t = ((s = r == null ? void 0 : r.constructor) == null ? void 0 : s.name) ?? typeof r;
    throw new Error(`Tag not resolved for ${t} value`);
  }
  return o;
}
function w(n, e, { anchors: o, doc: r }) {
  if (!r.directives)
    return "";
  const s = [], t = (a(n) || y(n)) && n.anchor;
  t && u(t) && (o.add(t), s.push(`&${t}`));
  const i = n.tag ? n.tag : e.default ? null : e.tag;
  return i && s.push(r.directives.tagString(i)), s.join(" ");
}
function T(n, e, o, r) {
  var f;
  if (g(n))
    return n.toString(e, o, r);
  if (c(n)) {
    if (e.doc.directives)
      return n.toString(e);
    if ((f = e.resolvedAliases) != null && f.has(n))
      throw new TypeError("Cannot stringify circular structure without alias nodes");
    e.resolvedAliases ? e.resolvedAliases.add(n) : e.resolvedAliases = /* @__PURE__ */ new Set([n]), n = n.resolve(e.doc);
  }
  let s;
  const t = S(n) ? n : e.doc.createNode(n, { onTagObj: (d) => s = d });
  s || (s = v(e.doc.schema.tags, t));
  const i = w(t, s, e);
  i.length > 0 && (e.indentAtStart = (e.indentAtStart ?? 0) + i.length + 1);
  const l = typeof s.stringify == "function" ? s.stringify(t, e, o, r) : a(t) ? p(t, e, o, r) : t.toString(e, o, r);
  return i ? a(t) || l[0] === "{" || l[0] === "[" ? `${i} ${l}` : `${i}
${e.indent}${l}` : l;
}
export {
  P as createStringifyContext,
  T as stringify
};
