import { createNode as u } from "./accelbyte-widgets721.mjs";
import { isNode as f, isPair as c, isCollection as i, isScalar as a } from "./accelbyte-widgets711.mjs";
import { NodeBase as d } from "./accelbyte-widgets726.mjs";
function h(o, n, t) {
  let e = t;
  for (let s = n.length - 1; s >= 0; --s) {
    const r = n[s];
    if (typeof r == "number" && Number.isInteger(r) && r >= 0) {
      const l = [];
      l[r] = e, e = l;
    } else
      e = /* @__PURE__ */ new Map([[r, e]]);
  }
  return u(e, void 0, {
    aliasDuplicateObjects: !1,
    keepUndefined: !1,
    onAnchor: () => {
      throw new Error("This should not happen, please report a bug.");
    },
    schema: o,
    sourceObjects: /* @__PURE__ */ new Map()
  });
}
const m = (o) => o == null || typeof o == "object" && !!o[Symbol.iterator]().next().done;
class g extends d {
  constructor(n, t) {
    super(n), Object.defineProperty(this, "schema", {
      value: t,
      configurable: !0,
      enumerable: !1,
      writable: !0
    });
  }
  clone(n) {
    const t = Object.create(Object.getPrototypeOf(this), Object.getOwnPropertyDescriptors(this));
    return n && (t.schema = n), t.items = t.items.map((e) => f(e) || c(e) ? e.clone(n) : e), this.range && (t.range = this.range.slice()), t;
  }
  addIn(n, t) {
    if (m(n))
      this.add(t);
    else {
      const [e, ...s] = n, r = this.get(e, !0);
      if (i(r))
        r.addIn(s, t);
      else if (r === void 0 && this.schema)
        this.set(e, h(this.schema, s, t));
      else
        throw new Error(`Expected YAML collection at ${e}. Remaining path: ${s}`);
    }
  }
  deleteIn(n) {
    const [t, ...e] = n;
    if (e.length === 0)
      return this.delete(t);
    const s = this.get(t, !0);
    if (i(s))
      return s.deleteIn(e);
    throw new Error(`Expected YAML collection at ${t}. Remaining path: ${e}`);
  }
  getIn(n, t) {
    const [e, ...s] = n, r = this.get(e, !0);
    return s.length === 0 ? !t && a(r) ? r.value : r : i(r) ? r.getIn(s, t) : void 0;
  }
  hasAllNullValues(n) {
    return this.items.every((t) => {
      if (!c(t))
        return !1;
      const e = t.value;
      return e == null || n && a(e) && e.value == null && !e.commentBefore && !e.comment && !e.tag;
    });
  }
  hasIn(n) {
    const [t, ...e] = n;
    if (e.length === 0)
      return this.has(t);
    const s = this.get(t, !0);
    return i(s) ? s.hasIn(e) : !1;
  }
  setIn(n, t) {
    const [e, ...s] = n;
    if (s.length === 0)
      this.set(e, t);
    else {
      const r = this.get(e, !0);
      if (i(r))
        r.setIn(s, t);
      else if (r === void 0 && this.schema)
        this.set(e, h(this.schema, s, t));
      else
        throw new Error(`Expected YAML collection at ${e}. Remaining path: ${s}`);
    }
  }
}
g.maxFlowStringSingleLineLength = 60;
export {
  g as Collection,
  h as collectionFromPath,
  m as isEmptyPath
};
