import { isDocument as M, isNode as j, isPair as r, isCollection as N, isMap as s, isSeq as C, isScalar as q, isAlias as O } from "./accelbyte-widgets711.mjs";
const t = Symbol("break visit"), y = Symbol("skip children"), m = Symbol("remove node");
function b(e, c) {
  const l = A(c);
  M(e) ? S(null, e.contents, l, Object.freeze([e])) === m && (e.contents = null) : S(null, e, l, Object.freeze([]));
}
b.BREAK = t;
b.SKIP = y;
b.REMOVE = m;
function S(e, c, l, n) {
  const u = z(e, c, l, n);
  if (j(u) || r(u))
    return K(e, n, u), S(e, u, l, n);
  if (typeof u != "symbol") {
    if (N(c)) {
      n = Object.freeze(n.concat(c));
      for (let f = 0; f < c.items.length; ++f) {
        const a = S(f, c.items[f], l, n);
        if (typeof a == "number")
          f = a - 1;
        else {
          if (a === t)
            return t;
          a === m && (c.items.splice(f, 1), f -= 1);
        }
      }
    } else if (r(c)) {
      n = Object.freeze(n.concat(c));
      const f = S("key", c.key, l, n);
      if (f === t)
        return t;
      f === m && (c.key = null);
      const a = S("value", c.value, l, n);
      if (a === t)
        return t;
      a === m && (c.value = null);
    }
  }
  return u;
}
function A(e) {
  return typeof e == "object" && (e.Collection || e.Node || e.Value) ? Object.assign({
    Alias: e.Node,
    Map: e.Node,
    Scalar: e.Node,
    Seq: e.Node
  }, e.Value && {
    Map: e.Value,
    Scalar: e.Value,
    Seq: e.Value
  }, e.Collection && {
    Map: e.Collection,
    Seq: e.Collection
  }, e) : e;
}
function z(e, c, l, n) {
  var u, f, a, V, E;
  if (typeof l == "function")
    return l(e, c, n);
  if (s(c))
    return (u = l.Map) == null ? void 0 : u.call(l, e, c, n);
  if (C(c))
    return (f = l.Seq) == null ? void 0 : f.call(l, e, c, n);
  if (r(c))
    return (a = l.Pair) == null ? void 0 : a.call(l, e, c, n);
  if (q(c))
    return (V = l.Scalar) == null ? void 0 : V.call(l, e, c, n);
  if (O(c))
    return (E = l.Alias) == null ? void 0 : E.call(l, e, c, n);
}
function K(e, c, l) {
  const n = c[c.length - 1];
  if (N(n))
    n.items[e] = l;
  else if (r(n))
    e === "key" ? n.key = l : n.value = l;
  else if (M(n))
    n.contents = l;
  else {
    const u = O(n) ? "alias" : "scalar";
    throw new Error(`Cannot replace node with ${u} parent`);
  }
}
export {
  b as visit
};
