@instantdb/core
Version:
Instant's core local abstraction
1,827 lines • 136 kB
JavaScript
var In = Object.defineProperty;
var En = (t, e, n) => e in t ? In(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
var y = (t, e, n) => En(t, typeof e != "symbol" ? e + "" : e, n);
function D(t) {
if (typeof t == "number")
return (Math.abs(t * 2654435761) >>> 0).toString(16);
if (typeof t == "boolean") return t ? "1" : "0";
if (t === null) return "null";
if (t === void 0) return "undefined";
if (typeof t == "string") {
let e = 2166136261;
for (let n = 0; n < t.length; n++)
e ^= t.charCodeAt(n), e += (e << 1) + (e << 4) + (e << 7) + (e << 8) + (e << 24), e = e >>> 0;
return e.toString(16);
}
if (Array.isArray(t)) {
let e = 2166136261;
for (let n = 0; n < t.length; n++) {
e ^= (n + 1) * 2654435761;
const r = D(t[n]);
for (let s = 0; s < r.length; s++)
e ^= r.charCodeAt(s), e *= 16777619, e = e >>> 0;
}
return e.toString(16);
}
if (typeof t == "object") {
let e = 2166136261;
const n = Object.keys(t).sort();
for (let r = 0; r < n.length; r++) {
const s = n[r], i = D(s);
e ^= parseInt(i, 16), e *= 16777619, e = e >>> 0;
const o = D(t[s]);
e ^= parseInt(o, 16), e *= 16777619, e = e >>> 0;
}
return e.toString(16);
}
return D(String(t));
}
const P = {
Remove: "remove",
Replace: "replace",
Add: "add"
}, Wt = Symbol.for("__MUTATIVE_PROXY_DRAFT__"), jn = Symbol("__MUTATIVE_RAW_RETURN_SYMBOL__"), pe = Symbol.iterator, $ = {
mutable: "mutable",
immutable: "immutable"
}, Ye = {};
function ie(t, e) {
return t instanceof Map ? t.has(e) : Object.prototype.hasOwnProperty.call(t, e);
}
function mt(t, e) {
if (e in t) {
let n = Reflect.getPrototypeOf(t);
for (; n; ) {
const r = Reflect.getOwnPropertyDescriptor(n, e);
if (r)
return r;
n = Reflect.getPrototypeOf(n);
}
}
}
function Xe(t) {
return Object.getPrototypeOf(t) === Set.prototype;
}
function Ze(t) {
return Object.getPrototypeOf(t) === Map.prototype;
}
function U(t) {
var e;
return (e = t.copy) !== null && e !== void 0 ? e : t.original;
}
function Y(t) {
return !!m(t);
}
function m(t) {
return typeof t != "object" ? null : t == null ? void 0 : t[Wt];
}
function et(t) {
var e;
const n = m(t);
return n ? (e = n.copy) !== null && e !== void 0 ? e : n.original : t;
}
function q(t, e) {
if (!t || typeof t != "object")
return !1;
let n;
return Object.getPrototypeOf(t) === Object.prototype || Array.isArray(t) || t instanceof Map || t instanceof Set || !!(e != null && e.mark) && ((n = e.mark(t, $)) === $.immutable || typeof n == "function");
}
function Qt(t, e = []) {
if (Object.hasOwnProperty.call(t, "key")) {
const n = t.parent.copy, r = m(B(n, t.key));
if (r !== null && (r == null ? void 0 : r.original) !== t.original)
return null;
const s = t.parent.type === 3, i = s ? Array.from(t.parent.setMap.keys()).indexOf(t.key) : t.key;
if (!(s && n.size > i || ie(n, i)))
return null;
e.push(i);
}
if (t.parent)
return Qt(t.parent, e);
e.reverse();
try {
Rn(t.copy, e);
} catch {
return null;
}
return e;
}
function X(t) {
return Array.isArray(t) ? 1 : t instanceof Map ? 2 : t instanceof Set ? 3 : 0;
}
function B(t, e) {
return X(t) === 2 ? t.get(e) : t[e];
}
function ce(t, e, n) {
X(t) === 2 ? t.set(e, n) : t[e] = n;
}
function Ae(t, e) {
const n = m(t);
return (n ? U(n) : t)[e];
}
function J(t, e) {
return t === e ? t !== 0 || 1 / t === 1 / e : t !== t && e !== e;
}
function je(t) {
if (t)
for (; t.finalities.revoke.length > 0; )
t.finalities.revoke.pop()();
}
function K(t, e) {
return e ? t : [""].concat(t).map((n) => {
const r = `${n}`;
return r.indexOf("/") === -1 && r.indexOf("~") === -1 ? r : r.replace(/~/g, "~0").replace(/\//g, "~1");
}).join("/");
}
function Rn(t, e) {
for (let n = 0; n < e.length - 1; n += 1) {
const r = e[n];
if (t = B(X(t) === 3 ? Array.from(t) : t, r), typeof t != "object")
throw new Error(`Cannot resolve patch at '${e.join("/")}'.`);
}
return t;
}
function xn(t) {
const e = Object.create(Object.getPrototypeOf(t));
return Reflect.ownKeys(t).forEach((n) => {
let r = Reflect.getOwnPropertyDescriptor(t, n);
if (r.enumerable && r.configurable && r.writable) {
e[n] = t[n];
return;
}
r.writable || (r.writable = !0, r.configurable = !0), (r.get || r.set) && (r = {
configurable: !0,
writable: !0,
enumerable: r.enumerable,
value: t[n]
}), Reflect.defineProperty(e, n, r);
}), e;
}
const Dn = Object.prototype.propertyIsEnumerable;
function Jt(t, e) {
let n;
if (Array.isArray(t))
return Array.prototype.concat.call(t);
if (t instanceof Set) {
if (!Xe(t)) {
const r = Object.getPrototypeOf(t).constructor;
return new r(t.values());
}
return Set.prototype.difference ? Set.prototype.difference.call(t, /* @__PURE__ */ new Set()) : new Set(t.values());
} else if (t instanceof Map) {
if (!Ze(t)) {
const r = Object.getPrototypeOf(t).constructor;
return new r(t);
}
return new Map(t);
} else if (e != null && e.mark && (n = e.mark(t, $), n !== void 0) && n !== $.mutable) {
if (n === $.immutable)
return xn(t);
if (typeof n == "function") {
if (e.enablePatches || e.enableAutoFreeze)
throw new Error("You can't use mark and patches or auto freeze together.");
return n();
}
throw new Error(`Unsupported mark result: ${n}`);
} else if (typeof t == "object" && Object.getPrototypeOf(t) === Object.prototype) {
const r = {};
return Object.keys(t).forEach((s) => {
r[s] = t[s];
}), Object.getOwnPropertySymbols(t).forEach((s) => {
Dn.call(t, s) && (r[s] = t[s]);
}), r;
} else
throw new Error("Please check mark() to ensure that it is a stable marker draftable function.");
}
function E(t) {
t.copy || (t.copy = Jt(t.original, t.options));
}
function re(t) {
if (!q(t))
return et(t);
if (Array.isArray(t))
return t.map(re);
if (t instanceof Map) {
const n = Array.from(t.entries()).map(([r, s]) => [
r,
re(s)
]);
if (!Ze(t)) {
const r = Object.getPrototypeOf(t).constructor;
return new r(n);
}
return new Map(n);
}
if (t instanceof Set) {
const n = Array.from(t).map(re);
if (!Xe(t)) {
const r = Object.getPrototypeOf(t).constructor;
return new r(n);
}
return new Set(n);
}
const e = Object.create(Object.getPrototypeOf(t));
for (const n in t)
e[n] = re(t[n]);
return e;
}
function ye(t) {
return Y(t) ? re(t) : t;
}
function z(t) {
var e;
t.assignedMap = (e = t.assignedMap) !== null && e !== void 0 ? e : /* @__PURE__ */ new Map(), t.operated || (t.operated = !0, t.parent && z(t.parent));
}
function St() {
throw new Error("Cannot modify frozen object");
}
function ee(t, e, n, r, s) {
{
n = n ?? /* @__PURE__ */ new WeakMap(), r = r ?? [], s = s ?? [];
const o = n.has(t) ? n.get(t) : t;
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 f = r[u];
return typeof c == "object" && (f instanceof Map || f instanceof Set) ? Array.from(f.keys()).indexOf(c) : c;
}).join("/")}`);
r.push(o), s.push(e);
} else
r.push(o);
}
if (Object.isFrozen(t) || Y(t)) {
r.pop(), s.pop();
return;
}
switch (X(t)) {
case 2:
for (const [a, c] of t)
ee(a, a, n, r, s), ee(c, a, n, r, s);
t.set = t.clear = t.delete = St;
break;
case 3:
for (const a of t)
ee(a, a, n, r, s);
t.add = t.clear = t.delete = St;
break;
case 1:
Object.freeze(t);
let o = 0;
for (const a of t)
ee(a, o, n, r, s), o += 1;
break;
default:
Object.freeze(t), Object.keys(t).forEach((a) => {
const c = t[a];
ee(c, a, n, r, s);
});
}
r.pop(), s.pop();
}
function tt(t, e) {
const n = X(t);
if (n === 0)
Reflect.ownKeys(t).forEach((r) => {
e(r, t[r], t);
});
else if (n === 1) {
let r = 0;
for (const s of t)
e(r, s, t), r += 1;
} else
t.forEach((r, s) => e(s, r, t));
}
function Bt(t, e, n) {
if (Y(t) || !q(t, n) || e.has(t) || Object.isFrozen(t))
return;
const r = t instanceof Set, s = r ? /* @__PURE__ */ new Map() : void 0;
if (e.add(t), tt(t, (i, o) => {
var a;
if (Y(o)) {
const c = m(o);
E(c);
const u = !((a = c.assignedMap) === null || a === void 0) && a.size || c.operated ? c.copy : c.original;
ce(r ? s : t, i, u);
} else
Bt(o, e, n);
}), s) {
const i = t, o = Array.from(i);
i.clear(), o.forEach((a) => {
i.add(s.has(a) ? s.get(a) : a);
});
}
}
function Un(t, e) {
const n = t.type === 3 ? t.setMap : t.copy;
t.finalities.revoke.length > 1 && t.assignedMap.get(e) && n && Bt(B(n, e), t.finalities.handledSet, t.options);
}
function Re(t) {
t.type === 3 && t.copy && (t.copy.clear(), t.setMap.forEach((e) => {
t.copy.add(et(e));
}));
}
function xe(t, e, n, r) {
if (t.operated && t.assignedMap && t.assignedMap.size > 0 && !t.finalized) {
if (n && r) {
const i = Qt(t);
i && e(t, i, n, r);
}
t.finalized = !0;
}
}
function nt(t, e, n, r) {
const s = m(n);
s && (s.callbacks || (s.callbacks = []), s.callbacks.push((i, o) => {
var a;
const c = t.type === 3 ? t.setMap : t.copy;
if (J(B(c, e), n)) {
let u = s.original;
s.copy && (u = s.copy), Re(t), xe(t, r, i, o), t.options.enableAutoFreeze && (t.options.updatedValues = (a = t.options.updatedValues) !== null && a !== void 0 ? a : /* @__PURE__ */ new WeakMap(), t.options.updatedValues.set(u, s.original)), ce(c, e, u);
}
}), t.options.enableAutoFreeze && s.finalities !== t.finalities && (t.options.enableAutoFreeze = !1)), q(n, t.options) && t.finalities.draft.push(() => {
const i = t.type === 3 ? t.setMap : t.copy;
J(B(i, e), n) && Un(t, e);
});
}
function $n(t, e, n, r, s) {
let { original: i, assignedMap: o, options: a } = t, c = t.copy;
c.length < i.length && ([i, c] = [c, i], [n, r] = [r, n]);
for (let u = 0; u < i.length; u += 1)
if (o.get(u.toString()) && c[u] !== i[u]) {
const f = e.concat([u]), l = K(f, s);
n.push({
op: P.Replace,
path: l,
// If it is a draft, it needs to be deep cloned, and it may also be non-draft.
value: ye(c[u])
}), r.push({
op: P.Replace,
path: l,
// If it is a draft, it needs to be deep cloned, and it may also be non-draft.
value: ye(i[u])
});
}
for (let u = i.length; u < c.length; u += 1) {
const f = e.concat([u]), l = K(f, s);
n.push({
op: P.Add,
path: l,
// If it is a draft, it needs to be deep cloned, and it may also be non-draft.
value: ye(c[u])
});
}
if (i.length < c.length) {
const { arrayLengthAssignment: u = !0 } = a.enablePatches;
if (u) {
const f = e.concat(["length"]), l = K(f, s);
r.push({
op: P.Replace,
path: l,
value: i.length
});
} else
for (let f = c.length; i.length < f; f -= 1) {
const l = e.concat([f - 1]), d = K(l, s);
r.push({
op: P.Remove,
path: d
});
}
}
}
function Nn({ original: t, copy: e, assignedMap: n }, r, s, i, o) {
n.forEach((a, c) => {
const u = B(t, c), f = ye(B(e, c)), l = a ? ie(t, c) ? P.Replace : P.Add : P.Remove;
if (J(u, f) && l === P.Replace)
return;
const d = r.concat(c), h = K(d, o);
s.push(l === P.Remove ? { op: l, path: h } : { op: l, path: h, value: f }), i.push(l === P.Add ? { op: P.Remove, path: h } : l === P.Remove ? { op: P.Add, path: h, value: u } : { op: P.Replace, path: h, value: u });
});
}
function Ln({ original: t, copy: e }, n, r, s, i) {
let o = 0;
t.forEach((a) => {
if (!e.has(a)) {
const c = n.concat([o]), u = K(c, i);
r.push({
op: P.Remove,
path: u,
value: a
}), s.unshift({
op: P.Add,
path: u,
value: a
});
}
o += 1;
}), o = 0, e.forEach((a) => {
if (!t.has(a)) {
const c = n.concat([o]), u = K(c, i);
r.push({
op: P.Add,
path: u,
value: a
}), s.unshift({
op: P.Remove,
path: u,
value: a
});
}
o += 1;
});
}
function ae(t, e, n, r) {
const { pathAsArray: s = !0 } = t.options.enablePatches;
switch (t.type) {
case 0:
case 2:
return Nn(t, e, n, r, s);
case 1:
return $n(t, e, n, r, s);
case 3:
return Ln(t, e, n, r, s);
}
}
const be = (t, e, n = !1) => {
if (typeof t == "object" && t !== null && (!q(t, e) || n))
throw new Error("Strict mode: Mutable data cannot be accessed directly, please use 'unsafe(callback)' wrap.");
}, De = {
get size() {
return U(m(this)).size;
},
has(t) {
return U(m(this)).has(t);
},
set(t, e) {
const n = m(this), r = U(n);
return (!r.has(t) || !J(r.get(t), e)) && (E(n), z(n), n.assignedMap.set(t, !0), n.copy.set(t, e), nt(n, t, e, ae)), this;
},
delete(t) {
if (!this.has(t))
return !1;
const e = m(this);
return E(e), z(e), e.original.has(t) ? e.assignedMap.set(t, !1) : e.assignedMap.delete(t), e.copy.delete(t), !0;
},
clear() {
const t = m(this);
if (this.size) {
E(t), z(t), t.assignedMap = /* @__PURE__ */ new Map();
for (const [e] of t.original)
t.assignedMap.set(e, !1);
t.copy.clear();
}
},
forEach(t, e) {
const n = m(this);
U(n).forEach((r, s) => {
t.call(e, this.get(s), s, this);
});
},
get(t) {
var e, n;
const r = m(this), s = U(r).get(t), i = ((n = (e = r.options).mark) === null || n === void 0 ? void 0 : n.call(e, s, $)) === $.mutable;
if (r.options.strict && be(s, r.options, i), i || r.finalized || !q(s, r.options) || s !== r.original.get(t))
return s;
const o = Ye.createDraft({
original: s,
parentDraft: r,
key: t,
finalities: r.finalities,
options: r.options
});
return E(r), r.copy.set(t, o), o;
},
keys() {
return U(m(this)).keys();
},
values() {
const t = this.keys();
return {
[pe]: () => this.values(),
next: () => {
const e = t.next();
return e.done ? e : {
done: !1,
value: this.get(e.value)
};
}
};
},
entries() {
const t = this.keys();
return {
[pe]: () => this.entries(),
next: () => {
const e = t.next();
if (e.done)
return e;
const n = this.get(e.value);
return {
done: !1,
value: [e.value, n]
};
}
};
},
[pe]() {
return this.entries();
}
}, qn = Reflect.ownKeys(De), Ot = (t, e, { isValuesIterator: n }) => () => {
var r, s;
const i = e.next();
if (i.done)
return i;
const o = i.value;
let a = t.setMap.get(o);
const c = m(a), u = ((s = (r = t.options).mark) === null || s === void 0 ? void 0 : s.call(r, a, $)) === $.mutable;
if (t.options.strict && be(o, t.options, u), !u && !c && q(o, t.options) && !t.finalized && t.original.has(o)) {
const f = Ye.createDraft({
original: o,
parentDraft: t,
key: o,
finalities: t.finalities,
options: t.options
});
t.setMap.set(o, f), a = f;
} else c && (a = c.proxy);
return {
done: !1,
value: n ? a : [a, a]
};
}, we = {
get size() {
return m(this).setMap.size;
},
has(t) {
const e = m(this);
if (e.setMap.has(t))
return !0;
E(e);
const n = m(t);
return !!(n && e.setMap.has(n.original));
},
add(t) {
const e = m(this);
return this.has(t) || (E(e), z(e), e.assignedMap.set(t, !0), e.setMap.set(t, t), nt(e, t, t, ae)), this;
},
delete(t) {
if (!this.has(t))
return !1;
const e = m(this);
E(e), z(e);
const n = m(t);
return n && e.setMap.has(n.original) ? (e.assignedMap.set(n.original, !1), e.setMap.delete(n.original)) : (!n && e.setMap.has(t) ? e.assignedMap.set(t, !1) : e.assignedMap.delete(t), e.setMap.delete(t));
},
clear() {
if (!this.size)
return;
const t = m(this);
E(t), z(t);
for (const e of t.original)
t.assignedMap.set(e, !1);
t.setMap.clear();
},
values() {
const t = m(this);
E(t);
const e = t.setMap.keys();
return {
[Symbol.iterator]: () => this.values(),
next: Ot(t, e, { isValuesIterator: !0 })
};
},
entries() {
const t = m(this);
E(t);
const e = t.setMap.keys();
return {
[Symbol.iterator]: () => this.entries(),
next: Ot(t, e, {
isValuesIterator: !1
})
};
},
keys() {
return this.values();
},
[pe]() {
return this.values();
},
forEach(t, e) {
const n = this.values();
let r = n.next();
for (; !r.done; )
t.call(e, r.value, r.value, this), r = n.next();
}
};
Set.prototype.difference && Object.assign(we, {
intersection(t) {
return Set.prototype.intersection.call(new Set(this.values()), t);
},
union(t) {
return Set.prototype.union.call(new Set(this.values()), t);
},
difference(t) {
return Set.prototype.difference.call(new Set(this.values()), t);
},
symmetricDifference(t) {
return Set.prototype.symmetricDifference.call(new Set(this.values()), t);
},
isSubsetOf(t) {
return Set.prototype.isSubsetOf.call(new Set(this.values()), t);
},
isSupersetOf(t) {
return Set.prototype.isSupersetOf.call(new Set(this.values()), t);
},
isDisjointFrom(t) {
return Set.prototype.isDisjointFrom.call(new Set(this.values()), t);
}
});
const Fn = Reflect.ownKeys(we), Ht = /* @__PURE__ */ new WeakSet(), Kt = {
get(t, e, n) {
var r, s;
const i = (r = t.copy) === null || r === void 0 ? void 0 : r[e];
if (i && Ht.has(i))
return i;
if (e === Wt)
return t;
let o;
if (t.options.mark) {
const u = e === "size" && (t.original instanceof Map || t.original instanceof Set) ? Reflect.get(t.original, e) : Reflect.get(t.original, e, n);
if (o = t.options.mark(u, $), o === $.mutable)
return t.options.strict && be(u, t.options, !0), u;
}
const a = U(t);
if (a instanceof Map && qn.includes(e)) {
if (e === "size")
return Object.getOwnPropertyDescriptor(De, "size").get.call(t.proxy);
const u = De[e];
if (u)
return u.bind(t.proxy);
}
if (a instanceof Set && Fn.includes(e)) {
if (e === "size")
return Object.getOwnPropertyDescriptor(we, "size").get.call(t.proxy);
const u = we[e];
if (u)
return u.bind(t.proxy);
}
if (!ie(a, e)) {
const u = mt(a, e);
return u ? "value" in u ? u.value : (
// !case: support for getter
(s = u.get) === null || s === void 0 ? void 0 : s.call(t.proxy)
) : void 0;
}
const c = a[e];
if (t.options.strict && be(c, t.options), t.finalized || !q(c, t.options))
return c;
if (c === Ae(t.original, e)) {
if (E(t), t.copy[e] = rt({
original: t.original[e],
parentDraft: t,
key: t.type === 1 ? Number(e) : e,
finalities: t.finalities,
options: t.options
}), typeof o == "function") {
const u = m(t.copy[e]);
return E(u), z(u), u.copy;
}
return t.copy[e];
}
return c;
},
set(t, e, n) {
var r;
if (t.type === 3 || t.type === 2)
throw new Error("Map/Set draft does not support any property assignment.");
let s;
if (t.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 = mt(U(t), e);
if (i != null && i.set)
return i.set.call(t.proxy, n), !0;
const o = Ae(U(t), e), a = m(o);
return a && J(a.original, n) ? (t.copy[e] = n, t.assignedMap = (r = t.assignedMap) !== null && r !== void 0 ? r : /* @__PURE__ */ new Map(), t.assignedMap.set(e, !1), !0) : (J(n, o) && (n !== void 0 || ie(t.original, e)) || (E(t), z(t), ie(t.original, e) && J(n, t.original[e]) ? t.assignedMap.delete(e) : t.assignedMap.set(e, !0), t.copy[e] = n, nt(t, e, n, ae)), !0);
},
has(t, e) {
return e in U(t);
},
ownKeys(t) {
return Reflect.ownKeys(U(t));
},
getOwnPropertyDescriptor(t, e) {
const n = U(t), r = Reflect.getOwnPropertyDescriptor(n, e);
return r && {
writable: !0,
configurable: t.type !== 1 || e !== "length",
enumerable: r.enumerable,
value: n[e]
};
},
getPrototypeOf(t) {
return Reflect.getPrototypeOf(t.original);
},
setPrototypeOf() {
throw new Error("Cannot call 'setPrototypeOf()' on drafts");
},
defineProperty() {
throw new Error("Cannot call 'defineProperty()' on drafts");
},
deleteProperty(t, e) {
var n;
return t.type === 1 ? Kt.set.call(this, t, e, void 0, t.proxy) : (Ae(t.original, e) !== void 0 || e in t.original ? (E(t), z(t), t.assignedMap.set(e, !1)) : (t.assignedMap = (n = t.assignedMap) !== null && n !== void 0 ? n : /* @__PURE__ */ new Map(), t.assignedMap.delete(e)), t.copy && delete t.copy[e], !0);
}
};
function rt(t) {
const { original: e, parentDraft: n, key: r, finalities: s, options: i } = t, o = X(e), a = {
type: o,
finalized: !1,
parent: n,
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 t) && (a.key = r);
const { proxy: c, revoke: u } = Proxy.revocable(o === 1 ? Object.assign([], a) : a, Kt);
if (s.revoke.push(u), Ht.add(c), a.proxy = c, n) {
const f = n;
f.finalities.draft.push((l, d) => {
var h, p;
const b = m(c);
let w = f.type === 3 ? f.setMap : f.copy;
const _ = B(w, r), g = m(_);
if (g) {
let O = g.original;
g.operated && (O = et(_)), Re(g), xe(g, ae, l, d), f.options.enableAutoFreeze && (f.options.updatedValues = (h = f.options.updatedValues) !== null && h !== void 0 ? h : /* @__PURE__ */ new WeakMap(), f.options.updatedValues.set(O, g.original)), ce(w, r, O);
}
(p = b.callbacks) === null || p === void 0 || p.forEach((O) => {
O(l, d);
});
});
} else {
const f = m(c);
f.finalities.draft.push((l, d) => {
Re(f), xe(f, ae, l, d);
});
}
return c;
}
Ye.createDraft = rt;
function zn(t, e, n, r, s) {
var i;
const o = m(t), a = (i = o == null ? void 0 : o.original) !== null && i !== void 0 ? i : t, c = !!e.length;
if (o != null && o.operated)
for (; o.finalities.draft.length > 0; )
o.finalities.draft.pop()(n, r);
const u = c ? e[0] : o ? o.operated ? o.copy : o.original : t;
return o && je(o), s && ee(u, u, o == null ? void 0 : o.options.updatedValues), [
u,
n && c ? [{ op: P.Replace, path: [], value: e[0] }] : n,
r && c ? [{ op: P.Replace, path: [], value: a }] : r
];
}
function Vn(t, e) {
var n;
const r = {
draft: [],
revoke: [],
handledSet: /* @__PURE__ */ new WeakSet()
};
let s, i;
e.enablePatches && (s = [], i = []);
const a = ((n = e.mark) === null || n === void 0 ? void 0 : n.call(e, t, $)) === $.mutable || !q(t, e) ? t : rt({
original: t,
parentDraft: null,
finalities: r,
options: e
});
return [
a,
(c = []) => {
const [u, f, l] = zn(a, c, s, i, e.enableAutoFreeze);
return e.enablePatches ? [u, f, l] : u;
}
];
}
function Ue(t) {
const { rootDraft: e, value: n, useRawReturn: r = !1, isRoot: s = !0 } = t;
tt(n, (i, o, a) => {
const c = m(o);
if (c && e && c.finalities === e.finalities) {
t.isContainDraft = !0;
const u = c.original;
if (a instanceof Set) {
const f = Array.from(a);
a.clear(), f.forEach((l) => a.add(i === l ? u : l));
} else
ce(a, i, u);
} else typeof o == "object" && o !== null && (t.value = o, t.isRoot = !1, Ue(t));
}), s && (t.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 Gt(t) {
var e;
const n = m(t);
if (!q(t, n == null ? void 0 : n.options))
return t;
const r = X(t);
if (n && !n.operated)
return n.original;
let s;
function i() {
s = r === 2 ? Ze(t) ? new Map(t) : new (Object.getPrototypeOf(t)).constructor(t) : r === 3 ? Array.from(n.setMap.values()) : Jt(t, n == null ? void 0 : n.options);
}
if (n) {
n.finalized = !0;
try {
i();
} finally {
n.finalized = !1;
}
} else
s = t;
if (tt(s, (o, a) => {
if (n && J(B(n.original, o), a))
return;
const c = Gt(a);
c !== a && (s === t && i(), ce(s, o, c));
}), r === 3) {
const o = (e = n == null ? void 0 : n.original) !== null && e !== void 0 ? e : s;
return Xe(o) ? new Set(s) : new (Object.getPrototypeOf(o)).constructor(s);
}
return s;
}
function Mt(t) {
if (!Y(t))
throw new Error(`current() is only used for Draft, parameter: ${t}`);
return Gt(t);
}
const Wn = (t) => function e(n, r, s) {
var i, o, a;
if (typeof n == "function" && typeof r != "function")
return function(S, ...I) {
return e(S, (A) => n.call(this, A, ...I), r);
};
const c = n, u = r;
let f = s;
if (typeof r != "function" && (f = r), f !== void 0 && Object.prototype.toString.call(f) !== "[object Object]")
throw new Error(`Invalid options: ${f}, 'options' should be an object.`);
f = Object.assign(Object.assign({}, t), f);
const l = Y(c) ? Mt(c) : c, d = Array.isArray(f.mark) ? (S, I) => {
for (const A of f.mark) {
if (typeof A != "function")
throw new Error(`Invalid mark: ${A}, 'mark' should be a function.`);
const F = A(S, I);
if (F)
return F;
}
} : f.mark, h = (i = f.enablePatches) !== null && i !== void 0 ? i : !1, p = (o = f.strict) !== null && o !== void 0 ? o : !1, w = {
enableAutoFreeze: (a = f.enableAutoFreeze) !== null && a !== void 0 ? a : !1,
mark: d,
strict: p,
enablePatches: h
};
if (!q(l, w) && typeof l == "object" && l !== 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 [_, g] = Vn(l, w);
if (typeof r != "function") {
if (!q(l, w))
throw new Error("Invalid base state: create() only supports plain objects, arrays, Set, Map or using mark() to mark the state as immutable.");
return [_, g];
}
let O;
try {
O = u(_);
} catch (S) {
throw je(m(_)), S;
}
const k = (S) => {
const I = m(_);
if (!Y(S)) {
if (S !== void 0 && !J(S, _) && (I != null && I.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 F = S == null ? void 0 : S[jn];
if (F) {
const lt = F[0];
return w.strict && typeof S == "object" && S !== null && Ue({
rootDraft: I,
value: S,
useRawReturn: !0
}), g([lt]);
}
if (S !== void 0)
return typeof S == "object" && S !== null && Ue({ rootDraft: I, value: S }), g([S]);
}
if (S === _ || S === void 0)
return g([]);
const A = m(S);
if (w === A.options) {
if (A.operated)
throw new Error("Cannot return a modified child draft.");
return g([Mt(S)]);
}
return g([S]);
};
return O instanceof Promise ? O.then(k, (S) => {
throw je(m(_)), S;
}) : k(O);
}, $e = Wn();
Object.prototype.constructor.toString();
function Yt(t, e) {
const n = Object.keys(t), r = Object.keys(e);
return n.length === r.length && Object.keys(t).every((s) => e.hasOwnProperty(s));
}
function Pt(t, e) {
return Object.keys(t).length === Object.keys(e).length && Object.keys(t).every(
(n) => e.hasOwnProperty(n) && t[n] === e[n]
);
}
function _e(t, e) {
return typeof t != "object" || typeof e != "object" || t === null || e === null ? t === e : Yt(t, e) ? Object.keys(t).every(
(n) => _e(t[n], e[n])
) : !1;
}
function st(t) {
if (!se(t))
return t;
const e = {};
for (const [n, r] of Object.entries(t))
r !== void 0 && (e[n] = r);
return e;
}
function Xt(t, e) {
if (!se(t) || !se(e))
return e;
const n = { ...t };
for (const r of Object.keys(e)) {
if (e[r] === void 0) continue;
if (e[r] === null) {
delete n[r];
continue;
}
const s = se(t[r]) && se(e[r]);
n[r] = s ? Xt(t[r], e[r]) : e[r];
}
return n;
}
function se(t) {
return typeof t == "object" && t !== null && !Array.isArray(t);
}
function Qn(t, e, n) {
if (!t || e.length === 0)
return;
let r = t || {};
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, n) : r[s] = n;
}
function vt(t, e, n) {
if (!t || e.length === 0)
return;
let r = t || {};
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]] = n;
}
function Zt(t, e) {
if (!t || e.length === 0)
return;
const [n, ...r] = e;
if (n in t) {
if (r.length === 0) {
Array.isArray(t) ? t.splice(n, 1) : delete t[n];
return;
}
Zt(t[n], r), Jn(t[n]) && delete t[n];
}
}
function Jn(t) {
return t && Object.keys(t).length === 0;
}
function Bn(t) {
return new Date(t);
}
function Hn(t) {
return /* @__PURE__ */ new Date(t + "Z");
}
function Kn(t) {
return /* @__PURE__ */ new Date(t + "T00:00:00Z");
}
function Gn(t) {
const [e, n] = t.split(" ");
return /* @__PURE__ */ new Date(e + "T" + n + "Z");
}
function Yn(t) {
const [e, n] = t.split(" ");
return /* @__PURE__ */ new Date(e + "T" + n + "Z");
}
function Xn(t) {
return new Date(t);
}
function Zn(t) {
const e = /^(\w{3}) (\w{3}) (\d{2}) (\d{4})$/;
if (!t.match(e))
throw new Error(`Unable to parse \`${t}\` as a date.`);
const r = /* @__PURE__ */ new Date(t + " UTC");
return new Date(
Date.UTC(
r.getUTCFullYear(),
r.getUTCMonth(),
r.getUTCDate(),
0,
0,
0,
0
)
);
}
function er(t) {
const e = /^(.+T.+)([+-])(\d{2})$/, n = t.match(e);
if (n) {
const [, r, s, i] = n, o = `${r}${s}${i}:00`;
return new Date(o);
}
return null;
}
const tr = [
Yn,
Zn,
Xn,
Kn,
Hn,
Bn,
er,
Gn
];
function nr(t, e) {
try {
const n = t(e);
return n instanceof Date && !isNaN(n.getTime()) ? n : null;
} catch {
return null;
}
}
function en(t) {
for (const e of tr) {
const n = nr(e, t);
if (n)
return n;
}
return null;
}
function rr(t) {
try {
const e = JSON.parse(t);
return typeof e == "string" ? en(e) : null;
} catch {
return null;
}
}
function me(t) {
if (t instanceof Date)
return t;
if (typeof t == "string") {
const e = en(t) || rr(t);
if (!e)
throw new Error(`Unable to parse \`${t}\` as a date.`);
return e;
} else if (typeof t == "number")
return new Date(t);
throw new Error(
`Invalid date value \`${t}\`. Expected a date, number, or string, got type ${typeof t}.`
);
}
function sr(t) {
return t.cardinality === "one";
}
function it(t) {
return t["value-type"] === "ref";
}
function ot(t) {
return t["value-type"] === "blob";
}
function ue(t, e) {
return t[e];
}
function Se(t, e) {
return e.reduce((n, r) => n && n.get(r), t);
}
function N(t, e) {
if (e.length === 0) throw new Error("path must have at least one element");
if (e.length === 1) {
t.delete(e[0]);
return;
}
const [n, ...r] = e;
t.has(n) && N(t.get(n), r);
}
function j(t, e, n) {
if (e.length === 0) throw new Error("path must have at least one element");
if (e.length === 1) {
t.set(e[0], n);
return;
}
const [r, ...s] = e;
let i = t.get(r);
i || (i = /* @__PURE__ */ new Map(), t.set(r, i)), j(i, s, n);
}
function tn(t, e, n) {
const r = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map();
for (const o of e) {
let [a, c, u, f] = o;
const l = ue(t, c);
if (!l) {
console.warn("no such attr", a, t);
continue;
}
l["checked-data-type"] === "date" && n && (u = me(u), o[2] = u), it(l) && j(i, [u, c, a], o), j(r, [a, c, u], o), j(s, [c, a, u], o);
}
return { eav: r, aev: s, vae: i };
}
function nn(t) {
const e = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map();
for (const i of Object.values(t)) {
const o = i["forward-identity"], [a, c, u] = o, f = i["reverse-identity"];
if (j(r, [c, u], i), ot(i) && j(e, [c, u], i), i["primary?"] && j(n, [c], i), f) {
const [l, d, h] = f;
j(s, [d, h], i);
}
}
return { blobAttrs: e, primaryKeys: n, forwardIdents: r, revIdents: s };
}
function ir(t) {
return {
__type: t.__type,
attrs: t.attrs,
triples: R(t.eav, 3),
cardinalityInference: t.cardinalityInference,
linkIndex: t.linkIndex,
useDateObjects: t.useDateObjects
};
}
function or(t) {
return Ne(
t.attrs,
t.triples,
t.cardinalityInference,
t.linkIndex,
t.useDateObjects
);
}
function at(t) {
t.attrIndexes = nn(t.attrs);
}
function Ne(t, e, n, r, s) {
const i = tn(t, e, s);
return i.useDateObjects = s, i.attrs = t, i.attrIndexes = nn(t), i.cardinalityInference = n, i.linkIndex = r, i.__type = "store", i;
}
function fe(t, e) {
var s, i;
let n;
if (Array.isArray(e[0])) {
const [o, a] = e[0], c = t.aev.get(o);
if (!c)
return null;
n = (s = R(c, 2).find((f) => f[2] === a)) == null ? void 0 : s[0];
} else
n = e[0];
if (!n)
return null;
const r = e[2];
if (Array.isArray(r) && r.length === 2 && t.aev.get(r[0])) {
const [o, a] = r, c = t.aev.get(o);
if (!c)
return null;
const f = (i = R(c, 2).find((b) => b[2] === a)) == null ? void 0 : i[0];
if (!f)
return null;
const [l, d, h, ...p] = e;
return [n, d, f, ...p];
} else {
const [o, ...a] = e;
return [n, ...a];
}
}
function ar(t, e) {
const n = fe(t, e);
if (!n)
return;
const [r, s, i] = n, o = ue(t.attrs, s);
o && (N(t.eav, [r, s, i]), N(t.aev, [s, r, i]), it(o) && N(t.vae, [i, s, r]));
}
let cr = 0;
function rn(t, e, n) {
const [r, s, i] = n;
let o;
const a = Se(t.ea, [r, s, i]);
return a && (o = a[3]), o || Date.now() * 10 + cr++;
}
function ur(t, e) {
const n = fe(t, e);
if (!n)
return;
let [r, s, i] = n;
const o = ue(t.attrs, s);
if (!o)
return;
o["checked-data-type"] === "date" && t.useDateObjects && (i = me(i));
const a = Se(t.eav, [r, s, i]), c = (a == null ? void 0 : a[3]) ?? rn(t, o, n), u = [r, s, i, c];
sr(o) ? (j(t.eav, [r, s], /* @__PURE__ */ new Map([[i, u]])), j(t.aev, [s, r], /* @__PURE__ */ new Map([[i, u]]))) : (j(t.eav, [r, s, i], u), j(t.aev, [s, r, i], u)), it(o) && j(t.vae, [i, s, r], u);
}
function fr(t, e) {
var d;
const n = fe(t, e);
if (!n)
return;
const [r, s, i] = n, o = ue(t.attrs, s);
if (!o) return;
if (!ot(o))
throw new Error("merge operation is not supported for links");
const a = Se(t.eav, [r, s]);
if (!a) return;
const c = (d = a.values().next()) == null ? void 0 : d.value;
if (!c) return;
const u = c[2], f = Xt(u, i), l = [
r,
s,
f,
rn(t, o, c)
];
j(t.eav, [r, s], /* @__PURE__ */ new Map([[f, l]]));
}
function Le(t, e) {
var c, u;
const [n, r] = e, s = fe(t, [n]);
if (!s)
return;
const [i] = s, o = t.eav.get(i);
if (o) {
for (const f of o.keys()) {
const l = t.attrs[f];
l && l["on-delete-reverse"] === "cascade" && R(o.get(f), 1).forEach(
([d, h, p]) => {
var b;
return Le(t, [p, (b = l["reverse-identity"]) == null ? void 0 : b[1]]);
}
), // Fall back to deleting everything if we've rehydrated tx-steps from
// the store that didn't set `etype` in deleteEntity
(!r || // If we don't know about the attr, let's just get rid of it
!l || // Make sure it matches the etype
((c = l["forward-identity"]) == null ? void 0 : c[1]) === r) && (N(t.aev, [f, i]), N(t.eav, [i, f]));
}
o.size === 0 && N(t.eav, [i]);
}
const a = t.vae.get(i) && R(t.vae.get(i), 2);
a && a.forEach((f) => {
var b, w, _;
const [l, d, h] = f, p = t.attrs[d];
(!r || !p || ((b = p["reverse-identity"]) == null ? void 0 : b[1]) === r) && (N(t.eav, [l, d, h]), N(t.aev, [d, l, h]), N(t.vae, [h, d, l])), p && p["on-delete"] === "cascade" && ((w = p["reverse-identity"]) == null ? void 0 : w[1]) === r && Le(t, [l, (_ = p["forward-identity"]) == null ? void 0 : _[1]]);
}), ((u = t.vae.get(i)) == null ? void 0 : u.size) === 0 && N(t.vae, [i]);
}
function sn(t, e) {
const n = tn(
t.attrs,
e,
t.useDateObjects
);
Object.keys(n).forEach((r) => {
t[r] = n[r];
});
}
function lr(t, [e]) {
t.attrs[e.id] = e, at(t);
}
function on(t) {
return R(t.eav, 3);
}
function dr(t, [e]) {
if (!t.attrs[e]) return;
const n = on(t).filter(([r, s]) => s !== e);
delete t.attrs[e], at(t), sn(t, n);
}
function hr(t, [e]) {
const n = t.attrs[e.id];
n && (t.attrs[e.id] = { ...n, ...e }, at(t), sn(t, on(t)));
}
function pr(t, e) {
const [n, ...r] = e;
switch (n) {
case "add-triple":
ur(t, r);
break;
case "deep-merge-triple":
fr(t, r);
break;
case "retract-triple":
ar(t, r);
break;
case "delete-entity":
Le(t, r);
break;
case "add-attr":
lr(t, r);
break;
case "delete-attr":
dr(t, r);
break;
case "update-attr":
hr(t, r);
break;
case "rule-params":
break;
default:
throw new Error(`unhandled transaction action: ${n}`);
}
}
function R(t, e, n = []) {
if (!t || e === 0)
return n;
if (e === 1) {
for (const r of t.values())
n.push(r);
return n;
}
for (const r of t.values())
R(r, e - 1, n);
return n;
}
function de(t, e, n) {
var i, o;
const r = [];
if (n != null && n.hasOwnProperty("$not")) {
for (const a of e.keys())
n.$not !== a && r.push(e.get(a));
return r;
}
if (n != null && n.hasOwnProperty("$isNull")) {
const { attrId: a, isNull: c, reverse: u } = n.$isNull;
if (u)
for (const f of e.keys()) {
const l = t.vae.get(f), d = !l || ((i = l.get(a)) == null ? void 0 : i.get(null)) || !l.get(a);
(c ? d : !d) && r.push(e.get(f));
}
else {
const f = t.aev.get(a);
for (const l of e.keys()) {
const d = !f || ((o = f.get(l)) == null ? void 0 : o.get(null)) || !f.get(l);
(c ? d : !d) && r.push(e.get(l));
}
}
return r;
}
if (n != null && n.$comparator)
return R(e, 1).filter(n.$op);
const s = n.in || n.$in || [n];
for (const a of s) {
const c = e.get(a);
c && r.push(c);
}
return r;
}
function yr(t, e, n) {
let r = "";
return t !== void 0 && (r += "e"), e !== void 0 && (r += "a"), n !== void 0 && (r += "v"), r;
}
function _r(t, [e, n, r]) {
var i, o;
switch (yr(e, n, r)) {
case "e": {
const a = t.eav.get(e);
return R(a, 2);
}
case "ea": {
const a = (i = t.eav.get(e)) == null ? void 0 : i.get(n);
return R(a, 1);
}
case "eav": {
const a = (o = t.eav.get(e)) == null ? void 0 : o.get(n);
return a ? de(t, a, r) : [];
}
case "ev": {
const a = t.eav.get(e);
if (!a)
return [];
const c = [];
for (const u of a.values())
c.push(...de(t, u, r));
return c;
}
case "a": {
const a = t.aev.get(n);
return R(a, 2);
}
case "av": {
const a = t.aev.get(n);
if (!a)
return [];
const c = [];
for (const u of a.values())
c.push(...de(t, u, r));
return c;
}
case "v": {
const a = [];
for (const c of t.eav.values())
for (const u of c.values())
a.push(...de(t, u, r));
return a;
}
default:
return R(t.eav, 3);
}
}
function br(t, e, n) {
var s;
const r = {};
for (const [i, o] of e.entries()) {
const a = (s = t.eav.get(n)) == null ? void 0 : s.get(o.id), c = R(a, 1);
for (const u of c)
r[i] = u[2];
}
return r;
}
function te(t, e, n) {
var r;
return (r = t.attrIndexes.forwardIdents.get(e)) == null ? void 0 : r.get(n);
}
function an(t, e, n) {
var r;
return (r = t.attrIndexes.revIdents.get(e)) == null ? void 0 : r.get(n);
}
function wr(t, e) {
return t.attrIndexes.blobAttrs.get(e);
}
function gr(t, e) {
var r;
const n = t.attrIndexes.primaryKeys.get(e);
return n || ((r = t.attrIndexes.forwardIdents.get(e)) == null ? void 0 : r.get("id"));
}
function mr(t, e) {
const n = fe(t, e);
if (!n)
return;
const [r, s, i] = n;
if (ue(t.attrs, s))
return Se(t.eav, [r, s]);
}
function Sr(t, e) {
const n = e.filter(([r, ...s]) => {
var a;
if (r !== "add-triple" && r !== "deep-merge-triple")
return !0;
const i = (a = s[3]) == null ? void 0 : a.mode;
if (i !== "create" && i !== "update")
return !0;
const o = mr(t, s);
return !(i === "create" && o || i === "update" && !o);
});
return $e(t, (r) => {
n.forEach((s) => {
pr(r, s);
});
});
}
function Or(t) {
return typeof t == "string" && t.startsWith("?");
}
function Mr(t, e, n) {
if (n.hasOwnProperty(t)) {
const r = n[t];
return cn(r, e, n);
}
return { ...n, [t]: e };
}
function At(t, e, n) {
return t === e ? n : null;
}
function Pr(t) {
switch (typeof t) {
case "string":
return t.startsWith("?") ? Mr : At;
default:
return At;
}
}
const vr = [
"in",
"$in",
"$not",
"$isNull",
"$comparator"
// covers all of $gt, $lt, etc.
];
function Ar(t) {
for (const e of vr)
if (t.hasOwnProperty(e))
return !0;
return !1;
}
function cn(t, e, n) {
return n ? typeof t == "object" ? Ar(t) ? n : null : Pr(t)(t, e, n) : null;
}
function Cr(t, e, n) {
return t.reduce((r, s, i) => {
const o = e[i];
return cn(s, o, r);
}, n);
}
function Tr(t, e, n) {
return Er(t, e, n).map((r) => Cr(e, r, n)).filter((r) => r);
}
function kr(t, e, n) {
return e.or ? e.or.patterns.flatMap((r) => qe(t, r, n)) : e.and ? e.and.patterns.reduce((r, s) => qe(t, s, r), n) : n.flatMap((r) => Tr(t, e, r));
}
function qe(t, e, n = [{}]) {
return e.reduce((r, s) => kr(t, s, r), n);
}
function ct(t, e) {
return Array.isArray(e) ? e.map((n) => ct(t, n)) : Or(e) ? t[e] : e;
}
function Ir(t, { find: e, where: n }) {
return qe(t, n).map((s) => ct(s, e));
}
function Er(t, e, n) {
return _r(t, ct(n, e));
}
const v = [];
for (let t = 0; t < 256; ++t)
v.push((t + 256).toString(16).slice(1));
function jr(t, e = 0) {
return (v[t[e + 0]] + v[t[e + 1]] + v[t[e + 2]] + v[t[e + 3]] + "-" + v[t[e + 4]] + v[t[e + 5]] + "-" + v[t[e + 6]] + v[t[e + 7]] + "-" + v[t[e + 8]] + v[t[e + 9]] + "-" + v[t[e + 10]] + v[t[e + 11]] + v[t[e + 12]] + v[t[e + 13]] + v[t[e + 14]] + v[t[e + 15]]).toLowerCase();
}
let Ce;
const Rr = new Uint8Array(16);
function xr() {
if (!Ce) {
if (typeof crypto > "u" || !crypto.getRandomValues)
throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
Ce = crypto.getRandomValues.bind(crypto);
}
return Ce(Rr);
}
const Dr = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto), Ct = { randomUUID: Dr };
function Ur(t, e, n) {
var s;
if (Ct.randomUUID && !t)
return Ct.randomUUID();
t = t || {};
const r = t.random ?? ((s = t.rng) == null ? void 0 : s.call(t)) ?? xr();
if (r.length < 16)
throw new Error("Random bytes length must be >= 16");
return r[6] = r[6] & 15 | 64, r[8] = r[8] & 63 | 128, jr(r);
}
function Tt(t) {
const e = t.replace(/-/g, ""), n = [];
for (let r = 0; r < e.length; r += 2)
n.push(parseInt(e.substring(r, r + 2), 16));
return n;
}
function $r(t, e) {
for (let n = 0; n < t.length; n++) {
if (t[n] < e[n]) return -1;
if (t[n] > e[n]) return 1;
}
return 0;
}
function Nr(t, e) {
return $r(Tt(t), Tt(e));
}
function C() {
return Ur();
}
let Lr = 0;
function oe(t) {
return Oe(`_${t}`, Lr++);
}
function Oe(t, e) {
return `?${t}-${e}`;
}
class ne extends Error {
constructor(e) {
super(e), this.name = "AttrNotFoundError";
}
}
function qr(t, e) {
const n = gr(t, e);
if (!n)
throw new ne(`Could not find id attr for ${e}`);
return n;
}
function kt(t, e, n, r) {
return [Fr(t, e, n, r)];
}
function Fr(t, e, n, r) {
return [
t(n, r),
qr(e, n).id,
t(n, r),
t("time", r)
];
}
function zr(t, e, n) {
return t.map((r) => r === e ? n : r);
}
function un(t, e, n, r, s) {
const i = te(e, n, s), o = an(e, n, s), a = i || o;
if (!a)
throw new ne(`Could not find attr for ${[n, s]}`);
if (a["value-type"] !== "ref")
throw new Error(`Attr ${a.id} is not a ref`);
const [c, u] = a["forward-identity"], [f, l] = a["reverse-identity"], d = r + 1, h = i ? [
t(u, r),
a.id,
t(l, d),
oe("time")
] : [
t(u, d),
a.id,
t(l, r),
oe("time")
];
return [i ? l : u, d, h, a, !!i];
}
function It(t, e) {
if (typeof e != "string")
return function(o) {
return !1;
};
const r = e.replace(/[.*+?^${}()|[\]\\]/g, "\\$&").replace(/%/g, ".*").replace(/_/g, "."), s = new RegExp(
`^${r}$`,
t ? void 0 : "i"
);
return function(o) {
return typeof o != "string" ? !1 : s.test(o);
};
}
function Vr(t, e) {
if (typeof e != "object" || e.hasOwnProperty("$in") || e.hasOwnProperty("in"))
return e;
const n = t["checked-data-type"] === "date";
if (e.hasOwnProperty("$gt"))
return {
$comparator: !0,
$op: n ? function(s) {
return new Date(s[2]) > new Date(e.$gt);
} : function(s) {
return s[2] > e.$gt;
}
};
if (e.hasOwnProperty("$gte"))
return {
$comparator: !0,
$op: n ? function(s) {
return new Date(s[2]) >= new Date(e.$gte);
} : function(s) {
return s[2] >= e.$gte;
}
};
if (e.hasOwnProperty("$lt"))
return {
$comparator: !0,
$op: n ? function(s) {
return new Date(s[2]) < new Date(e.$lt);
} : function(s) {
return s[2] < e.$lt;
}
};
if (e.hasOwnProperty("$lte"))
return {
$comparator: !0,
$op: n ? function(s) {
return new Date(s[2]) <= new Date(e.$lte);
} : function(s) {
return s[2] <= e.$lte;
}
};
if (e.hasOwnProperty("$like")) {
const r = It(!0, e.$like);
return {
$comparator: !0,
$op: function(i) {
return r(i[2]);
}
};
}
if (e.hasOwnProperty("$ilike")) {
const r = It(!1, e.$ilike);
return {
$comparator: !0,
$op: function(i) {
return r(i[2]);
}
};
}
return e;
}
function Wr(t, e, n, r, s, i) {
const o = te(e, n, s), a = an(e, n, s), c = o || a;
if (!c)
throw new ne(
`No attr for etype = ${n} label = ${s}`
);
if (i != null && i.hasOwnProperty("$isNull")) {
const u = te(e, n, "id");
if (!u)
throw new ne(
`No attr for etype = ${n} label = id`
);
return [
t(n, r),
u.id,
{ $isNull: { attrId: c.id, isNull: i.$isNull, reverse: !o } },
oe("time")
];
}
return o ? [
t(n, r),
c.id,
Vr(c, i),
oe("time")
] : [i, c.id, t(n, r), oe("time")];
}
function Qr(t, e, n, r, s) {
const [i, o, a] = s.reduce(
(c, u) => {
const [f, l, d] = c, [h, p, b] = un(
t,
e,
f,
l,
u
);
return [h, p, [...d, b]];
},
[n, r, []]
);
return [i, o, a];
}
function Fe(t, e, n, r, s, i) {
const o = s.slice(0, s.length - 1), a = s[s.length - 1], [c, u, f] = Qr(
t,
e,
n,
r,
o
), l = Wr(
t,
e,
c,
u,
a,
i
);
return f.concat([l]);
}
function Jr(t, e) {
return e ? [e].concat(t) : t;
}
function Br([t, e]) {
return t === "or" && Array.isArray(e);
}
function Hr([t, e]) {
return t === "and" && Array.isArray(e);
}
function Kr(t, e, n) {
return (r, s) => {
const i = t(r, s);
return e == i ? i : `${i}-${n}`;
};
}
function Et(t, e, n, r, s, i) {
const o = t(r, s), a = i.map((c, u) => {
const f = Kr(t, o, u);
return fn(f, n, r, s, c);
});
return { [e]: { patterns: a, joinSym: o } };
}
function Gr(t) {
const e = [];
for (let n = 1; n <= t.length; n++)
e.push(t.slice(0, n));
return e;
}
function jt(t, e, n, r, s) {
return Gr(s).map(
(i) => Fe(t, e, n, r, i, { $isNull: !0 })
);
}
function fn(t, e, n, r, s) {
return Object.entries(s).flatMap(([i, o]) => {
if (Br([i, o]))
return Et(t, "or", e, n, r, o);
if (Hr([i, o]))
return Et(t, "and", e, n, r, o);
if (i === "$entityIdStartsWith")
return [];
const a =