fetchium 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (138) hide show
  1. package/CHANGELOG.md +6 -5
  2. package/dist/cjs/development/QueryClient-CLi3ONNM.js +2 -0
  3. package/dist/cjs/development/QueryClient-CLi3ONNM.js.map +1 -0
  4. package/dist/cjs/development/QueryController-BQA49OYU.js +2 -0
  5. package/dist/cjs/development/QueryController-BQA49OYU.js.map +1 -0
  6. package/dist/cjs/development/index.js +1 -1
  7. package/dist/cjs/development/index.js.map +1 -1
  8. package/dist/cjs/development/mutation-CikIl_6k.js +2 -0
  9. package/dist/cjs/development/mutation-CikIl_6k.js.map +1 -0
  10. package/dist/cjs/development/react/index.js +1 -1
  11. package/dist/cjs/development/rest/index.js +2 -0
  12. package/dist/cjs/development/rest/index.js.map +1 -0
  13. package/dist/cjs/development/topic/index.js +2 -0
  14. package/dist/cjs/development/topic/index.js.map +1 -0
  15. package/dist/cjs/production/QueryClient-N0MJmuHW.js +2 -0
  16. package/dist/cjs/production/QueryClient-N0MJmuHW.js.map +1 -0
  17. package/dist/cjs/production/QueryController-BQA49OYU.js +2 -0
  18. package/dist/cjs/production/QueryController-BQA49OYU.js.map +1 -0
  19. package/dist/cjs/production/index.js +1 -1
  20. package/dist/cjs/production/index.js.map +1 -1
  21. package/dist/cjs/production/mutation-P_Yb4LI9.js +2 -0
  22. package/dist/cjs/production/mutation-P_Yb4LI9.js.map +1 -0
  23. package/dist/cjs/production/react/index.js +1 -1
  24. package/dist/cjs/production/rest/index.js +2 -0
  25. package/dist/cjs/production/rest/index.js.map +1 -0
  26. package/dist/cjs/production/topic/index.js +2 -0
  27. package/dist/cjs/production/topic/index.js.map +1 -0
  28. package/dist/esm/MutationResult.d.ts +0 -1
  29. package/dist/esm/MutationResult.d.ts.map +1 -1
  30. package/dist/esm/QueryClient.d.ts +26 -4
  31. package/dist/esm/QueryClient.d.ts.map +1 -1
  32. package/dist/esm/QueryController.d.ts +49 -0
  33. package/dist/esm/QueryController.d.ts.map +1 -0
  34. package/dist/esm/QueryResult.d.ts +10 -10
  35. package/dist/esm/QueryResult.d.ts.map +1 -1
  36. package/dist/esm/development/QueryClient-Dtde3pss.js +2572 -0
  37. package/dist/esm/development/QueryClient-Dtde3pss.js.map +1 -0
  38. package/dist/esm/development/QueryController-Ch_ncxiI.js +14 -0
  39. package/dist/esm/development/QueryController-Ch_ncxiI.js.map +1 -0
  40. package/dist/esm/development/index.js +29 -100
  41. package/dist/esm/development/index.js.map +1 -1
  42. package/dist/esm/development/mutation-UZshUQAf.js +58 -0
  43. package/dist/esm/development/mutation-UZshUQAf.js.map +1 -0
  44. package/dist/esm/development/react/index.js +1 -1
  45. package/dist/esm/development/rest/index.js +142 -0
  46. package/dist/esm/development/rest/index.js.map +1 -0
  47. package/dist/esm/development/{shared-Dq2yW78d.js → shared-DcuVH8Pf.js} +5 -5
  48. package/dist/esm/development/{shared-Dq2yW78d.js.map → shared-DcuVH8Pf.js.map} +1 -1
  49. package/dist/esm/development/stores/async.js +6 -6
  50. package/dist/esm/development/stores/sync.js +5 -5
  51. package/dist/esm/development/topic/index.js +86 -0
  52. package/dist/esm/development/topic/index.js.map +1 -0
  53. package/dist/esm/index.d.ts +5 -4
  54. package/dist/esm/index.d.ts.map +1 -1
  55. package/dist/esm/mutation.d.ts +6 -19
  56. package/dist/esm/mutation.d.ts.map +1 -1
  57. package/dist/esm/production/{QueryClient-BP0Z1rQV.js → QueryClient-YqnBxFy1.js} +972 -968
  58. package/dist/esm/production/QueryClient-YqnBxFy1.js.map +1 -0
  59. package/dist/esm/production/QueryController-Ch_ncxiI.js +14 -0
  60. package/dist/esm/production/QueryController-Ch_ncxiI.js.map +1 -0
  61. package/dist/esm/production/index.js +29 -100
  62. package/dist/esm/production/index.js.map +1 -1
  63. package/dist/esm/production/mutation-pgFl1uIY.js +58 -0
  64. package/dist/esm/production/mutation-pgFl1uIY.js.map +1 -0
  65. package/dist/esm/production/react/index.js +1 -1
  66. package/dist/esm/production/rest/index.js +142 -0
  67. package/dist/esm/production/rest/index.js.map +1 -0
  68. package/dist/esm/production/{shared-Dq2yW78d.js → shared-DcuVH8Pf.js} +5 -5
  69. package/dist/esm/production/{shared-Dq2yW78d.js.map → shared-DcuVH8Pf.js.map} +1 -1
  70. package/dist/esm/production/stores/async.js +6 -6
  71. package/dist/esm/production/stores/sync.js +5 -5
  72. package/dist/esm/production/topic/index.js +86 -0
  73. package/dist/esm/production/topic/index.js.map +1 -0
  74. package/dist/esm/query-types.d.ts +2 -4
  75. package/dist/esm/query-types.d.ts.map +1 -1
  76. package/dist/esm/query.d.ts +17 -39
  77. package/dist/esm/query.d.ts.map +1 -1
  78. package/dist/esm/rest/RESTMutation.d.ts +18 -0
  79. package/dist/esm/rest/RESTMutation.d.ts.map +1 -0
  80. package/dist/esm/rest/RESTQuery.d.ts +24 -0
  81. package/dist/esm/rest/RESTQuery.d.ts.map +1 -0
  82. package/dist/esm/rest/RESTQueryController.d.ts +34 -0
  83. package/dist/esm/rest/RESTQueryController.d.ts.map +1 -0
  84. package/dist/esm/rest/index.d.ts +5 -0
  85. package/dist/esm/rest/index.d.ts.map +1 -0
  86. package/dist/esm/stores/shared.d.ts.map +1 -1
  87. package/dist/esm/testing/MockClient.d.ts +64 -0
  88. package/dist/esm/testing/MockClient.d.ts.map +1 -0
  89. package/dist/esm/testing/auto-generate.d.ts +20 -0
  90. package/dist/esm/testing/auto-generate.d.ts.map +1 -0
  91. package/dist/esm/testing/entity-factory.d.ts +13 -0
  92. package/dist/esm/testing/entity-factory.d.ts.map +1 -0
  93. package/dist/esm/testing/index.d.ts +6 -0
  94. package/dist/esm/testing/index.d.ts.map +1 -0
  95. package/dist/esm/testing/types.d.ts +37 -0
  96. package/dist/esm/testing/types.d.ts.map +1 -0
  97. package/dist/esm/topic/TopicQuery.d.ts +10 -0
  98. package/dist/esm/topic/TopicQuery.d.ts.map +1 -0
  99. package/dist/esm/topic/TopicQueryController.d.ts +43 -0
  100. package/dist/esm/topic/TopicQueryController.d.ts.map +1 -0
  101. package/dist/esm/topic/index.d.ts +3 -0
  102. package/dist/esm/topic/index.d.ts.map +1 -0
  103. package/dist/esm/typeDefs.d.ts +1 -1
  104. package/dist/esm/types.d.ts +9 -4
  105. package/dist/esm/types.d.ts.map +1 -1
  106. package/package.json +51 -4
  107. package/plugin/.claude-plugin/plugin.json +10 -0
  108. package/plugin/agents/fetchium.md +168 -0
  109. package/plugin/docs/api/fetchium-react.md +135 -0
  110. package/plugin/docs/api/fetchium.md +674 -0
  111. package/plugin/docs/api/stores-async.md +219 -0
  112. package/plugin/docs/api/stores-sync.md +133 -0
  113. package/plugin/docs/core/entities.md +351 -0
  114. package/plugin/docs/core/queries.md +600 -0
  115. package/plugin/docs/core/streaming.md +550 -0
  116. package/plugin/docs/core/types.md +374 -0
  117. package/plugin/docs/data/caching.md +298 -0
  118. package/plugin/docs/data/live-data.md +435 -0
  119. package/plugin/docs/data/mutations.md +465 -0
  120. package/plugin/docs/guides/auth.md +318 -0
  121. package/plugin/docs/guides/error-handling.md +351 -0
  122. package/plugin/docs/guides/offline.md +270 -0
  123. package/plugin/docs/guides/testing.md +301 -0
  124. package/plugin/docs/quickstart.md +170 -0
  125. package/plugin/docs/reference/pagination.md +519 -0
  126. package/plugin/docs/reference/rest-queries.md +107 -0
  127. package/plugin/docs/reference/why-signalium.md +364 -0
  128. package/plugin/docs/setup/project-setup.md +319 -0
  129. package/plugin/install.mjs +88 -0
  130. package/plugin/skills/design/SKILL.md +140 -0
  131. package/plugin/skills/teach/SKILL.md +105 -0
  132. package/dist/cjs/development/QueryClient-CpmwggOn.js +0 -2
  133. package/dist/cjs/development/QueryClient-CpmwggOn.js.map +0 -1
  134. package/dist/cjs/production/QueryClient-qi3bR0eD.js +0 -2
  135. package/dist/cjs/production/QueryClient-qi3bR0eD.js.map +0 -1
  136. package/dist/esm/development/QueryClient-DRZtPKFD.js +0 -2568
  137. package/dist/esm/development/QueryClient-DRZtPKFD.js.map +0 -1
  138. package/dist/esm/production/QueryClient-BP0Z1rQV.js.map +0 -1
