import { createWatchListCurrentStateRecord as E, createWatchListSubscriptionEventCallbacks as W } from "./helpers.js";
import { UNSUBSCRIBE_TOKEN as h } from "./constants.js";
import { getMappedValue as C } from "../../../utils/collection/main.js";
import { sameValue as R } from "../../../utils/value/compare.js";
import { struct as _ } from "../../../utils/struct/main.js";
import { enumerable as c, getter as b } from "../../../utils/struct/property.js";
import { isFunction as v } from "../../../utils/value/is.js";
import { noop as w } from "../../../utils/common.js";
const k = (u) => u === h, L = (u) => {
  let e;
  const S = E(u), a = W(), r = /* @__PURE__ */ new Map(), f = /* @__PURE__ */ new WeakMap(), m = () => Object.freeze({ ...S }), i = () => r.size === 0, l = (t) => {
    if (i()) return;
    if (k(t)) {
      const o = [];
      return r.forEach((n, s) => {
        o.push(s);
        const g = f.get(s);
        for (; n--; ) g?.();
      }), o.forEach((n) => n(t)), !0;
    }
    const p = e;
    e = m();
    for (const o of Object.keys(e))
      if (!R(e[o], p[o]))
        return r.forEach((n, s) => s(e)), !0;
    return !1;
  }, d = (t) => {
    if (!v(t)) return w;
    const p = C(t, f, () => {
      let n = () => {
        const s = r.get(t) || 0;
        s === 1 ? (n = void 0, r.delete(t), f.delete(t), i() && (e = void 0, a.idle())) : s > 1 && r.set(t, s - 1);
      };
      return () => {
        n?.();
      };
    }), o = i();
    return r.set(
      t,
      (r.get(t) || 0) + 1
    ), o && (e = m(), a.resume()), t(e), p;
  };
  return _({
    idle: b(i),
    on: c(a),
    cancelSubscriptions: c(() => l(h)),
    requestNotification: c(() => l()),
    snapshot: b(() => e ?? m()),
    subscribe: c(d)
  });
};
export {
  L as createWatchlist,
  k as isWatchlistUnsubscribeToken
};
