UNPKG

@instantdb/core

Version:
1,858 lines • 234 kB
function G(n) { const e = We(n); let t = 3735928559 ^ e.length, r = 1103547991 ^ e.length; for (let s = 0; s < e.length; s++) { const i = e.charCodeAt(s); t = Math.imul(t ^ i, 2654435761), r = Math.imul(r ^ i, 1597334677); } return t = Math.imul(t ^ t >>> 16, 2246822507) ^ Math.imul(r ^ r >>> 13, 3266489909), r = Math.imul(r ^ r >>> 16, 2246822507) ^ Math.imul(t ^ t >>> 13, 3266489909), `${(r >>> 0).toString(16).padStart(8, "0")}${(t >>> 0).toString(16).padStart(8, "0")}`; } function We(n) { if (n && typeof n.toJSON == "function") return We(n.toJSON()); if (Array.isArray(n)) { let e = "["; for (let t = 0; t < n.length; t++) t > 0 && (e += ","), e += We(n[t]); return e + "]"; } if (n && typeof n == "object") { const e = Object.keys(n); e.sort(); let t = "{", r = !0; for (let s = 0; s < e.length; s++) { const i = e[s], o = n[i]; o !== void 0 && (r || (t += ","), t += JSON.stringify(i) + ":" + We(o), r = !1); } return t + "}"; } return n === void 0 ? "undefined" : typeof n == "bigint" ? `${n}n` : JSON.stringify(n) ?? String(n); } function Se(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 = Se(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 = Se(s); e ^= parseInt(i, 16), e *= 16777619, e = e >>> 0; const o = Se(n[s]); e ^= parseInt(o, 16), e *= 16777619, e = e >>> 0; } return e.toString(16); } return Se(String(n)); } const x = { Remove: "remove", Replace: "replace", Add: "add" }, On = /* @__PURE__ */ Symbol.for("__MUTATIVE_PROXY_DRAFT__"), wr = /* @__PURE__ */ Symbol("__MUTATIVE_RAW_RETURN_SYMBOL__"), Ve = Symbol.iterator, ee = { mutable: "mutable", immutable: "immutable" }, Pt = {}; function $e(n, e) { return n instanceof Map ? n.has(e) : Object.prototype.hasOwnProperty.call(n, e); } function Wt(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 jt(n) { return Object.getPrototypeOf(n) === Set.prototype; } function $t(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?.[On]; } function xt(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 In(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 || $e(t, i))) return null; e.push(i); } if (n.parent) return In(n.parent, e); e.reverse(); try { _r(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 De(n, e, t) { be(n) === 2 ? n.set(e, t) : n[e] = t; } function it(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 ht(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 _r(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 Sr(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 Tr = Object.prototype.propertyIsEnumerable; function Cn(n, e) { let t; if (Array.isArray(n)) return Array.prototype.concat.call(n); if (n instanceof Set) { if (!jt(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 (!$t(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 Sr(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) => { Tr.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 W(n) { n.copy || (n.copy = Cn(n.original, n.options)); } function Ee(n) { if (!ie(n)) return xt(n); if (Array.isArray(n)) return n.map(Ee); if (n instanceof Map) { const t = Array.from(n.entries()).map(([r, s]) => [ r, Ee(s) ]); if (!$t(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(Ee); if (!jt(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] = Ee(n[t]); return e; } function Qe(n) { return ye(n) ? Ee(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 Vt() { 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 = Vt; break; case 3: for (const a of n) _e(a, a, t, r, s); n.add = n.clear = n.delete = Vt; 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 Rt(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 En(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), Rt(n, (i, o) => { var a; if (ye(o)) { const c = I(o); W(c); const u = !((a = c.assignedMap) === null || a === void 0) && a.size || c.operated ? c.copy : c.original; De(r ? s : n, i, u); } else En(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 vr(n, e) { const t = n.type === 3 ? n.setMap : n.copy; n.finalities.revoke.length > 1 && n.assignedMap.get(e) && t && En(de(t, e), n.finalities.handledSet, n.options); } function pt(n) { n.type === 3 && n.copy && (n.copy.clear(), n.setMap.forEach((e) => { n.copy.add(xt(e)); })); } function yt(n, e, t, r) { if (n.operated && n.assignedMap && n.assignedMap.size > 0 && !n.finalized) { if (t && r) { const i = In(n); i && e(n, i, t, r); } n.finalized = !0; } } function Dt(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), pt(n), yt(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)), De(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) && vr(n, e); }); } function Ar(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: Qe(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: Qe(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: Qe(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 kr({ original: n, copy: e, assignedMap: t }, r, s, i, o) { t.forEach((a, c) => { const u = de(n, c), l = Qe(de(e, c)), f = a ? $e(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 Or({ 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 Re(n, e, t, r) { const { pathAsArray: s = !0 } = n.options.enablePatches; switch (n.type) { case 0: case 2: return kr(n, e, t, r, s); case 1: return Ar(n, e, t, r, s); case 3: return Or(n, e, t, r, s); } } const He = (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."); }, bt = { 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)) && (W(t), ce(t), t.assignedMap.set(n, !0), t.copy.set(n, e), Dt(t, n, e, Re)), this; }, delete(n) { if (!this.has(n)) return !1; const e = I(this); return W(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) { W(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 && He(s, r.options, i), i || r.finalized || !ie(s, r.options) || s !== r.original.get(n)) return s; const o = Pt.createDraft({ original: s, parentDraft: r, key: n, finalities: r.finalities, options: r.options }); return W(r), r.copy.set(n, o), o; }, keys() { return X(I(this)).keys(); }, values() { const n = this.keys(); return { [Ve]: () => this.values(), next: () => { const e = n.next(); return e.done ? e : { done: !1, value: this.get(e.value) }; } }; }, entries() { const n = this.keys(); return { [Ve]: () => 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] }; } }; }, [Ve]() { return this.entries(); } }, Ir = Reflect.ownKeys(bt), Qt = (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 && He(o, n.options, u), !u && !c && ie(o, n.options) && !n.finalized && n.original.has(o)) { const l = Pt.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] }; }, Je = { get size() { return I(this).setMap.size; }, has(n) { const e = I(this); if (e.setMap.has(n)) return !0; W(e); const t = I(n); return !!(t && e.setMap.has(t.original)); }, add(n) { const e = I(this); return this.has(n) || (W(e), ce(e), e.assignedMap.set(n, !0), e.setMap.set(n, n), Dt(e, n, n, Re)), this; }, delete(n) { if (!this.has(n)) return !1; const e = I(this); W(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); W(n), ce(n); for (const e of n.original) n.assignedMap.set(e, !1); n.setMap.clear(); }, values() { const n = I(this); W(n); const e = n.setMap.keys(); return { [Symbol.iterator]: () => this.values(), next: Qt(n, e, { isValuesIterator: !0 }) }; }, entries() { const n = I(this); W(n); const e = n.setMap.keys(); return { [Symbol.iterator]: () => this.entries(), next: Qt(n, e, { isValuesIterator: !1 }) }; }, keys() { return this.values(); }, [Ve]() { 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(Je, { 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 Cr = Reflect.ownKeys(Je), Mn = /* @__PURE__ */ new WeakSet(), Pn = { get(n, e, t) { var r, s; const i = (r = n.copy) === null || r === void 0 ? void 0 : r[e]; if (i && Mn.has(i)) return i; if (e === On) 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 && He(u, n.options, !0), u; } const a = X(n); if (a instanceof Map && Ir.includes(e)) { if (e === "size") return Object.getOwnPropertyDescriptor(bt, "size").get.call(n.proxy); const u = bt[e]; if (u) return u.bind(n.proxy); } if (a instanceof Set && Cr.includes(e)) { if (e === "size") return Object.getOwnPropertyDescriptor(Je, "size").get.call(n.proxy); const u = Je[e]; if (u) return u.bind(n.proxy); } if (!$e(a, e)) { const u = Wt(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 && He(c, n.options), n.finalized || !ie(c, n.options)) return c; if (c === it(n.original, e)) { if (W(n), n.copy[e] = Ut({ 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 W(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 = Wt(X(n), e); if (i?.set) return i.set.call(n.proxy, t), !0; const o = it(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 || $e(n.original, e)) || (W(n), ce(n), $e(n.original, e) && le(t, n.original[e]) ? n.assignedMap.delete(e) : n.assignedMap.set(e, !0), n.copy[e] = t, Dt(n, e, t, Re)), !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 ? Pn.set.call(this, n, e, void 0, n.proxy) : (it(n.original, e) !== void 0 || e in n.original ? (W(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 Ut(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, Pn); if (s.revoke.push(u), Mn.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 = xt(y)), pt(g), yt(g, Re, 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)), De(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) => { pt(l), yt(l, Re, f, d); }); } return c; } Pt.createDraft = Ut; function Er(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 && ht(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 Mr(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 : Ut({ original: n, parentDraft: null, finalities: r, options: e }); return [ a, (c = []) => { const [u, l, f] = Er(a, c, s, i, e.enableAutoFreeze); return e.enablePatches ? [u, l, f] : u; } ]; } function gt(n) { const { rootDraft: e, value: t, useRawReturn: r = !1, isRoot: s = !0 } = n; Rt(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 De(a, i, u); } else typeof o == "object" && o !== null && (n.value = o, n.isRoot = !1, gt(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 jn(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 ? $t(n) ? new Map(n) : new (Object.getPrototypeOf(n)).constructor(n) : r === 3 ? Array.from(t.setMap.values()) : Cn(n, t?.options); } if (t) { t.finalized = !0; try { i(); } finally { t.finalized = !1; } } else s = n; if (Rt(s, (o, a) => { if (t && le(de(t.original, o), a)) return; const c = jn(a); c !== a && (s === n && i(), De(s, o, c)); }), r === 3) { const o = (e = t?.original) !== null && e !== void 0 ? e : s; return jt(o) ? new Set(s) : new (Object.getPrototypeOf(o)).constructor(s); } return s; } function Bt(n) { if (!ye(n)) throw new Error(`current() is only used for Draft, parameter: ${n}`); return jn(n); } const Pr = (n) => function e(t, r, s) { var i, o, a; if (typeof t == "function" && typeof r != "function") return function(k, ...L) { return e(k, (C) => t.call(this, C, ...L), 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) ? Bt(c) : c, d = Array.isArray(l.mark) ? ((k, L) => { 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, L); 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] = Mr(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 ht(I(y)), k; } const R = (k) => { const L = I(y); if (!ye(k)) { if (k !== void 0 && !le(k, y) && L?.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?.[wr]; if (O) { const q = O[0]; return b.strict && typeof k == "object" && k !== null && gt({ rootDraft: L, value: k, useRawReturn: !0 }), g([q]); } if (k !== void 0) return typeof k == "object" && k !== null && gt({ rootDraft: L, 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([Bt(k)]); } return g([k]); }; return A instanceof Promise ? A.then(R, (k) => { throw ht(I(y)), k; }) : R(A); }, Ye = Pr(); Object.prototype.constructor.toString(); function $n(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 Gt(n, e) { return Object.keys(n).length === Object.keys(e).length && Object.keys(n).every( (t) => e.hasOwnProperty(t) && n[t] === e[t] ); } function Be(n, e) { return typeof n != "object" || typeof e != "object" || n === null || e === null ? n === e : $n(n, e) ? Object.keys(n).every( (t) => Be(n[t], e[t]) ) : !1; } function Lt(n) { if (!Me(n)) return n; const e = {}; for (const [t, r] of Object.entries(n)) r !== void 0 && (e[t] = r); return e; } function xn(n, e) { if (!Me(n) || !Me(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 = Me(n[r]) && Me(e[r]); t[r] = s ? xn(n[r], e[r]) : e[r]; } return t; } function Me(n) { return typeof n == "object" && n !== null && !Array.isArray(n); } function jr(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 Ht(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 Rn(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; } Rn(n[t], r), $r(n[t]) && delete n[t]; } } function $r(n) { return n && Object.keys(n).length === 0; } const Jt = /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$/, xr = { 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 Rr(n) { return new Date(n); } function Dr(n) { return /* @__PURE__ */ new Date(n + "Z"); } const Ur = /^(\d+)[\./-](\d+)[\./-](\d+)$/; function Lr(n) { const e = n.match(Ur); 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 Nr(n) { const [e, t] = n.split(" "); return /* @__PURE__ */ new Date(e + "T" + t + "Z"); } function Fr(n) { const [e, t] = n.split(" "); return /* @__PURE__ */ new Date(e + "T" + t + "Z"); } function Kr(n) { return new Date(n); } function qr(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 zr(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 Wr(n) { const e = /^(\d+)-(\d{1,2})-(\d{1,2})([ T])(\d{1,2})(?::(\d{1,2}))?(?::(\d{1,2}))?(.*)$/, t = n.match(e); if (t) { const [, r, s, i, o, a, c, u, l] = t, f = s.padStart(2, "0"), d = i.padStart(2, "0"); let h = a.padStart(2, "0"); c !== void 0 && (h += `:${c.padStart(2, "0")}`), u !== void 0 && (h += `:${u.padStart(2, "0")}`); const p = `${r}-${f}-${d}T${h}${l}`; return new Date(p); } return null; } function Vr(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 Qr(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 Br(n) { const e = n.match(Jt); if (!e) return null; const [t] = e, r = xr[t], s = new Date(n.replace(Jt, "Z")); return new Date(s.getTime() - r * 1e3); } const Gr = [ Lr, Fr, qr, Vr, Dr, zr, Nr, Rr, Qr, Br, Wr, Kr ]; function Hr(n, e) { try { const t = n(e); return t instanceof Date && !isNaN(t.getTime()) ? t : null; } catch { return null; } } function mt(n) { for (const e of Gr) { const t = Hr(e, n); if (t) return t; } return null; } function Jr(n) { try { const e = JSON.parse(n); return typeof e == "string" ? mt(e) : null; } catch { return null; } } function et(n) { if (n !== void 0) { if (n === null) return null; if (n instanceof Date) return n; if (typeof n == "string") { const e = mt(n) || Jr(n) || mt(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 (Ft(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 Yr(n) { return n.cardinality === "one"; } function Nt(n) { return n["value-type"] === "ref"; } function Ft(n) { return n["value-type"] === "blob"; } function Ie(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 Dn(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 = et(u), o[2] = u), Nt(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 Un(n) { return { triples: J(n.eav, 3), cardinalityInference: n.cardinalityInference, useDateObjects: n.useDateObjects, version: 1 }; } function Ln(n, e) { return Te( n, e.triples, e.cardinalityInference, e.useDateObjects ); } function Nn(n, e) { if (n) return new fe(n.attrs, n.linkIndex); if (e && "__type" in e) return new fe(e.attrs, e.linkIndex); } function Zr(n, e) { return Ie(n.eav, [e]) !== void 0; } function Te(n, e, t, r) { const s = Dn( n, e, r ); return s.cardinalityInference = t, s.useDateObjects = r, s; } function Ue(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 Fn(n, e, t) { const r = Ue(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]), Nt(a) && se(n.vae, [o, i, s])); } let Xr = 0; function Kn(n, e, t) { const [r, s, i] = t; let o; const a = Ie(n.eav, [r, s, i]); return a && (o = a[3]), o || Date.now() * 10 + Xr++; } function qn(n, e, t) { const r = Ue(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 = et(o)); const u = Ie(n.eav, [s, i, o])?.[3] ?? Kn(n, a, r), l = [s, i, o, u]; Yr(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)), Nt(a) && H(n.vae, [o, i, s], l); } function es(n, e, t) { const r = Ue(n, t); if (!r) return; const [s, i, o] = r, a = e.getAttr(i); if (!a) return; if (!Ft(a)) throw new Error("merge operation is not supported for links"); const c = Ie(n.eav, [s, i]); if (!c) return; const u = c.values().next()?.value; if (!u) return; const l = u[2], f = xn(l, o), d = [ s, i, f, Kn(n, a, u) ]; H(n.eav, [s, i], /* @__PURE__ */ new Map([[f, d]])), H(n.aev, [i, s], /* @__PURE__ */ new Map([[f, d]])); } function wt(n, e, t) { const [r, s] = t, i = Ue(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]) => wt(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 && wt(n, e, [l, h["forward-identity"]?.[1]]); }), n.vae.get(o)?.size === 0 && se(n.vae, [o]); } function zn(n, e, t) { const r = Dn( e, t, n.useDateObjects ); Object.keys(r).forEach((s) => { n[s] = r[s]; }); } function ts(n, [e]) { n.addAttr(e); } function Wn(n) { return J(n.eav, 3); } function ns(n, e, [t]) { if (!e.getAttr(t)) return; const r = Wn(n).filter(([s, i]) => i !== t); e.deleteAttr(t), zn(n, e, r); } function rs(n, e, [t]) { e.getAttr(t.id) && (e.updateAttr(t), zn(n, e, Wn(n))); } function ss(n, e, t) { const [r, ...s] = t; switch (r) { case "add-triple": qn(n, e, s); break; case "deep-merge-triple": es(n, e, s); break; case "retract-triple": Fn(n, e, s); break; case "delete-entity": wt(n, e, s); break; case "add-attr": ts(e, s); break; case "delete-attr": ns(n, e, s); break; case "update-attr": rs(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 Ne(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 is(n, e, t) { let r = ""; return n !== void 0 && (r += "e"), e !== void 0 && (r += "a"), t !== void 0 && (r += "v"), r; } function os(n, [e, t, r]) { switch (is(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 ? Ne(n, i, r) : []; } case "ev": { const i = n.eav.get(e); if (!i) return []; const o = []; for (const a of i.values()) o.push(...Ne(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(...Ne(n, a, r)); return o; } case "v": { const i = []; for (const o of n.eav.values()) for (const a of o.values()) i.push(...Ne(n, a, r)); return i; } default: return J(n.eav, 3); } } function as(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 cs(n, e) { return n.blobAttrs.get(e); } function Vn(n, e) { const t = n.primaryKeys.get(e); return t || n.forwardIdents.get(e)?.get("id"); } function us(n, e, t) { const r = Ue(n, t); if (!r) return; const [s, i, o] = r; if (e.getAttr(i)) return Ie(n.eav, [s, i]); } function ls(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 = Vn( e, f["forward-identity"][1] ); l = !!us(n, e, [ i, d?.id, i ]); } return !(u === "create" && l || u === "update" && !l); } ); return Ye( { store: n, attrsStore: e }, (s) => { r.forEach((i) => { ss(s.store, s.attrsStore, i); }); }, { mark: (s) => { if (s instanceof fe) return "immutable"; } } ); } function ds(n) { return typeof n == "string" && n.startsWith("?"); } function fs(n, e, t) { if (t.hasOwnProperty(n)) { c