import { stringifyCollection as c } from "./accelbyte-widgets730.mjs";
import { addPairToJSMap as p } from "./accelbyte-widgets728.mjs";
import { Collection as y } from "./accelbyte-widgets650.mjs";
import { isScalar as u, isPair as d, MAP as v } from "./accelbyte-widgets711.mjs";
import { Pair as m, createPair as g } from "./accelbyte-widgets715.mjs";
import { isScalarValue as w } from "./accelbyte-widgets651.mjs";
function a(h, e) {
  const s = u(e) ? e.value : e;
  for (const t of h)
    if (d(t) && (t.key === e || t.key === s || u(t.key) && t.key.value === s))
      return t;
}
class E extends y {
  static get tagName() {
    return "tag:yaml.org,2002:map";
  }
  constructor(e) {
    super(v, e), this.items = [];
  }
  static from(e, s, t) {
    const { keepUndefined: i, replacer: r } = t, o = new this(e), l = (n, f) => {
      if (typeof r == "function")
        f = r.call(s, n, f);
      else if (Array.isArray(r) && !r.includes(n))
        return;
      (f !== void 0 || i) && o.items.push(g(n, f, t));
    };
    if (s instanceof Map)
      for (const [n, f] of s)
        l(n, f);
    else if (s && typeof s == "object")
      for (const n of Object.keys(s))
        l(n, s[n]);
    return typeof e.sortMapEntries == "function" && o.items.sort(e.sortMapEntries), o;
  }
  add(e, s) {
    var o;
    let t;
    d(e) ? t = e : !e || typeof e != "object" || !("key" in e) ? t = new m(e, e == null ? void 0 : e.value) : t = new m(e.key, e.value);
    const i = a(this.items, t.key), r = (o = this.schema) == null ? void 0 : o.sortMapEntries;
    if (i) {
      if (!s)
        throw new Error(`Key ${t.key} already set`);
      u(i.value) && w(t.value) ? i.value.value = t.value : i.value = t.value;
    } else if (r) {
      const l = this.items.findIndex((n) => r(t, n) < 0);
      l === -1 ? this.items.push(t) : this.items.splice(l, 0, t);
    } else
      this.items.push(t);
  }
  delete(e) {
    const s = a(this.items, e);
    return s ? this.items.splice(this.items.indexOf(s), 1).length > 0 : !1;
  }
  get(e, s) {
    const t = a(this.items, e), i = t == null ? void 0 : t.value;
    return (!s && u(i) ? i.value : i) ?? void 0;
  }
  has(e) {
    return !!a(this.items, e);
  }
  set(e, s) {
    this.add(new m(e, s), !0);
  }
  toJSON(e, s, t) {
    const i = t ? new t() : s != null && s.mapAsMap ? /* @__PURE__ */ new Map() : {};
    s != null && s.onCreate && s.onCreate(i);
    for (const r of this.items)
      p(s, i, r);
    return i;
  }
  toString(e, s, t) {
    if (!e)
      return JSON.stringify(this);
    for (const i of this.items)
      if (!d(i))
        throw new Error(`Map items must all be pairs; found ${JSON.stringify(i)} instead`);
    return !e.allNullValues && this.hasAllNullValues(!1) && (e = Object.assign({}, e, { allNullValues: !0 })), c(this, e, {
      blockItemPrefix: "",
      flowChars: { start: "{", end: "}" },
      itemIndent: e.indent || "",
      onChompKeep: t,
      onComment: s
    });
  }
}
export {
  E as YAMLMap,
  a as findPair
};
