import "./collection.mjs";
import { __exports as o } from "../../../_virtual/collection.mjs";
var u = o;
function m(e, n) {
  return Object.prototype.hasOwnProperty.call(e, n.name || n);
}
function t(e, n, i) {
  var f = u.extend(i[n.name] || [], e, n, i);
  Object.defineProperty(i, n.name, {
    enumerable: n.enumerable,
    value: f
  }), f.length && f.forEach(function(o2) {
    e.set(o2, n.inverse, i);
  });
}
function d(e, n, i) {
  var f = n.inverse, o2 = i[n.name];
  Object.defineProperty(i, n.name, {
    configurable: n.configurable,
    enumerable: n.enumerable,
    get: function() {
      return o2;
    },
    set: function(c) {
      if (c !== o2) {
        var l = o2;
        o2 = null, l && e.unset(l, f, i), o2 = c, e.set(o2, f, i);
      }
    }
  });
}
function s(e, n) {
  if (!(this instanceof s))
    return new s(e, n);
  e.inverse = n, n.inverse = e, this.props = {}, this.props[e.name] = e, this.props[n.name] = n;
}
s.prototype.bind = function(e, n) {
  if (typeof n == "string") {
    if (!this.props[n])
      throw new Error("no property <" + n + "> in ref");
    n = this.props[n];
  }
  n.collection ? t(this, n, e) : d(this, n, e);
};
s.prototype.ensureRefsCollection = function(e, n) {
  var i = e[n.name];
  return u.isExtended(i) || t(this, n, e), i;
};
s.prototype.ensureBound = function(e, n) {
  m(e, n) || this.bind(e, n);
};
s.prototype.unset = function(e, n, i) {
  e && (this.ensureBound(e, n), n.collection ? this.ensureRefsCollection(e, n).remove(i) : e[n.name] = void 0);
};
s.prototype.set = function(e, n, i) {
  e && (this.ensureBound(e, n), n.collection ? this.ensureRefsCollection(e, n).add(i) : e[n.name] = i);
};
var r = s;
export {
  r
};
