import { Alias as E } from "./accelbyte-widgets714.mjs";
import { isEmptyPath as f, collectionFromPath as y } from "./accelbyte-widgets650.mjs";
import { NODE_TYPE as b, DOC as v, isNode as k, isCollection as o, isScalar as x } from "./accelbyte-widgets711.mjs";
import { Pair as D } from "./accelbyte-widgets715.mjs";
import { toJS as P } from "./accelbyte-widgets716.mjs";
import { Schema as J } from "./accelbyte-widgets717.mjs";
import { stringifyDocument as T } from "./accelbyte-widgets718.mjs";
import { anchorNames as M, findNewAnchor as B, createNodeAnchors as K } from "./accelbyte-widgets719.mjs";
import { applyReviver as _ } from "./accelbyte-widgets720.mjs";
import { createNode as C } from "./accelbyte-widgets721.mjs";
import { Directives as g } from "./accelbyte-widgets648.mjs";
class S {
  constructor(t, e, s) {
    this.commentBefore = null, this.comment = null, this.errors = [], this.warnings = [], Object.defineProperty(this, b, { value: v });
    let n = null;
    typeof e == "function" || Array.isArray(e) ? n = e : s === void 0 && e && (s = e, e = void 0);
    const i = Object.assign({
      intAsBigInt: !1,
      keepSourceTokens: !1,
      logLevel: "warn",
      prettyErrors: !0,
      strict: !0,
      uniqueKeys: !0,
      version: "1.2"
    }, s);
    this.options = i;
    let { version: r } = i;
    s != null && s._directives ? (this.directives = s._directives.atDocument(), this.directives.yaml.explicit && (r = this.directives.yaml.version)) : this.directives = new g({ version: r }), this.setSchema(r, s), this.contents = t === void 0 ? null : this.createNode(t, n, s);
  }
  clone() {
    const t = Object.create(S.prototype, {
      [b]: { value: v }
    });
    return t.commentBefore = this.commentBefore, t.comment = this.comment, t.errors = this.errors.slice(), t.warnings = this.warnings.slice(), t.options = Object.assign({}, this.options), this.directives && (t.directives = this.directives.clone()), t.schema = this.schema.clone(), t.contents = k(this.contents) ? this.contents.clone(t.schema) : this.contents, this.range && (t.range = this.range.slice()), t;
  }
  add(t) {
    c(this.contents) && this.contents.add(t);
  }
  addIn(t, e) {
    c(this.contents) && this.contents.addIn(t, e);
  }
  createAlias(t, e) {
    if (!t.anchor) {
      const s = M(this);
      t.anchor = !e || s.has(e) ? B(e || "a", s) : e;
    }
    return new E(t.anchor);
  }
  createNode(t, e, s) {
    let n;
    if (typeof e == "function")
      t = e.call({ "": t }, "", t), n = e;
    else if (Array.isArray(e)) {
      const j = (d) => typeof d == "number" || d instanceof String || d instanceof Number, w = e.filter(j).map(String);
      w.length > 0 && (e = e.concat(w)), n = e;
    } else
      s === void 0 && e && (s = e, e = void 0);
    const { aliasDuplicateObjects: i, anchorPrefix: r, flow: a, keepUndefined: h, onTagObj: l, tag: u } = s ?? {}, { onAnchor: p, setAnchors: A, sourceObjects: O } = K(
      this,
      r || "a"
    ), I = {
      aliasDuplicateObjects: i ?? !0,
      keepUndefined: h ?? !1,
      onAnchor: p,
      onTagObj: l,
      replacer: n,
      schema: this.schema,
      sourceObjects: O
    }, m = C(t, u, I);
    return a && o(m) && (m.flow = !0), A(), m;
  }
  createPair(t, e, s = {}) {
    const n = this.createNode(t, null, s), i = this.createNode(e, null, s);
    return new D(n, i);
  }
  delete(t) {
    return c(this.contents) ? this.contents.delete(t) : !1;
  }
  deleteIn(t) {
    return f(t) ? this.contents == null ? !1 : (this.contents = null, !0) : c(this.contents) ? this.contents.deleteIn(t) : !1;
  }
  get(t, e) {
    return o(this.contents) ? this.contents.get(t, e) : void 0;
  }
  getIn(t, e) {
    return f(t) ? !e && x(this.contents) ? this.contents.value : this.contents : o(this.contents) ? this.contents.getIn(t, e) : void 0;
  }
  has(t) {
    return o(this.contents) ? this.contents.has(t) : !1;
  }
  hasIn(t) {
    return f(t) ? this.contents !== void 0 : o(this.contents) ? this.contents.hasIn(t) : !1;
  }
  set(t, e) {
    this.contents == null ? this.contents = y(this.schema, [t], e) : c(this.contents) && this.contents.set(t, e);
  }
  setIn(t, e) {
    f(t) ? this.contents = e : this.contents == null ? this.contents = y(this.schema, Array.from(t), e) : c(this.contents) && this.contents.setIn(t, e);
  }
  setSchema(t, e = {}) {
    typeof t == "number" && (t = String(t));
    let s;
    switch (t) {
      case "1.1":
        this.directives ? this.directives.yaml.version = "1.1" : this.directives = new g({ version: "1.1" }), s = { merge: !0, resolveKnownTags: !1, schema: "yaml-1.1" };
        break;
      case "1.2":
      case "next":
        this.directives ? this.directives.yaml.version = t : this.directives = new g({ version: t }), s = { merge: !1, resolveKnownTags: !0, schema: "core" };
        break;
      case null:
        this.directives && delete this.directives, s = null;
        break;
      default: {
        const n = JSON.stringify(t);
        throw new Error(`Expected '1.1', '1.2' or null as first argument, but found: ${n}`);
      }
    }
    if (e.schema instanceof Object)
      this.schema = e.schema;
    else if (s)
      this.schema = new J(Object.assign(s, e));
    else
      throw new Error("With a null YAML version, the { schema: Schema } option is required");
  }
  toJS({ json: t, jsonArg: e, mapAsMap: s, maxAliasCount: n, onAnchor: i, reviver: r } = {}) {
    const a = {
      anchors: /* @__PURE__ */ new Map(),
      doc: this,
      keep: !t,
      mapAsMap: s === !0,
      mapKeyWarned: !1,
      maxAliasCount: typeof n == "number" ? n : 100
    }, h = P(this.contents, e ?? "", a);
    if (typeof i == "function")
      for (const { count: l, res: u } of a.anchors.values())
        i(u, l);
    return typeof r == "function" ? _(r, { "": h }, "", h) : h;
  }
  toJSON(t, e) {
    return this.toJS({ json: !0, jsonArg: t, mapAsMap: !1, onAnchor: e });
  }
  toString(t = {}) {
    if (this.errors.length > 0)
      throw new Error("Document with errors cannot be stringified");
    if ("indent" in t && (!Number.isInteger(t.indent) || Number(t.indent) <= 0)) {
      const e = JSON.stringify(t.indent);
      throw new Error(`"indent" option must be a positive integer, not ${e}`);
    }
    return T(this, t);
  }
}
function c(N) {
  if (o(N))
    return !0;
  throw new Error("Expected a YAML collection as document contents");
}
export {
  S as Document
};
