@instantdb/core
Version:
Instant's core local abstraction
1,877 lines • 229 kB
JavaScript
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 R = {
Remove: "remove",
Replace: "replace",
Add: "add"
}, _n = /* @__PURE__ */ Symbol.for("__MUTATIVE_PROXY_DRAFT__"), hr = /* @__PURE__ */ Symbol("__MUTATIVE_RAW_RETURN_SYMBOL__"), Fe = Symbol.iterator, Z = {
mutable: "mutable",
immutable: "immutable"
}, At = {};
function Me(n, e) {
return n instanceof Map ? n.has(e) : Object.prototype.hasOwnProperty.call(n, e);
}
function Ut(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 kt(n) {
return Object.getPrototypeOf(n) === Set.prototype;
}
function Ot(n) {
return Object.getPrototypeOf(n) === Map.prototype;
}
function Y(n) {
var e;
return (e = n.copy) !== null && e !== void 0 ? e : n.original;
}
function he(n) {
return !!A(n);
}
function A(n) {
return typeof n != "object" ? null : n?.[_n];
}
function It(n) {
var e;
const t = A(n);
return t ? (e = t.copy) !== null && e !== void 0 ? e : t.original : n;
}
function re(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, Z)) === Z.immutable || typeof t == "function");
}
function Sn(n, e = []) {
if (Object.hasOwnProperty.call(n, "key")) {
const t = n.parent.copy, r = A(ue(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 || Me(t, i)))
return null;
e.push(i);
}
if (n.parent)
return Sn(n.parent, e);
e.reverse();
try {
pr(n.copy, e);
} catch {
return null;
}
return e;
}
function pe(n) {
return Array.isArray(n) ? 1 : n instanceof Map ? 2 : n instanceof Set ? 3 : 0;
}
function ue(n, e) {
return pe(n) === 2 ? n.get(e) : n[e];
}
function $e(n, e, t) {
pe(n) === 2 ? n.set(e, t) : n[e] = t;
}
function et(n, e) {
const t = A(n);
return (t ? Y(t) : n)[e];
}
function ce(n, e) {
return n === e ? n !== 0 || 1 / n === 1 / e : n !== n && e !== e;
}
function ot(n) {
if (n)
for (; n.finalities.revoke.length > 0; )
n.finalities.revoke.pop()();
}
function fe(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 pr(n, e) {
for (let t = 0; t < e.length - 1; t += 1) {
const r = e[t];
if (n = ue(pe(n) === 3 ? Array.from(n) : n, r), typeof n != "object")
throw new Error(`Cannot resolve patch at '${e.join("/")}'.`);
}
return n;
}
function yr(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 br = Object.prototype.propertyIsEnumerable;
function Tn(n, e) {
let t;
if (Array.isArray(n))
return Array.prototype.concat.call(n);
if (n instanceof Set) {
if (!kt(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 (!Ot(n)) {
const r = Object.getPrototypeOf(n).constructor;
return new r(n);
}
return new Map(n);
} else if (e?.mark && (t = e.mark(n, Z), t !== void 0) && t !== Z.mutable) {
if (t === Z.immutable)
return yr(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) => {
br.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 z(n) {
n.copy || (n.copy = Tn(n.original, n.options));
}
function Ie(n) {
if (!re(n))
return It(n);
if (Array.isArray(n))
return n.map(Ie);
if (n instanceof Map) {
const t = Array.from(n.entries()).map(([r, s]) => [
r,
Ie(s)
]);
if (!Ot(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(Ie);
if (!kt(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] = Ie(n[t]);
return e;
}
function qe(n) {
return he(n) ? Ie(n) : n;
}
function oe(n) {
var e;
n.assignedMap = (e = n.assignedMap) !== null && e !== void 0 ? e : /* @__PURE__ */ new Map(), n.operated || (n.operated = !0, n.parent && oe(n.parent));
}
function Lt() {
throw new Error("Cannot modify frozen object");
}
function me(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) || he(n)) {
r.pop(), s.pop();
return;
}
switch (pe(n)) {
case 2:
for (const [a, c] of n)
me(a, a, t, r, s), me(c, a, t, r, s);
n.set = n.clear = n.delete = Lt;
break;
case 3:
for (const a of n)
me(a, a, t, r, s);
n.add = n.clear = n.delete = Lt;
break;
case 1:
Object.freeze(n);
let o = 0;
for (const a of n)
me(a, o, t, r, s), o += 1;
break;
default:
Object.freeze(n), Object.keys(n).forEach((a) => {
const c = n[a];
me(c, a, t, r, s);
});
}
r.pop(), s.pop();
}
function Ct(n, e) {
const t = pe(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 vn(n, e, t) {
if (he(n) || !re(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), Ct(n, (i, o) => {
var a;
if (he(o)) {
const c = A(o);
z(c);
const u = !((a = c.assignedMap) === null || a === void 0) && a.size || c.operated ? c.copy : c.original;
$e(r ? s : n, i, u);
} else
vn(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 gr(n, e) {
const t = n.type === 3 ? n.setMap : n.copy;
n.finalities.revoke.length > 1 && n.assignedMap.get(e) && t && vn(ue(t, e), n.finalities.handledSet, n.options);
}
function at(n) {
n.type === 3 && n.copy && (n.copy.clear(), n.setMap.forEach((e) => {
n.copy.add(It(e));
}));
}
function ct(n, e, t, r) {
if (n.operated && n.assignedMap && n.assignedMap.size > 0 && !n.finalized) {
if (t && r) {
const i = Sn(n);
i && e(n, i, t, r);
}
n.finalized = !0;
}
}
function Et(n, e, t, r) {
const s = A(t);
s && (s.callbacks || (s.callbacks = []), s.callbacks.push((i, o) => {
var a;
const c = n.type === 3 ? n.setMap : n.copy;
if (ce(ue(c, e), t)) {
let u = s.original;
s.copy && (u = s.copy), at(n), ct(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)), $e(c, e, u);
}
}), n.options.enableAutoFreeze && s.finalities !== n.finalities && (n.options.enableAutoFreeze = !1)), re(t, n.options) && n.finalities.draft.push(() => {
const i = n.type === 3 ? n.setMap : n.copy;
ce(ue(i, e), t) && gr(n, e);
});
}
function mr(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 = fe(l, s);
t.push({
op: R.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: R.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 = fe(l, s);
t.push({
op: R.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 = fe(l, s);
r.push({
op: R.Replace,
path: f,
value: i.length
});
} else
for (let l = c.length; i.length < l; l -= 1) {
const f = e.concat([l - 1]), d = fe(f, s);
r.push({
op: R.Remove,
path: d
});
}
}
}
function wr({ original: n, copy: e, assignedMap: t }, r, s, i, o) {
t.forEach((a, c) => {
const u = ue(n, c), l = qe(ue(e, c)), f = a ? Me(n, c) ? R.Replace : R.Add : R.Remove;
if (ce(u, l) && f === R.Replace)
return;
const d = r.concat(c), h = fe(d, o);
s.push(f === R.Remove ? { op: f, path: h } : { op: f, path: h, value: l }), i.push(f === R.Add ? { op: R.Remove, path: h } : f === R.Remove ? { op: R.Add, path: h, value: u } : { op: R.Replace, path: h, value: u });
});
}
function _r({ original: n, copy: e }, t, r, s, i) {
let o = 0;
n.forEach((a) => {
if (!e.has(a)) {
const c = t.concat([o]), u = fe(c, i);
r.push({
op: R.Remove,
path: u,
value: a
}), s.unshift({
op: R.Add,
path: u,
value: a
});
}
o += 1;
}), o = 0, e.forEach((a) => {
if (!n.has(a)) {
const c = t.concat([o]), u = fe(c, i);
r.push({
op: R.Add,
path: u,
value: a
}), s.unshift({
op: R.Remove,
path: u,
value: a
});
}
o += 1;
});
}
function je(n, e, t, r) {
const { pathAsArray: s = !0 } = n.options.enablePatches;
switch (n.type) {
case 0:
case 2:
return wr(n, e, t, r, s);
case 1:
return mr(n, e, t, r, s);
case 3:
return _r(n, e, t, r, s);
}
}
const We = (n, e, t = !1) => {
if (typeof n == "object" && n !== null && (!re(n, e) || t))
throw new Error("Strict mode: Mutable data cannot be accessed directly, please use 'unsafe(callback)' wrap.");
}, ut = {
get size() {
return Y(A(this)).size;
},
has(n) {
return Y(A(this)).has(n);
},
set(n, e) {
const t = A(this), r = Y(t);
return (!r.has(n) || !ce(r.get(n), e)) && (z(t), oe(t), t.assignedMap.set(n, !0), t.copy.set(n, e), Et(t, n, e, je)), this;
},
delete(n) {
if (!this.has(n))
return !1;
const e = A(this);
return z(e), oe(e), e.original.has(n) ? e.assignedMap.set(n, !1) : e.assignedMap.delete(n), e.copy.delete(n), !0;
},
clear() {
const n = A(this);
if (this.size) {
z(n), oe(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 = A(this);
Y(t).forEach((r, s) => {
n.call(e, this.get(s), s, this);
});
},
get(n) {
var e, t;
const r = A(this), s = Y(r).get(n), i = ((t = (e = r.options).mark) === null || t === void 0 ? void 0 : t.call(e, s, Z)) === Z.mutable;
if (r.options.strict && We(s, r.options, i), i || r.finalized || !re(s, r.options) || s !== r.original.get(n))
return s;
const o = At.createDraft({
original: s,
parentDraft: r,
key: n,
finalities: r.finalities,
options: r.options
});
return z(r), r.copy.set(n, o), o;
},
keys() {
return Y(A(this)).keys();
},
values() {
const n = this.keys();
return {
[Fe]: () => this.values(),
next: () => {
const e = n.next();
return e.done ? e : {
done: !1,
value: this.get(e.value)
};
}
};
},
entries() {
const n = this.keys();
return {
[Fe]: () => 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]
};
}
};
},
[Fe]() {
return this.entries();
}
}, Sr = Reflect.ownKeys(ut), Nt = (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 = A(a), u = ((s = (r = n.options).mark) === null || s === void 0 ? void 0 : s.call(r, a, Z)) === Z.mutable;
if (n.options.strict && We(o, n.options, u), !u && !c && re(o, n.options) && !n.finalized && n.original.has(o)) {
const l = At.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]
};
}, Ve = {
get size() {
return A(this).setMap.size;
},
has(n) {
const e = A(this);
if (e.setMap.has(n))
return !0;
z(e);
const t = A(n);
return !!(t && e.setMap.has(t.original));
},
add(n) {
const e = A(this);
return this.has(n) || (z(e), oe(e), e.assignedMap.set(n, !0), e.setMap.set(n, n), Et(e, n, n, je)), this;
},
delete(n) {
if (!this.has(n))
return !1;
const e = A(this);
z(e), oe(e);
const t = A(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 = A(this);
z(n), oe(n);
for (const e of n.original)
n.assignedMap.set(e, !1);
n.setMap.clear();
},
values() {
const n = A(this);
z(n);
const e = n.setMap.keys();
return {
[Symbol.iterator]: () => this.values(),
next: Nt(n, e, { isValuesIterator: !0 })
};
},
entries() {
const n = A(this);
z(n);
const e = n.setMap.keys();
return {
[Symbol.iterator]: () => this.entries(),
next: Nt(n, e, {
isValuesIterator: !1
})
};
},
keys() {
return this.values();
},
[Fe]() {
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(Ve, {
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 Tr = Reflect.ownKeys(Ve), An = /* @__PURE__ */ new WeakSet(), kn = {
get(n, e, t) {
var r, s;
const i = (r = n.copy) === null || r === void 0 ? void 0 : r[e];
if (i && An.has(i))
return i;
if (e === _n)
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, Z), o === Z.mutable)
return n.options.strict && We(u, n.options, !0), u;
}
const a = Y(n);
if (a instanceof Map && Sr.includes(e)) {
if (e === "size")
return Object.getOwnPropertyDescriptor(ut, "size").get.call(n.proxy);
const u = ut[e];
if (u)
return u.bind(n.proxy);
}
if (a instanceof Set && Tr.includes(e)) {
if (e === "size")
return Object.getOwnPropertyDescriptor(Ve, "size").get.call(n.proxy);
const u = Ve[e];
if (u)
return u.bind(n.proxy);
}
if (!Me(a, e)) {
const u = Ut(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 && We(c, n.options), n.finalized || !re(c, n.options))
return c;
if (c === et(n.original, e)) {
if (z(n), n.copy[e] = Mt({
original: n.original[e],
parentDraft: n,
key: n.type === 1 ? Number(e) : e,
finalities: n.finalities,
options: n.options
}), typeof o == "function") {
const u = A(n.copy[e]);
return z(u), oe(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 = Ut(Y(n), e);
if (i?.set)
return i.set.call(n.proxy, t), !0;
const o = et(Y(n), e), a = A(o);
return a && ce(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) : (ce(t, o) && (t !== void 0 || Me(n.original, e)) || (z(n), oe(n), Me(n.original, e) && ce(t, n.original[e]) ? n.assignedMap.delete(e) : n.assignedMap.set(e, !0), n.copy[e] = t, Et(n, e, t, je)), !0);
},
has(n, e) {
return e in Y(n);
},
ownKeys(n) {
return Reflect.ownKeys(Y(n));
},
getOwnPropertyDescriptor(n, e) {
const t = Y(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 ? kn.set.call(this, n, e, void 0, n.proxy) : (et(n.original, e) !== void 0 || e in n.original ? (z(n), oe(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 Mt(n) {
const { original: e, parentDraft: t, key: r, finalities: s, options: i } = n, o = pe(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, kn);
if (s.revoke.push(u), An.add(c), a.proxy = c, t) {
const l = t;
l.finalities.draft.push((f, d) => {
var h, p;
const m = A(c);
let g = l.type === 3 ? l.setMap : l.copy;
const y = ue(g, r), b = A(y);
if (b) {
let _ = b.original;
b.operated && (_ = It(y)), at(b), ct(b, je, f, d), l.options.enableAutoFreeze && (l.options.updatedValues = (h = l.options.updatedValues) !== null && h !== void 0 ? h : /* @__PURE__ */ new WeakMap(), l.options.updatedValues.set(_, b.original)), $e(g, r, _);
}
(p = m.callbacks) === null || p === void 0 || p.forEach((_) => {
_(f, d);
});
});
} else {
const l = A(c);
l.finalities.draft.push((f, d) => {
at(l), ct(l, je, f, d);
});
}
return c;
}
At.createDraft = Mt;
function vr(n, e, t, r, s) {
var i;
const o = A(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 && ot(o), s && me(u, u, o?.options.updatedValues), [
u,
t && c ? [{ op: R.Replace, path: [], value: e[0] }] : t,
r && c ? [{ op: R.Replace, path: [], value: a }] : r
];
}
function Ar(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, Z)) === Z.mutable || !re(n, e) ? n : Mt({
original: n,
parentDraft: null,
finalities: r,
options: e
});
return [
a,
(c = []) => {
const [u, l, f] = vr(a, c, s, i, e.enableAutoFreeze);
return e.enablePatches ? [u, l, f] : u;
}
];
}
function lt(n) {
const { rootDraft: e, value: t, useRawReturn: r = !1, isRoot: s = !0 } = n;
Ct(t, (i, o, a) => {
const c = A(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
$e(a, i, u);
} else typeof o == "object" && o !== null && (n.value = o, n.isRoot = !1, lt(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 On(n) {
var e;
const t = A(n);
if (!re(n, t?.options))
return n;
const r = pe(n);
if (t && !t.operated)
return t.original;
let s;
function i() {
s = r === 2 ? Ot(n) ? new Map(n) : new (Object.getPrototypeOf(n)).constructor(n) : r === 3 ? Array.from(t.setMap.values()) : Tn(n, t?.options);
}
if (t) {
t.finalized = !0;
try {
i();
} finally {
t.finalized = !1;
}
} else
s = n;
if (Ct(s, (o, a) => {
if (t && ce(ue(t.original, o), a))
return;
const c = On(a);
c !== a && (s === n && i(), $e(s, o, c));
}), r === 3) {
const o = (e = t?.original) !== null && e !== void 0 ? e : s;
return kt(o) ? new Set(s) : new (Object.getPrototypeOf(o)).constructor(s);
}
return s;
}
function Ft(n) {
if (!he(n))
throw new Error(`current() is only used for Draft, parameter: ${n}`);
return On(n);
}
const kr = (n) => function e(t, r, s) {
var i, o, a;
if (typeof t == "function" && typeof r != "function")
return function(w, ...E) {
return e(w, (P) => t.call(this, P, ...E), 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 = he(c) ? Ft(c) : c, d = Array.isArray(l.mark) ? ((w, E) => {
for (const P of l.mark) {
if (typeof P != "function")
throw new Error(`Invalid mark: ${P}, 'mark' should be a function.`);
const N = P(w, E);
if (N)
return N;
}
}) : l.mark, h = (i = l.enablePatches) !== null && i !== void 0 ? i : !1, p = (o = l.strict) !== null && o !== void 0 ? o : !1, g = {
enableAutoFreeze: (a = l.enableAutoFreeze) !== null && a !== void 0 ? a : !1,
mark: d,
strict: p,
enablePatches: h
};
if (!re(f, g) && 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, b] = Ar(f, g);
if (typeof r != "function") {
if (!re(f, g))
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, b];
}
let _;
try {
_ = u(y);
} catch (w) {
throw ot(A(y)), w;
}
const M = (w) => {
const E = A(y);
if (!he(w)) {
if (w !== void 0 && !ce(w, y) && E?.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 N = w?.[hr];
if (N) {
const H = N[0];
return g.strict && typeof w == "object" && w !== null && lt({
rootDraft: E,
value: w,
useRawReturn: !0
}), b([H]);
}
if (w !== void 0)
return typeof w == "object" && w !== null && lt({ rootDraft: E, value: w }), b([w]);
}
if (w === y || w === void 0)
return b([]);
const P = A(w);
if (g === P.options) {
if (P.operated)
throw new Error("Cannot return a modified child draft.");
return b([Ft(w)]);
}
return b([w]);
};
return _ instanceof Promise ? _.then(M, (w) => {
throw ot(A(y)), w;
}) : M(_);
}, Qe = kr();
Object.prototype.constructor.toString();
function In(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 Ke(n, e) {
return typeof n != "object" || typeof e != "object" || n === null || e === null ? n === e : In(n, e) ? Object.keys(n).every(
(t) => Ke(n[t], e[t])
) : !1;
}
function Pt(n) {
if (!Ce(n))
return n;
const e = {};
for (const [t, r] of Object.entries(n))
r !== void 0 && (e[t] = r);
return e;
}
function Cn(n, e) {
if (!Ce(n) || !Ce(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 = Ce(n[r]) && Ce(e[r]);
t[r] = s ? Cn(n[r], e[r]) : e[r];
}
return t;
}
function Ce(n) {
return typeof n == "object" && n !== null && !Array.isArray(n);
}
function Or(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 Kt(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 En(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;
}
En(n[t], r), Ir(n[t]) && delete n[t];
}
}
function Ir(n) {
return n && Object.keys(n).length === 0;
}
const zt = /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$/, Cr = {
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 Er(n) {
return new Date(n);
}
function Mr(n) {
return /* @__PURE__ */ new Date(n + "Z");
}
const Pr = /^(\d+)[\./-](\d+)[\./-](\d+)$/;
function jr(n) {
const e = n.match(Pr);
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 $r(n) {
const [e, t] = n.split(" ");
return /* @__PURE__ */ new Date(e + "T" + t + "Z");
}
function xr(n) {
const [e, t] = n.split(" ");
return /* @__PURE__ */ new Date(e + "T" + t + "Z");
}
function Rr(n) {
return new Date(n);
}
function Dr(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 Ur(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 Lr(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 Nr(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 Fr(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 qr(n) {
const e = n.match(zt);
if (!e)
return null;
const [t] = e, r = Cr[t], s = new Date(n.replace(zt, "Z"));
return new Date(s.getTime() - r * 1e3);
}
const Kr = [
jr,
xr,
Dr,
Nr,
Rr,
Mr,
Ur,
$r,
Er,
Fr,
qr,
Lr
];
function zr(n, e) {
try {
const t = n(e);
return t instanceof Date && !isNaN(t.getTime()) ? t : null;
} catch {
return null;
}
}
function dt(n) {
for (const e of Kr) {
const t = zr(e, n);
if (t)
return t;
}
return null;
}
function Wr(n) {
try {
const e = JSON.parse(n);
return typeof e == "string" ? dt(e) : null;
} catch {
return null;
}
}
function He(n) {
if (n !== void 0) {
if (n === null)
return null;
if (n instanceof Date)
return n;
if (typeof n == "string") {
const e = dt(n) || Wr(n) || dt(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 le {
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 ($t(e)) {
const [t, r, s] = e["forward-identity"];
Q(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"];
Q(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;
Q(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;
Q(this._revIdents, [s, i], e);
}
}
return this._revIdents;
}
toJSON() {
return { attrs: this.attrs, linkIndex: this.linkIndex };
}
}
function Vr(n) {
return n.cardinality === "one";
}
function jt(n) {
return n["value-type"] === "ref";
}
function $t(n) {
return n["value-type"] === "blob";
}
function Ae(n, e) {
return e.reduce((t, r) => t && t.get(r), n);
}
function ne(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) && ne(n.get(t), r);
}
function Q(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 Mn(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 = He(u), o[2] = u), jt(l) && Q(i, [u, c, a], o), Q(r, [a, c, u], o), Q(s, [c, a, u], o);
}
return { eav: r, aev: s, vae: i };
}
function Pn(n) {
return {
triples: B(n.eav, 3),
cardinalityInference: n.cardinalityInference,
useDateObjects: n.useDateObjects,
version: 1
};
}
function jn(n, e) {
return we(
n,
e.triples,
e.cardinalityInference,
e.useDateObjects
);
}
function $n(n, e) {
if (n)
return new le(n.attrs, n.linkIndex);
if (e && "__type" in e)
return new le(e.attrs, e.linkIndex);
}
function Qr(n, e) {
return Ae(n.eav, [e]) !== void 0;
}
function we(n, e, t, r) {
const s = Mn(
n,
e,
r
);
return s.cardinalityInference = t, s.useDateObjects = r, s;
}
function xe(n, e) {
let t;
if (Array.isArray(e[0])) {
const [s, i] = e[0], o = n.aev.get(s);
if (!o)
return null;
t = B(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 = B(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 xn(n, e, t) {
const r = xe(n, t);
if (!r)
return;
const [s, i, o] = r, a = e.getAttr(i);
a && (ne(n.eav, [s, i, o]), ne(n.aev, [i, s, o]), jt(a) && ne(n.vae, [o, i, s]));
}
let Br = 0;
function Rn(n, e, t) {
const [r, s, i] = t;
let o;
const a = Ae(n.eav, [r, s, i]);
return a && (o = a[3]), o || Date.now() * 10 + Br++;
}
function Dn(n, e, t) {
const r = xe(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 = He(o));
const u = Ae(n.eav, [s, i, o])?.[3] ?? Rn(n, a, r), l = [s, i, o, u];
Vr(a) ? (Q(n.eav, [s, i], /* @__PURE__ */ new Map([[o, l]])), Q(n.aev, [i, s], /* @__PURE__ */ new Map([[o, l]]))) : (Q(n.eav, [s, i, o], l), Q(n.aev, [i, s, o], l)), jt(a) && Q(n.vae, [o, i, s], l);
}
function Gr(n, e, t) {
const r = xe(n, t);
if (!r)
return;
const [s, i, o] = r, a = e.getAttr(i);
if (!a) return;
if (!$t(a))
throw new Error("merge operation is not supported for links");
const c = Ae(n.eav, [s, i]);
if (!c) return;
const u = c.values().next()?.value;
if (!u) return;
const l = u[2], f = Cn(l, o), d = [
s,
i,
f,
Rn(n, a, u)
];
Q(n.eav, [s, i], /* @__PURE__ */ new Map([[f, d]])), Q(n.aev, [i, s], /* @__PURE__ */ new Map([[f, d]]));
}
function ft(n, e, t) {
const [r, s] = t, i = xe(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" && B(a.get(u), 1).forEach(
([f, d, h]) => ft(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) && (ne(n.aev, [u, o]), ne(n.eav, [o, u]));
}
a.size === 0 && ne(n.eav, [o]);
}
const c = n.vae.get(o) && B(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) && (ne(n.eav, [l, f, d]), ne(n.aev, [f, l, d]), ne(n.vae, [d, f, l])), h && h["on-delete"] === "cascade" && h["reverse-identity"]?.[1] === s && ft(n, e, [l, h["forward-identity"]?.[1]]);
}), n.vae.get(o)?.size === 0 && ne(n.vae, [o]);
}
function Un(n, e, t) {
const r = Mn(
e,
t,
n.useDateObjects
);
Object.keys(r).forEach((s) => {
n[s] = r[s];
});
}
function Hr(n, [e]) {
n.addAttr(e);
}
function Ln(n) {
return B(n.eav, 3);
}
function Jr(n, e, [t]) {
if (!e.getAttr(t)) return;
const r = Ln(n).filter(([s, i]) => i !== t);
e.deleteAttr(t), Un(n, e, r);
}
function Yr(n, e, [t]) {
e.getAttr(t.id) && (e.updateAttr(t), Un(n, e, Ln(n)));
}
function Zr(n, e, t) {
const [r, ...s] = t;
switch (r) {
case "add-triple":
Dn(n, e, s);
break;
case "deep-merge-triple":
Gr(n, e, s);
break;
case "retract-triple":
xn(n, e, s);
break;
case "delete-entity":
ft(n, e, s);
break;
case "add-attr":
Hr(e, s);
break;
case "delete-attr":
Jr(n, e, s);
break;
case "update-attr":
Yr(n, e, s);
break;
case "restore-attr":
break;
case "rule-params":
break;
default:
throw new Error(`unhandled transaction action: ${r}`);
}
}
function B(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())
B(r, e - 1, t);
return t;
}
function De(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 B(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 Xr(n, e, t) {
let r = "";
return n !== void 0 && (r += "e"), e !== void 0 && (r += "a"), t !== void 0 && (r += "v"), r;
}
function es(n, [e, t, r]) {
switch (Xr(e, t, r)) {
case "e": {
const i = n.eav.get(e);
return B(i, 2);
}
case "ea": {
const i = n.eav.get(e)?.get(t);
return B(i, 1);
}
case "eav": {
const i = n.eav.get(e)?.get(t);
return i ? De(n, i, r) : [];
}
case "ev": {
const i = n.eav.get(e);
if (!i)
return [];
const o = [];
for (const a of i.values())
o.push(...De(n, a, r));
return o;
}
case "a": {
const i = n.aev.get(t);
return B(i, 2);
}
case "av": {
const i = n.aev.get(t);
if (!i)
return [];
const o = [];
for (const a of i.values())
o.push(...De(n, a, r));
return o;
}
case "v": {
const i = [];
for (const o of n.eav.values())
for (const a of o.values())
i.push(...De(n, a, r));
return i;
}
default:
return B(n.eav, 3);
}
}
function ts(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 = B(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 ye(n, e, t) {
return n.revIdents.get(e)?.get(t);
}
function ns(n, e) {
return n.blobAttrs.get(e);
}
function Nn(n, e) {
const t = n.primaryKeys.get(e);
return t || n.forwardIdents.get(e)?.get("id");
}
function rs(n, e, t) {
const r = xe(n, t);
if (!r)
return;
const [s, i, o] = r;
if (e.getAttr(i))
return Ae(n.eav, [s, i]);
}
function ss(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 = Nn(
e,
f["forward-identity"][1]
);
l = !!rs(n, e, [
i,
d?.id,
i
]);
}
return !(u === "create" && l || u === "update" && !l);
}
);
return Qe(
{ store: n, attrsStore: e },
(s) => {
r.forEach((i) => {
Zr(s.store, s.attrsStore, i);
});
},
{
mark: (s) => {
if (s instanceof le)
return "immutable";
}
}
);
}
function is(n) {
return typeof n == "string" && n.startsWith("?");
}
function os(n, e, t) {
if (t.hasOwnProperty(n)) {
const r = t[n];
return Fn(r, e, t);
}
return { ...t, [n]: e };
}
function Wt(n, e, t) {
return n === e ? t : null;
}
function as(n) {
return typeof n === "string" && n.startsWith("?") ? os : Wt;
}
const cs = [
"in",
"$in",
"$not",
"$isNull",
"$comparator"
// covers all of $gt, $lt, etc.
];
function us(n) {
for (const e of cs)
if (n.hasOwnProperty(e))
return !0;
return !1;
}
function Fn(n, e, t) {
return t ? typeof n == "object" ? us(n) ? t : null : as(n)(n, e, t) : null;
}
function ls(n, e, t) {
return n.reduce((r, s, i) => {
const o = e[i];
return Fn(s, o, r);
}, t);
}
function ds(n, e, t) {
return ps(n, e, t).map((r) => ls(e, r, t)).filter((r) => r);
}
function fs(n, e, t) {
return e.or ? e.or.patterns.flatMap((r) => ht(n, r, t)) : e.and ? e.and.patterns.reduce((r, s) => ht(n, s, r), t) : t.flatMap((r) => ds(n, e, r));
}
function ht(n, e, t = [{}]) {
return e.reduce((r, s) => fs(n, s, r), t);
}
function xt(n, e) {
return Array.isArray(e) ? e.map((t) => xt(n, t)) : is(e) ? n[e] : e;
}
function hs(n, { find: e, where: t }) {
return ht(n, t).map((s) => xt(s, e));
}
function ps(n, e, t) {
return es(n, xt(t, e));
}
const ys = /^(?:[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-ffff-ffff-ffff