@instantdb/core
Version:
Instant's core local abstraction
1,861 lines • 228 kB
JavaScript
var rr = Object.defineProperty;
var sr = (n, e, t) => e in n ? rr(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t;
var h = (n, e, t) => sr(n, typeof e != "symbol" ? e + "" : e, t);
function $(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 = $(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], i = $(s);
e ^= parseInt(i, 16), e *= 16777619, e = e >>> 0;
const o = $(n[s]);
e ^= parseInt(o, 16), e *= 16777619, e = e >>> 0;
}
return e.toString(16);
}
return $(String(n));
}
const j = {
Remove: "remove",
Replace: "replace",
Add: "add"
}, pn = Symbol.for("__MUTATIVE_PROXY_DRAFT__"), ir = Symbol("__MUTATIVE_RAW_RETURN_SYMBOL__"), xe = Symbol.iterator, B = {
mutable: "mutable",
immutable: "immutable"
}, St = {};
function ke(n, e) {
return n instanceof Map ? n.has(e) : Object.prototype.hasOwnProperty.call(n, e);
}
function Rt(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 At(n) {
return Object.getPrototypeOf(n) === Set.prototype;
}
function vt(n) {
return Object.getPrototypeOf(n) === Map.prototype;
}
function Q(n) {
var e;
return (e = n.copy) !== null && e !== void 0 ? e : n.original;
}
function le(n) {
return !!O(n);
}
function O(n) {
return typeof n != "object" ? null : n == null ? void 0 : n[pn];
}
function kt(n) {
var e;
const t = O(n);
return t ? (e = t.copy) !== null && e !== void 0 ? e : t.original : n;
}
function Z(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 != null && e.mark) && ((t = e.mark(n, B)) === B.immutable || typeof t == "function");
}
function yn(n, e = []) {
if (Object.hasOwnProperty.call(n, "key")) {
const t = n.parent.copy, r = O(se(t, n.key));
if (r !== null && (r == null ? void 0 : 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 || ke(t, i)))
return null;
e.push(i);
}
if (n.parent)
return yn(n.parent, e);
e.reverse();
try {
or(n.copy, e);
} catch {
return null;
}
return e;
}
function de(n) {
return Array.isArray(n) ? 1 : n instanceof Map ? 2 : n instanceof Set ? 3 : 0;
}
function se(n, e) {
return de(n) === 2 ? n.get(e) : n[e];
}
function Ce(n, e, t) {
de(n) === 2 ? n.set(e, t) : n[e] = t;
}
function Ye(n, e) {
const t = O(n);
return (t ? Q(t) : n)[e];
}
function re(n, e) {
return n === e ? n !== 0 || 1 / n === 1 / e : n !== n && e !== e;
}
function st(n) {
if (n)
for (; n.finalities.revoke.length > 0; )
n.finalities.revoke.pop()();
}
function ue(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 or(n, e) {
for (let t = 0; t < e.length - 1; t += 1) {
const r = e[t];
if (n = se(de(n) === 3 ? Array.from(n) : n, r), typeof n != "object")
throw new Error(`Cannot resolve patch at '${e.join("/")}'.`);
}
return n;
}
function ar(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 cr = Object.prototype.propertyIsEnumerable;
function bn(n, e) {
let t;
if (Array.isArray(n))
return Array.prototype.concat.call(n);
if (n instanceof Set) {
if (!At(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 (!vt(n)) {
const r = Object.getPrototypeOf(n).constructor;
return new r(n);
}
return new Map(n);
} else if (e != null && e.mark && (t = e.mark(n, B), t !== void 0) && t !== B.mutable) {
if (t === B.immutable)
return ar(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) => {
cr.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 F(n) {
n.copy || (n.copy = bn(n.original, n.options));
}
function Se(n) {
if (!Z(n))
return kt(n);
if (Array.isArray(n))
return n.map(Se);
if (n instanceof Map) {
const t = Array.from(n.entries()).map(([r, s]) => [
r,
Se(s)
]);
if (!vt(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(Se);
if (!At(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] = Se(n[t]);
return e;
}
function Ue(n) {
return le(n) ? Se(n) : n;
}
function te(n) {
var e;
n.assignedMap = (e = n.assignedMap) !== null && e !== void 0 ? e : /* @__PURE__ */ new Map(), n.operated || (n.operated = !0, n.parent && te(n.parent));
}
function xt() {
throw new Error("Cannot modify frozen object");
}
function pe(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) || le(n)) {
r.pop(), s.pop();
return;
}
switch (de(n)) {
case 2:
for (const [a, c] of n)
pe(a, a, t, r, s), pe(c, a, t, r, s);
n.set = n.clear = n.delete = xt;
break;
case 3:
for (const a of n)
pe(a, a, t, r, s);
n.add = n.clear = n.delete = xt;
break;
case 1:
Object.freeze(n);
let o = 0;
for (const a of n)
pe(a, o, t, r, s), o += 1;
break;
default:
Object.freeze(n), Object.keys(n).forEach((a) => {
const c = n[a];
pe(c, a, t, r, s);
});
}
r.pop(), s.pop();
}
function Ot(n, e) {
const t = de(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 gn(n, e, t) {
if (le(n) || !Z(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), Ot(n, (i, o) => {
var a;
if (le(o)) {
const c = O(o);
F(c);
const u = !((a = c.assignedMap) === null || a === void 0) && a.size || c.operated ? c.copy : c.original;
Ce(r ? s : n, i, u);
} else
gn(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 ur(n, e) {
const t = n.type === 3 ? n.setMap : n.copy;
n.finalities.revoke.length > 1 && n.assignedMap.get(e) && t && gn(se(t, e), n.finalities.handledSet, n.options);
}
function it(n) {
n.type === 3 && n.copy && (n.copy.clear(), n.setMap.forEach((e) => {
n.copy.add(kt(e));
}));
}
function ot(n, e, t, r) {
if (n.operated && n.assignedMap && n.assignedMap.size > 0 && !n.finalized) {
if (t && r) {
const i = yn(n);
i && e(n, i, t, r);
}
n.finalized = !0;
}
}
function It(n, e, t, r) {
const s = O(t);
s && (s.callbacks || (s.callbacks = []), s.callbacks.push((i, o) => {
var a;
const c = n.type === 3 ? n.setMap : n.copy;
if (re(se(c, e), t)) {
let u = s.original;
s.copy && (u = s.copy), it(n), ot(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)), Ce(c, e, u);
}
}), n.options.enableAutoFreeze && s.finalities !== n.finalities && (n.options.enableAutoFreeze = !1)), Z(t, n.options) && n.finalities.draft.push(() => {
const i = n.type === 3 ? n.setMap : n.copy;
re(se(i, e), t) && ur(n, e);
});
}
function lr(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]), d = ue(l, s);
t.push({
op: j.Replace,
path: d,
// If it is a draft, it needs to be deep cloned, and it may also be non-draft.
value: Ue(c[u])
}), r.push({
op: j.Replace,
path: d,
// If it is a draft, it needs to be deep cloned, and it may also be non-draft.
value: Ue(i[u])
});
}
for (let u = i.length; u < c.length; u += 1) {
const l = e.concat([u]), d = ue(l, s);
t.push({
op: j.Add,
path: d,
// If it is a draft, it needs to be deep cloned, and it may also be non-draft.
value: Ue(c[u])
});
}
if (i.length < c.length) {
const { arrayLengthAssignment: u = !0 } = a.enablePatches;
if (u) {
const l = e.concat(["length"]), d = ue(l, s);
r.push({
op: j.Replace,
path: d,
value: i.length
});
} else
for (let l = c.length; i.length < l; l -= 1) {
const d = e.concat([l - 1]), f = ue(d, s);
r.push({
op: j.Remove,
path: f
});
}
}
}
function dr({ original: n, copy: e, assignedMap: t }, r, s, i, o) {
t.forEach((a, c) => {
const u = se(n, c), l = Ue(se(e, c)), d = a ? ke(n, c) ? j.Replace : j.Add : j.Remove;
if (re(u, l) && d === j.Replace)
return;
const f = r.concat(c), p = ue(f, o);
s.push(d === j.Remove ? { op: d, path: p } : { op: d, path: p, value: l }), i.push(d === j.Add ? { op: j.Remove, path: p } : d === j.Remove ? { op: j.Add, path: p, value: u } : { op: j.Replace, path: p, value: u });
});
}
function fr({ original: n, copy: e }, t, r, s, i) {
let o = 0;
n.forEach((a) => {
if (!e.has(a)) {
const c = t.concat([o]), u = ue(c, i);
r.push({
op: j.Remove,
path: u,
value: a
}), s.unshift({
op: j.Add,
path: u,
value: a
});
}
o += 1;
}), o = 0, e.forEach((a) => {
if (!n.has(a)) {
const c = t.concat([o]), u = ue(c, i);
r.push({
op: j.Add,
path: u,
value: a
}), s.unshift({
op: j.Remove,
path: u,
value: a
});
}
o += 1;
});
}
function Ee(n, e, t, r) {
const { pathAsArray: s = !0 } = n.options.enablePatches;
switch (n.type) {
case 0:
case 2:
return dr(n, e, t, r, s);
case 1:
return lr(n, e, t, r, s);
case 3:
return fr(n, e, t, r, s);
}
}
const Ne = (n, e, t = !1) => {
if (typeof n == "object" && n !== null && (!Z(n, e) || t))
throw new Error("Strict mode: Mutable data cannot be accessed directly, please use 'unsafe(callback)' wrap.");
}, at = {
get size() {
return Q(O(this)).size;
},
has(n) {
return Q(O(this)).has(n);
},
set(n, e) {
const t = O(this), r = Q(t);
return (!r.has(n) || !re(r.get(n), e)) && (F(t), te(t), t.assignedMap.set(n, !0), t.copy.set(n, e), It(t, n, e, Ee)), this;
},
delete(n) {
if (!this.has(n))
return !1;
const e = O(this);
return F(e), te(e), e.original.has(n) ? e.assignedMap.set(n, !1) : e.assignedMap.delete(n), e.copy.delete(n), !0;
},
clear() {
const n = O(this);
if (this.size) {
F(n), te(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 = O(this);
Q(t).forEach((r, s) => {
n.call(e, this.get(s), s, this);
});
},
get(n) {
var e, t;
const r = O(this), s = Q(r).get(n), i = ((t = (e = r.options).mark) === null || t === void 0 ? void 0 : t.call(e, s, B)) === B.mutable;
if (r.options.strict && Ne(s, r.options, i), i || r.finalized || !Z(s, r.options) || s !== r.original.get(n))
return s;
const o = St.createDraft({
original: s,
parentDraft: r,
key: n,
finalities: r.finalities,
options: r.options
});
return F(r), r.copy.set(n, o), o;
},
keys() {
return Q(O(this)).keys();
},
values() {
const n = this.keys();
return {
[xe]: () => this.values(),
next: () => {
const e = n.next();
return e.done ? e : {
done: !1,
value: this.get(e.value)
};
}
};
},
entries() {
const n = this.keys();
return {
[xe]: () => 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]
};
}
};
},
[xe]() {
return this.entries();
}
}, hr = Reflect.ownKeys(at), Ut = (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 = O(a), u = ((s = (r = n.options).mark) === null || s === void 0 ? void 0 : s.call(r, a, B)) === B.mutable;
if (n.options.strict && Ne(o, n.options, u), !u && !c && Z(o, n.options) && !n.finalized && n.original.has(o)) {
const l = St.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]
};
}, qe = {
get size() {
return O(this).setMap.size;
},
has(n) {
const e = O(this);
if (e.setMap.has(n))
return !0;
F(e);
const t = O(n);
return !!(t && e.setMap.has(t.original));
},
add(n) {
const e = O(this);
return this.has(n) || (F(e), te(e), e.assignedMap.set(n, !0), e.setMap.set(n, n), It(e, n, n, Ee)), this;
},
delete(n) {
if (!this.has(n))
return !1;
const e = O(this);
F(e), te(e);
const t = O(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 = O(this);
F(n), te(n);
for (const e of n.original)
n.assignedMap.set(e, !1);
n.setMap.clear();
},
values() {
const n = O(this);
F(n);
const e = n.setMap.keys();
return {
[Symbol.iterator]: () => this.values(),
next: Ut(n, e, { isValuesIterator: !0 })
};
},
entries() {
const n = O(this);
F(n);
const e = n.setMap.keys();
return {
[Symbol.iterator]: () => this.entries(),
next: Ut(n, e, {
isValuesIterator: !1
})
};
},
keys() {
return this.values();
},
[xe]() {
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(qe, {
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 pr = Reflect.ownKeys(qe), mn = /* @__PURE__ */ new WeakSet(), _n = {
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 === pn)
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, B), o === B.mutable)
return n.options.strict && Ne(u, n.options, !0), u;
}
const a = Q(n);
if (a instanceof Map && hr.includes(e)) {
if (e === "size")
return Object.getOwnPropertyDescriptor(at, "size").get.call(n.proxy);
const u = at[e];
if (u)
return u.bind(n.proxy);
}
if (a instanceof Set && pr.includes(e)) {
if (e === "size")
return Object.getOwnPropertyDescriptor(qe, "size").get.call(n.proxy);
const u = qe[e];
if (u)
return u.bind(n.proxy);
}
if (!ke(a, e)) {
const u = Rt(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 && Ne(c, n.options), n.finalized || !Z(c, n.options))
return c;
if (c === Ye(n.original, e)) {
if (F(n), n.copy[e] = Et({
original: n.original[e],
parentDraft: n,
key: n.type === 1 ? Number(e) : e,
finalities: n.finalities,
options: n.options
}), typeof o == "function") {
const u = O(n.copy[e]);
return F(u), te(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 = Rt(Q(n), e);
if (i != null && i.set)
return i.set.call(n.proxy, t), !0;
const o = Ye(Q(n), e), a = O(o);
return a && re(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) : (re(t, o) && (t !== void 0 || ke(n.original, e)) || (F(n), te(n), ke(n.original, e) && re(t, n.original[e]) ? n.assignedMap.delete(e) : n.assignedMap.set(e, !0), n.copy[e] = t, It(n, e, t, Ee)), !0);
},
has(n, e) {
return e in Q(n);
},
ownKeys(n) {
return Reflect.ownKeys(Q(n));
},
getOwnPropertyDescriptor(n, e) {
const t = Q(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 ? _n.set.call(this, n, e, void 0, n.proxy) : (Ye(n.original, e) !== void 0 || e in n.original ? (F(n), te(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 Et(n) {
const { original: e, parentDraft: t, key: r, finalities: s, options: i } = n, o = de(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, _n);
if (s.revoke.push(u), mn.add(c), a.proxy = c, t) {
const l = t;
l.finalities.draft.push((d, f) => {
var p, y;
const g = O(c);
let m = l.type === 3 ? l.setMap : l.copy;
const w = se(m, r), b = O(w);
if (b) {
let _ = b.original;
b.operated && (_ = kt(w)), it(b), ot(b, Ee, d, f), l.options.enableAutoFreeze && (l.options.updatedValues = (p = l.options.updatedValues) !== null && p !== void 0 ? p : /* @__PURE__ */ new WeakMap(), l.options.updatedValues.set(_, b.original)), Ce(m, r, _);
}
(y = g.callbacks) === null || y === void 0 || y.forEach((_) => {
_(d, f);
});
});
} else {
const l = O(c);
l.finalities.draft.push((d, f) => {
it(l), ot(l, Ee, d, f);
});
}
return c;
}
St.createDraft = Et;
function yr(n, e, t, r, s) {
var i;
const o = O(n), a = (i = o == null ? void 0 : o.original) !== null && i !== void 0 ? i : n, c = !!e.length;
if (o != null && 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 && st(o), s && pe(u, u, o == null ? void 0 : o.options.updatedValues), [
u,
t && c ? [{ op: j.Replace, path: [], value: e[0] }] : t,
r && c ? [{ op: j.Replace, path: [], value: a }] : r
];
}
function br(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, B)) === B.mutable || !Z(n, e) ? n : Et({
original: n,
parentDraft: null,
finalities: r,
options: e
});
return [
a,
(c = []) => {
const [u, l, d] = yr(a, c, s, i, e.enableAutoFreeze);
return e.enablePatches ? [u, l, d] : u;
}
];
}
function ct(n) {
const { rootDraft: e, value: t, useRawReturn: r = !1, isRoot: s = !0 } = n;
Ot(t, (i, o, a) => {
const c = O(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((d) => a.add(i === d ? u : d));
} else
Ce(a, i, u);
} else typeof o == "object" && o !== null && (n.value = o, n.isRoot = !1, ct(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 wn(n) {
var e;
const t = O(n);
if (!Z(n, t == null ? void 0 : t.options))
return n;
const r = de(n);
if (t && !t.operated)
return t.original;
let s;
function i() {
s = r === 2 ? vt(n) ? new Map(n) : new (Object.getPrototypeOf(n)).constructor(n) : r === 3 ? Array.from(t.setMap.values()) : bn(n, t == null ? void 0 : t.options);
}
if (t) {
t.finalized = !0;
try {
i();
} finally {
t.finalized = !1;
}
} else
s = n;
if (Ot(s, (o, a) => {
if (t && re(se(t.original, o), a))
return;
const c = wn(a);
c !== a && (s === n && i(), Ce(s, o, c));
}), r === 3) {
const o = (e = t == null ? void 0 : t.original) !== null && e !== void 0 ? e : s;
return At(o) ? new Set(s) : new (Object.getPrototypeOf(o)).constructor(s);
}
return s;
}
function Lt(n) {
if (!le(n))
throw new Error(`current() is only used for Draft, parameter: ${n}`);
return wn(n);
}
const gr = (n) => function e(t, r, s) {
var i, o, a;
if (typeof t == "function" && typeof r != "function")
return function(T, ...S) {
return e(T, (A) => t.call(this, A, ...S), 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 d = le(c) ? Lt(c) : c, f = Array.isArray(l.mark) ? (T, S) => {
for (const A of l.mark) {
if (typeof A != "function")
throw new Error(`Invalid mark: ${A}, 'mark' should be a function.`);
const C = A(T, S);
if (C)
return C;
}
} : l.mark, p = (i = l.enablePatches) !== null && i !== void 0 ? i : !1, y = (o = l.strict) !== null && o !== void 0 ? o : !1, m = {
enableAutoFreeze: (a = l.enableAutoFreeze) !== null && a !== void 0 ? a : !1,
mark: f,
strict: y,
enablePatches: p
};
if (!Z(d, m) && typeof d == "object" && d !== 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 [w, b] = br(d, m);
if (typeof r != "function") {
if (!Z(d, m))
throw new Error("Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.");
return [w, b];
}
let _;
try {
_ = u(w);
} catch (T) {
throw st(O(w)), T;
}
const v = (T) => {
const S = O(w);
if (!le(T)) {
if (T !== void 0 && !re(T, w) && (S != null && S.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 C = T == null ? void 0 : T[ir];
if (C) {
const R = C[0];
return m.strict && typeof T == "object" && T !== null && ct({
rootDraft: S,
value: T,
useRawReturn: !0
}), b([R]);
}
if (T !== void 0)
return typeof T == "object" && T !== null && ct({ rootDraft: S, value: T }), b([T]);
}
if (T === w || T === void 0)
return b([]);
const A = O(T);
if (m === A.options) {
if (A.operated)
throw new Error("Cannot return a modified child draft.");
return b([Lt(T)]);
}
return b([T]);
};
return _ instanceof Promise ? _.then(v, (T) => {
throw st(O(w)), T;
}) : v(_);
}, Ke = gr();
Object.prototype.constructor.toString();
function Tn(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 Ft(n, e) {
return Object.keys(n).length === Object.keys(e).length && Object.keys(n).every(
(t) => e.hasOwnProperty(t) && n[t] === e[t]
);
}
function Le(n, e) {
return typeof n != "object" || typeof e != "object" || n === null || e === null ? n === e : Tn(n, e) ? Object.keys(n).every(
(t) => Le(n[t], e[t])
) : !1;
}
function Ct(n) {
if (!Ae(n))
return n;
const e = {};
for (const [t, r] of Object.entries(n))
r !== void 0 && (e[t] = r);
return e;
}
function Sn(n, e) {
if (!Ae(n) || !Ae(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 = Ae(n[r]) && Ae(e[r]);
t[r] = s ? Sn(n[r], e[r]) : e[r];
}
return t;
}
function Ae(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 Nt(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 An(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;
}
An(n[t], r), _r(n[t]) && delete n[t];
}
}
function _r(n) {
return n && Object.keys(n).length === 0;
}
const qt = /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$/, wr = {
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 Tr(n) {
return new Date(n);
}
function Sr(n) {
return /* @__PURE__ */ new Date(n + "Z");
}
const Ar = /^(\d+)[\./-](\d+)[\./-](\d+)$/;
function vr(n) {
const e = n.match(Ar);
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 kr(n) {
const [e, t] = n.split(" ");
return /* @__PURE__ */ new Date(e + "T" + t + "Z");
}
function Or(n) {
const [e, t] = n.split(" ");
return /* @__PURE__ */ new Date(e + "T" + t + "Z");
}
function Ir(n) {
return new Date(n);
}
function Er(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 Cr(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 Mr(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 Pr(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 jr(n) {
switch (n) {
case "epoch":
return /* @__PURE__ */ new Date(0);
case "infinity":
case "-infinity":
case "today":
case "tomorrow":
case "yesterday":
return null;
}
}
function Dr(n) {
const e = n.match(qt);
if (!e)
return null;
const [t] = e, r = wr[t], s = new Date(n.replace(qt, "Z"));
return new Date(s.getTime() - r * 1e3);
}
const $r = [
vr,
Or,
Er,
Pr,
Ir,
Sr,
Cr,
kr,
Tr,
jr,
Dr,
Mr
];
function Rr(n, e) {
try {
const t = n(e);
return t instanceof Date && !isNaN(t.getTime()) ? t : null;
} catch {
return null;
}
}
function ut(n) {
for (const e of $r) {
const t = Rr(e, n);
if (t)
return t;
}
return null;
}
function xr(n) {
try {
const e = JSON.parse(n);
return typeof e == "string" ? ut(e) : null;
} catch {
return null;
}
}
function Ve(n) {
if (n !== void 0) {
if (n === null)
return null;
if (n instanceof Date)
return n;
if (typeof n == "string") {
const e = ut(n) || xr(n) || ut(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 ae {
constructor(e, t) {
h(this, "attrs");
h(this, "linkIndex");
h(this, "_blobAttrs", null);
h(this, "_primaryKeys", null);
h(this, "_forwardIdents", null);
h(this, "_revIdents", null);
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 (Pt(e)) {
const [t, r, s] = e["forward-identity"];
K(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"];
K(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;
K(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;
K(this._revIdents, [s, i], e);
}
}
return this._revIdents;
}
toJSON() {
return { attrs: this.attrs, linkIndex: this.linkIndex };
}
}
function Ur(n) {
return n.cardinality === "one";
}
function Mt(n) {
return n["value-type"] === "ref";
}
function Pt(n) {
return n["value-type"] === "blob";
}
function _e(n, e) {
return e.reduce((t, r) => t && t.get(r), n);
}
function Y(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) && Y(n.get(t), r);
}
function K(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 vn(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 = Ve(u), o[2] = u), Mt(l) && K(i, [u, c, a], o), K(r, [a, c, u], o), K(s, [c, a, u], o);
}
return { eav: r, aev: s, vae: i };
}
function kn(n) {
return {
triples: z(n.eav, 3),
cardinalityInference: n.cardinalityInference,
useDateObjects: n.useDateObjects,
version: 1
};
}
function On(n, e) {
return ye(
n,
e.triples,
e.cardinalityInference,
e.useDateObjects
);
}
function In(n, e) {
if (n)
return new ae(n.attrs, n.linkIndex);
if (e && "__type" in e)
return new ae(e.attrs, e.linkIndex);
}
function Lr(n, e) {
return _e(n.eav, [e]) !== void 0;
}
function ye(n, e, t, r) {
const s = vn(
n,
e,
r
);
return s.cardinalityInference = t, s.useDateObjects = r, s;
}
function Me(n, e) {
var s, i;
let t;
if (Array.isArray(e[0])) {
const [o, a] = e[0], c = n.aev.get(o);
if (!c)
return null;
t = (s = z(c, 2).find((l) => l[2] === a)) == null ? void 0 : s[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 [o, a] = r, c = n.aev.get(o);
if (!c)
return null;
const l = (i = z(c, 2).find((g) => g[2] === a)) == null ? void 0 : i[0];
if (!l)
return null;
const [d, f, p, ...y] = e;
return [t, f, l, ...y];
} else {
const [o, ...a] = e;
return [t, ...a];
}
}
function En(n, e, t) {
const r = Me(n, t);
if (!r)
return;
const [s, i, o] = r, a = e.getAttr(i);
a && (Y(n.eav, [s, i, o]), Y(n.aev, [i, s, o]), Mt(a) && Y(n.vae, [o, i, s]));
}
let Fr = 0;
function Cn(n, e, t) {
const [r, s, i] = t;
let o;
const a = _e(n.eav, [r, s, i]);
return a && (o = a[3]), o || Date.now() * 10 + Fr++;
}
function Mn(n, e, t) {
const r = Me(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 = Ve(o));
const c = _e(n.eav, [s, i, o]), u = (c == null ? void 0 : c[3]) ?? Cn(n, a, r), l = [s, i, o, u];
Ur(a) ? (K(n.eav, [s, i], /* @__PURE__ */ new Map([[o, l]])), K(n.aev, [i, s], /* @__PURE__ */ new Map([[o, l]]))) : (K(n.eav, [s, i, o], l), K(n.aev, [i, s, o], l)), Mt(a) && K(n.vae, [o, i, s], l);
}
function Nr(n, e, t) {
var p;
const r = Me(n, t);
if (!r)
return;
const [s, i, o] = r, a = e.getAttr(i);
if (!a) return;
if (!Pt(a))
throw new Error("merge operation is not supported for links");
const c = _e(n.eav, [s, i]);
if (!c) return;
const u = (p = c.values().next()) == null ? void 0 : p.value;
if (!u) return;
const l = u[2], d = Sn(l, o), f = [
s,
i,
d,
Cn(n, a, u)
];
K(n.eav, [s, i], /* @__PURE__ */ new Map([[d, f]])), K(n.aev, [i, s], /* @__PURE__ */ new Map([[d, f]]));
}
function lt(n, e, t) {
var u, l;
const [r, s] = t, i = Me(n, [r]);
if (!i)
return;
const [o] = i, a = n.eav.get(o);
if (a) {
for (const d of a.keys()) {
const f = e.getAttr(d);
f && f["on-delete-reverse"] === "cascade" && z(a.get(d), 1).forEach(
([p, y, g]) => {
var m;
return lt(n, e, [g, (m = f["reverse-identity"]) == null ? void 0 : m[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
!f || // Make sure it matches the etype
((u = f["forward-identity"]) == null ? void 0 : u[1]) === s) && (Y(n.aev, [d, o]), Y(n.eav, [o, d]));
}
a.size === 0 && Y(n.eav, [o]);
}
const c = n.vae.get(o) && z(n.vae.get(o), 2);
c && c.forEach((d) => {
var m, w, b;
const [f, p, y] = d, g = e.getAttr(p);
(!s || !g || ((m = g["reverse-identity"]) == null ? void 0 : m[1]) === s) && (Y(n.eav, [f, p, y]), Y(n.aev, [p, f, y]), Y(n.vae, [y, p, f])), g && g["on-delete"] === "cascade" && ((w = g["reverse-identity"]) == null ? void 0 : w[1]) === s && lt(n, e, [f, (b = g["forward-identity"]) == null ? void 0 : b[1]]);
}), ((l = n.vae.get(o)) == null ? void 0 : l.size) === 0 && Y(n.vae, [o]);
}
function Pn(n, e, t) {
const r = vn(
e,
t,
n.useDateObjects
);
Object.keys(r).forEach((s) => {
n[s] = r[s];
});
}
function qr(n, [e]) {
n.addAttr(e);
}
function jn(n) {
return z(n.eav, 3);
}
function Kr(n, e, [t]) {
if (!e.getAttr(t)) return;
const r = jn(n).filter(([s, i]) => i !== t);
e.deleteAttr(t), Pn(n, e, r);
}
function zr(n, e, [t]) {
e.getAttr(t.id) && (e.updateAttr(t), Pn(n, e, jn(n)));
}
function Wr(n, e, t) {
const [r, ...s] = t;
switch (r) {
case "add-triple":
Mn(n, e, s);
break;
case "deep-merge-triple":
Nr(n, e, s);
break;
case "retract-triple":
En(n, e, s);
break;
case "delete-entity":
lt(n, e, s);
break;
case "add-attr":
qr(e, s);
break;
case "delete-attr":
Kr(n, e, s);
break;
case "update-attr":
zr(n, e, s);
break;
case "restore-attr":
break;
case "rule-params":
break;
default:
throw new Error(`unhandled transaction action: ${r}`);
}
}
function z(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())
z(r, e - 1, t);
return t;
}
function Pe(n, e, t) {
var i;
const r = [];
if (t != null && t.hasOwnProperty("$not")) {
for (const o of e.keys())
t.$not !== o && r.push(e.get(o));
return r;
}
if (t != null && t.hasOwnProperty("$isNull")) {
const { attrId: o, isNull: a, reverse: c } = t.$isNull;
if (c)
for (const u of e.keys()) {
const l = n.vae.get(u), d = !l || !l.get(o);
(a ? d : !d) && r.push(e.get(u));
}
else {
const u = n.aev.get(o);
for (const l of e.keys()) {
const d = !u || ((i = u.get(l)) == null ? void 0 : i.get(null)) || !u.get(l);
(a ? d : !d) && r.push(e.get(l));
}
}
return r;
}
if (t != null && t.$comparator)
return z(e, 1).filter(t.$op);
const s = t.in || t.$in || [t];
for (const o of s) {
const a = e.get(o);
a && r.push(a);
}
return r;
}
function Vr(n, e, t) {
let r = "";
return n !== void 0 && (r += "e"), e !== void 0 && (r += "a"), t !== void 0 && (r += "v"), r;
}
function Qr(n, [e, t, r]) {
var i, o;
switch (Vr(e, t, r)) {
case "e": {
const a = n.eav.get(e);
return z(a, 2);
}
case "ea": {
const a = (i = n.eav.get(e)) == null ? void 0 : i.get(t);
return z(a, 1);
}
case "eav": {
const a = (o = n.eav.get(e)) == null ? void 0 : o.get(t);
return a ? Pe(n, a, r) : [];
}
case "ev": {
const a = n.eav.get(e);
if (!a)
return [];
const c = [];
for (const u of a.values())
c.push(...Pe(n, u, r));
return c;
}
case "a": {
const a = n.aev.get(t);
return z(a, 2);
}
case "av": {
const a = n.aev.get(t);
if (!a)
return [];
const c = [];
for (const u of a.values())
c.push(...Pe(n, u, r));
return c;
}
case "v": {
const a = [];
for (const c of n.eav.values())
for (const u of c.values())
a.push(...Pe(n, u, r));
return a;
}
default:
return z(n.eav, 3);
}
}
function Br(n, e, t) {
var s;
const r = {};
if (!e)
return r;
for (const [i, o] of e.entries()) {
const a = (s = n.eav.get(t)) == null ? void 0 : s.get(o.id), c = z(a, 1);
for (const u of c)
r[i] = u[2];
}
return r;
}
function D(n, e, t) {
var r;
return (r = n.forwardIdents.get(e)) == null ? void 0 : r.get(t);
}
function fe(n, e, t) {
var r;
return (r = n.revIdents.get(e)) == null ? void 0 : r.get(t);
}
function Gr(n, e) {
return n.blobAttrs.get(e);
}
function Dn(n, e) {
var r;
const t = n.primaryKeys.get(e);
return t || ((r = n.forwardIdents.get(e)) == null ? void 0 : r.get("id"));
}
function Hr(n, e, t) {
const r = Me(n, t);
if (!r)
return;
const [s, i, o] = r;
if (e.getAttr(i))
return _e(n.eav, [s, i]);
}
function Jr(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 == null ? void 0 : c.mode;
if (u !== "create" && u !== "update")
return !0;
let l = !1;
const d = e.getAttr(o);
if (d) {
const f = Dn(
e,
d["forward-identity"][1]
);
l = !!Hr(n, e, [
i,
f == null ? void 0 : f.id,
i
]);
}
return !(u === "create" && l || u === "update" && !l);
}
);
return Ke(
{ store: n, attrsStore: e },
(s) => {
r.forEach((i) => {
Wr(s.store, s.attrsStore, i);
});
},
{
mark: (s) => {
if (s instanceof ae)
return "immutable";
}
}
);
}
function Yr(n) {
return typeof n == "string" && n.startsWith("?");
}
function Zr(n, e, t) {
if (t.hasOwnProperty(n)) {
const r = t[n];
return $n(r, e, t);
}
return { ...t, [n]: e };
}
function Kt(n, e, t) {
return n === e ? t : null;
}
function Xr(n) {
switch (typeof n) {
case "string":
return n.startsWith("?") ? Zr : Kt;
default:
return Kt;
}
}
const es = [
"in",
"$in",
"$not",
"$isNull",
"$comparator"
// covers all of $gt, $lt, etc.
];
function ts(n) {
for