UNPKG

@instantdb/core

Version:
1,877 lines • 231 kB
function L(n) { if (typeof n == "number") return (Math.abs(n * 2654435761) >>> 0).toString(16); if (typeof n == "boolean") return n ? "1" : "0"; if (n === null) return "null"; if (n === void 0) return "undefined"; if (typeof n == "string") { let e = 2166136261; for (let t = 0; t < n.length; t++) e ^= n.charCodeAt(t), e += (e << 1) + (e << 4) + (e << 7) + (e << 8) + (e << 24), e = e >>> 0; return e.toString(16); } if (Array.isArray(n)) { let e = 2166136261; for (let t = 0; t < n.length; t++) { e ^= (t + 1) * 2654435761; const r = L(n[t]); for (let s = 0; s < r.length; s++) e ^= r.charCodeAt(s), e *= 16777619, e = e >>> 0; } return e.toString(16); } if (typeof n == "object") { let e = 2166136261; const t = Object.keys(n).sort(); for (let r = 0; r < t.length; r++) { const s = t[r]; if (n[s] === void 0) continue; const i = L(s); e ^= parseInt(i, 16), e *= 16777619, e = e >>> 0; const o = L(n[s]); e ^= parseInt(o, 16), e *= 16777619, e = e >>> 0; } return e.toString(16); } return L(String(n)); } const x = { Remove: "remove", Replace: "replace", Add: "add" }, An = /* @__PURE__ */ Symbol.for("__MUTATIVE_PROXY_DRAFT__"), gr = /* @__PURE__ */ Symbol("__MUTATIVE_RAW_RETURN_SYMBOL__"), ze = Symbol.iterator, ee = { mutable: "mutable", immutable: "immutable" }, Et = {}; function je(n, e) { return n instanceof Map ? n.has(e) : Object.prototype.hasOwnProperty.call(n, e); } function qt(n, e) { if (e in n) { let t = Reflect.getPrototypeOf(n); for (; t; ) { const r = Reflect.getOwnPropertyDescriptor(t, e); if (r) return r; t = Reflect.getPrototypeOf(t); } } } function Mt(n) { return Object.getPrototypeOf(n) === Set.prototype; } function Pt(n) { return Object.getPrototypeOf(n) === Map.prototype; } function X(n) { var e; return (e = n.copy) !== null && e !== void 0 ? e : n.original; } function ye(n) { return !!I(n); } function I(n) { return typeof n != "object" ? null : n?.[An]; } function jt(n) { var e; const t = I(n); return t ? (e = t.copy) !== null && e !== void 0 ? e : t.original : n; } function ie(n, e) { if (!n || typeof n != "object") return !1; let t; return Object.getPrototypeOf(n) === Object.prototype || Array.isArray(n) || n instanceof Map || n instanceof Set || !!e?.mark && ((t = e.mark(n, ee)) === ee.immutable || typeof t == "function"); } function kn(n, e = []) { if (Object.hasOwnProperty.call(n, "key")) { const t = n.parent.copy, r = I(de(t, n.key)); if (r !== null && r?.original !== n.original) return null; const s = n.parent.type === 3, i = s ? Array.from(n.parent.setMap.keys()).indexOf(n.key) : n.key; if (!(s && t.size > i || je(t, i))) return null; e.push(i); } if (n.parent) return kn(n.parent, e); e.reverse(); try { mr(n.copy, e); } catch { return null; } return e; } function be(n) { return Array.isArray(n) ? 1 : n instanceof Map ? 2 : n instanceof Set ? 3 : 0; } function de(n, e) { return be(n) === 2 ? n.get(e) : n[e]; } function Re(n, e, t) { be(n) === 2 ? n.set(e, t) : n[e] = t; } function rt(n, e) { const t = I(n); return (t ? X(t) : n)[e]; } function le(n, e) { return n === e ? n !== 0 || 1 / n === 1 / e : n !== n && e !== e; } function dt(n) { if (n) for (; n.finalities.revoke.length > 0; ) n.finalities.revoke.pop()(); } function pe(n, e) { return e ? n : [""].concat(n).map((t) => { const r = `${t}`; return r.indexOf("/") === -1 && r.indexOf("~") === -1 ? r : r.replace(/~/g, "~0").replace(/\//g, "~1"); }).join("/"); } function mr(n, e) { for (let t = 0; t < e.length - 1; t += 1) { const r = e[t]; if (n = de(be(n) === 3 ? Array.from(n) : n, r), typeof n != "object") throw new Error(`Cannot resolve patch at '${e.join("/")}'.`); } return n; } function wr(n) { const e = Object.create(Object.getPrototypeOf(n)); return Reflect.ownKeys(n).forEach((t) => { let r = Reflect.getOwnPropertyDescriptor(n, t); if (r.enumerable && r.configurable && r.writable) { e[t] = n[t]; return; } r.writable || (r.writable = !0, r.configurable = !0), (r.get || r.set) && (r = { configurable: !0, writable: !0, enumerable: r.enumerable, value: n[t] }), Reflect.defineProperty(e, t, r); }), e; } const _r = Object.prototype.propertyIsEnumerable; function On(n, e) { let t; if (Array.isArray(n)) return Array.prototype.concat.call(n); if (n instanceof Set) { if (!Mt(n)) { const r = Object.getPrototypeOf(n).constructor; return new r(n.values()); } return Set.prototype.difference ? Set.prototype.difference.call(n, /* @__PURE__ */ new Set()) : new Set(n.values()); } else if (n instanceof Map) { if (!Pt(n)) { const r = Object.getPrototypeOf(n).constructor; return new r(n); } return new Map(n); } else if (e?.mark && (t = e.mark(n, ee), t !== void 0) && t !== ee.mutable) { if (t === ee.immutable) return wr(n); if (typeof t == "function") { if (e.enablePatches || e.enableAutoFreeze) throw new Error("You can't use mark and patches or auto freeze together."); return t(); } throw new Error(`Unsupported mark result: ${t}`); } else if (typeof n == "object" && Object.getPrototypeOf(n) === Object.prototype) { const r = {}; return Object.keys(n).forEach((s) => { r[s] = n[s]; }), Object.getOwnPropertySymbols(n).forEach((s) => { _r.call(n, s) && (r[s] = n[s]); }), r; } else throw new Error("Please check mark() to ensure that it is a stable marker draftable function."); } function V(n) { n.copy || (n.copy = On(n.original, n.options)); } function Ce(n) { if (!ie(n)) return jt(n); if (Array.isArray(n)) return n.map(Ce); if (n instanceof Map) { const t = Array.from(n.entries()).map(([r, s]) => [ r, Ce(s) ]); if (!Pt(n)) { const r = Object.getPrototypeOf(n).constructor; return new r(t); } return new Map(t); } if (n instanceof Set) { const t = Array.from(n).map(Ce); if (!Mt(n)) { const r = Object.getPrototypeOf(n).constructor; return new r(t); } return new Set(t); } const e = Object.create(Object.getPrototypeOf(n)); for (const t in n) e[t] = Ce(n[t]); return e; } function We(n) { return ye(n) ? Ce(n) : n; } function ce(n) { var e; n.assignedMap = (e = n.assignedMap) !== null && e !== void 0 ? e : /* @__PURE__ */ new Map(), n.operated || (n.operated = !0, n.parent && ce(n.parent)); } function zt() { throw new Error("Cannot modify frozen object"); } function _e(n, e, t, r, s) { { t = t ?? /* @__PURE__ */ new WeakMap(), r = r ?? [], s = s ?? []; const o = t.has(n) ? t.get(n) : n; if (r.length > 0) { const a = r.indexOf(o); if (o && typeof o == "object" && a !== -1) throw r[0] === o ? new Error("Forbids circular reference") : new Error(`Forbids circular reference: ~/${s.slice(0, a).map((c, u) => { if (typeof c == "symbol") return `[${c.toString()}]`; const l = r[u]; return typeof c == "object" && (l instanceof Map || l instanceof Set) ? Array.from(l.keys()).indexOf(c) : c; }).join("/")}`); r.push(o), s.push(e); } else r.push(o); } if (Object.isFrozen(n) || ye(n)) { r.pop(), s.pop(); return; } switch (be(n)) { case 2: for (const [a, c] of n) _e(a, a, t, r, s), _e(c, a, t, r, s); n.set = n.clear = n.delete = zt; break; case 3: for (const a of n) _e(a, a, t, r, s); n.add = n.clear = n.delete = zt; break; case 1: Object.freeze(n); let o = 0; for (const a of n) _e(a, o, t, r, s), o += 1; break; default: Object.freeze(n), Object.keys(n).forEach((a) => { const c = n[a]; _e(c, a, t, r, s); }); } r.pop(), s.pop(); } function $t(n, e) { const t = be(n); if (t === 0) Reflect.ownKeys(n).forEach((r) => { e(r, n[r], n); }); else if (t === 1) { let r = 0; for (const s of n) e(r, s, n), r += 1; } else n.forEach((r, s) => e(s, r, n)); } function In(n, e, t) { if (ye(n) || !ie(n, t) || e.has(n) || Object.isFrozen(n)) return; const r = n instanceof Set, s = r ? /* @__PURE__ */ new Map() : void 0; if (e.add(n), $t(n, (i, o) => { var a; if (ye(o)) { const c = I(o); V(c); const u = !((a = c.assignedMap) === null || a === void 0) && a.size || c.operated ? c.copy : c.original; Re(r ? s : n, i, u); } else In(o, e, t); }), s) { const i = n, o = Array.from(i); i.clear(), o.forEach((a) => { i.add(s.has(a) ? s.get(a) : a); }); } } function Sr(n, e) { const t = n.type === 3 ? n.setMap : n.copy; n.finalities.revoke.length > 1 && n.assignedMap.get(e) && t && In(de(t, e), n.finalities.handledSet, n.options); } function ft(n) { n.type === 3 && n.copy && (n.copy.clear(), n.setMap.forEach((e) => { n.copy.add(jt(e)); })); } function ht(n, e, t, r) { if (n.operated && n.assignedMap && n.assignedMap.size > 0 && !n.finalized) { if (t && r) { const i = kn(n); i && e(n, i, t, r); } n.finalized = !0; } } function xt(n, e, t, r) { const s = I(t); s && (s.callbacks || (s.callbacks = []), s.callbacks.push((i, o) => { var a; const c = n.type === 3 ? n.setMap : n.copy; if (le(de(c, e), t)) { let u = s.original; s.copy && (u = s.copy), ft(n), ht(n, r, i, o), n.options.enableAutoFreeze && (n.options.updatedValues = (a = n.options.updatedValues) !== null && a !== void 0 ? a : /* @__PURE__ */ new WeakMap(), n.options.updatedValues.set(u, s.original)), Re(c, e, u); } }), n.options.enableAutoFreeze && s.finalities !== n.finalities && (n.options.enableAutoFreeze = !1)), ie(t, n.options) && n.finalities.draft.push(() => { const i = n.type === 3 ? n.setMap : n.copy; le(de(i, e), t) && Sr(n, e); }); } function Tr(n, e, t, r, s) { let { original: i, assignedMap: o, options: a } = n, c = n.copy; c.length < i.length && ([i, c] = [c, i], [t, r] = [r, t]); for (let u = 0; u < i.length; u += 1) if (o.get(u.toString()) && c[u] !== i[u]) { const l = e.concat([u]), f = pe(l, s); t.push({ op: x.Replace, path: f, // If it is a draft, it needs to be deep cloned, and it may also be non-draft. value: We(c[u]) }), r.push({ op: x.Replace, path: f, // If it is a draft, it needs to be deep cloned, and it may also be non-draft. value: We(i[u]) }); } for (let u = i.length; u < c.length; u += 1) { const l = e.concat([u]), f = pe(l, s); t.push({ op: x.Add, path: f, // If it is a draft, it needs to be deep cloned, and it may also be non-draft. value: We(c[u]) }); } if (i.length < c.length) { const { arrayLengthAssignment: u = !0 } = a.enablePatches; if (u) { const l = e.concat(["length"]), f = pe(l, s); r.push({ op: x.Replace, path: f, value: i.length }); } else for (let l = c.length; i.length < l; l -= 1) { const f = e.concat([l - 1]), d = pe(f, s); r.push({ op: x.Remove, path: d }); } } } function vr({ original: n, copy: e, assignedMap: t }, r, s, i, o) { t.forEach((a, c) => { const u = de(n, c), l = We(de(e, c)), f = a ? je(n, c) ? x.Replace : x.Add : x.Remove; if (le(u, l) && f === x.Replace) return; const d = r.concat(c), h = pe(d, o); s.push(f === x.Remove ? { op: f, path: h } : { op: f, path: h, value: l }), i.push(f === x.Add ? { op: x.Remove, path: h } : f === x.Remove ? { op: x.Add, path: h, value: u } : { op: x.Replace, path: h, value: u }); }); } function Ar({ original: n, copy: e }, t, r, s, i) { let o = 0; n.forEach((a) => { if (!e.has(a)) { const c = t.concat([o]), u = pe(c, i); r.push({ op: x.Remove, path: u, value: a }), s.unshift({ op: x.Add, path: u, value: a }); } o += 1; }), o = 0, e.forEach((a) => { if (!n.has(a)) { const c = t.concat([o]), u = pe(c, i); r.push({ op: x.Add, path: u, value: a }), s.unshift({ op: x.Remove, path: u, value: a }); } o += 1; }); } function xe(n, e, t, r) { const { pathAsArray: s = !0 } = n.options.enablePatches; switch (n.type) { case 0: case 2: return vr(n, e, t, r, s); case 1: return Tr(n, e, t, r, s); case 3: return Ar(n, e, t, r, s); } } const Be = (n, e, t = !1) => { if (typeof n == "object" && n !== null && (!ie(n, e) || t)) throw new Error("Strict mode: Mutable data cannot be accessed directly, please use 'unsafe(callback)' wrap."); }, pt = { get size() { return X(I(this)).size; }, has(n) { return X(I(this)).has(n); }, set(n, e) { const t = I(this), r = X(t); return (!r.has(n) || !le(r.get(n), e)) && (V(t), ce(t), t.assignedMap.set(n, !0), t.copy.set(n, e), xt(t, n, e, xe)), this; }, delete(n) { if (!this.has(n)) return !1; const e = I(this); return V(e), ce(e), e.original.has(n) ? e.assignedMap.set(n, !1) : e.assignedMap.delete(n), e.copy.delete(n), !0; }, clear() { const n = I(this); if (this.size) { V(n), ce(n), n.assignedMap = /* @__PURE__ */ new Map(); for (const [e] of n.original) n.assignedMap.set(e, !1); n.copy.clear(); } }, forEach(n, e) { const t = I(this); X(t).forEach((r, s) => { n.call(e, this.get(s), s, this); }); }, get(n) { var e, t; const r = I(this), s = X(r).get(n), i = ((t = (e = r.options).mark) === null || t === void 0 ? void 0 : t.call(e, s, ee)) === ee.mutable; if (r.options.strict && Be(s, r.options, i), i || r.finalized || !ie(s, r.options) || s !== r.original.get(n)) return s; const o = Et.createDraft({ original: s, parentDraft: r, key: n, finalities: r.finalities, options: r.options }); return V(r), r.copy.set(n, o), o; }, keys() { return X(I(this)).keys(); }, values() { const n = this.keys(); return { [ze]: () => this.values(), next: () => { const e = n.next(); return e.done ? e : { done: !1, value: this.get(e.value) }; } }; }, entries() { const n = this.keys(); return { [ze]: () => this.entries(), next: () => { const e = n.next(); if (e.done) return e; const t = this.get(e.value); return { done: !1, value: [e.value, t] }; } }; }, [ze]() { return this.entries(); } }, kr = Reflect.ownKeys(pt), Wt = (n, e, { isValuesIterator: t }) => () => { var r, s; const i = e.next(); if (i.done) return i; const o = i.value; let a = n.setMap.get(o); const c = I(a), u = ((s = (r = n.options).mark) === null || s === void 0 ? void 0 : s.call(r, a, ee)) === ee.mutable; if (n.options.strict && Be(o, n.options, u), !u && !c && ie(o, n.options) && !n.finalized && n.original.has(o)) { const l = Et.createDraft({ original: o, parentDraft: n, key: o, finalities: n.finalities, options: n.options }); n.setMap.set(o, l), a = l; } else c && (a = c.proxy); return { done: !1, value: t ? a : [a, a] }; }, Ge = { get size() { return I(this).setMap.size; }, has(n) { const e = I(this); if (e.setMap.has(n)) return !0; V(e); const t = I(n); return !!(t && e.setMap.has(t.original)); }, add(n) { const e = I(this); return this.has(n) || (V(e), ce(e), e.assignedMap.set(n, !0), e.setMap.set(n, n), xt(e, n, n, xe)), this; }, delete(n) { if (!this.has(n)) return !1; const e = I(this); V(e), ce(e); const t = I(n); return t && e.setMap.has(t.original) ? (e.assignedMap.set(t.original, !1), e.setMap.delete(t.original)) : (!t && e.setMap.has(n) ? e.assignedMap.set(n, !1) : e.assignedMap.delete(n), e.setMap.delete(n)); }, clear() { if (!this.size) return; const n = I(this); V(n), ce(n); for (const e of n.original) n.assignedMap.set(e, !1); n.setMap.clear(); }, values() { const n = I(this); V(n); const e = n.setMap.keys(); return { [Symbol.iterator]: () => this.values(), next: Wt(n, e, { isValuesIterator: !0 }) }; }, entries() { const n = I(this); V(n); const e = n.setMap.keys(); return { [Symbol.iterator]: () => this.entries(), next: Wt(n, e, { isValuesIterator: !1 }) }; }, keys() { return this.values(); }, [ze]() { return this.values(); }, forEach(n, e) { const t = this.values(); let r = t.next(); for (; !r.done; ) n.call(e, r.value, r.value, this), r = t.next(); } }; Set.prototype.difference && Object.assign(Ge, { intersection(n) { return Set.prototype.intersection.call(new Set(this.values()), n); }, union(n) { return Set.prototype.union.call(new Set(this.values()), n); }, difference(n) { return Set.prototype.difference.call(new Set(this.values()), n); }, symmetricDifference(n) { return Set.prototype.symmetricDifference.call(new Set(this.values()), n); }, isSubsetOf(n) { return Set.prototype.isSubsetOf.call(new Set(this.values()), n); }, isSupersetOf(n) { return Set.prototype.isSupersetOf.call(new Set(this.values()), n); }, isDisjointFrom(n) { return Set.prototype.isDisjointFrom.call(new Set(this.values()), n); } }); const Or = Reflect.ownKeys(Ge), Cn = /* @__PURE__ */ new WeakSet(), En = { get(n, e, t) { var r, s; const i = (r = n.copy) === null || r === void 0 ? void 0 : r[e]; if (i && Cn.has(i)) return i; if (e === An) return n; let o; if (n.options.mark) { const u = e === "size" && (n.original instanceof Map || n.original instanceof Set) ? Reflect.get(n.original, e) : Reflect.get(n.original, e, t); if (o = n.options.mark(u, ee), o === ee.mutable) return n.options.strict && Be(u, n.options, !0), u; } const a = X(n); if (a instanceof Map && kr.includes(e)) { if (e === "size") return Object.getOwnPropertyDescriptor(pt, "size").get.call(n.proxy); const u = pt[e]; if (u) return u.bind(n.proxy); } if (a instanceof Set && Or.includes(e)) { if (e === "size") return Object.getOwnPropertyDescriptor(Ge, "size").get.call(n.proxy); const u = Ge[e]; if (u) return u.bind(n.proxy); } if (!je(a, e)) { const u = qt(a, e); return u ? "value" in u ? u.value : ( // !case: support for getter (s = u.get) === null || s === void 0 ? void 0 : s.call(n.proxy) ) : void 0; } const c = a[e]; if (n.options.strict && Be(c, n.options), n.finalized || !ie(c, n.options)) return c; if (c === rt(n.original, e)) { if (V(n), n.copy[e] = Rt({ original: n.original[e], parentDraft: n, key: n.type === 1 ? Number(e) : e, finalities: n.finalities, options: n.options }), typeof o == "function") { const u = I(n.copy[e]); return V(u), ce(u), u.copy; } return n.copy[e]; } return c; }, set(n, e, t) { var r; if (n.type === 3 || n.type === 2) throw new Error("Map/Set draft does not support any property assignment."); let s; if (n.type === 1 && e !== "length" && !(Number.isInteger(s = Number(e)) && s >= 0 && (e === 0 || s === 0 || String(s) === String(e)))) throw new Error("Only supports setting array indices and the 'length' property."); const i = qt(X(n), e); if (i?.set) return i.set.call(n.proxy, t), !0; const o = rt(X(n), e), a = I(o); return a && le(a.original, t) ? (n.copy[e] = t, n.assignedMap = (r = n.assignedMap) !== null && r !== void 0 ? r : /* @__PURE__ */ new Map(), n.assignedMap.set(e, !1), !0) : (le(t, o) && (t !== void 0 || je(n.original, e)) || (V(n), ce(n), je(n.original, e) && le(t, n.original[e]) ? n.assignedMap.delete(e) : n.assignedMap.set(e, !0), n.copy[e] = t, xt(n, e, t, xe)), !0); }, has(n, e) { return e in X(n); }, ownKeys(n) { return Reflect.ownKeys(X(n)); }, getOwnPropertyDescriptor(n, e) { const t = X(n), r = Reflect.getOwnPropertyDescriptor(t, e); return r && { writable: !0, configurable: n.type !== 1 || e !== "length", enumerable: r.enumerable, value: t[e] }; }, getPrototypeOf(n) { return Reflect.getPrototypeOf(n.original); }, setPrototypeOf() { throw new Error("Cannot call 'setPrototypeOf()' on drafts"); }, defineProperty() { throw new Error("Cannot call 'defineProperty()' on drafts"); }, deleteProperty(n, e) { var t; return n.type === 1 ? En.set.call(this, n, e, void 0, n.proxy) : (rt(n.original, e) !== void 0 || e in n.original ? (V(n), ce(n), n.assignedMap.set(e, !1)) : (n.assignedMap = (t = n.assignedMap) !== null && t !== void 0 ? t : /* @__PURE__ */ new Map(), n.assignedMap.delete(e)), n.copy && delete n.copy[e], !0); } }; function Rt(n) { const { original: e, parentDraft: t, key: r, finalities: s, options: i } = n, o = be(e), a = { type: o, finalized: !1, parent: t, original: e, copy: null, proxy: null, finalities: s, options: i, // Mapping of draft Set items to their corresponding draft values. setMap: o === 3 ? new Map(e.entries()) : void 0 }; (r || "key" in n) && (a.key = r); const { proxy: c, revoke: u } = Proxy.revocable(o === 1 ? Object.assign([], a) : a, En); if (s.revoke.push(u), Cn.add(c), a.proxy = c, t) { const l = t; l.finalities.draft.push((f, d) => { var h, p; const m = I(c); let b = l.type === 3 ? l.setMap : l.copy; const y = de(b, r), g = I(y); if (g) { let A = g.original; g.operated && (A = jt(y)), ft(g), ht(g, xe, f, d), l.options.enableAutoFreeze && (l.options.updatedValues = (h = l.options.updatedValues) !== null && h !== void 0 ? h : /* @__PURE__ */ new WeakMap(), l.options.updatedValues.set(A, g.original)), Re(b, r, A); } (p = m.callbacks) === null || p === void 0 || p.forEach((A) => { A(f, d); }); }); } else { const l = I(c); l.finalities.draft.push((f, d) => { ft(l), ht(l, xe, f, d); }); } return c; } Et.createDraft = Rt; function Ir(n, e, t, r, s) { var i; const o = I(n), a = (i = o?.original) !== null && i !== void 0 ? i : n, c = !!e.length; if (o?.operated) for (; o.finalities.draft.length > 0; ) o.finalities.draft.pop()(t, r); const u = c ? e[0] : o ? o.operated ? o.copy : o.original : n; return o && dt(o), s && _e(u, u, o?.options.updatedValues), [ u, t && c ? [{ op: x.Replace, path: [], value: e[0] }] : t, r && c ? [{ op: x.Replace, path: [], value: a }] : r ]; } function Cr(n, e) { var t; const r = { draft: [], revoke: [], handledSet: /* @__PURE__ */ new WeakSet() }; let s, i; e.enablePatches && (s = [], i = []); const a = ((t = e.mark) === null || t === void 0 ? void 0 : t.call(e, n, ee)) === ee.mutable || !ie(n, e) ? n : Rt({ original: n, parentDraft: null, finalities: r, options: e }); return [ a, (c = []) => { const [u, l, f] = Ir(a, c, s, i, e.enableAutoFreeze); return e.enablePatches ? [u, l, f] : u; } ]; } function yt(n) { const { rootDraft: e, value: t, useRawReturn: r = !1, isRoot: s = !0 } = n; $t(t, (i, o, a) => { const c = I(o); if (c && e && c.finalities === e.finalities) { n.isContainDraft = !0; const u = c.original; if (a instanceof Set) { const l = Array.from(a); a.clear(), l.forEach((f) => a.add(i === f ? u : f)); } else Re(a, i, u); } else typeof o == "object" && o !== null && (n.value = o, n.isRoot = !1, yt(n)); }), s && (n.isContainDraft || console.warn("The return value does not contain any draft, please use 'rawReturn()' to wrap the return value to improve performance."), r && console.warn("The return value contains drafts, please don't use 'rawReturn()' to wrap the return value.")); } function Mn(n) { var e; const t = I(n); if (!ie(n, t?.options)) return n; const r = be(n); if (t && !t.operated) return t.original; let s; function i() { s = r === 2 ? Pt(n) ? new Map(n) : new (Object.getPrototypeOf(n)).constructor(n) : r === 3 ? Array.from(t.setMap.values()) : On(n, t?.options); } if (t) { t.finalized = !0; try { i(); } finally { t.finalized = !1; } } else s = n; if ($t(s, (o, a) => { if (t && le(de(t.original, o), a)) return; const c = Mn(a); c !== a && (s === n && i(), Re(s, o, c)); }), r === 3) { const o = (e = t?.original) !== null && e !== void 0 ? e : s; return Mt(o) ? new Set(s) : new (Object.getPrototypeOf(o)).constructor(s); } return s; } function Vt(n) { if (!ye(n)) throw new Error(`current() is only used for Draft, parameter: ${n}`); return Mn(n); } const Er = (n) => function e(t, r, s) { var i, o, a; if (typeof t == "function" && typeof r != "function") return function(k, ...F) { return e(k, (C) => t.call(this, C, ...F), r); }; const c = t, u = r; let l = s; if (typeof r != "function" && (l = r), l !== void 0 && Object.prototype.toString.call(l) !== "[object Object]") throw new Error(`Invalid options: ${l}, 'options' should be an object.`); l = Object.assign(Object.assign({}, n), l); const f = ye(c) ? Vt(c) : c, d = Array.isArray(l.mark) ? ((k, F) => { for (const C of l.mark) { if (typeof C != "function") throw new Error(`Invalid mark: ${C}, 'mark' should be a function.`); const O = C(k, F); if (O) return O; } }) : l.mark, h = (i = l.enablePatches) !== null && i !== void 0 ? i : !1, p = (o = l.strict) !== null && o !== void 0 ? o : !1, b = { enableAutoFreeze: (a = l.enableAutoFreeze) !== null && a !== void 0 ? a : !1, mark: d, strict: p, enablePatches: h }; if (!ie(f, b) && typeof f == "object" && f !== null) throw new Error("Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable."); const [y, g] = Cr(f, b); if (typeof r != "function") { if (!ie(f, b)) throw new Error("Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable."); return [y, g]; } let A; try { A = u(y); } catch (k) { throw dt(I(y)), k; } const R = (k) => { const F = I(y); if (!ye(k)) { if (k !== void 0 && !le(k, y) && F?.operated) throw new Error("Either the value is returned as a new non-draft value, or only the draft is modified without returning any value."); const O = k?.[gr]; if (O) { const z = O[0]; return b.strict && typeof k == "object" && k !== null && yt({ rootDraft: F, value: k, useRawReturn: !0 }), g([z]); } if (k !== void 0) return typeof k == "object" && k !== null && yt({ rootDraft: F, value: k }), g([k]); } if (k === y || k === void 0) return g([]); const C = I(k); if (b === C.options) { if (C.operated) throw new Error("Cannot return a modified child draft."); return g([Vt(k)]); } return g([k]); }; return A instanceof Promise ? A.then(R, (k) => { throw dt(I(y)), k; }) : R(A); }, He = Er(); Object.prototype.constructor.toString(); function Pn(n, e) { const t = Object.keys(n), r = Object.keys(e); return t.length === r.length && Object.keys(n).every((s) => e.hasOwnProperty(s)); } function Qt(n, e) { return Object.keys(n).length === Object.keys(e).length && Object.keys(n).every( (t) => e.hasOwnProperty(t) && n[t] === e[t] ); } function Ve(n, e) { return typeof n != "object" || typeof e != "object" || n === null || e === null ? n === e : Pn(n, e) ? Object.keys(n).every( (t) => Ve(n[t], e[t]) ) : !1; } function Dt(n) { if (!Ee(n)) return n; const e = {}; for (const [t, r] of Object.entries(n)) r !== void 0 && (e[t] = r); return e; } function jn(n, e) { if (!Ee(n) || !Ee(e)) return e; const t = { ...n }; for (const r of Object.keys(e)) { if (e[r] === void 0) continue; if (e[r] === null) { delete t[r]; continue; } const s = Ee(n[r]) && Ee(e[r]); t[r] = s ? jn(n[r], e[r]) : e[r]; } return t; } function Ee(n) { return typeof n == "object" && n !== null && !Array.isArray(n); } function Mr(n, e, t) { if (!n || e.length === 0) return; let r = n || {}; for (let i = 0; i < e.length - 1; i++) { const o = e[i]; (!(o in r) || typeof r[o] != "object") && (r[o] = typeof e[i + 1] == "number" ? [] : {}), r = r[o]; } const s = e[e.length - 1]; Array.isArray(r) && typeof s == "number" ? r.splice(s, 0, t) : r[s] = t; } function Bt(n, e, t) { if (!n || e.length === 0) return; let r = n || {}; for (let s = 0; s < e.length - 1; s++) { const i = e[s]; (!(i in r) || typeof r[i] != "object") && (r[i] = typeof e[s + 1] == "number" ? [] : {}), r = r[i]; } r[e[e.length - 1]] = t; } function $n(n, e) { if (!n || e.length === 0) return; const [t, ...r] = e; if (t in n) { if (r.length === 0) { Array.isArray(n) ? n.splice(t, 1) : delete n[t]; return; } $n(n[t], r), Pr(n[t]) && delete n[t]; } } function Pr(n) { return n && Object.keys(n).length === 0; } const Gt = /ZULU|YEKT|YEKST|YAPT|YAKT|YAKST|XJT|WGT|WGST|WFT|WETDST|WET|WDT|WAT|WAST|WAKT|WADT|VUT|VOLT|VLAT|VLAST|VET|UZT|UZST|UYT|UYST|UTC|UT|ULAT|ULAST|UCT|TVT|TRUT|TOT|TMT|TKT|TJT|TFT|TAHT|SGT|SCT|SAST|SADT|RET|PYT|PYST|PWT|PST|PONT|PMST|PMDT|PKT|PKST|PHT|PGT|PETT|PETST|PET|PDT|OMST|OMSST|NZT|NZST|NZDT|NUT|NST|NPT|NOVT|NOVST|NFT|NDT|MYT|MVT|MUT|MUST|MST|MSK|MSD|MPT|MMT|MHT|MEZ|METDST|MET|MESZ|MEST|MDT|MAWT|MART|MAGT|MAGST|LKT|LINT|LIGT|LHST|LHDT|KST|KRAT|KRAST|KOST|KGT|KGST|KDT|JST|JAYT|IST|IRT|IRKT|IRKST|IOT|IDT|ICT|HST|HKT|GYT|GMT|GILT|GFT|GET|GEST|GAMT|GALT|FNT|FNST|FKT|FKST|FJT|FJST|FET|EST|EGT|EGST|EETDST|EET|EEST|EDT|EAT|EAST|EASST|DDUT|DAVT|CXT|CST|COT|CLT|CLST|CKT|CHUT|CHAST|CHADT|CETDST|CET|CEST|CDT|CCT|CAST|CADT|BTT|BST|BRT|BRST|BRA|BOT|BORT|BNT|BDT|BDST|AZT|AZST|AZOT|AZOST|AWST|AWSST|AST|ART|ARST|ANAT|ANAST|AMT|AMST|ALMT|ALMST|AKST|AKDT|AFT|AEST|AESST|AEDT|ADT|ACWST|ACT|ACST|ACSST|ACDT$/, jr = { ZULU: 0, YEKT: 18e3, YEKST: 21600, YAPT: 36e3, YAKT: 32400, YAKST: 32400, XJT: 21600, WGT: -10800, WGST: -7200, WFT: 43200, WETDST: 3600, WET: 0, WDT: 32400, WAT: 3600, WAST: 25200, WAKT: 43200, WADT: 28800, VUT: 39600, VOLT: 10800, VLAT: 36e3, VLAST: 36e3, VET: -14400, UZT: 18e3, UZST: 21600, UYT: -10800, UYST: -7200, UTC: 0, UT: 0, ULAT: 28800, ULAST: 32400, UCT: 0, TVT: 43200, TRUT: 36e3, TOT: 46800, TMT: 18e3, TKT: 46800, TJT: 18e3, TFT: 18e3, TAHT: -36e3, SGT: 28800, SCT: 14400, SAST: 7200, SADT: 37800, RET: 14400, PYT: -14400, PYST: -10800, PWT: 32400, PST: -28800, PONT: 39600, PMST: -10800, PMDT: -7200, PKT: 18e3, PKST: 21600, PHT: 28800, PGT: 36e3, PETT: 43200, PETST: 43200, PET: -18e3, PDT: -25200, OMST: 21600, OMSST: 21600, NZT: 43200, NZST: 43200, NZDT: 46800, NUT: -39600, NST: -12600, NPT: 20700, NOVT: 25200, NOVST: 25200, NFT: -12600, NDT: -9e3, MYT: 28800, MVT: 18e3, MUT: 14400, MUST: 18e3, MST: -25200, MSK: 10800, MSD: 14400, MPT: 36e3, MMT: 23400, MHT: 43200, MEZ: 3600, METDST: 7200, MET: 3600, MESZ: 7200, MEST: 7200, MDT: -21600, MAWT: 18e3, MART: -34200, MAGT: 39600, MAGST: 39600, LKT: 19800, LINT: 50400, LIGT: 36e3, LHST: 37800, LHDT: 37800, KST: 32400, KRAT: 25200, KRAST: 25200, KOST: 39600, KGT: 21600, KGST: 21600, KDT: 36e3, JST: 32400, JAYT: 32400, IST: 7200, IRT: 12600, IRKT: 28800, IRKST: 28800, IOT: 21600, IDT: 10800, ICT: 25200, HST: -36e3, HKT: 28800, GYT: -14400, GMT: 0, GILT: 43200, GFT: -10800, GET: 14400, GEST: 14400, GAMT: -32400, GALT: -21600, FNT: -7200, FNST: -3600, FKT: -10800, FKST: -10800, FJT: 43200, FJST: 46800, FET: 10800, EST: -18e3, EGT: -3600, EGST: 0, EETDST: 10800, EET: 7200, EEST: 10800, EDT: -14400, EAT: 10800, EAST: -21600, EASST: -21600, DDUT: 36e3, DAVT: 25200, CXT: 25200, CST: -21600, COT: -18e3, CLT: -14400, CLST: -10800, CKT: -36e3, CHUT: 36e3, CHAST: 45900, CHADT: 49500, CETDST: 7200, CET: 3600, CEST: 7200, CDT: -18e3, CCT: 28800, CAST: 34200, CADT: 37800, BTT: 21600, BST: 3600, BRT: -10800, BRST: -7200, BRA: -10800, BOT: -14400, BORT: 28800, BNT: 28800, BDT: 21600, BDST: 7200, AZT: 14400, AZST: 14400, AZOT: -3600, AZOST: 0, AWST: 28800, AWSST: 32400, AST: -14400, ART: -10800, ARST: -10800, ANAT: 43200, ANAST: 43200, AMT: -14400, AMST: 14400, ALMT: 21600, ALMST: 25200, AKST: -32400, AKDT: -28800, AFT: 16200, AEST: 36e3, AESST: 39600, AEDT: 39600, ADT: -10800, ACWST: 31500, ACT: -18e3, ACST: 34200, ACSST: 37800, ACDT: 37800 }; function $r(n) { return new Date(n); } function xr(n) { return /* @__PURE__ */ new Date(n + "Z"); } const Rr = /^(\d+)[\./-](\d+)[\./-](\d+)$/; function Dr(n) { const e = n.match(Rr); if (!e) return null; const [t, r, s, i] = e; return r <= 0 || s <= 0 || i <= 0 ? null : r > 999 ? new Date(Date.UTC(r, s - 1, i, 0, 0, 0, 0)) : new Date(Date.UTC(i, r - 1, s, 0, 0, 0, 0)); } function Ur(n) { const [e, t] = n.split(" "); return /* @__PURE__ */ new Date(e + "T" + t + "Z"); } function Lr(n) { const [e, t] = n.split(" "); return /* @__PURE__ */ new Date(e + "T" + t + "Z"); } function Fr(n) { return new Date(n); } function Nr(n) { const e = /^(\w{3}) (\w{3}) (\d{2}) (\d{4})$/; if (!n.match(e)) throw new Error(`Unable to parse \`${n}\` as a date.`); const r = /* @__PURE__ */ new Date(n + " UTC"); return new Date( Date.UTC( r.getUTCFullYear(), r.getUTCMonth(), r.getUTCDate(), 0, 0, 0, 0 ) ); } function Kr(n) { const e = /^(.+T.+)([+-])(\d{2})$/, t = n.match(e); if (t) { const [, r, s, i] = t, o = `${r}${s}${i}:00`; return new Date(o); } return null; } function qr(n) { const e = /^(\d+)-(\d{1,2})-(\d{1,2})([ T])(.+)$/, t = n.match(e); if (t) { const [, r, s, i, o, a] = t, c = s.padStart(2, "0"), u = i.padStart(2, "0"), l = `${r}-${c}-${u}T${a}`; return new Date(l); } return null; } function zr(n) { const [e, t] = n.split(", "), [r, s, i] = e.split("/").map(Number), o = t.match(/(\d{1,2}):(\d{2}):(\d{2}) (AM|PM)/); if (!o) throw new Error(`Unable to parse time from: ${n}`); let [, a, c, u, l] = o; return a = Number(a), c = Number(c), u = Number(u), l === "PM" && a !== 12 ? a += 12 : l === "AM" && a === 12 && (a = 0), new Date(Date.UTC(i, r - 1, s, a, c, u)); } function Wr(n) { switch (n) { case "epoch": return /* @__PURE__ */ new Date(0); // These are not implemented yet because we need some way for the // client and server to aggree on the values case "infinity": case "-infinity": case "today": case "tomorrow": case "yesterday": return null; } } function Vr(n) { const e = n.match(Gt); if (!e) return null; const [t] = e, r = jr[t], s = new Date(n.replace(Gt, "Z")); return new Date(s.getTime() - r * 1e3); } const Qr = [ Dr, Lr, Nr, zr, Fr, xr, Kr, Ur, $r, Wr, Vr, qr ]; function Br(n, e) { try { const t = n(e); return t instanceof Date && !isNaN(t.getTime()) ? t : null; } catch { return null; } } function bt(n) { for (const e of Qr) { const t = Br(e, n); if (t) return t; } return null; } function Gr(n) { try { const e = JSON.parse(n); return typeof e == "string" ? bt(e) : null; } catch { return null; } } function Ze(n) { if (n !== void 0) { if (n === null) return null; if (n instanceof Date) return n; if (typeof n == "string") { const e = bt(n) || Gr(n) || bt(n.trim()); if (!e) throw new Error(`Unable to parse \`${n}\` as a date.`); return e; } else if (typeof n == "number") return new Date(n); throw new Error( `Invalid date value \`${n}\`. Expected a date, number, or string, got type ${typeof n}.` ); } } class fe { attrs; linkIndex; _blobAttrs = null; _primaryKeys = null; _forwardIdents = null; _revIdents = null; constructor(e, t) { this.attrs = e, this.linkIndex = t; } resetAttrIndexes() { this._blobAttrs = null, this._primaryKeys = null, this._forwardIdents = null, this._revIdents = null; } addAttr(e) { this.attrs[e.id] = e, this.resetAttrIndexes(); } deleteAttr(e) { delete this.attrs[e], this.resetAttrIndexes(); } updateAttr(e) { const t = this.attrs[e.id]; t && (this.attrs[e.id] = { ...t, ...e }, this.resetAttrIndexes()); } getAttr(e) { return this.attrs[e]; } get blobAttrs() { if (this._blobAttrs) return this._blobAttrs; this._blobAttrs = /* @__PURE__ */ new Map(); for (const e of Object.values(this.attrs)) if (Lt(e)) { const [t, r, s] = e["forward-identity"]; H(this.blobAttrs, [r, s], e); } return this._blobAttrs; } get primaryKeys() { if (this._primaryKeys) return this._primaryKeys; this._primaryKeys = /* @__PURE__ */ new Map(); for (const e of Object.values(this.attrs)) if (e["primary?"]) { const [t, r] = e["forward-identity"]; H(this._primaryKeys, [r], e); } return this._primaryKeys; } get forwardIdents() { if (this._forwardIdents) return this._forwardIdents; this._forwardIdents = /* @__PURE__ */ new Map(); for (const e of Object.values(this.attrs)) { const t = e["forward-identity"], [r, s, i] = t; H(this._forwardIdents, [s, i], e); } return this._forwardIdents; } get revIdents() { if (this._revIdents) return this._revIdents; this._revIdents = /* @__PURE__ */ new Map(); for (const e of Object.values(this.attrs)) { const t = e["reverse-identity"]; if (t) { const [r, s, i] = t; H(this._revIdents, [s, i], e); } } return this._revIdents; } toJSON() { return { attrs: this.attrs, linkIndex: this.linkIndex }; } } function Hr(n) { return n.cardinality === "one"; } function Ut(n) { return n["value-type"] === "ref"; } function Lt(n) { return n["value-type"] === "blob"; } function Oe(n, e) { return e.reduce((t, r) => t && t.get(r), n); } function se(n, e) { if (e.length === 0) throw new Error("path must have at least one element"); if (e.length === 1) { n.delete(e[0]); return; } const [t, ...r] = e; n.has(t) && se(n.get(t), r); } function H(n, e, t) { let r = n; const s = e.length - 1; for (let i = 0; i < s; i++) { const o = e[i]; let a = r.get(o); a === void 0 && (a = /* @__PURE__ */ new Map(), r.set(o, a)), r = a; } s > -1 && r.set(e[s], t); } function xn(n, e, t) { const r = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(); for (const o of e) { let [a, c, u] = o; const l = n.getAttr(c); if (!l) { console.warn("no such attr", c, a); continue; } l["checked-data-type"] === "date" && t && (u = Ze(u), o[2] = u), Ut(l) && H(i, [u, c, a], o), H(r, [a, c, u], o), H(s, [c, a, u], o); } return { eav: r, aev: s, vae: i }; } function Rn(n) { return { triples: J(n.eav, 3), cardinalityInference: n.cardinalityInference, useDateObjects: n.useDateObjects, version: 1 }; } function Dn(n, e) { return Se( n, e.triples, e.cardinalityInference, e.useDateObjects ); } function Un(n, e) { if (n) return new fe(n.attrs, n.linkIndex); if (e && "__type" in e) return new fe(e.attrs, e.linkIndex); } function Jr(n, e) { return Oe(n.eav, [e]) !== void 0; } function Se(n, e, t, r) { const s = xn( n, e, r ); return s.cardinalityInference = t, s.useDateObjects = r, s; } function De(n, e) { let t; if (Array.isArray(e[0])) { const [s, i] = e[0], o = n.aev.get(s); if (!o) return null; t = J(o, 2).find((c) => c[2] === i)?.[0]; } else t = e[0]; if (!t) return null; const r = e[2]; if (Array.isArray(r) && r.length === 2 && n.aev.get(r[0])) { const [s, i] = r, o = n.aev.get(s); if (!o) return null; const c = J(o, 2).find((h) => h[2] === i)?.[0]; if (!c) return null; const [u, l, f, ...d] = e; return [t, l, c, ...d]; } else { const [s, ...i] = e; return [t, ...i]; } } function Ln(n, e, t) { const r = De(n, t); if (!r) return; const [s, i, o] = r, a = e.getAttr(i); a && (se(n.eav, [s, i, o]), se(n.aev, [i, s, o]), Ut(a) && se(n.vae, [o, i, s])); } let Yr = 0; function Fn(n, e, t) { const [r, s, i] = t; let o; const a = Oe(n.eav, [r, s, i]); return a && (o = a[3]), o || Date.now() * 10 + Yr++; } function Nn(n, e, t) { const r = De(n, t); if (!r) return; let [s, i, o] = r; const a = e.getAttr(i); if (!a) return; a["checked-data-type"] === "date" && n.useDateObjects && (o = Ze(o)); const u = Oe(n.eav, [s, i, o])?.[3] ?? Fn(n, a, r), l = [s, i, o, u]; Hr(a) ? (H(n.eav, [s, i], /* @__PURE__ */ new Map([[o, l]])), H(n.aev, [i, s], /* @__PURE__ */ new Map([[o, l]]))) : (H(n.eav, [s, i, o], l), H(n.aev, [i, s, o], l)), Ut(a) && H(n.vae, [o, i, s], l); } function Zr(n, e, t) { const r = De(n, t); if (!r) return; const [s, i, o] = r, a = e.getAttr(i); if (!a) return; if (!Lt(a)) throw new Error("merge operation is not supported for links"); const c = Oe(n.eav, [s, i]); if (!c) return; const u = c.values().next()?.value; if (!u) return; const l = u[2], f = jn(l, o), d = [ s, i, f, Fn(n, a, u) ]; H(n.eav, [s, i], /* @__PURE__ */ new Map([[f, d]])), H(n.aev, [i, s], /* @__PURE__ */ new Map([[f, d]])); } function gt(n, e, t) { const [r, s] = t, i = De(n, [r]); if (!i) return; const [o] = i, a = n.eav.get(o); if (a) { for (const u of a.keys()) { const l = e.getAttr(u); l && l["on-delete-reverse"] === "cascade" && J(a.get(u), 1).forEach( ([f, d, h]) => gt(n, e, [h, l["reverse-identity"]?.[1]]) ), // Fall back to deleting everything if we've rehydrated tx-steps from // the store that didn't set `etype` in deleteEntity (!s || // If we don't know about the attr, let's just get rid of it !l || // Make sure it matches the etype l["forward-identity"]?.[1] === s) && (se(n.aev, [u, o]), se(n.eav, [o, u])); } a.size === 0 && se(n.eav, [o]); } const c = n.vae.get(o) && J(n.vae.get(o), 2); c && c.forEach((u) => { const [l, f, d] = u, h = e.getAttr(f); (!s || !h || h["reverse-identity"]?.[1] === s) && (se(n.eav, [l, f, d]), se(n.aev, [f, l, d]), se(n.vae, [d, f, l])), h && h["on-delete"] === "cascade" && h["reverse-identity"]?.[1] === s && gt(n, e, [l, h["forward-identity"]?.[1]]); }), n.vae.get(o)?.size === 0 && se(n.vae, [o]); } function Kn(n, e, t) { const r = xn( e, t, n.useDateObjects ); Object.keys(r).forEach((s) => { n[s] = r[s]; }); } function Xr(n, [e]) { n.addAttr(e); } function qn(n) { return J(n.eav, 3); } function es(n, e, [t]) { if (!e.getAttr(t)) return; const r = qn(n).filter(([s, i]) => i !== t); e.deleteAttr(t), Kn(n, e, r); } function ts(n, e, [t]) { e.getAttr(t.id) && (e.updateAttr(t), Kn(n, e, qn(n))); } function ns(n, e, t) { const [r, ...s] = t; switch (r) { case "add-triple": Nn(n, e, s); break; case "deep-merge-triple": Zr(n, e, s); break; case "retract-triple": Ln(n, e, s); break; case "delete-entity": gt(n, e, s); break; case "add-attr": Xr(e, s); break; case "delete-attr": es(n, e, s); break; case "update-attr": ts(n, e, s); break; case "restore-attr": break; case "rule-params": break; default: throw new Error(`unhandled transaction action: ${r}`); } } function J(n, e, t = []) { if (!n || e === 0) return t; if (e === 1) { for (const r of n.values()) t.push(r); return t; } for (const r of n.values()) J(r, e - 1, t); return t; } function Le(n, e, t) { const r = []; if (t?.hasOwnProperty("$not")) { for (const i of e.keys()) t.$not !== i && r.push(e.get(i)); return r; } if (t?.hasOwnProperty("$isNull")) { const { attrId: i, isNull: o, reverse: a } = t.$isNull; if (a) for (const c of e.keys()) { const u = n.vae.get(c), l = !u || !u.get(i); (o ? l : !l) && r.push(e.get(c)); } else { const c = n.aev.get(i); for (const u of e.keys()) { const l = !c || c.get(u)?.get(null) || !c.get(u); (o ? l : !l) && r.push(e.get(u)); } } return r; } if (t?.$comparator) return J(e, 1).filter(t.$op); const s = t.in || t.$in || [t]; for (const i of s) { const o = e.get(i); o && r.push(o); } return r; } function rs(n, e, t) { let r = ""; return n !== void 0 && (r += "e"), e !== void 0 && (r += "a"), t !== void 0 && (r += "v"), r; } function ss(n, [e, t, r]) { switch (rs(e, t, r)) { case "e": { const i = n.eav.get(e); return J(i, 2); } case "ea": { const i = n.eav.get(e)?.get(t); return J(i, 1); } case "eav": { const i = n.eav.get(e)?.get(t); return i ? Le(n, i, r) : []; } case "ev": { const i = n.eav.get(e); if (!i) return []; const o = []; for (const a of i.values()) o.push(...Le(n, a, r)); return o; } case "a": { const i = n.aev.get(t); return J(i, 2); } case "av": { const i = n.aev.get(t); if (!i) return []; const o = []; for (const a of i.values()) o.push(...Le(n, a, r)); return o; } case "v": { const i = []; for (const o of n.eav.values()) for (const a of o.values()) i.push(...Le(n, a, r)); return i; } default: return J(n.eav, 3); } } function is(n, e, t) { const r = {}; if (!e) return r; for (const [s, i] of e.entries()) { const o = n.eav.get(t)?.get(i.id), a = J(o, 1); for (const c of a) r[s] = c[2]; } return r; } function U(n, e, t) { return n.forwardIdents.get(e)?.get(t); } function ge(n, e, t) { return n.revIdents.get(e)?.get(t); } function os(n, e) { return n.blobAttrs.get(e); } function zn(n, e) { const t = n.primaryKeys.get(e); return t || n.forwardIdents.get(e)?.get("id"); } function as(n, e, t) { const r = De(n, t); if (!r) return; const [s, i, o] = r; if (e.getAttr(i)) return Oe(n.eav, [s, i]); } function cs(n, e, t) { const r = t.filter( ([s, i, o, a, c]) => { if (s !== "add-triple" && s !== "deep-merge-triple") return !0; const u = c?.mode; if (u !== "create" && u !== "update") return !0; let l = !1; const f = e.getAttr(o); if (f) { const d = zn( e, f["forward-identity"][1] ); l = !!as(n, e, [ i, d?.id, i ]); } return !(u === "create" && l || u === "update" && !l); } ); return He( { store: n, attrsStore: e }, (s) => { r.forEach((i) => { ns(s.store, s.attrsStore, i); }); }, { mark: (s) => { if (s instanceof fe) return "immutable"; } } ); } function us(n) { return typeof n == "string" && n.startsWith("?"); } function ls(n, e, t) { if (t.hasOwnProperty(n)) { const r = t[n]; return Wn(r, e, t); } return { ...t, [n]: e }; } function Ht(n, e, t) { return n === e ? t : null; } function ds(n) { return typeof n === "string" && n.startsWith("?") ? ls : Ht; } const fs = [ "in", "$in", "$not", "$isNull", "$comparator" // covers all of $gt, $lt, etc. ]; function hs(n) { for (const e of fs) if (n.hasOwnProperty(e)) return !0; return !1; } function Wn(n, e, t) { return t ? typeof n == "object" ? hs(n) ? t : null : ds(n)(n, e, t) : null; } function ps(n, e, t) { return n.reduce((r, s, i) => { const o = e[i]; return Wn(s, o, r); }, t); } function ys(n, e, t) { return ms(n, e, t).map((r) => ps(e, r, t)).filter((r) => r); } function bs(n, e, t) { return e.or ? e.or.patterns.flatMap((r) => mt(n, r, t)) : e.and ? e.and.patterns.reduce((r, s) => mt(n, s, r), t) : t.flatMap((r) => ys(n, e, r)); } function mt(n, e, t = [{}]) { return e.reduce((r, s) => bs(n, s, r), t); } function Ft(n, e) { return Array.isArray(e) ? e.map((t) => Ft(n, t)) : us(e) ? n[e] : e; } function gs(n, { find: e, where: t }) { return mt(n, t).map((s) => Ft(s, e)); } function ms(n, e, t) { return ss(n, Ft(t, e)); } const ws = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-