var p = Object.defineProperty;
var n = (i, e) => p(i, "name", { value: e, configurable: !0 });
import { getUnixTime as r } from "../../../lib0@0.2.101/node_modules/lib0/time.js";
import { floor as m } from "../../../lib0@0.2.101/node_modules/lib0/math.js";
import { Observable as f } from "../../../lib0@0.2.101/node_modules/lib0/observable.js";
import { equalityDeep as g } from "../../../lib0@0.2.101/node_modules/lib0/function.js";
import "yjs";
const d = 3e4;
class U extends f {
  static {
    n(this, "Awareness");
  }
  /**
   * @param {Y.Doc} doc
   */
  constructor(e) {
    super(), this.doc = e, this.clientID = e.clientID, this.states = /* @__PURE__ */ new Map(), this.meta = /* @__PURE__ */ new Map(), this._checkInterval = /** @type {any} */
    setInterval(() => {
      const t = r();
      this.getLocalState() !== null && d / 2 <= t - /** @type {{lastUpdated:number}} */
      this.meta.get(this.clientID).lastUpdated && this.setLocalState(this.getLocalState());
      const s = [];
      this.meta.forEach((a, l) => {
        l !== this.clientID && d <= t - a.lastUpdated && this.states.has(l) && s.push(l);
      }), s.length > 0 && I(this, s, "timeout");
    }, m(d / 10)), e.on("destroy", () => {
      this.destroy();
    }), this.setLocalState({});
  }
  destroy() {
    this.emit("destroy", [this]), this.setLocalState(null), super.destroy(), clearInterval(this._checkInterval);
  }
  /**
   * @return {Object<string,any>|null}
   */
  getLocalState() {
    return this.states.get(this.clientID) || null;
  }
  /**
   * @param {Object<string,any>|null} state
   */
  setLocalState(e) {
    const t = this.clientID, s = this.meta.get(t), a = s === void 0 ? 0 : s.clock + 1, l = this.states.get(t);
    e === null ? this.states.delete(t) : this.states.set(t, e), this.meta.set(t, {
      clock: a,
      lastUpdated: r()
    });
    const o = [], u = [], h = [], c = [];
    e === null ? c.push(t) : l == null ? e != null && o.push(t) : (u.push(t), g(l, e) || h.push(t)), (o.length > 0 || h.length > 0 || c.length > 0) && this.emit("change", [{ added: o, updated: h, removed: c }, "local"]), this.emit("update", [{ added: o, updated: u, removed: c }, "local"]);
  }
  /**
   * @param {string} field
   * @param {any} value
   */
  setLocalStateField(e, t) {
    const s = this.getLocalState();
    s !== null && this.setLocalState({
      ...s,
      [e]: t
    });
  }
  /**
   * @return {Map<number,Object<string,any>>}
   */
  getStates() {
    return this.states;
  }
}
const I = /* @__PURE__ */ n((i, e, t) => {
  const s = [];
  for (let a = 0; a < e.length; a++) {
    const l = e[a];
    if (i.states.has(l)) {
      if (i.states.delete(l), l === i.clientID) {
        const o = (
          /** @type {MetaClientState} */
          i.meta.get(l)
        );
        i.meta.set(l, {
          clock: o.clock + 1,
          lastUpdated: r()
        });
      }
      s.push(l);
    }
  }
  s.length > 0 && (i.emit("change", [{ added: [], updated: [], removed: s }, t]), i.emit("update", [{ added: [], updated: [], removed: s }, t]));
}, "removeAwarenessStates");
export {
  U as Awareness,
  d as outdatedTimeout,
  I as removeAwarenessStates
};