@@ -0,0 +1,2572 @@
1
+ import { notifier as ft, relay as Tt, setScopeOwner as we, reactiveMethod as Ee, signal as Pt, context as jt, getContext as be, task as Ce, reactiveSignal as Se, isSignal as Oe, watcher as Re } from "signalium";
2
+ import { registerCustomHash as xe, hashValue as N } from "signalium/utils";
3
+ import { d as $t } from "./shared-DcuVH8Pf.js";
4
+ import { SyncQueryStore as Ae, MemoryPersistentStore as Ne } from "./stores/sync.js";
5
+ var V = /* @__PURE__ */ ((e) => (e.Always = "always", e.Online = "online", e.OfflineFirst = "offlineFirst", e))(V || {}), l = /* @__PURE__ */ ((e) => (e[e.UNDEFINED = 1] = "UNDEFINED", e[e.NULL = 2] = "NULL", e[e.NUMBER = 4] = "NUMBER", e[e.STRING = 8] = "STRING", e[e.BOOLEAN = 16] = "BOOLEAN", e[e.OBJECT = 32] = "OBJECT", e[e.ARRAY = 64] = "ARRAY", e[e.ID = 128] = "ID", e[e.RECORD = 256] = "RECORD", e[e.UNION = 512] = "UNION", e[e.ENTITY = 1024] = "ENTITY", e[e.HAS_FORMAT = 4096] = "HAS_FORMAT", e[e.IS_EAGER_FORMAT = 8192] = "IS_EAGER_FORMAT", e[e.PARSE_RESULT = 16384] = "PARSE_RESULT", e[e.LIVE = 32768] = "LIVE", e))(l || {});
6
+ const D = /* @__PURE__ */ Symbol("array"), F = /* @__PURE__ */ Symbol("record"), Lt = /* @__PURE__ */ Symbol("QUERY_ID");
7
+ var ut = /* @__PURE__ */ ((e) => (e[e.Array = 0] = "Array", e[e.Value = 1] = "Value", e))(ut || {});
8
+ class B {
9
+ type;
10
+ entityDefs;
11
+ constraintFieldRefs;
12
+ sort;
13
+ valueType;
14
+ onCreate;
15
+ onUpdate;
16
+ onDelete;
17
+ constructor(t, n, i, r, s, o, a, c) {
18
+ this.type = t, this.entityDefs = n, this.constraintFieldRefs = i, this.sort = r, this.valueType = s, this.onCreate = o, this.onUpdate = a, this.onDelete = c;
19
+ }
20
+ static array(t, n, i) {
21
+ return new B(
22
+ 0,
23
+ t,
24
+ n,
25
+ i,
26
+ void 0,
27
+ void 0,
28
+ void 0,
29
+ void 0
30
+ );
31
+ }
32
+ static value(t, n, i, r, s, o) {
33
+ return new B(
34
+ 1,
35
+ t,
36
+ n,
37
+ void 0,
38
+ i,
39
+ r,
40
+ s,
41
+ o
42
+ );
43
+ }
44
+ }
45
+ const S = /* @__PURE__ */ new WeakMap();
46
+ function T(e) {
47
+ return S.get(e);
48
+ }
49
+ var X = /* @__PURE__ */ ((e) => (e[e.Query = 0] = "Query", e[e.Entity = 1] = "Entity", e))(X || {});
50
+ class Ie {
51
+ constructor(t, n, i) {
52
+ this._onEvict = n, this._intervalId = setInterval(this._tick, t * 6e4 * i);
53
+ }
54
+ _currentFlush = /* @__PURE__ */ new Map();
55
+ _nextFlush = /* @__PURE__ */ new Map();
56
+ _intervalId;
57
+ schedule(t, n) {
58
+ this._nextFlush.set(t, n);
59
+ }
60
+ cancel(t) {
61
+ this._currentFlush.delete(t), this._nextFlush.delete(t);
62
+ }
63
+ _tick = () => {
64
+ const { _currentFlush: t, _nextFlush: n, _onEvict: i } = this;
65
+ for (const [r, s] of t)
66
+ i(r, s);
67
+ this._currentFlush = n, this._nextFlush = /* @__PURE__ */ new Map();
68
+ };
69
+ destroy() {
70
+ clearInterval(this._intervalId);
71
+ }
72
+ }
73
+ class De {
74
+ _buckets = /* @__PURE__ */ new Map();
75
+ _nextTickEntries = /* @__PURE__ */ new Map();
76
+ _nextTickScheduled = !1;
77
+ _onEvict;
78
+ _multiplier;
79
+ constructor(t, n = 1) {
80
+ this._onEvict = t, this._multiplier = n;
81
+ }
82
+ schedule(t, n, i) {
83
+ if (n === 1 / 0) return;
84
+ if (n === 0) {
85
+ const { _nextTickEntries: o } = this;
86
+ o.set(t, i), this._nextTickScheduled || (this._nextTickScheduled = !0, setTimeout(this._flushNextTick, 0));
87
+ return;
88
+ }
89
+ const { _buckets: r } = this;
90
+ let s = r.get(n);
91
+ s || (s = new Ie(n, this._onEvict, this._multiplier), r.set(n, s)), s.schedule(t, i);
92
+ }
93
+ cancel(t, n) {
94
+ if (n !== 1 / 0) {
95
+ if (n === 0) {
96
+ this._nextTickEntries.delete(t);
97
+ return;
98
+ }
99
+ this._buckets.get(n)?.cancel(t);
100
+ }
101
+ }
102
+ _flushNextTick = () => {
103
+ const { _nextTickEntries: t, _onEvict: n } = this;
104
+ this._nextTickScheduled = !1;
105
+ for (const [i, r] of t)
106
+ n(i, r);
107
+ t.clear();
108
+ };
109
+ destroy() {
110
+ const { _buckets: t, _nextTickEntries: n } = this;
111
+ for (const i of t.values())
112
+ i.destroy();
113
+ t.clear(), n.clear();
114
+ }
115
+ }
116
+ class Fe {
117
+ schedule(t, n, i) {
118
+ }
119
+ cancel(t, n) {
120
+ }
121
+ destroy() {
122
+ }
123
+ }
124
+ const W = /* @__PURE__ */ Symbol("fieldRef"), I = /* @__PURE__ */ Symbol("fieldRefPath"), lt = /* @__PURE__ */ Symbol("DEFINITION_TARGET"), z = /* @__PURE__ */ Symbol("CANCEL_PROXY"), Me = /\[([^\]]+)\]/g;
125
+ function Ut(e) {
126
+ const t = {
127
+ [W]: !0,
128
+ [I]: e
129
+ };
130
+ return new Proxy(t, ke);
131
+ }
132
+ const ke = {
133
+ get(e, t) {
134
+ if (t === W) return !0;
135
+ if (t === I) return e[I];
136
+ if (t === Symbol.toPrimitive || t === "toString" || t === "valueOf") {
137
+ const i = e[I];
138
+ return () => `[${i.join(".")}]`;
139
+ }
140
+ if (typeof t == "symbol") return;
141
+ const n = e[I];
142
+ return Ut([...n, t]);
143
+ },
144
+ has(e, t) {
145
+ return t === W || t === I;
146
+ }
147
+ };
148
+ function qt(e) {
149
+ return typeof e == "object" && e !== null && e[W] === !0;
150
+ }
151
+ function Vt(e) {
152
+ return e[I];
153
+ }
154
+ function Kt(e) {
155
+ let t = !1;
156
+ return new Proxy(e, {
157
+ set(n, i, r) {
158
+ return n[i] = r, !0;
159
+ },
160
+ get(n, i) {
161
+ if (t)
162
+ throw new Error("Definition proxy accessed after extraction. Avoid arrow functions that capture `this`.");
163
+ return i === lt ? n : i === z ? () => {
164
+ t = !0;
165
+ } : typeof i == "symbol" ? n[i] : Ut([i]);
166
+ }
167
+ });
168
+ }
169
+ function Te(e) {
170
+ const t = e[lt];
171
+ e[z]();
172
+ const n = {};
173
+ for (const s of Object.getOwnPropertyNames(t))
174
+ n[s] = t[s];
175
+ const i = {};
176
+ let r = Object.getPrototypeOf(t);
177
+ for (; r && r !== Object.prototype; ) {
178
+ for (const s of Object.getOwnPropertyNames(r)) {
179
+ if (s === "constructor") continue;
180
+ const o = Object.getOwnPropertyDescriptor(r, s);
181
+ typeof o.value == "function" && !(s in i) && (i[s] = o.value);
182
+ }
183
+ r = Object.getPrototypeOf(r);
184
+ }
185
+ return { fields: n, methods: i };
186
+ }
187
+ function dt(e, t) {
188
+ let n = t;
189
+ for (const i of e) {
190
+ if (n == null) return;
191
+ n = n[i];
192
+ }
193
+ return n;
194
+ }
195
+ function Pe(e, t) {
196
+ return e.replace(Me, (n, i) => {
197
+ const r = i.split("."), s = dt(r, t);
198
+ return s != null ? encodeURIComponent(String(s)) : "";
199
+ });
200
+ }
201
+ function J(e, t) {
202
+ if (qt(e))
203
+ return dt(Vt(e), t);
204
+ if (typeof e == "string")
205
+ return Pe(e, t);
206
+ if (Array.isArray(e))
207
+ return e.map((n) => J(n, t));
208
+ if (typeof e == "object" && e !== null && Object.getPrototypeOf(e) === Object.prototype) {
209
+ const n = {};
210
+ for (const i of Object.keys(e))
211
+ n[i] = J(e[i], t);
212
+ return n;
213
+ }
214
+ return e;
215
+ }
216
+ function it(e, t, n) {
217
+ const i = e.fields, r = e.methods, s = { params: t }, o = {};
218
+ for (const [a, c] of Object.entries(i))
219
+ o[a] = J(c, s);
220
+ o.params = t, o.context = n;
221
+ for (const [a, c] of Object.entries(r))
222
+ o[a] = c.bind(o);
223
+ return o;
224
+ }
225
+ class Bt {
226
+ static cache;
227
+ constructor() {
228
+ return Kt(this);
229
+ }
230
+ }
231
+ const Qt = Object.entries, rt = Object.keys;
232
+ let Yt = () => {
233
+ };
234
+ {
235
+ const t = (i, r) => {
236
+ if (i.size !== r.size) return !1;
237
+ for (const s of i)
238
+ if (!r.has(s)) return !1;
239
+ return !0;
240
+ }, n = (i, r) => {
241
+ if (i === r) return !0;
242
+ if (typeof i != typeof r) return !1;
243
+ if (typeof i == "number") {
244
+ const s = r;
245
+ if ((i & 65535) !== (s & 65535)) return !1;
246
+ const o = i >> 16, a = s >> 16;
247
+ return !(o !== 0 && a !== 0 && o !== a);
248
+ }
249
+ if (typeof i == "string") return i === r;
250
+ if (i instanceof Set && r instanceof Set)
251
+ return t(i, r);
252
+ if (i instanceof m && r instanceof m) {
253
+ const s = i.mask, o = r.mask;
254
+ if ((s & 65535) !== (o & 65535)) return !1;
255
+ const a = s >> 16, c = o >> 16;
256
+ if (a !== 0 && c !== 0 && a !== c) return !1;
257
+ if (i.shape === r.shape) return !0;
258
+ if (i.shape !== void 0 && r.shape !== void 0 && typeof i.shape == "object" && typeof r.shape == "object") {
259
+ const f = i.shape, d = r.shape;
260
+ for (const u of Object.keys(f))
261
+ if (u in d && !n(f[u], d[u])) return !1;
262
+ for (const u of Object.keys(d))
263
+ if (u in f && !n(f[u], d[u])) return !1;
264
+ }
265
+ return !0;
266
+ }
267
+ return !1;
268
+ };
269
+ Yt = (i, r, s, o) => {
270
+ if (!n(s, o))
271
+ throw new Error(
272
+ `[fetchium] Entity typename '${i}' has incompatible type for field '${r}' across different entity definitions`
273
+ );
274
+ };
275
+ }
276
+ function Ct(e) {
277
+ return typeof e == "number" ? e | l.UNDEFINED : e instanceof m ? (e.mask & l.UNDEFINED) !== 0 ? e : m.cloneWith(e, l.UNDEFINED) : e;
278
+ }
279
+ function St(e) {
280
+ return e instanceof m && (e.mask & l.OBJECT) !== 0 && (e.mask & (l.ENTITY | l.ARRAY | l.UNION | l.RECORD | l.LIVE)) === 0;
281
+ }
282
+ function Gt(e, t, n) {
283
+ const i = /* @__PURE__ */ new Set();
284
+ for (const s of e)
285
+ if (s !== void 0)
286
+ for (const o of Object.keys(s))
287
+ i.add(o);
288
+ const r = {};
289
+ for (const s of i) {
290
+ let o = 0, a;
291
+ const c = [];
292
+ let f = !0;
293
+ for (const d of e) {
294
+ const u = d?.[s];
295
+ u !== void 0 ? (o++, a === void 0 && (a = u), a !== void 0 && u !== a && !St(u) && Yt(n, s, a, u), St(u) ? c.push(u.shape) : (f = !1, c.push(void 0))) : (f = !1, c.push(void 0));
296
+ }
297
+ if (f && o > 0) {
298
+ const d = Gt(c, t, n), u = new m(l.OBJECT, d);
299
+ r[s] = o < t ? Ct(u) : u;
300
+ } else
301
+ r[s] = o < t ? Ct(a) : a;
302
+ }
303
+ return r;
304
+ }
305
+ class m {
306
+ mask;
307
+ shape;
308
+ typenameField = void 0;
309
+ typenameValue = void 0;
310
+ idField = void 0;
311
+ values = void 0;
312
+ /**
313
+ * Methods object for entity definitions.
314
+ * Shared across all proxies, but each proxy binds its own reactive method wrappers.
315
+ */
316
+ _methods = void 0;
317
+ /**
318
+ * Entity configuration including stream options.
319
+ */
320
+ _entityConfig = void 0;
321
+ /**
322
+ * The original Entity class for this definition.
323
+ * Used by createEntityProxy to set the proxy prototype so `instanceof` works.
324
+ */
325
+ _entityClass = void 0;
326
+ /**
327
+ * Entity cache options (e.g. gcTime for in-memory eviction).
328
+ */
329
+ _entityCache = void 0;
330
+ /**
331
+ * Live data configuration (shared by LiveArray and LiveValue).
332
+ */
333
+ _liveConfig = void 0;
334
+ constructor(t, n, i, r, s, o) {
335
+ this.mask = t, this.shape = n, this.values = i, this.typenameField = r, this.typenameValue = s, this.idField = o;
336
+ }
337
+ static merge(t) {
338
+ if (t.length === 1) return t[0];
339
+ const n = t.length, i = t.map((f) => f.shape), r = t[0].typenameValue ?? "(unknown)", s = Gt(i, n, r);
340
+ let o, a, c;
341
+ for (const f of t) {
342
+ if (o === void 0 && f.idField !== void 0)
343
+ o = f.idField;
344
+ else if (o !== void 0 && f.idField !== void 0 && f.idField !== o)
345
+ throw new Error(
346
+ `[fetchium] Entity typename '${f.typenameValue}' has conflicting id fields: '${String(o)}' vs '${String(f.idField)}'`
347
+ );
348
+ a === void 0 && (a = f.typenameField), c === void 0 && (c = f.typenameValue);
349
+ }
350
+ return new m(
351
+ l.ENTITY | l.OBJECT,
352
+ s,
353
+ void 0,
354
+ a,
355
+ c,
356
+ o
357
+ );
358
+ }
359
+ static cloneWith(t, n) {
360
+ const i = new m(
361
+ n | t.mask,
362
+ t.shape,
363
+ t.values,
364
+ t.typenameField,
365
+ t.typenameValue,
366
+ t.idField
367
+ );
368
+ return i._methods = t._methods, i._entityConfig = t._entityConfig, i._entityClass = t._entityClass, i._entityCache = t._entityCache, i._liveConfig = t._liveConfig, i;
369
+ }
370
+ }
371
+ class $ extends Set {
372
+ lowercaseMap;
373
+ // lowercase -> canonical (strings only)
374
+ constructor(t) {
375
+ super(t), this.lowercaseMap = /* @__PURE__ */ new Map();
376
+ for (const n of t)
377
+ if (typeof n == "string") {
378
+ const i = n.toLowerCase(), r = this.lowercaseMap.get(i);
379
+ if (r !== void 0)
380
+ throw new Error(
381
+ `Case-insensitive enum cannot have multiple values with the same lowercase form: '${r}' and '${n}' both become '${i}'`
382
+ );
383
+ this.lowercaseMap.set(i, n);
384
+ }
385
+ }
386
+ /**
387
+ * Check if a value exists in the set (case-insensitively for strings).
388
+ * Used for backwards compatibility with Set-based checks.
389
+ */
390
+ has(t) {
391
+ return this.get(t) !== void 0;
392
+ }
393
+ /**
394
+ * Get the canonical value for a given input.
395
+ * For strings, performs case-insensitive lookup and returns the canonical casing.
396
+ * For numbers/booleans, performs exact match.
397
+ * Returns undefined if no match is found.
398
+ */
399
+ get(t) {
400
+ if (typeof t == "string")
401
+ return this.lowercaseMap.get(t.toLowerCase());
402
+ if (super.has(t))
403
+ return t;
404
+ }
405
+ }
406
+ const je = 1128875347;
407
+ xe($, (e) => {
408
+ let t = je;
409
+ for (const n of e)
410
+ t += N(n);
411
+ return t >>> 0;
412
+ });
413
+ function ht(e, t) {
414
+ return new m(e, t);
415
+ }
416
+ function $e(e) {
417
+ return ht(l.ARRAY, e);
418
+ }
419
+ function Le(e) {
420
+ return ht(l.RECORD | l.OBJECT, e);
421
+ }
422
+ function Ue(e) {
423
+ return ht(l.PARSE_RESULT, e);
424
+ }
425
+ function Ht(e, t) {
426
+ let n = e, i, r, s;
427
+ for (const [o, a] of Qt(t))
428
+ switch (typeof a) {
429
+ case "number":
430
+ if ((a & l.ID) !== 0) {
431
+ if (i !== void 0)
432
+ throw new Error(`Duplicate id field: ${o}`);
433
+ i = o;
434
+ }
435
+ break;
436
+ case "string":
437
+ if (r !== void 0 && r !== o)
438
+ throw new Error(`Duplicate typename field: ${o}`);
439
+ r = o, s = a;
440
+ break;
441
+ case "object":
442
+ if (a instanceof $ || a instanceof Set)
443
+ break;
444
+ a.mask & l.LIVE && (n |= l.LIVE);
445
+ break;
446
+ }
447
+ return new m(n, t, void 0, r, s, i);
448
+ }
449
+ function qe(e) {
450
+ return Ht(l.OBJECT, e);
451
+ }
452
+ function Ot(e, t, n) {
453
+ const i = e.mask;
454
+ if ((i & l.UNION) !== 0) {
455
+ const r = e;
456
+ if (r.typenameField !== void 0) {
457
+ if (n !== void 0 && n !== r.typenameField)
458
+ throw new Error(
459
+ `Union typename field conflict: Cannot merge unions with different typename fields ('${n}' vs '${r.typenameField}')`
460
+ );
461
+ n = r.typenameField;
462
+ }
463
+ const s = r.shape;
464
+ if (s !== void 0)
465
+ for (const o of [...rt(s), D, F]) {
466
+ const a = s[o];
467
+ if (t[o] !== void 0 && t[o] !== a)
468
+ throw new Error(
469
+ `Union merge conflict: Duplicate typename value '${String(o)}' found when merging nested unions (${String(t[o])} vs ${String(a)})`
470
+ );
471
+ t[o] = a;
472
+ }
473
+ } else if ((i & l.ARRAY) !== 0) {
474
+ if (t[D] !== void 0)
475
+ throw new Error("Array shape already defined");
476
+ t[D] = e.shape;
477
+ } else if ((i & l.RECORD) !== 0) {
478
+ if (t[F] !== void 0)
479
+ throw new Error("Record shape already defined");
480
+ t[F] = e.shape;
481
+ } else {
482
+ const r = e.typenameField, s = e.typenameValue;
483
+ if (s === void 0)
484
+ throw new Error(
485
+ "Object definitions must have a typename to be in a union with other objects, records, or arrays"
486
+ );
487
+ if (n !== void 0 && r !== n)
488
+ throw new Error("Object definitions must have the same typename field to be in the same union");
489
+ n = r, t[s] = e;
490
+ }
491
+ return n;
492
+ }
493
+ function yt(...e) {
494
+ const t = e;
495
+ let n = 0, i = 0, r, s, o, a, c = 0;
496
+ for (const d of t) {
497
+ if (typeof d == "number") {
498
+ n |= d;
499
+ continue;
500
+ }
501
+ if (d instanceof Set) {
502
+ if (s === void 0)
503
+ s = new Set(d);
504
+ else
505
+ for (const u of d)
506
+ s.add(u);
507
+ continue;
508
+ }
509
+ if (i++, c |= d.mask, i === 1) {
510
+ r = d;
511
+ continue;
512
+ }
513
+ i === 2 && (o = /* @__PURE__ */ Object.create(null), a = Ot(r, o, a)), a = Ot(d, o, a);
514
+ }
515
+ if (i === 0)
516
+ return s === void 0 ? n : n === 0 ? s : new m(n | l.UNION, void 0, s);
517
+ if (i === 1)
518
+ return m.cloneWith(r, n);
519
+ const f = n | c | l.UNION;
520
+ return new m(f, o, s, a);
521
+ }
522
+ function pt(e, t, n) {
523
+ const i = e;
524
+ if (typeof i == "number")
525
+ return i | t;
526
+ if (i instanceof Set)
527
+ return yt(e, t);
528
+ let r = n.get(i);
529
+ return r === void 0 && (r = m.cloneWith(i, t), n.set(i, r)), r;
530
+ }
531
+ const Ve = /* @__PURE__ */ new WeakMap(), Ke = /* @__PURE__ */ new WeakMap(), Be = /* @__PURE__ */ new WeakMap();
532
+ function Qe(e) {
533
+ return pt(e, l.UNDEFINED | l.NULL, Be);
534
+ }
535
+ function Ye(e) {
536
+ return pt(e, l.UNDEFINED, Ve);
537
+ }
538
+ function Ge(e) {
539
+ return pt(e, l.NULL, Ke);
540
+ }
541
+ function He(e) {
542
+ return e;
543
+ }
544
+ function We(e) {
545
+ return /* @__PURE__ */ new Set([e]);
546
+ }
547
+ const Wt = ((...e) => new Set(e));
548
+ Wt.caseInsensitive = (...e) => new $(e);
549
+ const gt = 16;
550
+ let ze = 0;
551
+ const st = [], zt = [], Jt = /* @__PURE__ */ new Map(), Xt = /* @__PURE__ */ new Map(), Z = /* @__PURE__ */ new WeakSet();
552
+ class Q {
553
+ _raw;
554
+ _formatted;
555
+ _parsed;
556
+ _formatId;
557
+ constructor(t, n, i) {
558
+ this._raw = t, this._formatId = n, i ? (this._formatted = st[n](t), this._parsed = !0) : this._parsed = !1, Z.add(this);
559
+ }
560
+ getValue() {
561
+ return this._parsed || (this._formatted = st[this._formatId](this._raw), this._parsed = !0), this._formatted;
562
+ }
563
+ toJSON() {
564
+ return this._parsed ? zt[this._formatId](this._formatted) : this._raw;
565
+ }
566
+ }
567
+ function Je(e) {
568
+ const t = Jt.get(e);
569
+ if (t === void 0)
570
+ throw new Error(`Format ${e} not registered`);
571
+ return t;
572
+ }
573
+ function Rt(e) {
574
+ const t = e >> gt;
575
+ return Xt.get(t);
576
+ }
577
+ function Zt(e, t, n, i, r) {
578
+ const s = ze++;
579
+ st[s] = n, zt[s] = i, Xt.set(s, e);
580
+ const o = r?.eager ?? !0, c = s << gt | t | l.HAS_FORMAT | (o ? l.IS_EAGER_FORMAT : 0);
581
+ Jt.set(e, c);
582
+ }
583
+ Zt(
584
+ "date",
585
+ l.STRING,
586
+ (e) => {
587
+ const t = e.match(/^(\d{4})-(\d{2})-(\d{2})$/);
588
+ if (!t)
589
+ throw new Error(`Invalid date string: ${e}. Expected YYYY-MM-DD format.`);
590
+ const [, n, i, r] = t, s = new Date(Date.UTC(parseInt(n, 10), parseInt(i, 10) - 1, parseInt(r, 10)));
591
+ if (isNaN(s.getTime()))
592
+ throw new Error(`Invalid date string: ${e}`);
593
+ return s;
594
+ },
595
+ (e) => {
596
+ const t = e.getUTCFullYear(), n = String(e.getUTCMonth() + 1).padStart(2, "0"), i = String(e.getUTCDate()).padStart(2, "0");
597
+ return `${t}-${n}-${i}`;
598
+ }
599
+ );
600
+ Zt(
601
+ "date-time",
602
+ l.STRING,
603
+ (e) => {
604
+ const t = new Date(e);
605
+ if (isNaN(t.getTime()))
606
+ throw new Error(`Invalid date-time string: ${e}`);
607
+ return t;
608
+ },
609
+ (e) => e.toISOString()
610
+ );
611
+ const xt = /* @__PURE__ */ new WeakMap();
612
+ function j(e) {
613
+ let t = xt.get(e);
614
+ if (t === void 0) {
615
+ const n = new e(), i = n[lt] ?? n;
616
+ n[z] && n[z]();
617
+ const r = {};
618
+ for (const [f, d] of Qt(i)) {
619
+ if (!(typeof d == "number" || typeof d == "string" || d instanceof Set || d instanceof m || d instanceof $))
620
+ throw new Error(
621
+ `[fetchium] Entity '${e.name}' field '${f}' has an invalid type definition. All entity fields must be type definitions (e.g. t.string, t.number, t.entity(...), etc). Got: ${typeof d == "object" ? d?.constructor?.name ?? typeof d : typeof d}`
622
+ );
623
+ r[f] = d;
624
+ }
625
+ const s = Object.getPrototypeOf(e.prototype);
626
+ if (s != null) {
627
+ const f = s.constructor;
628
+ if (f !== Bt && typeof f == "function") {
629
+ const u = j(f).shape;
630
+ for (const h of rt(u))
631
+ if (h in r && r[h] !== u[h])
632
+ throw new Error(`Cannot extend: field '${h}' already exists in type definition`);
633
+ }
634
+ }
635
+ const o = {}, a = e.prototype;
636
+ for (const f of Object.getOwnPropertyNames(a))
637
+ f !== "constructor" && typeof a[f] == "function" && (o[f] = a[f]);
638
+ t = Ht(l.ENTITY | l.OBJECT, r), t._entityClass = e, rt(o).length > 0 && (t._methods = o), typeof o.__subscribe == "function" && (t._entityConfig = { hasSubscribe: !0 });
639
+ const c = e;
640
+ c.cache && (t._entityCache = c.cache), xt.set(e, t);
641
+ }
642
+ return t;
643
+ }
644
+ function Xe(e) {
645
+ return j(e);
646
+ }
647
+ function te(e, t) {
648
+ if (t == null) return;
649
+ const n = /* @__PURE__ */ new Map();
650
+ if (Array.isArray(t))
651
+ for (const i of t) {
652
+ const [r, s] = i, a = j(r).typenameValue;
653
+ if (a === void 0) continue;
654
+ const c = [];
655
+ for (const [f, d] of Object.entries(s))
656
+ c.push([f, d]);
657
+ c.length > 0 && n.set(a, c);
658
+ }
659
+ else {
660
+ const r = Object.entries(t);
661
+ if (r.length === 0) return;
662
+ const s = r.map(([o, a]) => [o, a]);
663
+ for (const o of e) {
664
+ const a = o.typenameValue;
665
+ a !== void 0 && n.set(a, s);
666
+ }
667
+ }
668
+ return n.size > 0 ? n : void 0;
669
+ }
670
+ function ee(e) {
671
+ return Array.isArray(e) ? e.map((t) => j(t)) : [j(e)];
672
+ }
673
+ function Ze(e, t) {
674
+ const n = ee(e), i = n.length === 1 ? n[0] : yt(...n.map((o) => o)), r = l.ARRAY | l.LIVE, s = new m(r, i);
675
+ return s._liveConfig = B.array(
676
+ n,
677
+ te(n, t?.constraints),
678
+ t?.sort
679
+ ), s;
680
+ }
681
+ function tn(e, t, n) {
682
+ const i = ee(t), r = e, s = l.LIVE, o = new m(s, void 0);
683
+ return o._liveConfig = B.value(
684
+ i,
685
+ te(i, n?.constraints),
686
+ r,
687
+ n.onCreate,
688
+ n.onUpdate,
689
+ n.onDelete
690
+ ), o;
691
+ }
692
+ const en = {
693
+ format: Je,
694
+ typename: He,
695
+ const: We,
696
+ enum: Wt,
697
+ id: l.ID | l.STRING | l.NUMBER,
698
+ string: l.STRING,
699
+ number: l.NUMBER,
700
+ boolean: l.BOOLEAN,
701
+ null: l.NULL,
702
+ undefined: l.UNDEFINED,
703
+ array: $e,
704
+ object: qe,
705
+ record: Le,
706
+ union: yt,
707
+ nullish: Qe,
708
+ optional: Ye,
709
+ nullable: Ge,
710
+ result: Ue,
711
+ entity: Xe,
712
+ liveArray: Ze,
713
+ liveValue: tn
714
+ };
715
+ function K(e) {
716
+ if (e instanceof $)
717
+ return Array.from(e).map((i) => typeof i == "string" ? `"${i}"` : String(i)).join(" | ");
718
+ if (e instanceof Set)
719
+ return Array.from(e).map((i) => typeof i == "string" ? `"${i}"` : String(i)).join(" | ");
720
+ if (typeof e == "string")
721
+ return `"${e}"`;
722
+ if (typeof e == "boolean")
723
+ return String(e);
724
+ if (typeof e == "number") {
725
+ if ((e & l.HAS_FORMAT) !== 0) {
726
+ const r = Rt(e);
727
+ if (r)
728
+ return `"${r}"`;
729
+ }
730
+ const i = [];
731
+ return e & l.UNDEFINED && i.push("undefined"), e & l.NULL && i.push("null"), e & l.NUMBER && i.push("number"), e & l.STRING && i.push("string"), e & l.BOOLEAN && i.push("boolean"), e & l.OBJECT && i.push("object"), e & l.ARRAY && i.push("array"), i.length === 0 ? "unknown" : i.length === 1 ? i[0] : i.join(" | ");
732
+ }
733
+ let t = e.mask;
734
+ if (t & l.UNION) {
735
+ const n = e, i = [];
736
+ if (n.values !== void 0 && n.values.size > 0)
737
+ for (const s of n.values) {
738
+ const o = typeof s == "string" ? `"${s}"` : String(s);
739
+ i.push(o);
740
+ }
741
+ if (n.shape !== void 0) {
742
+ n.shape[D] !== void 0 && i.push(`Array<${K(n.shape[D])}>`), n.shape[F] !== void 0 && i.push(`Record<string, ${K(n.shape[F])}>`);
743
+ for (const [s, o] of Object.entries(n.shape))
744
+ s !== D && s !== F && i.push(s);
745
+ }
746
+ if (t = n.mask, (t & l.HAS_FORMAT) !== 0) {
747
+ const s = Rt(t);
748
+ s && i.push(`"${s}"`);
749
+ }
750
+ return t & l.UNDEFINED && i.push("undefined"), t & l.NULL && i.push("null"), t & l.NUMBER && i.push("number"), t & l.STRING && i.push("string"), t & l.BOOLEAN && i.push("boolean"), i.length === 0 ? "union" : i.join(" | ");
751
+ }
752
+ if (t & l.ENTITY)
753
+ return `Entity<${e.typenameValue}>`;
754
+ if (t & l.ARRAY) {
755
+ const n = e.shape;
756
+ return `Array<${K(n)}>`;
757
+ }
758
+ if (t & l.RECORD) {
759
+ const n = e.shape;
760
+ return `Record<string, ${K(n)}>`;
761
+ }
762
+ if (t & l.OBJECT) {
763
+ const n = e.typenameValue;
764
+ return n ? `Object<${n}>` : "object";
765
+ }
766
+ return "unknown";
767
+ }
768
+ function q(e, t, n) {
769
+ return new TypeError(
770
+ `Validation error at ${e}: expected ${K(t)}, got ${typeof n == "object" ? n === null ? "null" : Array.isArray(n) ? "array" : "object" : typeof n}`
771
+ );
772
+ }
773
+ const ne = Array.isArray;
774
+ function At(e) {
775
+ if (e === null) return l.NULL;
776
+ switch (typeof e) {
777
+ case "number":
778
+ return l.NUMBER;
779
+ case "string":
780
+ return l.STRING;
781
+ case "boolean":
782
+ return l.BOOLEAN;
783
+ case "undefined":
784
+ return l.UNDEFINED;
785
+ case "object":
786
+ return ne(e) ? l.ARRAY : l.OBJECT;
787
+ default:
788
+ throw new Error(`Invalid type: ${typeof e}`);
789
+ }
790
+ }
791
+ function kn(e) {
792
+ return P(e);
793
+ }
794
+ function P(e) {
795
+ if (e === null || typeof e != "object")
796
+ return e;
797
+ if (ne(e))
798
+ return e.map((n) => P(n));
799
+ if (e instanceof Date)
800
+ return new Date(e.getTime());
801
+ if (e instanceof Map) {
802
+ const n = /* @__PURE__ */ new Map();
803
+ for (const [i, r] of e)
804
+ n.set(P(i), P(r));
805
+ return n;
806
+ }
807
+ if (e instanceof Set) {
808
+ const n = /* @__PURE__ */ new Set();
809
+ for (const i of e)
810
+ n.add(P(i));
811
+ return n;
812
+ }
813
+ const t = {};
814
+ for (const n of Object.keys(e))
815
+ t[n] = P(e[n]);
816
+ return t;
817
+ }
818
+ const mt = Object.entries, nn = () => {
819
+ };
820
+ class ot {
821
+ queryClient = void 0;
822
+ preloadedEntities = void 0;
823
+ warn = nn;
824
+ /** When true, missing optional fields on existing entities are set to
825
+ * undefined. False for mutation events (truly partial payloads). */
826
+ isPartialEvent = !1;
827
+ seen = void 0;
828
+ seenByKey = void 0;
829
+ reset(t, n, i, r = !1) {
830
+ this.queryClient = t, this.preloadedEntities = n, this.warn = i, this.isPartialEvent = r, t !== void 0 && (this.seen === void 0 ? (this.seen = /* @__PURE__ */ new Map(), this.seenByKey = /* @__PURE__ */ new Map()) : (this.seen.clear(), this.seenByKey.clear()));
831
+ }
832
+ }
833
+ function ie(e, t, n) {
834
+ return M(e, t, n, "");
835
+ }
836
+ function rn(e, t, n) {
837
+ return vt(e, t, n);
838
+ }
839
+ function Nt(e, t, n, i) {
840
+ const r = e >> gt;
841
+ if ((e & l.IS_EAGER_FORMAT) !== 0)
842
+ try {
843
+ return new Q(t, r, !0);
844
+ } catch (o) {
845
+ if ((e & l.UNDEFINED) !== 0) {
846
+ n.warn("Invalid formatted value for optional type, defaulting to undefined", {
847
+ value: t,
848
+ path: i,
849
+ error: o instanceof Error ? o.message : String(o)
850
+ });
851
+ return;
852
+ }
853
+ throw o;
854
+ }
855
+ return new Q(t, r, !1);
856
+ }
857
+ function M(e, t, n, i) {
858
+ const r = t;
859
+ if (r instanceof $) {
860
+ const c = r.get(e);
861
+ if (c === void 0) throw q(i, r, e);
862
+ return c;
863
+ }
864
+ if (r instanceof Set) {
865
+ if (!r.has(e)) throw q(i, r, e);
866
+ return e;
867
+ }
868
+ if (typeof r == "string") {
869
+ if (e == null) return r;
870
+ if (e !== r) throw q(i, r, e);
871
+ return e;
872
+ }
873
+ if (typeof r == "number") {
874
+ const c = At(e);
875
+ if ((r & c) === 0) {
876
+ if ((r & l.UNDEFINED) !== 0) {
877
+ n.warn("Invalid value for optional type, defaulting to undefined", { value: e, path: i });
878
+ return;
879
+ }
880
+ throw q(i, r, e);
881
+ }
882
+ return (r & l.HAS_FORMAT) !== 0 && e !== null && e !== void 0 ? Nt(r, e, n, i) : e;
883
+ }
884
+ const s = r.mask, o = r._liveConfig;
885
+ if (o !== void 0 && o.type === ut.Value)
886
+ return o.valueType !== void 0 ? M(e, o.valueType, n, i) : e;
887
+ if ((s & l.PARSE_RESULT) !== 0)
888
+ try {
889
+ return { success: !0, value: M(e, r.shape, n, i) };
890
+ } catch (c) {
891
+ return { success: !1, error: c instanceof Error ? c : new Error(String(c)) };
892
+ }
893
+ const a = At(e);
894
+ if ((s & a) === 0 && !r.values?.has(e)) {
895
+ if ((s & l.UNDEFINED) !== 0) {
896
+ n.warn("Invalid value for optional type, defaulting to undefined", { value: e, path: i });
897
+ return;
898
+ }
899
+ throw q(i, s, e);
900
+ }
901
+ return a < l.OBJECT ? (s & l.HAS_FORMAT) !== 0 && e !== null && e !== void 0 ? Nt(s, e, n, i) : e : (s & l.UNION) !== 0 ? sn(a, e, r, n, i) : a === l.ARRAY ? re(e, r.shape, n, i) : (s & l.RECORD) !== 0 ? se(e, r.shape, n, i) : (s & l.ENTITY) !== 0 && n.queryClient !== void 0 ? vt(e, r, n) : oe(e, r, n, i);
902
+ }
903
+ function sn(e, t, n, i, r) {
904
+ if (e === l.ARRAY) {
905
+ const s = n.shape[D];
906
+ return s === void 0 || typeof s == "number" ? t : re(t, s, i, r);
907
+ } else {
908
+ const s = n.typenameField, o = s ? t[s] : void 0;
909
+ if (o === void 0 || typeof o != "string") {
910
+ const c = n.shape[F];
911
+ if (c === void 0)
912
+ throw new Error(
913
+ `Typename field '${s}' is required for union discrimination but was not found in the data`
914
+ );
915
+ return se(t, c, i, r);
916
+ }
917
+ const a = n.shape[o];
918
+ if (a === void 0 || typeof a == "number")
919
+ throw new Error(`Unknown typename '${o}' in union`);
920
+ return a.mask & l.ENTITY && i.queryClient !== void 0 ? vt(t, a, i) : oe(t, a, i, r);
921
+ }
922
+ }
923
+ function re(e, t, n, i) {
924
+ const r = [];
925
+ for (let s = 0; s < e.length; s++)
926
+ try {
927
+ r.push(M(e[s], t, n, `${i}[${s}]`));
928
+ } catch (o) {
929
+ n.warn("Failed to parse array item, filtering out", {
930
+ index: s,
931
+ value: e[s],
932
+ error: o instanceof Error ? o.message : String(o)
933
+ });
934
+ }
935
+ return r;
936
+ }
937
+ function se(e, t, n, i) {
938
+ for (const [r, s] of mt(e))
939
+ e[r] = M(s, t, n, `${i}["${r}"]`);
940
+ return e;
941
+ }
942
+ function oe(e, t, n, i) {
943
+ if (S.has(e))
944
+ return e;
945
+ const r = t.shape;
946
+ for (const [s, o] of mt(r))
947
+ e[s] = M(e[s], o, n, `${i}.${s}`);
948
+ return e;
949
+ }
950
+ function vt(e, t, n) {
951
+ const i = n.queryClient, r = n.preloadedEntities;
952
+ let s, o;
953
+ if (r !== void 0)
954
+ s = e.__entityRef, o = s;
955
+ else {
956
+ const p = e[t.idField];
957
+ if (p == null || typeof p != "string" && typeof p != "number")
958
+ throw new Error(`Entity id must be a string or number: ${t.typenameValue} (got ${typeof p})`);
959
+ o = p, s = N([t.typenameValue, o]);
960
+ }
961
+ const a = n.seenByKey.get(s);
962
+ if (a !== void 0)
963
+ return a.data;
964
+ if (r !== void 0) {
965
+ const w = i.entityMap.getEntity(s)?.data ?? r.get(s);
966
+ if (w === void 0)
967
+ throw new Error(`Cached entity ${s} not found in preloaded map`);
968
+ e = w;
969
+ }
970
+ const c = {};
971
+ typeof t.idField == "symbol" && (c[t.idField] = o);
972
+ const f = i.entityMap.getEntity(s), d = n.isPartialEvent && f !== void 0, u = {
973
+ key: s,
974
+ shape: t,
975
+ data: c,
976
+ rawKeys: d ? new Set(Object.keys(e)) : void 0
977
+ };
978
+ n.seen.set(c, u), n.seenByKey.set(s, u);
979
+ const h = `[[${t.typenameValue}:${o}]]`, g = t.shape;
980
+ for (const [p, w] of mt(g))
981
+ d && !(p in e) || (c[p] = M(e[p], w, n, `${h}.${p}`));
982
+ return c;
983
+ }
984
+ function on(e, t) {
985
+ return an(e, t.shape, t.typenameField);
986
+ }
987
+ function an(e, t, n) {
988
+ if (t === void 0) return !0;
989
+ for (const i of Object.keys(t)) {
990
+ if (i === n) continue;
991
+ const r = t[i];
992
+ if (r instanceof m) {
993
+ if ((r.mask & l.UNDEFINED) !== 0) continue;
994
+ if (!(i in e) || e[i] === void 0) return !1;
995
+ } else if (typeof r == "number") {
996
+ if ((r & l.UNDEFINED) !== 0) continue;
997
+ if (!(i in e) || e[i] === void 0) return !1;
998
+ } else if (!(i in e) || e[i] === void 0) return !1;
999
+ }
1000
+ return !0;
1001
+ }
1002
+ const cn = Object.prototype, H = /* @__PURE__ */ new WeakMap();
1003
+ function A(e) {
1004
+ if (typeof e != "object" || e === null) return e;
1005
+ if (Z.has(e)) return A(e.getValue());
1006
+ if (S.has(e)) return e;
1007
+ if (Array.isArray(e)) {
1008
+ let t = H.get(e);
1009
+ return t === void 0 && (t = new Proxy(e, fn), H.set(e, t)), t;
1010
+ }
1011
+ if (Object.getPrototypeOf(e) === cn) {
1012
+ let t = H.get(e);
1013
+ return t === void 0 && (t = new Proxy(e, un), H.set(e, t)), t;
1014
+ }
1015
+ return e;
1016
+ }
1017
+ const fn = {
1018
+ get(e, t, n) {
1019
+ if (typeof t == "string") {
1020
+ const i = Number(t);
1021
+ if (Number.isInteger(i) && i >= 0 && i < e.length)
1022
+ return A(e[i]);
1023
+ }
1024
+ return Reflect.get(e, t, n);
1025
+ },
1026
+ set() {
1027
+ throw new Error("Cannot mutate a read-only array");
1028
+ },
1029
+ deleteProperty() {
1030
+ throw new Error("Cannot mutate a read-only array");
1031
+ }
1032
+ }, un = {
1033
+ get(e, t, n) {
1034
+ return typeof t == "string" ? A(e[t]) : Reflect.get(e, t, n);
1035
+ },
1036
+ set() {
1037
+ throw new Error("Cannot mutate a read-only object");
1038
+ },
1039
+ deleteProperty() {
1040
+ throw new Error("Cannot mutate a read-only object");
1041
+ },
1042
+ has(e, t) {
1043
+ return t in e;
1044
+ },
1045
+ ownKeys(e) {
1046
+ return Reflect.ownKeys(e);
1047
+ },
1048
+ getOwnPropertyDescriptor(e, t) {
1049
+ return Object.getOwnPropertyDescriptor(e, t);
1050
+ }
1051
+ };
1052
+ class ln {
1053
+ _notifier;
1054
+ _queryClient;
1055
+ _proxies = /* @__PURE__ */ new Map();
1056
+ key;
1057
+ typename;
1058
+ id;
1059
+ idField;
1060
+ data;
1061
+ refCount = 0;
1062
+ entityRefs;
1063
+ liveCollections = [];
1064
+ satisfiedDefs = /* @__PURE__ */ new WeakSet();
1065
+ parseId = -1;
1066
+ _entityCache;
1067
+ _extraMethods;
1068
+ _extraGetters;
1069
+ constructor(t, n, i, r, s, o) {
1070
+ this._notifier = ft(), this._queryClient = o, this.key = t, this.typename = n, this.id = i, this.idField = r, this.data = s, this.entityRefs = void 0;
1071
+ }
1072
+ retain() {
1073
+ this.refCount++;
1074
+ const t = this._entityCache?.gcTime;
1075
+ t !== void 0 && this._queryClient.gcManager.cancel(this.key, t);
1076
+ }
1077
+ release() {
1078
+ if (--this.refCount > 0) return;
1079
+ if (this.refCount < 0)
1080
+ throw new Error(`Entity ${this.typename}:${this.id} released more times than retained`);
1081
+ const t = this._entityCache?.gcTime;
1082
+ t !== void 0 ? this._queryClient.gcManager.schedule(this.key, t, X.Entity) : this.evict();
1083
+ }
1084
+ evict() {
1085
+ const t = this.liveCollections.slice();
1086
+ this.liveCollections.length = 0;
1087
+ for (const i of t) i.destroy();
1088
+ this._queryClient.entityMap.remove(this.key);
1089
+ const n = this.entityRefs;
1090
+ if (this.entityRefs = void 0, n)
1091
+ for (const i of n.keys()) i.release();
1092
+ }
1093
+ setChildRefs(t, n) {
1094
+ const i = this.entityRefs;
1095
+ if (t !== void 0 && t.size > 0)
1096
+ for (const r of t.keys())
1097
+ (i === void 0 || !i.has(r)) && r.retain();
1098
+ if (i !== void 0 && i.size > 0)
1099
+ for (const r of i.keys())
1100
+ (t === void 0 || !t.has(r)) && r.release();
1101
+ this.entityRefs = t, n && this.save();
1102
+ }
1103
+ addChildRef(t) {
1104
+ this.entityRefs === void 0 && (this.entityRefs = /* @__PURE__ */ new Map());
1105
+ const n = this.entityRefs.get(t) ?? 0;
1106
+ this.entityRefs.set(t, n + 1), n === 0 && t.retain(), this.save();
1107
+ }
1108
+ removeChildRef(t) {
1109
+ if (this.entityRefs === void 0) return;
1110
+ const n = this.entityRefs.get(t);
1111
+ n !== void 0 && (n <= 1 ? (this.entityRefs.delete(t), t.release()) : this.entityRefs.set(t, n - 1), this.save());
1112
+ }
1113
+ getProxy(t) {
1114
+ const n = t;
1115
+ let i = this._proxies.get(n);
1116
+ return i === void 0 && (i = hn(this, this.key, t, this._notifier, this._queryClient), this._proxies.set(n, i)), i;
1117
+ }
1118
+ get proxy() {
1119
+ return this._proxies.values().next().value;
1120
+ }
1121
+ satisfiesDef(t) {
1122
+ return this.satisfiedDefs.has(t) ? !0 : on(this.data, t) ? (this.satisfiedDefs.add(t), !0) : !1;
1123
+ }
1124
+ save() {
1125
+ this._queryClient.entityMap.save(this);
1126
+ }
1127
+ notify() {
1128
+ this._notifier.notify();
1129
+ }
1130
+ consume() {
1131
+ this._notifier.consume();
1132
+ }
1133
+ }
1134
+ function dn(e, t, n) {
1135
+ const i = [];
1136
+ for (const r of e) {
1137
+ if (typeof r != "object" || r === null) continue;
1138
+ const s = S.get(r);
1139
+ if (s === void 0) continue;
1140
+ const o = n.entityMap.getEntity(s);
1141
+ o !== void 0 && o.satisfiesDef(t) && i.push(r);
1142
+ }
1143
+ return i;
1144
+ }
1145
+ function hn(e, t, n, i, r) {
1146
+ const s = n.shape ?? {}, o = n, a = o._methods, c = o._entityClass, f = o._entityConfig, d = c ? c.prototype : Bt.prototype, u = n.typenameField, h = /* @__PURE__ */ new Map(), g = /* @__PURE__ */ new Map(), p = () => ({ __entityRef: t });
1147
+ let w;
1148
+ f?.hasSubscribe && a && "__subscribe" in a && (w = Tt((E) => {
1149
+ const y = (b) => {
1150
+ b.__eventSource = t, r.applyMutationEvent(b);
1151
+ }, _ = a.__subscribe.call(v, y);
1152
+ return E.value = v, _;
1153
+ }));
1154
+ let v;
1155
+ if (u && !(u in s))
1156
+ throw new Error(`typenameField "${u}" must be declared in the entity shape`);
1157
+ const O = Object.keys(s);
1158
+ if (O.includes("__typename") || O.push("__typename"), a)
1159
+ for (const E of Object.keys(a))
1160
+ O.includes(E) || O.push(E);
1161
+ let k, R = O;
1162
+ function L() {
1163
+ const E = e._extraMethods;
1164
+ if (E !== k) {
1165
+ if (k = E, R = O.slice(), E !== void 0)
1166
+ for (const _ of Object.keys(E))
1167
+ R.includes(_) || R.push(_);
1168
+ const y = e._extraGetters;
1169
+ if (y !== void 0)
1170
+ for (const _ of Object.keys(y))
1171
+ R.includes(_) || R.push(_);
1172
+ }
1173
+ return R;
1174
+ }
1175
+ const U = {
1176
+ getPrototypeOf() {
1177
+ return d;
1178
+ },
1179
+ get(E, y) {
1180
+ if (typeof y == "symbol") return;
1181
+ if (y === "toJSON") return p;
1182
+ if (y === "__context") return r.getContext();
1183
+ if (y === "__typename") return e.typename;
1184
+ if (w?.value, i.consume(), typeof y == "string") {
1185
+ const b = e._extraGetters;
1186
+ if (b !== void 0 && y in b)
1187
+ return b[y]();
1188
+ const x = e._extraMethods;
1189
+ if (x !== void 0 && y in x) {
1190
+ let C = h.get(y);
1191
+ return C || (C = x[y].bind(v), h.set(y, C)), C;
1192
+ }
1193
+ if (a && y in a) {
1194
+ let C = h.get(y);
1195
+ return C || (C = Ee(v, a[y].bind(v)), h.set(y, C)), C;
1196
+ }
1197
+ }
1198
+ const _ = e.data[y];
1199
+ if (typeof _ == "object" && _ !== null && Z.has(_))
1200
+ return A(_.getValue());
1201
+ if (Array.isArray(_) && typeof y == "string") {
1202
+ const b = s[y];
1203
+ if (b instanceof m && (b.mask & l.ARRAY) !== 0) {
1204
+ const x = b.shape;
1205
+ if (x instanceof m && (x.mask & l.ENTITY) !== 0) {
1206
+ const C = x.typenameValue;
1207
+ if (C !== void 0) {
1208
+ const Et = r.getEntityDefsForTypename(C);
1209
+ if (Et !== void 0 && Et.length > 1) {
1210
+ const tt = g.get(y);
1211
+ if (tt !== void 0 && tt.source === _)
1212
+ return A(tt.filtered);
1213
+ const bt = dn(_, x, r);
1214
+ return g.set(y, { source: _, filtered: bt }), A(bt);
1215
+ }
1216
+ }
1217
+ }
1218
+ }
1219
+ return A(_);
1220
+ }
1221
+ return A(_);
1222
+ },
1223
+ set() {
1224
+ throw new Error("Entity properties are read-only");
1225
+ },
1226
+ has(E, y) {
1227
+ if (y === "__typename") return !0;
1228
+ if (typeof y == "string") {
1229
+ const _ = e._extraGetters;
1230
+ if (_ && y in _) return !0;
1231
+ const b = e._extraMethods;
1232
+ if (b && y in b || a && y in a) return !0;
1233
+ }
1234
+ return y in s;
1235
+ },
1236
+ ownKeys() {
1237
+ return L();
1238
+ },
1239
+ getOwnPropertyDescriptor(E, y) {
1240
+ if (y === "__typename")
1241
+ return { enumerable: !0, configurable: !0, value: e.typename, writable: !1 };
1242
+ if (y in s)
1243
+ return { enumerable: !0, configurable: !0, value: U.get(E, y, v), writable: !1 };
1244
+ if (typeof y == "string") {
1245
+ const _ = e._extraGetters;
1246
+ if (_ && y in _)
1247
+ return { enumerable: !0, configurable: !0, value: U.get(E, y, v), writable: !1 };
1248
+ const b = e._extraMethods;
1249
+ if (b && y in b)
1250
+ return { enumerable: !0, configurable: !0, value: U.get(E, y, v), writable: !1 };
1251
+ if (a && y in a)
1252
+ return { enumerable: !1, configurable: !0, value: U.get(E, y, v), writable: !1 };
1253
+ }
1254
+ }
1255
+ };
1256
+ return v = new Proxy({}, U), S.set(v, t), we(v, r), v;
1257
+ }
1258
+ class yn {
1259
+ instances = /* @__PURE__ */ new Map();
1260
+ persistEntity;
1261
+ constructor(t) {
1262
+ this.persistEntity = t;
1263
+ }
1264
+ hasEntity(t) {
1265
+ return this.instances.has(t);
1266
+ }
1267
+ getEntity(t) {
1268
+ return this.instances.get(t);
1269
+ }
1270
+ getOrCreateEntity(t, n, i, r) {
1271
+ let s = this.instances.get(t);
1272
+ if (s === void 0) {
1273
+ const o = i.idField;
1274
+ if (o === void 0)
1275
+ throw new Error(`Entity id field is required ${i.typenameValue}`);
1276
+ const a = n[o];
1277
+ if (typeof a != "string" && typeof a != "number")
1278
+ throw new Error(`Entity id must be string or number: ${i.typenameValue}`);
1279
+ const c = i;
1280
+ s = new ln(t, i.typenameValue, a, o, n, r), s._entityCache = c._entityCache, this.instances.set(t, s);
1281
+ }
1282
+ return s.parseId = r.currentParseId, s;
1283
+ }
1284
+ remove(t) {
1285
+ this.instances.delete(t);
1286
+ }
1287
+ save(t) {
1288
+ let n;
1289
+ if (t.entityRefs) {
1290
+ n = /* @__PURE__ */ new Set();
1291
+ for (const i of t.entityRefs.keys()) n.add(i.key);
1292
+ }
1293
+ this.persistEntity(t.key, t.data, n);
1294
+ }
1295
+ }
1296
+ class ae {
1297
+ onlineSignal;
1298
+ manualOverride = void 0;
1299
+ eventListenersAttached = !1;
1300
+ constructor(t) {
1301
+ const n = t ?? this.detectOnlineStatus();
1302
+ this.onlineSignal = Pt(n), this.canAttachListeners() && this.attachEventListeners();
1303
+ }
1304
+ /**
1305
+ * Returns true if the network is currently online
1306
+ */
1307
+ get isOnline() {
1308
+ return this.manualOverride !== void 0 ? this.manualOverride : this.onlineSignal.value;
1309
+ }
1310
+ /**
1311
+ * Manually set the network status (useful for testing)
1312
+ */
1313
+ setNetworkStatus(t) {
1314
+ this.manualOverride = t, this.onlineSignal.value = t;
1315
+ }
1316
+ /**
1317
+ * Clear manual override and return to automatic detection
1318
+ */
1319
+ clearManualOverride() {
1320
+ this.manualOverride = void 0, this.onlineSignal.value = this.detectOnlineStatus();
1321
+ }
1322
+ /**
1323
+ * Get the reactive signal for online status
1324
+ * This allows reactive functions to depend on network status
1325
+ */
1326
+ getOnlineSignal() {
1327
+ return this.onlineSignal;
1328
+ }
1329
+ /**
1330
+ * Detect current online status from the environment
1331
+ */
1332
+ detectOnlineStatus() {
1333
+ return typeof navigator < "u" && "onLine" in navigator ? navigator.onLine : !0;
1334
+ }
1335
+ /**
1336
+ * Check if we can attach event listeners (browser or React Native)
1337
+ */
1338
+ canAttachListeners() {
1339
+ return typeof window < "u" && typeof window.addEventListener == "function";
1340
+ }
1341
+ /**
1342
+ * Remove event listeners and clean up resources.
1343
+ */
1344
+ destroy() {
1345
+ this.eventListenersAttached && this.handleOnline && this.handleOffline && (window.removeEventListener("online", this.handleOnline), window.removeEventListener("offline", this.handleOffline), this.eventListenersAttached = !1);
1346
+ }
1347
+ handleOnline = void 0;
1348
+ handleOffline = void 0;
1349
+ /**
1350
+ * Attach event listeners for online/offline events
1351
+ */
1352
+ attachEventListeners() {
1353
+ this.eventListenersAttached || (this.handleOnline = () => {
1354
+ this.manualOverride === void 0 && (this.onlineSignal.value = !0);
1355
+ }, this.handleOffline = () => {
1356
+ this.manualOverride === void 0 && (this.onlineSignal.value = !1);
1357
+ }, window.addEventListener("online", this.handleOnline), window.addEventListener("offline", this.handleOffline), this.eventListenersAttached = !0);
1358
+ }
1359
+ }
1360
+ class ce {
1361
+ static onlineSignal = Pt(!0);
1362
+ get isOnline() {
1363
+ return !0;
1364
+ }
1365
+ setNetworkStatus(t) {
1366
+ }
1367
+ clearManualOverride() {
1368
+ }
1369
+ getOnlineSignal() {
1370
+ return ce.onlineSignal;
1371
+ }
1372
+ destroy() {
1373
+ }
1374
+ }
1375
+ const pn = new ae(), Tn = jt(pn);
1376
+ function _t(e, t = typeof window > "u") {
1377
+ let n;
1378
+ e === !1 ? n = 0 : e === void 0 || e === !0 ? n = t ? 0 : 3 : typeof e == "number" ? n = e : n = e.retries;
1379
+ const i = typeof e == "object" && e.retryDelay ? e.retryDelay : (r) => 1e3 * Math.pow(2, r);
1380
+ return { retries: n, retryDelay: i };
1381
+ }
1382
+ class Pn {
1383
+ static cache;
1384
+ /**
1385
+ * The controller class responsible for sending requests for this query type.
1386
+ * Must be set on each concrete Query subclass (or inherited from a base like RESTQuery).
1387
+ */
1388
+ static controller;
1389
+ params;
1390
+ config;
1391
+ constructor() {
1392
+ return Kt(this);
1393
+ }
1394
+ }
1395
+ const It = /* @__PURE__ */ new WeakMap();
1396
+ class G {
1397
+ constructor(t, n) {
1398
+ this.captured = n, this.statics = t;
1399
+ }
1400
+ statics;
1401
+ createExecutionContext(t, n) {
1402
+ return it(this.captured, t, n);
1403
+ }
1404
+ resolveOptions(t) {
1405
+ const { methods: n } = this.captured, i = n.getConfig ? n.getConfig.call(t) : t.config, r = _t(i?.retry);
1406
+ return { config: i, retryConfig: r };
1407
+ }
1408
+ static for(t) {
1409
+ let n = It.get(t);
1410
+ if (n !== void 0)
1411
+ return n;
1412
+ const i = new t(), r = Te(i), s = String(r.methods.getIdentityKey.call(r.fields)), o = r.fields.result, a = o instanceof m ? o : en.object(o), c = (a.mask & l.ENTITY) !== 0, f = t.cache, d = r.fields.fetchNext, u = t.controller;
1413
+ if (!u)
1414
+ throw new Error(
1415
+ `Query class "${t.name}" must define a static \`controller\` property. Extend RESTQuery (from fetchium/rest) or set \`static controller = MyController\` on your query class.`
1416
+ );
1417
+ const h = typeof u.prototype.sendNext == "function", g = c ? a : new m(
1418
+ l.ENTITY | l.OBJECT,
1419
+ a.shape,
1420
+ void 0,
1421
+ void 0,
1422
+ s,
1423
+ // typenameValue — unique per query class
1424
+ Lt
1425
+ // idField — symbol, injected onto payload before parse
1426
+ );
1427
+ return n = new G(
1428
+ { id: s, shape: g, cache: f, rawFetchNext: d, hasSendNext: h, isEntityResult: c, controllerClass: u },
1429
+ r
1430
+ ), It.set(t, n), n;
1431
+ }
1432
+ }
1433
+ const jn = (e, t) => {
1434
+ const n = G.for(e);
1435
+ return wt(n, t);
1436
+ };
1437
+ function $n(e, ...t) {
1438
+ const n = G.for(e), i = be(An);
1439
+ if (i === void 0)
1440
+ throw new Error("QueryClient not found");
1441
+ const r = t[0];
1442
+ return i.getQuery(n, r);
1443
+ }
1444
+ function fe(e, t) {
1445
+ return new Promise((n, i) => {
1446
+ if (t?.aborted) {
1447
+ i(t.reason);
1448
+ return;
1449
+ }
1450
+ const r = setTimeout(n, e);
1451
+ t?.addEventListener(
1452
+ "abort",
1453
+ () => {
1454
+ clearTimeout(r), i(t.reason);
1455
+ },
1456
+ { once: !0 }
1457
+ );
1458
+ });
1459
+ }
1460
+ async function at(e, t, n) {
1461
+ if (t.retries < 0)
1462
+ throw new Error("retries must be non-negative");
1463
+ const i = Math.max(0, t.retries);
1464
+ let r;
1465
+ for (let s = 0; s <= i; s++) {
1466
+ n?.throwIfAborted();
1467
+ try {
1468
+ return await e();
1469
+ } catch (o) {
1470
+ if (r = o, s >= i) throw o;
1471
+ await fe(t.retryDelay(s), n);
1472
+ }
1473
+ }
1474
+ throw r;
1475
+ }
1476
+ class gn {
1477
+ def;
1478
+ queryKey;
1479
+ storageKey = -1;
1480
+ /** The public-facing ReactivePromise returned to consumers. */
1481
+ relay;
1482
+ queryClient;
1483
+ initialized = !1;
1484
+ updatedAt = void 0;
1485
+ params = void 0;
1486
+ unsubscribe = void 0;
1487
+ _relayState = void 0;
1488
+ wasPaused = !1;
1489
+ currentParams = void 0;
1490
+ debounceTimer = void 0;
1491
+ /** Resolved per-instance options (depend on actual params). */
1492
+ config = void 0;
1493
+ retryConfig = _t(void 0);
1494
+ /** Controller for aborting in-flight fetches and retry waits. */
1495
+ _abortController = void 0;
1496
+ /** Cached execution context, recreated only when storageKey (params) changes. */
1497
+ _executionCtx = void 0;
1498
+ _executionCtxKey = -1;
1499
+ /** Root entity that holds parsed data, proxy, child refs, and bindings.
1500
+ * For entity results, this is undefined until the first apply discovers it. */
1501
+ rootEntity;
1502
+ /** Extra methods (__refetch, __fetchNext) attached to the root entity proxy. */
1503
+ _extraMethods = {};
1504
+ /** Query id injected as QUERY_ID on non-entity payloads. */
1505
+ _queryId = 0;
1506
+ get key() {
1507
+ return this.queryKey;
1508
+ }
1509
+ get relayState() {
1510
+ if (!this._relayState)
1511
+ throw new Error("Relay state not initialized");
1512
+ return this._relayState;
1513
+ }
1514
+ constructor(t, n, i, r) {
1515
+ this.def = t, this.queryClient = n, this.queryKey = i, this.params = r, this._extraMethods = { __refetch: this.refetch }, t.statics.hasSendNext && (this._extraMethods.__fetchNext = this.fetchNext);
1516
+ const s = kt(r);
1517
+ this._queryId = s !== void 0 ? N(s) : 0, this.relay = Tt(
1518
+ (o) => {
1519
+ this._relayState = o;
1520
+ const a = () => {
1521
+ clearTimeout(this.debounceTimer), this.debounceTimer = void 0, this._abortController?.abort(), this._abortController = void 0, this._fetchNextAbort?.abort(), this._fetchNextAbort = void 0, this._fetchNextPromise = void 0, this.unsubscribe?.(), this.unsubscribe = void 0;
1522
+ const f = this.config?.gcTime ?? $t;
1523
+ this.queryClient.gcManager.schedule(this.queryKey, f, X.Query);
1524
+ }, c = (f = !1) => {
1525
+ const { wasPaused: d, isPaused: u, initialized: h } = this;
1526
+ if (this.wasPaused = u, u && !d && h) {
1527
+ a();
1528
+ return;
1529
+ }
1530
+ const g = kt(this.params), p = wt(this.def, g), w = p !== this.storageKey;
1531
+ w && (this.currentParams = g, this.storageKey = p), this.getOrCreateExecutionContext(), this.initialized ? d || f ? (this.queryClient.activateQuery(this), f && this.updatedAt !== void 0 && this.setupSubscription(), (this.config?.refreshStaleOnReconnect ?? !0) && this.isStale && this.runDebounced()) : w && (this.setupSubscription(), this.runDebounced()) : (this.queryClient.activateQuery(this), this.initialize());
1532
+ };
1533
+ return c(!0), {
1534
+ update: c,
1535
+ deactivate: a
1536
+ };
1537
+ },
1538
+ { desc: `Query(${t.statics.id})` }
1539
+ );
1540
+ }
1541
+ /** Apply raw data (fresh or cached) to the root entity and return the proxy. */
1542
+ applyData(t, n, i = !1, r) {
1543
+ const s = this.def;
1544
+ return this.rootEntity = this.queryClient.parseAndApplyRootEntity(
1545
+ t,
1546
+ this._queryId,
1547
+ s.statics.shape,
1548
+ n,
1549
+ i,
1550
+ r
1551
+ ), this.rootEntity._extraMethods === void 0 && (this.rootEntity._extraMethods = this._extraMethods, this.rootEntity._extraGetters = {
1552
+ __hasNext: () => this.hasNext,
1553
+ __isFetchingNext: () => this._fetchNextPromise !== void 0
1554
+ }), this.rootEntity.getProxy(s.statics.shape);
1555
+ }
1556
+ /** Save query metadata (the __entityRef pointer, updatedAt, ref set). */
1557
+ saveQueryMetadata() {
1558
+ if (this.rootEntity === void 0 || this.updatedAt === void 0) return;
1559
+ const t = new Map(this.rootEntity.entityRefs ?? []);
1560
+ t.set(this.rootEntity, 1), this.queryClient.saveQueryData(
1561
+ this.def,
1562
+ this.storageKey,
1563
+ { __entityRef: this.rootEntity.key },
1564
+ this.updatedAt,
1565
+ t
1566
+ );
1567
+ }
1568
+ async initialize() {
1569
+ const t = this.queryClient, n = this.relayState;
1570
+ this.initialized = !0;
1571
+ let i;
1572
+ try {
1573
+ i = await t.loadCachedQuery(this.def, this.storageKey), i !== void 0 && (this.updatedAt = i.updatedAt, n.value = this.applyData(i.value, !1, !1, i.preloadedEntities));
1574
+ } catch (r) {
1575
+ t.store.deleteQuery(this.storageKey), t.getContext().log?.warn?.("Failed to initialize query, the query cache may be corrupted or invalid", r);
1576
+ }
1577
+ if (!this.isPaused)
1578
+ try {
1579
+ if (i !== void 0 && this.setupSubscription(), i === void 0 || this.isStale) {
1580
+ if (await fe(0), this.isPaused) return;
1581
+ this.runQueryImmediately();
1582
+ }
1583
+ } catch (r) {
1584
+ n.setError(r);
1585
+ }
1586
+ }
1587
+ setupSubscription() {
1588
+ this.unsubscribe?.(), this.unsubscribe = void 0;
1589
+ const t = this.config?.subscribe;
1590
+ if (!t) return;
1591
+ const n = this._executionCtx;
1592
+ this.unsubscribe = t.call(n, (i) => {
1593
+ i.__eventSource = this.queryKey, this.queryClient.applyMutationEvent(i);
1594
+ });
1595
+ }
1596
+ getOrCreateExecutionContext() {
1597
+ return (this._executionCtx === void 0 || this._executionCtxKey !== this.storageKey) && (this._executionCtxKey = this.storageKey, this._executionCtx = this.def.createExecutionContext(
1598
+ this.currentParams ?? {},
1599
+ this.queryClient.getContext()
1600
+ ), this._executionCtx.refetch = () => this.refetch(), this._executionCtx.rawFetchNext = this.def.statics.rawFetchNext), this.resolveAndApplyOptions(), this._executionCtx;
1601
+ }
1602
+ resolveAndApplyOptions() {
1603
+ const t = this.def.resolveOptions(this._executionCtx);
1604
+ this.config = t.config, this.retryConfig = t.retryConfig;
1605
+ }
1606
+ async runQuery() {
1607
+ const t = this.def;
1608
+ if (this.isPaused)
1609
+ throw new Error("Query is paused due to network status");
1610
+ const n = this.getOrCreateExecutionContext(), i = this.queryClient.getController(t.statics.controllerClass), r = this._abortController?.signal ?? new AbortController().signal;
1611
+ return at(
1612
+ async () => {
1613
+ const s = await i.send(n, r);
1614
+ this.updatedAt = Date.now();
1615
+ const o = this.applyData(s, !0);
1616
+ return this.saveQueryMetadata(), this.unsubscribe === void 0 && this.setupSubscription(), o;
1617
+ },
1618
+ this.retryConfig,
1619
+ r
1620
+ );
1621
+ }
1622
+ runQueryImmediately() {
1623
+ this._abortController?.abort(), this._abortController = new AbortController(), this._fetchNextAbort?.abort(), this._fetchNextAbort = void 0, this._fetchNextPromise = void 0, this.relayState.setPromise(this.runQuery());
1624
+ }
1625
+ runDebounced() {
1626
+ if (this.relayState.isPending) return;
1627
+ const t = this.config?.debounce ?? 0;
1628
+ clearTimeout(this.debounceTimer), this.debounceTimer = setTimeout(() => {
1629
+ this.debounceTimer = void 0, this.runQueryImmediately();
1630
+ }, t);
1631
+ }
1632
+ // ======================================================
1633
+ // Public methods
1634
+ // ======================================================
1635
+ refetch = () => this.relayState.isPending ? this.relay : (this.runQueryImmediately(), this.relay);
1636
+ markStale() {
1637
+ this.updatedAt = 0;
1638
+ }
1639
+ get resolvedParams() {
1640
+ return this.currentParams;
1641
+ }
1642
+ /** In-flight fetchNext promise for deduplication. */
1643
+ _fetchNextPromise = void 0;
1644
+ /** Controller for aborting in-flight fetchNext requests. */
1645
+ _fetchNextAbort = void 0;
1646
+ fetchNext = () => {
1647
+ if (this.updatedAt === void 0)
1648
+ throw new Error("Cannot call __fetchNext before initial data has loaded");
1649
+ return this._fetchNextPromise !== void 0 ? this._fetchNextPromise : (queueMicrotask(() => this.rootEntity?.notify()), this._fetchNextPromise = this.runFetchNext().then(
1650
+ (t) => (this._fetchNextPromise = void 0, this.rootEntity?.notify(), t),
1651
+ (t) => {
1652
+ throw this._fetchNextPromise = void 0, this.rootEntity?.notify(), t;
1653
+ }
1654
+ ), this._fetchNextPromise);
1655
+ };
1656
+ get hasNext() {
1657
+ if (this.rootEntity === void 0 || !this._executionCtx) return !1;
1658
+ const t = this.queryClient.getController(this.def.statics.controllerClass);
1659
+ return t.hasNext ? (this._executionCtx.resultData = this.rootEntity.data, t.hasNext(this._executionCtx)) : !1;
1660
+ }
1661
+ async runFetchNext() {
1662
+ const t = this.def;
1663
+ this._fetchNextAbort = new AbortController();
1664
+ const n = this._fetchNextAbort.signal, i = this.getOrCreateExecutionContext();
1665
+ i.resultData = this.rootEntity.data;
1666
+ const r = this.queryClient.getController(t.statics.controllerClass);
1667
+ return at(
1668
+ async () => {
1669
+ const s = await r.sendNext(i, n);
1670
+ this.updatedAt = Date.now();
1671
+ const o = this.applyData(s, !0, !0);
1672
+ return this.saveQueryMetadata(), o;
1673
+ },
1674
+ this.retryConfig,
1675
+ n
1676
+ );
1677
+ }
1678
+ // ======================================================
1679
+ // Internal computed properties
1680
+ // ======================================================
1681
+ get isStale() {
1682
+ if (this.updatedAt === void 0)
1683
+ return !0;
1684
+ const t = this.config?.staleTime ?? 0;
1685
+ return Date.now() - this.updatedAt >= t;
1686
+ }
1687
+ get isPaused() {
1688
+ const t = this.config?.networkMode ?? V.Online;
1689
+ if (t === V.Always)
1690
+ return !1;
1691
+ const n = this.queryClient.networkManager.getOnlineSignal().value;
1692
+ switch (t) {
1693
+ case V.Online:
1694
+ return !n;
1695
+ case V.OfflineFirst:
1696
+ return !n && this.updatedAt === void 0;
1697
+ default:
1698
+ return !1;
1699
+ }
1700
+ }
1701
+ }
1702
+ class mn {
1703
+ def;
1704
+ queryClient;
1705
+ _inFlight = !1;
1706
+ task;
1707
+ constructor(t, n) {
1708
+ this.def = t, this.queryClient = n, this.task = this.createTask();
1709
+ }
1710
+ createTask() {
1711
+ return Ce(
1712
+ async (t) => {
1713
+ if (this._inFlight)
1714
+ throw new Error("A mutation is already in progress. Await the previous call before starting a new one.");
1715
+ this._inFlight = !0;
1716
+ try {
1717
+ const n = await this.executeWithRetry(t), i = this.validateResponse(n);
1718
+ return this.processEffects(t, i), i;
1719
+ } finally {
1720
+ this._inFlight = !1;
1721
+ }
1722
+ },
1723
+ { desc: `Mutation(${this.def.id})` }
1724
+ );
1725
+ }
1726
+ validateResponse(t) {
1727
+ const n = this.def.responseShape;
1728
+ if (!(n instanceof m))
1729
+ return t;
1730
+ const i = this.queryClient.getContext().log?.warn ?? (() => {
1731
+ }), r = new ot();
1732
+ return r.reset(void 0, void 0, i), ie(t, n, r);
1733
+ }
1734
+ // ======================================================
1735
+ // Effects processing
1736
+ // ======================================================
1737
+ processEffects(t, n) {
1738
+ let i;
1739
+ if (this.def.hasGetEffects) {
1740
+ const s = it(
1741
+ this.def.captured,
1742
+ t ?? {},
1743
+ this.queryClient.getContext()
1744
+ );
1745
+ s.result = n, i = s.getEffects();
1746
+ } else if (this.def.effects !== void 0) {
1747
+ const s = { params: t, result: n };
1748
+ i = J(this.def.effects, s);
1749
+ }
1750
+ if (i === void 0) return;
1751
+ const r = this.queryClient;
1752
+ et(i.creates, "create", r), et(i.updates, "update", r), et(i.deletes, "delete", r), i.invalidates && r.invalidateQueries(i.invalidates);
1753
+ }
1754
+ // ======================================================
1755
+ // Retry logic
1756
+ // ======================================================
1757
+ executeWithRetry(t) {
1758
+ const n = _t(this.def.config?.retry, !0), i = this.queryClient.getController(this.def.controllerClass);
1759
+ if (!i.sendMutation)
1760
+ throw new Error(
1761
+ `Controller "${this.def.controllerClass.name}" does not implement sendMutation(). Add a sendMutation() method to handle mutations.`
1762
+ );
1763
+ return at(async () => {
1764
+ const r = new AbortController(), s = it(
1765
+ this.def.captured,
1766
+ t ?? {},
1767
+ this.queryClient.getContext()
1768
+ );
1769
+ return await i.sendMutation(s, r.signal);
1770
+ }, n);
1771
+ }
1772
+ }
1773
+ function vn(e) {
1774
+ return typeof e == "string" ? e : j(e).typenameValue;
1775
+ }
1776
+ function et(e, t, n) {
1777
+ if (e)
1778
+ for (const [i, r] of e) {
1779
+ const s = vn(i);
1780
+ s !== void 0 && n.applyMutationEvent({ type: t, typename: s, data: r });
1781
+ }
1782
+ }
1783
+ const ue = "__eventSource";
1784
+ function le(e) {
1785
+ return { field: e, segments: e.indexOf(".") !== -1 ? e.split(".") : void 0 };
1786
+ }
1787
+ function _n(e) {
1788
+ return e.map(le);
1789
+ }
1790
+ function wn(e, t) {
1791
+ if (t.segments === void 0) return e[t.field];
1792
+ let n = e;
1793
+ for (const i of t.segments) {
1794
+ if (n == null) return;
1795
+ n = n[i];
1796
+ }
1797
+ return n;
1798
+ }
1799
+ function de(e, t) {
1800
+ const n = [];
1801
+ for (let i = 0; i < t.length; i++) {
1802
+ const r = t[i], s = wn(e, r);
1803
+ if (s === void 0) return;
1804
+ n.push(r.field, s);
1805
+ }
1806
+ return N(n);
1807
+ }
1808
+ function En(e, t) {
1809
+ if (e === void 0) return;
1810
+ const n = /* @__PURE__ */ new Map();
1811
+ for (const [i, r] of e) {
1812
+ const s = r.slice().sort((c, f) => c[0] < f[0] ? -1 : c[0] > f[0] ? 1 : 0), o = [];
1813
+ let a = !0;
1814
+ for (const [c, f] of s) {
1815
+ let d;
1816
+ if (qt(f)) {
1817
+ const u = Vt(f);
1818
+ d = dt(u, t);
1819
+ } else
1820
+ d = f;
1821
+ if (d === void 0) {
1822
+ a = !1;
1823
+ break;
1824
+ }
1825
+ o.push(c, d);
1826
+ }
1827
+ a && n.set(i, N(o));
1828
+ }
1829
+ return n.size > 0 ? n : void 0;
1830
+ }
1831
+ function Dt(e, t) {
1832
+ const n = e.get(t);
1833
+ if (n !== void 0)
1834
+ return n.map(([i]) => i).sort();
1835
+ }
1836
+ class bn {
1837
+ fields;
1838
+ fieldPaths;
1839
+ _bindings = /* @__PURE__ */ new Map();
1840
+ constructor(t) {
1841
+ this.fields = t, this.fieldPaths = t.map(le);
1842
+ }
1843
+ register(t, n) {
1844
+ let i = this._bindings.get(t);
1845
+ i === void 0 && (i = /* @__PURE__ */ new Set(), this._bindings.set(t, i)), i.add(n);
1846
+ }
1847
+ unregister(t, n) {
1848
+ const i = this._bindings.get(t);
1849
+ i !== void 0 && (i.delete(n), i.size === 0 && this._bindings.delete(t));
1850
+ }
1851
+ getMatching(t) {
1852
+ return this._bindings.get(t);
1853
+ }
1854
+ get isEmpty() {
1855
+ return this._bindings.size === 0;
1856
+ }
1857
+ }
1858
+ function Ft(e) {
1859
+ return N(e);
1860
+ }
1861
+ class Cn {
1862
+ _groups = /* @__PURE__ */ new Map();
1863
+ getOrCreateGroup(t) {
1864
+ const n = Ft(t);
1865
+ let i = this._groups.get(n);
1866
+ return i === void 0 && (i = new bn(t), this._groups.set(n, i)), i;
1867
+ }
1868
+ register(t, n, i) {
1869
+ this.getOrCreateGroup(n).register(t, i);
1870
+ }
1871
+ unregister(t, n, i) {
1872
+ const r = Ft(n), s = this._groups.get(r);
1873
+ s !== void 0 && (s.unregister(t, i), s.isEmpty && this._groups.delete(r));
1874
+ }
1875
+ registerBinding(t, n) {
1876
+ const i = t._constraintHashes.get(n);
1877
+ if (i === void 0) return;
1878
+ const r = Dt(t._constraintFieldRefs, n);
1879
+ r !== void 0 && this.register(i, r, t);
1880
+ }
1881
+ unregisterBinding(t, n) {
1882
+ const i = t._constraintHashes.get(n);
1883
+ if (i === void 0) return;
1884
+ const r = Dt(t._constraintFieldRefs, n);
1885
+ r !== void 0 && this.unregister(i, r, t);
1886
+ }
1887
+ routeEvent(t, n, i, r, s, o) {
1888
+ for (const a of this._groups.values()) {
1889
+ const c = de(n, a.fieldPaths);
1890
+ if (c === void 0) continue;
1891
+ const f = a.getMatching(c);
1892
+ if (f !== void 0)
1893
+ for (const d of f)
1894
+ d.onEvent(t, i, r, s, o);
1895
+ }
1896
+ }
1897
+ }
1898
+ function Mt(e) {
1899
+ const t = /* @__PURE__ */ new Set();
1900
+ for (const n of e)
1901
+ if (typeof n == "object" && n !== null) {
1902
+ const i = T(n);
1903
+ i !== void 0 && t.add(i);
1904
+ }
1905
+ return t;
1906
+ }
1907
+ class he {
1908
+ _queryClient;
1909
+ _parent;
1910
+ _constraintHashes;
1911
+ _entityDefsByTypename;
1912
+ _constraintFieldRefs;
1913
+ instance;
1914
+ constructor(t, n, i, r, s, o) {
1915
+ this._queryClient = i, this._parent = r, this._constraintHashes = s, this._constraintFieldRefs = n, this.instance = o, this._entityDefsByTypename = /* @__PURE__ */ new Map();
1916
+ for (const a of t)
1917
+ a.typenameValue !== void 0 && this._entityDefsByTypename.set(a.typenameValue, a);
1918
+ Z.add(this);
1919
+ }
1920
+ getValue() {
1921
+ return this.instance.getValue();
1922
+ }
1923
+ toJSON() {
1924
+ return this.instance.getRawValue();
1925
+ }
1926
+ reset(t) {
1927
+ this.instance.reset(t);
1928
+ }
1929
+ append(t) {
1930
+ this.instance.append(t);
1931
+ }
1932
+ /**
1933
+ * Handle an entity event. The entity has already been created/updated at the
1934
+ * root level by applyMutationEvent. The binding just checks if the entity's
1935
+ * current data satisfies this binding's shape and forwards to the instance.
1936
+ */
1937
+ onEvent(t, n, i, r, s) {
1938
+ const o = this._entityDefsByTypename.get(t);
1939
+ if (o === void 0) return;
1940
+ const a = this._queryClient.entityMap.getEntity(n);
1941
+ if (i === "delete") {
1942
+ const f = a !== void 0 ? a.getProxy(o) : s;
1943
+ f !== void 0 && (this.instance.onEvent(n, f, s ?? a?.data ?? {}, "delete"), r?.());
1944
+ return;
1945
+ }
1946
+ if (a === void 0 || !a.satisfiesDef(o)) return;
1947
+ r?.();
1948
+ const c = a.getProxy(o);
1949
+ this.instance.onEvent(n, c, a.data, i);
1950
+ }
1951
+ destroy() {
1952
+ this._queryClient.unregisterLiveCollection(this);
1953
+ const t = this._parent.liveCollections, n = t.indexOf(this);
1954
+ n !== -1 && t.splice(n, 1);
1955
+ }
1956
+ }
1957
+ class Sn {
1958
+ _notifier;
1959
+ _items;
1960
+ _keys;
1961
+ _outputSignal;
1962
+ _queryClient;
1963
+ _parent;
1964
+ constructor(t, n, i, r, s, o) {
1965
+ this._notifier = ft(), this._items = i, this._keys = Mt(i), this._queryClient = t, this._parent = n;
1966
+ const a = r !== void 0 && s !== void 0, c = o !== void 0;
1967
+ (a || c) && (this._outputSignal = Se(() => {
1968
+ this._notifier.consume();
1969
+ let f = this._items;
1970
+ if (a) {
1971
+ const d = [];
1972
+ for (const u of f) {
1973
+ if (typeof u != "object" || u === null) {
1974
+ d.push(u);
1975
+ continue;
1976
+ }
1977
+ const h = T(u);
1978
+ if (h === void 0) {
1979
+ d.push(u);
1980
+ continue;
1981
+ }
1982
+ const g = t.entityMap.getEntity(h);
1983
+ if (g === void 0) {
1984
+ d.push(u);
1985
+ continue;
1986
+ }
1987
+ g.consume(), de(g.data, r) === s && d.push(u);
1988
+ }
1989
+ f = d;
1990
+ }
1991
+ return c && (f = (f === this._items ? f.slice() : f).sort(o)), f;
1992
+ }));
1993
+ }
1994
+ onEvent(t, n, i, r) {
1995
+ switch (r) {
1996
+ case "create":
1997
+ this.add(t, n);
1998
+ break;
1999
+ case "update":
2000
+ !this.has(t) && n !== void 0 && this.add(t, n);
2001
+ break;
2002
+ case "delete":
2003
+ this.remove(t);
2004
+ break;
2005
+ }
2006
+ }
2007
+ getValue() {
2008
+ return this._outputSignal !== void 0 ? this._outputSignal.value : (this._notifier.consume(), this._items);
2009
+ }
2010
+ getRawValue() {
2011
+ return this._items;
2012
+ }
2013
+ add(t, n) {
2014
+ if (this._keys.has(t)) return !1;
2015
+ this._keys.add(t), this._items.push(n);
2016
+ const i = this._queryClient.entityMap.getEntity(t);
2017
+ return i !== void 0 && (this._parent.addChildRef(i), i.save()), this._notifier.notify(), !0;
2018
+ }
2019
+ remove(t) {
2020
+ if (!this._keys.has(t)) return !1;
2021
+ this._keys.delete(t);
2022
+ const n = this._findIndex(t);
2023
+ n !== -1 && this._items.splice(n, 1);
2024
+ const i = this._queryClient.entityMap.getEntity(t);
2025
+ return i !== void 0 && this._parent.removeChildRef(i), this._notifier.notify(), !0;
2026
+ }
2027
+ has(t) {
2028
+ return this._keys.has(t);
2029
+ }
2030
+ reset(t) {
2031
+ const n = this._items, i = Array.isArray(t) ? t : [];
2032
+ this._items = i, this._keys = Mt(i);
2033
+ for (const r of i)
2034
+ if (typeof r == "object" && r !== null) {
2035
+ const s = T(r);
2036
+ if (s !== void 0) {
2037
+ const o = this._queryClient.entityMap.getEntity(s);
2038
+ o !== void 0 && this._parent.addChildRef(o);
2039
+ }
2040
+ }
2041
+ for (const r of n)
2042
+ if (typeof r == "object" && r !== null) {
2043
+ const s = T(r);
2044
+ if (s !== void 0) {
2045
+ const o = this._queryClient.entityMap.getEntity(s);
2046
+ o !== void 0 && this._parent.removeChildRef(o);
2047
+ }
2048
+ }
2049
+ this._notifier.notify();
2050
+ }
2051
+ append(t) {
2052
+ if (Array.isArray(t))
2053
+ for (const n of t) {
2054
+ if (typeof n != "object" || n === null) continue;
2055
+ const i = T(n);
2056
+ i !== void 0 && this.add(i, n);
2057
+ }
2058
+ }
2059
+ _findIndex(t) {
2060
+ for (let n = 0; n < this._items.length; n++) {
2061
+ const i = this._items[n];
2062
+ if (typeof i == "object" && i !== null && T(i) === t)
2063
+ return n;
2064
+ }
2065
+ return -1;
2066
+ }
2067
+ }
2068
+ class On {
2069
+ _notifier;
2070
+ _value;
2071
+ _createdKeys;
2072
+ _deletedKeys;
2073
+ _queryClient;
2074
+ _parent;
2075
+ _onCreate;
2076
+ _onUpdate;
2077
+ _onDelete;
2078
+ constructor(t, n, i, r, s, o) {
2079
+ this._notifier = ft(), this._value = i, this._createdKeys = /* @__PURE__ */ new Set(), this._deletedKeys = /* @__PURE__ */ new Set(), this._queryClient = t, this._parent = n, this._onCreate = r, this._onUpdate = s, this._onDelete = o;
2080
+ }
2081
+ onEvent(t, n, i, r) {
2082
+ switch (r) {
2083
+ case "create":
2084
+ if (this._createdKeys.has(t)) return;
2085
+ this._createdKeys.add(t), this._value = this._onCreate(this._value, n);
2086
+ break;
2087
+ case "update":
2088
+ this._value = this._onUpdate(this._value, n ?? i);
2089
+ break;
2090
+ case "delete":
2091
+ if (this._deletedKeys.has(t)) return;
2092
+ this._deletedKeys.add(t), this._value = this._onDelete(this._value, n ?? i);
2093
+ break;
2094
+ }
2095
+ this._notifier.notify();
2096
+ }
2097
+ getValue() {
2098
+ return this._notifier.consume(), this._value;
2099
+ }
2100
+ getRawValue() {
2101
+ return this._value;
2102
+ }
2103
+ reset(t) {
2104
+ this._value = t, this._createdKeys.clear(), this._deletedKeys.clear(), this._notifier.notify();
2105
+ }
2106
+ append(t) {
2107
+ }
2108
+ }
2109
+ function ye(e, t, n, i, r) {
2110
+ let s = e.constraintFieldRefs;
2111
+ if (s === void 0) {
2112
+ s = /* @__PURE__ */ new Map();
2113
+ for (const f of e.entityDefs) {
2114
+ const d = f.typenameValue;
2115
+ d !== void 0 && s.set(d, [[ue, n.key]]);
2116
+ }
2117
+ }
2118
+ const o = En(s, i) ?? /* @__PURE__ */ new Map();
2119
+ let a;
2120
+ if (e.type === ut.Array) {
2121
+ let f, d;
2122
+ if (e.constraintFieldRefs !== void 0 && o.size > 0 && o.size === 1)
2123
+ for (const [u] of e.constraintFieldRefs) {
2124
+ const h = o.get(u);
2125
+ if (h !== void 0) {
2126
+ d = h;
2127
+ const g = e.constraintFieldRefs.get(u);
2128
+ g !== void 0 && (f = _n(g.map(([p]) => p)));
2129
+ break;
2130
+ }
2131
+ }
2132
+ a = new Sn(
2133
+ r,
2134
+ n,
2135
+ Array.isArray(t) ? t : [],
2136
+ f,
2137
+ d,
2138
+ e.sort
2139
+ );
2140
+ } else
2141
+ a = new On(
2142
+ r,
2143
+ n,
2144
+ t,
2145
+ e.onCreate,
2146
+ e.onUpdate,
2147
+ e.onDelete
2148
+ );
2149
+ const c = new he(
2150
+ e.entityDefs,
2151
+ s,
2152
+ r,
2153
+ n,
2154
+ o,
2155
+ a
2156
+ );
2157
+ return n.liveCollections.push(c), r.registerLiveCollection(c), c;
2158
+ }
2159
+ const pe = Object.entries, ge = Object.prototype;
2160
+ function nt(e, t, n, i = !1) {
2161
+ const r = e.queryClient;
2162
+ r.currentParseId++;
2163
+ const s = e.seen, o = /* @__PURE__ */ new Map();
2164
+ return { data: Y(t, s, r, n, o, i), entityRefs: o };
2165
+ }
2166
+ function Y(e, t, n, i, r, s) {
2167
+ if (typeof e != "object" || e === null) return e;
2168
+ const o = t.get(e);
2169
+ if (o !== void 0)
2170
+ return Rn(o, t, n, i, r, s);
2171
+ if (Array.isArray(e)) {
2172
+ for (let a = 0; a < e.length; a++) {
2173
+ const c = e[a];
2174
+ typeof c == "object" && c !== null && !(c instanceof Q) && !S.has(c) && (e[a] = Y(c, t, n, i, r, s));
2175
+ }
2176
+ return e;
2177
+ }
2178
+ if (Object.getPrototypeOf(e) === ge && !S.has(e)) {
2179
+ const a = e;
2180
+ for (const c of Object.keys(a)) {
2181
+ const f = a[c];
2182
+ typeof f == "object" && f !== null && !(f instanceof Q) && !S.has(f) && (a[c] = Y(f, t, n, i, r, s));
2183
+ }
2184
+ }
2185
+ return e;
2186
+ }
2187
+ function me(e) {
2188
+ return typeof e == "object" && e !== null && !(e instanceof Q) && !S.has(e);
2189
+ }
2190
+ function Rn(e, t, n, i, r, s) {
2191
+ const { key: o, data: a, shape: c, rawKeys: f } = e, d = c.shape, u = n.prepareEntity(o, a, c), h = u.data, g = h !== a, p = g && f !== void 0 && u.entityRefs !== void 0 ? new Map(u.entityRefs) : /* @__PURE__ */ new Map();
2192
+ if (g ? (ve(
2193
+ d,
2194
+ a,
2195
+ h,
2196
+ f,
2197
+ u,
2198
+ h,
2199
+ t,
2200
+ n,
2201
+ i,
2202
+ p,
2203
+ s
2204
+ ), u.notify()) : _e(d, a, u, a, t, n, i, p, s), s && u.liveCollections.length > 0)
2205
+ for (const v of u.liveCollections) {
2206
+ const O = v.instance.getRawValue();
2207
+ if (Array.isArray(O))
2208
+ for (const k of O) {
2209
+ if (typeof k != "object" || k === null) continue;
2210
+ const R = S.get(k);
2211
+ if (R === void 0) continue;
2212
+ const L = n.entityMap.getEntity(R);
2213
+ L !== void 0 && p.set(L, (p.get(L) ?? 0) + 1);
2214
+ }
2215
+ }
2216
+ u.setChildRefs(p.size > 0 ? p : void 0, i);
2217
+ const w = u.getProxy(c);
2218
+ return r.set(u, (r.get(u) ?? 0) + 1), w;
2219
+ }
2220
+ function ve(e, t, n, i, r, s, o, a, c, f, d) {
2221
+ for (const [u, h] of pe(e))
2222
+ if (!(i !== void 0 && !i.has(u)))
2223
+ if (me(t[u]) && (t[u] = Y(t[u], o, a, c, f, d)), h instanceof m && h._liveConfig !== void 0) {
2224
+ const g = n[u];
2225
+ g instanceof he ? d ? g.append(t[u]) : g.reset(t[u]) : n[u] = ye(
2226
+ h._liveConfig,
2227
+ t[u],
2228
+ r,
2229
+ s,
2230
+ a
2231
+ );
2232
+ } else {
2233
+ const g = t[u], p = n[u];
2234
+ if (ct(g) && ct(p)) {
2235
+ const w = h instanceof m && h.shape !== void 0 ? h.shape : void 0;
2236
+ if (w !== void 0)
2237
+ ve(
2238
+ w,
2239
+ g,
2240
+ p,
2241
+ void 0,
2242
+ r,
2243
+ s,
2244
+ o,
2245
+ a,
2246
+ c,
2247
+ f,
2248
+ d
2249
+ );
2250
+ else
2251
+ for (const v of Object.keys(g))
2252
+ p[v] = g[v];
2253
+ n[u] = p;
2254
+ } else
2255
+ n[u] = g;
2256
+ }
2257
+ }
2258
+ function _e(e, t, n, i, r, s, o, a, c) {
2259
+ for (const [f, d] of pe(e))
2260
+ if (f in t)
2261
+ if (me(t[f]) && (t[f] = Y(t[f], r, s, o, a, c)), d instanceof m && d._liveConfig !== void 0)
2262
+ t[f] = ye(
2263
+ d._liveConfig,
2264
+ t[f],
2265
+ n,
2266
+ i,
2267
+ s
2268
+ );
2269
+ else {
2270
+ const u = t[f];
2271
+ if (ct(u)) {
2272
+ const h = d instanceof m && d.shape !== void 0 ? d.shape : void 0;
2273
+ h !== void 0 && _e(h, u, n, i, r, s, o, a, c);
2274
+ }
2275
+ }
2276
+ }
2277
+ function ct(e) {
2278
+ return typeof e == "object" && e !== null && !Array.isArray(e) && Object.getPrototypeOf(e) === ge && !S.has(e);
2279
+ }
2280
+ function Ln(e) {
2281
+ if (e !== void 0)
2282
+ return typeof e == "string" ? e : typeof e == "function" ? e() : e.value;
2283
+ }
2284
+ function xn(e) {
2285
+ return Oe(e);
2286
+ }
2287
+ function kt(e) {
2288
+ if (e === void 0)
2289
+ return;
2290
+ const t = {};
2291
+ for (const [n, i] of Object.entries(e))
2292
+ xn(i) ? t[n] = i.value : t[n] = i;
2293
+ return t;
2294
+ }
2295
+ const wt = (e, t) => N([e.statics.id, t]);
2296
+ class Un {
2297
+ entityMap;
2298
+ queryInstances = /* @__PURE__ */ new Map();
2299
+ mutationInstances = /* @__PURE__ */ new Map();
2300
+ gcManager;
2301
+ networkManager;
2302
+ isServer;
2303
+ store;
2304
+ currentParseId = 0;
2305
+ context;
2306
+ typenameRegistry = /* @__PURE__ */ new Map();
2307
+ constraintRegistry = /* @__PURE__ */ new Map();
2308
+ mergedDefCache = /* @__PURE__ */ new Map();
2309
+ controllers = /* @__PURE__ */ new Map();
2310
+ networkUnsubscribe;
2311
+ constructor(t = {}) {
2312
+ const {
2313
+ store: n = new Ae(new Ne()),
2314
+ log: i,
2315
+ evictionMultiplier: r,
2316
+ controllers: s,
2317
+ networkManager: o,
2318
+ gcManager: a,
2319
+ ...c
2320
+ } = t;
2321
+ this.isServer = typeof window > "u", this.store = n, this.context = { ...c, log: i ?? console, evictionMultiplier: r }, this.gcManager = t.gcManager ?? (this.isServer ? new Fe() : new De(this.handleEviction, r)), this.networkManager = t.networkManager ?? new ae(), this.entityMap = new yn((u, h, g) => this.store.saveEntity(u, h, g));
2322
+ for (const u of t.controllers ?? [])
2323
+ this.controllers.set(u.constructor, u), u.register(this);
2324
+ const f = this.networkManager.getOnlineSignal(), d = Re(() => f.value);
2325
+ this.networkUnsubscribe = d.addListener(
2326
+ () => {
2327
+ const u = f.value;
2328
+ for (const h of this.controllers.values())
2329
+ h.onNetworkStatusChange?.(u);
2330
+ },
2331
+ { skipInitial: !0 }
2332
+ ), this.store.purgeStaleQueries?.();
2333
+ }
2334
+ /**
2335
+ * Returns the registered controller instance for the given controller class.
2336
+ * Throws if no controller of that class has been registered.
2337
+ */
2338
+ getController(t) {
2339
+ let n = this.controllers.get(t);
2340
+ if (!n) {
2341
+ try {
2342
+ n = new t();
2343
+ } catch {
2344
+ throw new Error(
2345
+ `No controller registered for ${t.name} and auto-instantiation failed. Pass an instance via QueryClient config: new QueryClient({ store, controllers: [new ${t.name}(...)] })`
2346
+ );
2347
+ }
2348
+ this.controllers.set(t, n), n.register(this);
2349
+ }
2350
+ return n;
2351
+ }
2352
+ getContext() {
2353
+ return this.context;
2354
+ }
2355
+ // ======================================================
2356
+ // Typename Registry (per-client)
2357
+ // ======================================================
2358
+ registerEntityDef(t) {
2359
+ const n = t.typenameValue;
2360
+ if (n === void 0 || t._entityClass === void 0) return;
2361
+ const i = this.typenameRegistry.get(n);
2362
+ if (i !== void 0) {
2363
+ if (i.indexOf(t) !== -1) return;
2364
+ i.push(t), this.mergedDefCache.delete(n), this.getMergedDef(n);
2365
+ } else
2366
+ this.typenameRegistry.set(n, [t]);
2367
+ }
2368
+ getEntityDefsForTypename(t) {
2369
+ return this.typenameRegistry.get(t);
2370
+ }
2371
+ getMergedDef(t) {
2372
+ let n = this.mergedDefCache.get(t);
2373
+ if (n !== void 0) return n;
2374
+ const i = this.typenameRegistry.get(t);
2375
+ if (i !== void 0)
2376
+ return n = m.merge(i), this.mergedDefCache.set(t, n), n;
2377
+ }
2378
+ saveQueryData(t, n, i, r, s) {
2379
+ const o = s !== void 0 && s.size > 0 ? new Set([...s.keys()].map((a) => a.key)) : void 0;
2380
+ this.store.saveQuery(t, n, i, r, o);
2381
+ }
2382
+ activateQuery(t) {
2383
+ const { def: n, queryKey: i, storageKey: r, config: s } = t;
2384
+ this.store.activateQuery(n, r);
2385
+ const o = s?.gcTime ?? $t;
2386
+ this.gcManager.cancel(i, o);
2387
+ }
2388
+ loadCachedQuery(t, n) {
2389
+ return this.store.loadQuery(t, n);
2390
+ }
2391
+ /**
2392
+ * Loads a query from the document store and returns a QueryResult
2393
+ * that triggers fetches and prepopulates with cached data
2394
+ */
2395
+ getQuery(t, n) {
2396
+ const i = wt(t, n);
2397
+ let r = this.queryInstances.get(i);
2398
+ return r === void 0 && (r = new gn(t, this, i, n), this.queryInstances.set(i, r)), r.relay;
2399
+ }
2400
+ /**
2401
+ * Gets or creates a MutationResult for the given mutation definition.
2402
+ * Mutations are cached by their definition ID.
2403
+ */
2404
+ getMutation(t) {
2405
+ const n = t.id;
2406
+ let i = this.mutationInstances.get(n);
2407
+ return i === void 0 && (i = new mn(t, this), this.mutationInstances.set(n, i)), i.task;
2408
+ }
2409
+ /**
2410
+ * Parse data: validates, formats, produces parsed entity data objects.
2411
+ * Does NOT touch the entity store. Call applyRefs() after to commit entities.
2412
+ */
2413
+ parseData(t, n, i) {
2414
+ const r = this.context.log?.warn ?? (() => {
2415
+ }), s = new ot();
2416
+ return s.reset(this, i, r), { data: ie(t, n, s), ctx: s };
2417
+ }
2418
+ /**
2419
+ * Apply entities from parseData() via a single depth-first walk: creates/
2420
+ * updates EntityInstances, replaces parsed data with proxies, counts child
2421
+ * refs. Returns the reified data and root-level entity refs.
2422
+ */
2423
+ applyRefs(t, n = !0, i = !1) {
2424
+ return nt(t.ctx, t.data, n, i);
2425
+ }
2426
+ /**
2427
+ * Parse and apply data as a root entity. For non-entity results, injects
2428
+ * QUERY_ID onto the payload. Returns the root EntityInstance (created or
2429
+ * found in the store by the standard entity pipeline).
2430
+ */
2431
+ parseAndApplyRootEntity(t, n, i, r, s = !1, o) {
2432
+ typeof i.idField == "symbol" && typeof t == "object" && t !== null && !("__entityRef" in t) && (t[Lt] = n);
2433
+ const a = this.parseData(t, i, o), c = nt(a.ctx, a.data, r, s), f = S.get(c.data);
2434
+ return this.entityMap.getEntity(f);
2435
+ }
2436
+ prepareEntity(t, n, i) {
2437
+ return this.registerEntityDef(i), this.entityMap.getOrCreateEntity(t, n, i, this);
2438
+ }
2439
+ // ======================================================
2440
+ // Mutation Events
2441
+ // ======================================================
2442
+ applyMutationEvent(t) {
2443
+ const { type: n, typename: i } = t, r = this.getMergedDef(i);
2444
+ if (r === void 0) return;
2445
+ const s = r.idField;
2446
+ if (s === void 0 || typeof s == "symbol") return;
2447
+ const o = t.data, a = t.id !== void 0 ? t.id : n === "delete" && (typeof o == "string" || typeof o == "number") ? o : o[s];
2448
+ if (a === void 0) return;
2449
+ const c = N([i, a]), f = t.__eventSource, d = typeof o == "object" && o !== null ? o : {}, u = this.entityMap.getEntity(c);
2450
+ if (n === "delete") {
2451
+ const w = u !== void 0 ? u.data : d;
2452
+ this.routeEvent(i, w, c, n, f, void 0, w);
2453
+ return;
2454
+ }
2455
+ try {
2456
+ const w = this.context.log?.warn ?? (() => {
2457
+ }), v = new ot();
2458
+ v.reset(
2459
+ this,
2460
+ void 0,
2461
+ w,
2462
+ /* isPartialEvent */
2463
+ !0
2464
+ );
2465
+ const O = rn(d, r, v);
2466
+ nt(v, O, !0);
2467
+ } catch (w) {
2468
+ if (this.context.log?.warn?.("Failed to apply mutation event", w), u === void 0) {
2469
+ const v = this.entityMap.getEntity(c);
2470
+ v !== void 0 && v.evict();
2471
+ }
2472
+ return;
2473
+ }
2474
+ const h = this.entityMap.getEntity(c);
2475
+ if (h === void 0) return;
2476
+ this.entityMap.save(h);
2477
+ const g = u === void 0;
2478
+ let p = !1;
2479
+ this.routeEvent(i, h.data, c, n, f, () => {
2480
+ p = !0;
2481
+ }), g && !p && h.evict();
2482
+ }
2483
+ // ======================================================
2484
+ // Query Invalidation
2485
+ // ======================================================
2486
+ invalidateQueries(t) {
2487
+ for (const n of t) {
2488
+ const i = Array.isArray(n), r = i ? n[0] : n, s = i ? n[1] : void 0, a = G.for(r).statics.id;
2489
+ for (const [, c] of this.queryInstances)
2490
+ c.def.statics.id === a && (s === void 0 || Nn(c.resolvedParams, s)) && c.markStale();
2491
+ }
2492
+ }
2493
+ // ======================================================
2494
+ // In-Memory GC
2495
+ // ======================================================
2496
+ handleEviction = (t, n) => {
2497
+ if (n === X.Query) {
2498
+ const r = this.queryInstances.get(t);
2499
+ if (r === void 0) return;
2500
+ r.rootEntity?.evict(), this.queryInstances.delete(t);
2501
+ return;
2502
+ }
2503
+ const i = this.entityMap.getEntity(t);
2504
+ i !== void 0 && i.evict();
2505
+ };
2506
+ // ======================================================
2507
+ // Constraint Registry (Live Data)
2508
+ // ======================================================
2509
+ getOrCreateMatcher(t) {
2510
+ let n = this.constraintRegistry.get(t);
2511
+ return n === void 0 && (n = new Cn(), this.constraintRegistry.set(t, n)), n;
2512
+ }
2513
+ registerLiveCollection(t) {
2514
+ for (const [n, i] of t._entityDefsByTypename)
2515
+ this.registerEntityDef(i), this.getOrCreateMatcher(n).registerBinding(t, n);
2516
+ }
2517
+ unregisterLiveCollection(t) {
2518
+ for (const n of t._entityDefsByTypename.keys()) {
2519
+ const i = this.constraintRegistry.get(n);
2520
+ i !== void 0 && i.unregisterBinding(t, n);
2521
+ }
2522
+ }
2523
+ routeEvent(t, n, i, r, s, o, a) {
2524
+ const c = this.constraintRegistry.get(t);
2525
+ if (c === void 0) return;
2526
+ const f = s !== void 0 ? { ...n, [ue]: s } : n;
2527
+ c.routeEvent(t, f, i, r, o, a);
2528
+ }
2529
+ destroy() {
2530
+ this.networkUnsubscribe?.(), this.gcManager.destroy(), this.networkManager.destroy();
2531
+ for (const t of this.controllers.values())
2532
+ t.destroy?.();
2533
+ this.controllers.clear(), this.queryInstances.clear(), this.mutationInstances.clear(), this.constraintRegistry.clear(), this.typenameRegistry.clear(), this.mergedDefCache.clear();
2534
+ }
2535
+ }
2536
+ const An = jt(void 0);
2537
+ function Nn(e, t) {
2538
+ if (e === void 0) return !1;
2539
+ for (const n in t)
2540
+ if (e[n] !== t[n]) return !1;
2541
+ return !0;
2542
+ }
2543
+ export {
2544
+ D as A,
2545
+ Bt as E,
2546
+ De as G,
2547
+ B as L,
2548
+ l as M,
2549
+ ae as N,
2550
+ Lt as Q,
2551
+ F as R,
2552
+ m as V,
2553
+ ut as a,
2554
+ Tn as b,
2555
+ V as c,
2556
+ Fe as d,
2557
+ ce as e,
2558
+ Pn as f,
2559
+ Un as g,
2560
+ An as h,
2561
+ pn as i,
2562
+ kn as j,
2563
+ $n as k,
2564
+ Kt as l,
2565
+ Te as m,
2566
+ J as n,
2567
+ Ln as o,
2568
+ jn as q,
2569
+ Zt as r,
2570
+ en as t
2571
+ };
2572
+ //# sourceMappingURL=QueryClient-Dtde3pss.js.map