cogsbox-state 0.5.472 → 0.5.473

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 (45) hide show
  1. package/dist/CogsState.d.ts +98 -82
  2. package/dist/CogsState.d.ts.map +1 -1
  3. package/dist/CogsState.jsx +1020 -950
  4. package/dist/CogsState.jsx.map +1 -1
  5. package/dist/Components.d.ts.map +1 -1
  6. package/dist/Components.jsx +276 -217
  7. package/dist/Components.jsx.map +1 -1
  8. package/dist/PluginRunner.d.ts +10 -0
  9. package/dist/PluginRunner.d.ts.map +1 -0
  10. package/dist/PluginRunner.jsx +128 -0
  11. package/dist/PluginRunner.jsx.map +1 -0
  12. package/dist/index.d.ts +2 -0
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +33 -26
  15. package/dist/index.js.map +1 -1
  16. package/dist/pluginStore.d.ts +43 -0
  17. package/dist/pluginStore.d.ts.map +1 -0
  18. package/dist/pluginStore.js +52 -0
  19. package/dist/pluginStore.js.map +1 -0
  20. package/dist/plugins.d.ts +1326 -0
  21. package/dist/plugins.d.ts.map +1 -0
  22. package/dist/plugins.js +76 -0
  23. package/dist/plugins.js.map +1 -0
  24. package/dist/store.d.ts +50 -15
  25. package/dist/store.d.ts.map +1 -1
  26. package/dist/store.js +509 -470
  27. package/dist/store.js.map +1 -1
  28. package/dist/utility.d.ts +1 -1
  29. package/dist/utility.d.ts.map +1 -1
  30. package/dist/utility.js +12 -12
  31. package/dist/utility.js.map +1 -1
  32. package/dist/validation.d.ts +7 -0
  33. package/dist/validation.d.ts.map +1 -0
  34. package/dist/validation.js +39 -0
  35. package/dist/validation.js.map +1 -0
  36. package/package.json +13 -3
  37. package/src/CogsState.tsx +658 -457
  38. package/src/Components.tsx +272 -194
  39. package/src/PluginRunner.tsx +208 -0
  40. package/src/index.ts +2 -0
  41. package/src/pluginStore.ts +159 -0
  42. package/src/plugins.ts +548 -0
  43. package/src/store.ts +748 -493
  44. package/src/utility.ts +31 -31
  45. package/src/validation.ts +84 -0
@@ -1,812 +1,865 @@
1
1
  "use client";
2
- import { jsx as ne, Fragment as Pe } from "react/jsx-runtime";
3
- import { useState as te, useRef as R, useEffect as q, useCallback as we, useLayoutEffect as ce, useMemo as pe, createElement as Ae, startTransition as Ce } from "react";
4
- import { transformStateFunc as De, isFunction as z, isDeepEqual as oe } from "./utility.js";
5
- import { ValidationWrapper as Oe, IsolatedComponentWrapper as Ue, FormElementWrapper as Fe, MemoizedCogsItemWrapper as je } from "./Components.jsx";
6
- import Ne from "superjson";
7
- import { v4 as ee } from "uuid";
8
- import { getGlobalStore as T, formRefStore as Me } from "./store.js";
9
- import { useCogsConfig as _e } from "./CogsStateClient.jsx";
2
+ import { jsx as ne, Fragment as Ue } from "react/jsx-runtime";
3
+ import { pluginStore as Q } from "./pluginStore.js";
4
+ import { useState as ee, useRef as F, useCallback as Te, useEffect as z, useLayoutEffect as ce, useMemo as Me, createElement as Ee, startTransition as _e } from "react";
5
+ import { transformStateFunc as Oe, isFunction as G, isDeepEqual as le, isArray as je, getDifferences as Ne } from "./utility.js";
6
+ import { ValidationWrapper as Fe, IsolatedComponentWrapper as xe, FormElementWrapper as Re, MemoizedCogsItemWrapper as Be } from "./Components.jsx";
7
+ import Le from "superjson";
8
+ import { v4 as ae } from "uuid";
9
+ import { getGlobalStore as T, updateShadowTypeInfo as Ie } from "./store.js";
10
+ import { useCogsConfig as Pe } from "./CogsStateClient.jsx";
11
+ import { runValidation as ze } from "./validation.js";
10
12
  const {
11
13
  getInitialOptions: W,
12
14
  updateInitialStateGlobal: ke,
13
- getShadowMetadata: V,
15
+ getShadowMetadata: b,
14
16
  setShadowMetadata: J,
15
- getShadowValue: N,
17
+ getShadowValue: x,
16
18
  initializeShadowState: se,
17
- updateShadowAtPath: Re,
18
- insertShadowArrayElement: xe,
19
- insertManyShadowArrayElements: Le,
20
- removeShadowArrayElement: Be,
21
- setInitialStateOptions: le,
22
- setServerStateUpdate: Ee,
23
- markAsDirty: Ie,
24
- addPathComponent: qe,
25
- clearSelectedIndexesForState: We,
26
- addStateLog: Ge,
27
- setSyncInfo: He,
28
- clearSelectedIndex: ze,
29
- getSyncInfo: Je,
30
- notifyPathSubscribers: Ye
19
+ initializeAndMergeShadowState: We,
20
+ updateShadowAtPath: qe,
21
+ insertShadowArrayElement: He,
22
+ insertManyShadowArrayElements: Ce,
23
+ removeShadowArrayElement: Ge,
24
+ setInitialStateOptions: ue,
25
+ setServerStateUpdate: $e,
26
+ markAsDirty: ge,
27
+ addPathComponent: Je,
28
+ clearSelectedIndexesForState: Ye,
29
+ addStateLog: Ze,
30
+ setSyncInfo: Pt,
31
+ clearSelectedIndex: Qe,
32
+ getSyncInfo: Xe,
33
+ notifyPathSubscribers: Ke,
34
+ getPluginMetaDataMap: et,
35
+ setPluginMetaData: tt,
36
+ removePluginMetaData: rt
31
37
  // Note: The old functions are no longer imported under their original names
32
- } = T.getState();
33
- function B(e, r, d) {
34
- const s = V(e, r);
38
+ } = T.getState(), { notifyUpdate: nt } = Q.getState();
39
+ function L(e, n, u) {
40
+ const s = b(e, n);
35
41
  if (!!!s?.arrayKeys)
36
- return { isArray: !1, value: T.getState().getShadowValue(e, r), keys: [] };
37
- const i = r.length > 0 ? r.join(".") : "root", S = d?.arrayViews?.[i] ?? s.arrayKeys;
38
- return Array.isArray(S) && S.length === 0 ? { isArray: !0, value: [], keys: [] } : { isArray: !0, value: T.getState().getShadowValue(e, r, S), keys: S ?? [] };
42
+ return { isArray: !1, value: T.getState().getShadowValue(e, n), keys: [] };
43
+ const i = n.length > 0 ? n.join(".") : "root", h = u?.arrayViews?.[i] ?? s.arrayKeys;
44
+ return Array.isArray(h) && h.length === 0 ? { isArray: !0, value: [], keys: [] } : { isArray: !0, value: T.getState().getShadowValue(e, n, h), keys: h ?? [] };
39
45
  }
40
- function me(e, r, d) {
46
+ function ve(e, n, u) {
41
47
  for (let s = 0; s < e.length; s++)
42
- if (d(e[s], s)) {
43
- const f = r[s];
44
- if (f)
45
- return { key: f, index: s, value: e[s] };
48
+ if (u(e[s], s)) {
49
+ const g = n[s];
50
+ if (g)
51
+ return { key: g, index: s, value: e[s] };
46
52
  }
47
53
  return null;
48
54
  }
49
- function he(e, r) {
50
- const d = W(e) || {};
51
- le(e, {
52
- ...d,
53
- ...r
54
- });
55
+ function we(e, n) {
56
+ const s = {
57
+ ...W(e) || {},
58
+ ...n
59
+ };
60
+ (s.validation?.zodSchemaV4 || s.validation?.zodSchemaV3) && !s.validation?.onBlur && (s.validation.onBlur = "error"), ue(e, s);
55
61
  }
56
- function $e({
62
+ function Ae({
57
63
  stateKey: e,
58
- options: r,
59
- initialOptionsPart: d
64
+ options: n,
65
+ initialOptionsPart: u
60
66
  }) {
61
- const s = W(e) || {};
62
- let i = { ...d[e] || {}, ...s }, S = !1;
63
- if (r) {
64
- const l = (t, n) => {
65
- for (const u in n)
66
- n.hasOwnProperty(u) && (n[u] instanceof Object && !Array.isArray(n[u]) && t[u] instanceof Object ? oe(t[u], n[u]) || (l(t[u], n[u]), S = !0) : t[u] !== n[u] && (t[u] = n[u], S = !0));
67
+ const s = W(e) || {}, g = u[e] || {};
68
+ let i = { ...g, ...s }, h = !1;
69
+ if (n) {
70
+ const l = (t, a) => {
71
+ for (const S in a)
72
+ a.hasOwnProperty(S) && (a[S] instanceof Object && !Array.isArray(a[S]) && t[S] instanceof Object ? le(t[S], a[S]) || (l(t[S], a[S]), h = !0) : t[S] !== a[S] && (t[S] = a[S], h = !0));
67
73
  return t;
68
74
  };
69
- i = l(i, r);
75
+ i = l(i, n);
70
76
  }
71
- i.syncOptions && (!r || !r.hasOwnProperty("syncOptions")) && (S = !0), (i.validation && i?.validation?.zodSchemaV4 || i?.validation?.zodSchemaV3) && (r?.validation?.hasOwnProperty("onBlur") || s?.validation?.hasOwnProperty("onBlur") || (i.validation.onBlur = "error")), S && le(e, i);
77
+ if (i.validation && (n?.validation?.hasOwnProperty("onBlur") || s?.validation?.hasOwnProperty("onBlur") || g?.validation?.hasOwnProperty("onBlur") || (i.validation.onBlur = "error", h = !0)), h) {
78
+ ue(e, i);
79
+ const l = s?.validation?.zodSchemaV4 || s?.validation?.zodSchemaV3, t = i.validation?.zodSchemaV4 && !s?.validation?.zodSchemaV4, a = i.validation?.zodSchemaV3 && !s?.validation?.zodSchemaV3;
80
+ !l && (t || a) && (t ? Ie(
81
+ e,
82
+ i.validation.zodSchemaV4,
83
+ "zod4"
84
+ ) : a && Ie(
85
+ e,
86
+ i.validation.zodSchemaV3,
87
+ "zod3"
88
+ ), K(e));
89
+ }
90
+ return i;
72
91
  }
73
- function wt(e, { formElements: r, validation: d }) {
74
- return { initialState: e, formElements: r, validation: d };
92
+ function kt(e, n) {
93
+ return {
94
+ ...n,
95
+ initialState: e,
96
+ _addStateOptions: !0
97
+ };
75
98
  }
76
- const Ze = (e, r) => {
77
- let d = e;
78
- const [s, f] = De(d);
79
- r?.__fromSyncSchema && r?.__syncNotifications && T.getState().setInitialStateOptions("__notifications", r.__syncNotifications), r?.__fromSyncSchema && r?.__apiParamsMap && T.getState().setInitialStateOptions("__apiParamsMap", r.__apiParamsMap), Object.keys(s).forEach((l) => {
80
- let t = f[l] || {};
81
- const n = {
99
+ const Ct = (e, n) => {
100
+ n?.plugins && Q.getState().setRegisteredPlugins(n.plugins);
101
+ const [u, s] = Oe(e);
102
+ Object.keys(u).forEach((l) => {
103
+ let t = s[l] || {};
104
+ const a = {
82
105
  ...t
83
106
  };
84
- if (r?.formElements && (n.formElements = {
85
- ...r.formElements,
107
+ n?.formElements && (a.formElements = {
108
+ ...n.formElements,
86
109
  ...t.formElements || {}
87
- }), r?.validation && (n.validation = {
88
- ...r.validation,
110
+ }), a.validation = {
111
+ onBlur: "error",
112
+ ...n?.validation,
89
113
  ...t.validation || {}
90
- }, r.validation.key && !t.validation?.key && (n.validation.key = `${r.validation.key}.${l}`)), r?.__syncSchemas?.[l]?.schemas?.validation && (n.validation = {
91
- zodSchemaV4: r.__syncSchemas[l].schemas.validation,
92
- ...t.validation
93
- }), Object.keys(n).length > 0) {
94
- const u = W(l);
95
- u ? le(l, {
96
- ...u,
97
- ...n
98
- }) : le(l, n);
99
- }
100
- }), Object.keys(s).forEach((l) => {
101
- se(l, s[l]);
114
+ }, n?.validation?.key && !t.validation?.key && (a.validation.key = `${n.validation.key}.${l}`);
115
+ const S = W(l), M = S ? {
116
+ ...S,
117
+ ...a,
118
+ formElements: {
119
+ ...S.formElements,
120
+ ...a.formElements
121
+ },
122
+ validation: {
123
+ ...S.validation,
124
+ ...a.validation
125
+ }
126
+ } : a;
127
+ ue(l, M);
128
+ }), Object.keys(u).forEach((l) => {
129
+ se(l, u[l]);
102
130
  });
103
- const i = (l, t) => {
104
- const [n] = te(t?.componentId ?? ee());
105
- $e({
131
+ const g = (l, t) => {
132
+ const [a] = ee(t?.componentId ?? ae()), S = Ae({
106
133
  stateKey: l,
107
134
  options: t,
108
- initialOptionsPart: f
109
- });
110
- const u = N(l, []) || s[l], C = t?.modifyState ? t.modifyState(u) : u;
111
- return it(C, {
135
+ initialOptionsPart: s
136
+ }), M = F(S);
137
+ M.current = S;
138
+ const E = x(l, []) || u[l], P = yt(E, {
112
139
  stateKey: l,
113
140
  syncUpdate: t?.syncUpdate,
114
- componentId: n,
141
+ componentId: a,
115
142
  localStorage: t?.localStorage,
116
143
  middleware: t?.middleware,
117
144
  reactiveType: t?.reactiveType,
118
145
  reactiveDeps: t?.reactiveDeps,
119
146
  defaultState: t?.defaultState,
120
147
  dependencies: t?.dependencies,
121
- serverState: t?.serverState,
122
- syncOptions: t?.syncOptions,
123
- __useSync: r?.__useSync
148
+ serverState: t?.serverState
124
149
  });
150
+ return z(() => {
151
+ t && Q.getState().setPluginOptionsForState(l, t);
152
+ }, [l, t]), z(() => (console.log("adding handler 1", l, P), Q.getState().stateHandlers.set(l, P), () => {
153
+ Q.getState().stateHandlers.delete(l);
154
+ }), [l, P]), P;
125
155
  };
126
- function S(l, t) {
127
- $e({ stateKey: l, options: t, initialOptionsPart: f }), t.localStorage && Xe(l, t), ie(l);
156
+ function i(l, t) {
157
+ Ae({ stateKey: l, options: t, initialOptionsPart: s }), t.localStorage && at(l, t), K(l);
128
158
  }
129
- return { useCogsState: i, setCogsOptions: S };
130
- };
131
- function pt(e, r) {
132
- const d = e.schemas, s = {}, f = {};
133
- for (const i in d) {
134
- const S = d[i];
135
- S?.relations && S?.schemas?.defaults ? s[i] = S.schemas.defaults : s[i] = S?.schemas?.defaults || {}, console.log("initialState", s), S?.api?.queryData?._paramType && (f[i] = S.api.queryData._paramType);
159
+ function h(l) {
160
+ const a = Q.getState().registeredPlugins.map((M) => l.hasOwnProperty(M.name) ? {
161
+ ...M,
162
+ formWrapper: l[M.name]
163
+ } : M);
164
+ Q.getState().setRegisteredPlugins(a), Object.keys(u).forEach((M) => {
165
+ const E = W(M) || {}, P = {
166
+ ...E,
167
+ formElements: {
168
+ ...E.formElements || {},
169
+ ...l
170
+ }
171
+ };
172
+ ue(M, P);
173
+ });
136
174
  }
137
- return Ze(s, {
138
- __fromSyncSchema: !0,
139
- __syncNotifications: e.notifications,
140
- __apiParamsMap: f,
141
- __useSync: r,
142
- __syncSchemas: d
143
- });
144
- }
145
- const Qe = (e, r, d, s, f) => {
146
- d?.log && console.log(
175
+ return {
176
+ useCogsState: g,
177
+ setCogsOptionsByKey: i,
178
+ setCogsFormElements: h
179
+ };
180
+ }, ot = (e, n, u, s, g) => {
181
+ u?.log && console.log(
147
182
  "saving to localstorage",
148
- r,
149
- d.localStorage?.key,
183
+ n,
184
+ u.localStorage?.key,
150
185
  s
151
186
  );
152
- const i = z(d?.localStorage?.key) ? d.localStorage?.key(e) : d?.localStorage?.key;
187
+ const i = G(u?.localStorage?.key) ? u.localStorage?.key(e) : u?.localStorage?.key;
153
188
  if (i && s) {
154
- const S = `${s}-${r}-${i}`;
189
+ const h = `${s}-${n}-${i}`;
155
190
  let l;
156
191
  try {
157
- l = fe(S)?.lastSyncedWithServer;
192
+ l = Se(h)?.lastSyncedWithServer;
158
193
  } catch {
159
194
  }
160
- const t = V(r, []), n = {
195
+ const t = b(n, []), a = {
161
196
  state: e,
162
197
  lastUpdated: Date.now(),
163
198
  lastSyncedWithServer: l,
164
199
  stateSource: t?.stateSource,
165
200
  baseServerState: t?.baseServerState
166
- }, u = Ne.serialize(n);
201
+ }, S = Le.serialize(a);
167
202
  window.localStorage.setItem(
168
- S,
169
- JSON.stringify(u.json)
203
+ h,
204
+ JSON.stringify(S.json)
170
205
  );
171
206
  }
172
- }, fe = (e) => {
207
+ }, Se = (e) => {
173
208
  if (!e) return null;
174
209
  try {
175
- const r = window.localStorage.getItem(e);
176
- return r ? JSON.parse(r) : null;
177
- } catch (r) {
178
- return console.error("Error loading from localStorage:", r), null;
210
+ const n = window.localStorage.getItem(e);
211
+ return n ? JSON.parse(n) : null;
212
+ } catch (n) {
213
+ return console.error("Error loading from localStorage:", n), null;
179
214
  }
180
- }, Xe = (e, r) => {
181
- const d = N(e, []), { sessionId: s } = _e(), f = z(r?.localStorage?.key) ? r.localStorage.key(d) : r?.localStorage?.key;
182
- if (f && s) {
183
- const i = fe(
184
- `${s}-${e}-${f}`
215
+ }, at = (e, n) => {
216
+ const u = x(e, []), { sessionId: s } = Pe(), g = G(n?.localStorage?.key) ? n.localStorage.key(u) : n?.localStorage?.key;
217
+ if (g && s) {
218
+ const i = Se(
219
+ `${s}-${e}-${g}`
185
220
  );
186
221
  if (i && i.lastUpdated > (i.lastSyncedWithServer || 0))
187
- return ie(e), !0;
222
+ return K(e), !0;
188
223
  }
189
224
  return !1;
190
- }, ie = (e) => {
191
- const r = V(e, []);
192
- if (!r) return;
193
- const d = /* @__PURE__ */ new Set();
194
- r?.components?.forEach((s) => {
195
- (s ? Array.isArray(s.reactiveType) ? s.reactiveType : [s.reactiveType || "component"] : null)?.includes("none") || d.add(() => s.forceUpdate());
225
+ }, K = (e) => {
226
+ const n = b(e, []);
227
+ if (!n) return;
228
+ const u = /* @__PURE__ */ new Set();
229
+ n?.components?.forEach((s) => {
230
+ (s ? Array.isArray(s.reactiveType) ? s.reactiveType : [s.reactiveType || "component"] : null)?.includes("none") || u.add(() => s.forceUpdate());
196
231
  }), queueMicrotask(() => {
197
- d.forEach((s) => s());
232
+ u.forEach((s) => s());
198
233
  });
199
234
  };
200
- function ue(e, r, d, s) {
201
- const f = V(e, r);
202
- if (J(e, r, {
203
- ...f,
235
+ function de(e, n, u, s) {
236
+ const g = b(e, n);
237
+ if (J(e, n, {
238
+ ...g,
204
239
  isDirty: !1,
205
240
  stateSource: "server",
206
241
  lastServerSync: s || Date.now()
207
- }), Array.isArray(d)) {
208
- const i = V(e, r);
209
- i?.arrayKeys && i.arrayKeys.forEach((S, l) => {
210
- const t = [...r, S], n = d[l];
211
- n !== void 0 && ue(
242
+ }), Array.isArray(u)) {
243
+ const i = b(e, n);
244
+ i?.arrayKeys && i.arrayKeys.forEach((h, l) => {
245
+ const t = [...n, h], a = u[l];
246
+ a !== void 0 && de(
212
247
  e,
213
248
  t,
214
- n,
249
+ a,
215
250
  s
216
251
  );
217
252
  });
218
- } else d && typeof d == "object" && d.constructor === Object && Object.keys(d).forEach((i) => {
219
- const S = [...r, i], l = d[i];
220
- ue(e, S, l, s);
253
+ } else u && typeof u == "object" && u.constructor === Object && Object.keys(u).forEach((i) => {
254
+ const h = [...n, i], l = u[i];
255
+ de(e, h, l, s);
221
256
  });
222
257
  }
223
- let de = [], Te = !1;
224
- function Ke() {
225
- Te || (Te = !0, queueMicrotask(ot));
226
- }
227
- function et(e, r, d) {
228
- const s = T.getState().getShadowValue(e, r), f = z(d) ? d(s) : d;
229
- Re(e, r, f), Ie(e, r, { bubble: !0 });
230
- const i = V(e, r);
231
- return {
232
- type: "update",
233
- oldValue: s,
234
- newValue: f,
235
- shadowMeta: i
236
- };
258
+ let fe = [], be = !1;
259
+ function st() {
260
+ be || (be = !0, console.log("Scheduling flush"), queueMicrotask(() => {
261
+ console.log("Actually flushing"), gt();
262
+ }));
237
263
  }
238
- function tt(e, r) {
239
- e?.signals?.length && e.signals.forEach(({ parentId: d, position: s, effect: f }) => {
240
- const i = document.querySelector(`[data-parent-id="${d}"]`);
264
+ function it(e, n) {
265
+ e?.signals?.length && e.signals.forEach(({ parentId: u, position: s, effect: g }) => {
266
+ const i = document.querySelector(`[data-parent-id="${u}"]`);
241
267
  if (!i) return;
242
- const S = Array.from(i.childNodes);
243
- if (!S[s]) return;
244
- let l = r;
245
- if (f && r !== null)
268
+ const h = Array.from(i.childNodes);
269
+ if (!h[s]) return;
270
+ let l = n;
271
+ if (g && n !== null)
246
272
  try {
247
- l = new Function("state", `return (${f})(state)`)(
248
- r
273
+ l = new Function("state", `return (${g})(state)`)(
274
+ n
249
275
  );
250
276
  } catch (t) {
251
277
  console.error("Error evaluating effect function:", t);
252
278
  }
253
- l !== null && typeof l == "object" && (l = JSON.stringify(l)), S[s].textContent = String(l ?? "");
279
+ l !== null && typeof l == "object" && (l = JSON.stringify(l)), h[s].textContent = String(l ?? "");
254
280
  });
255
281
  }
256
- function rt(e, r, d) {
257
- const s = V(e, []);
282
+ function ct(e, n, u) {
283
+ const s = b(e, []);
258
284
  if (!s?.components)
259
285
  return /* @__PURE__ */ new Set();
260
- const f = /* @__PURE__ */ new Set();
261
- let i = r;
262
- d.type === "insert" && d.itemId && (i = r);
263
- let S = [...i];
264
- for (; ; ) {
265
- const l = V(e, S);
266
- if (l?.pathComponents && l.pathComponents.forEach((t) => {
267
- const n = s.components?.get(t);
268
- n && ((Array.isArray(n.reactiveType) ? n.reactiveType : [n.reactiveType || "component"]).includes("none") || f.add(n));
269
- }), S.length === 0) break;
270
- S.pop();
271
- }
272
- return s.components.forEach((l, t) => {
273
- if (f.has(l))
274
- return;
275
- const n = Array.isArray(l.reactiveType) ? l.reactiveType : [l.reactiveType || "component"];
276
- if (n.includes("all"))
277
- f.add(l);
278
- else if (n.includes("deps") && l.depsFunction) {
279
- const u = N(e, []), C = l.depsFunction(u);
280
- (C === !0 || Array.isArray(C) && !oe(l.prevDeps, C)) && (l.prevDeps = C, f.add(l));
286
+ const g = /* @__PURE__ */ new Set();
287
+ if (u.type === "update") {
288
+ let i = [...n];
289
+ for (; ; ) {
290
+ const h = b(e, i);
291
+ if (h?.pathComponents && h.pathComponents.forEach((l) => {
292
+ const t = s.components?.get(l);
293
+ t && ((Array.isArray(t.reactiveType) ? t.reactiveType : [t.reactiveType || "component"]).includes("none") || g.add(t));
294
+ }), i.length === 0) break;
295
+ i.pop();
296
+ }
297
+ u.newValue && typeof u.newValue == "object" && !je(u.newValue) && Ne(u.newValue, u.oldValue).forEach((l) => {
298
+ const t = l.split("."), a = [...n, ...t], S = b(e, a);
299
+ S?.pathComponents && S.pathComponents.forEach((M) => {
300
+ const E = s.components?.get(M);
301
+ E && ((Array.isArray(E.reactiveType) ? E.reactiveType : [E.reactiveType || "component"]).includes("none") || g.add(E));
302
+ });
303
+ });
304
+ } else if (u.type === "insert" || u.type === "cut" || u.type === "insert_many") {
305
+ let h = [...u.type === "insert" ? n : n.slice(0, -1)];
306
+ for (; ; ) {
307
+ const l = b(e, h);
308
+ if (l?.pathComponents && l.pathComponents.forEach((t) => {
309
+ const a = s.components?.get(t);
310
+ a && g.add(a);
311
+ }), h.length === 0) break;
312
+ h.pop();
281
313
  }
282
- }), f;
314
+ }
315
+ return g;
316
+ }
317
+ function lt(e, n, u) {
318
+ const s = T.getState().getShadowValue(e, n), g = G(u) ? u(s) : u;
319
+ qe(e, n, g), ge(e, n, { bubble: !0 });
320
+ const i = b(e, n);
321
+ return {
322
+ type: "update",
323
+ oldValue: s,
324
+ newValue: g,
325
+ shadowMeta: i
326
+ };
327
+ }
328
+ function ut(e, n, u) {
329
+ Ce(e, n, u), ge(e, n, { bubble: !0 });
330
+ const s = b(e, n);
331
+ return {
332
+ type: "insert_many",
333
+ count: u.length,
334
+ shadowMeta: s,
335
+ path: n
336
+ };
283
337
  }
284
- function nt(e, r, d, s) {
285
- let f;
286
- if (z(d)) {
287
- const { value: t } = K(e, r);
288
- f = d({ state: t, uuid: ee() });
338
+ function dt(e, n, u, s, g) {
339
+ let i;
340
+ if (G(u)) {
341
+ const { value: a } = X(e, n);
342
+ i = u({ state: a });
289
343
  } else
290
- f = d;
291
- const i = xe(e, r, f, s);
292
- Ie(e, r, { bubble: !0 });
293
- const S = V(e, r);
294
- let l;
295
- return S?.arrayKeys, {
344
+ i = u;
345
+ const h = He(
346
+ e,
347
+ n,
348
+ i,
349
+ s,
350
+ g
351
+ );
352
+ ge(e, n, { bubble: !0 });
353
+ const l = b(e, n);
354
+ let t;
355
+ return l?.arrayKeys && s !== void 0 && s > 0 && (t = l.arrayKeys[s - 1]), {
296
356
  type: "insert",
297
- newValue: f,
298
- shadowMeta: S,
299
- path: r,
300
- // Just the array path now
301
- itemId: i,
302
- insertAfterId: l
357
+ newValue: i,
358
+ shadowMeta: l,
359
+ path: n,
360
+ itemId: h,
361
+ insertAfterId: t
303
362
  };
304
363
  }
305
- function at(e, r) {
306
- const d = r.slice(0, -1), s = N(e, r);
307
- return Be(e, r), Ie(e, d, { bubble: !0 }), { type: "cut", oldValue: s, parentPath: d };
364
+ function ft(e, n) {
365
+ const u = n.slice(0, -1), s = x(e, n);
366
+ return Ge(e, n), ge(e, u, { bubble: !0 }), { type: "cut", oldValue: s, parentPath: u };
308
367
  }
309
- function ot() {
310
- const e = /* @__PURE__ */ new Set(), r = [], d = [];
311
- for (const s of de) {
368
+ function gt() {
369
+ const e = /* @__PURE__ */ new Set(), n = [], u = [];
370
+ for (const s of fe) {
312
371
  if (s.status && s.updateType) {
313
- d.push(s);
372
+ u.push(s);
314
373
  continue;
315
374
  }
316
- const f = s, i = f.type === "cut" ? null : f.newValue;
317
- f.shadowMeta?.signals?.length > 0 && r.push({ shadowMeta: f.shadowMeta, displayValue: i }), rt(
318
- f.stateKey,
319
- f.path,
320
- f
375
+ const g = s, i = g.type === "cut" ? null : g.newValue;
376
+ g.shadowMeta?.signals?.length > 0 && n.push({ shadowMeta: g.shadowMeta, displayValue: i }), ct(
377
+ g.stateKey,
378
+ g.path,
379
+ g
321
380
  ).forEach((l) => {
322
381
  e.add(l);
323
382
  });
324
383
  }
325
- d.length > 0 && Ge(d), r.forEach(({ shadowMeta: s, displayValue: f }) => {
326
- tt(s, f);
384
+ u.length > 0 && Ze(u), n.forEach(({ shadowMeta: s, displayValue: g }) => {
385
+ it(s, g);
327
386
  }), e.forEach((s) => {
328
387
  s.forceUpdate();
329
- }), de = [], Te = !1;
388
+ }), fe = [], be = !1;
330
389
  }
331
- function st(e, r, d, s) {
332
- return (i, S, l, t) => {
333
- f(e, S, i, l);
390
+ function St(e, n, u) {
391
+ return (g, i, h) => {
392
+ s(e, i, g, h);
334
393
  };
335
- function f(i, S, l, t) {
336
- let n;
337
- switch (t.updateType) {
394
+ function s(g, i, h, l) {
395
+ let t;
396
+ switch (l.updateType) {
338
397
  case "update":
339
- n = et(i, S, l);
398
+ t = lt(g, i, h);
340
399
  break;
341
400
  case "insert":
342
- n = nt(i, S, l);
401
+ t = dt(
402
+ g,
403
+ i,
404
+ h,
405
+ l.index,
406
+ l.itemId
407
+ );
408
+ break;
409
+ case "insert_many":
410
+ t = ut(g, i, h);
343
411
  break;
344
412
  case "cut":
345
- n = at(i, S);
413
+ t = ft(g, i);
346
414
  break;
347
415
  }
348
- n.stateKey = i, n.path = S, de.push(n), Ke();
349
- const u = {
416
+ t.stateKey = g, t.path = i, fe.push(t), st();
417
+ const a = {
350
418
  timeStamp: Date.now(),
351
- stateKey: i,
352
- path: S,
353
- updateType: t.updateType,
419
+ stateKey: g,
420
+ path: i,
421
+ updateType: l.updateType,
354
422
  status: "new",
355
- oldValue: n.oldValue,
356
- newValue: n.newValue ?? null,
357
- itemId: n.itemId,
358
- insertAfterId: n.insertAfterId
423
+ oldValue: t.oldValue,
424
+ newValue: t.newValue ?? null,
425
+ itemId: t.itemId,
426
+ insertAfterId: t.insertAfterId,
427
+ metaData: l.metaData
359
428
  };
360
- de.push(u), n.newValue !== void 0 && Qe(
361
- n.newValue,
362
- i,
363
- s.current,
364
- d
365
- ), s.current?.middleware && s.current.middleware({ update: u }), t.sync !== !1 && r.current?.connected && r.current.updateState({ operation: u });
429
+ fe.push(a), t.newValue !== void 0 && ot(
430
+ t.newValue,
431
+ g,
432
+ u.current,
433
+ n
434
+ ), u.current?.middleware && u.current.middleware({ update: a }), ze(a, l.validationTrigger || "programmatic"), nt(a);
366
435
  }
367
436
  }
368
- function it(e, {
369
- stateKey: r,
370
- localStorage: d,
437
+ function yt(e, {
438
+ stateKey: n,
439
+ localStorage: u,
371
440
  formElements: s,
372
- reactiveDeps: f,
441
+ reactiveDeps: g,
373
442
  reactiveType: i,
374
- componentId: S,
443
+ componentId: h,
375
444
  defaultState: l,
376
- syncUpdate: t,
377
- dependencies: n,
378
- serverState: u,
379
- __useSync: C
445
+ dependencies: t,
446
+ serverState: a
380
447
  } = {}) {
381
- const [x, O] = te({}), { sessionId: U } = _e();
382
- let G = !r;
383
- const [w] = te(r ?? ee()), m = R(S ?? ee()), Y = R(
448
+ const [S, M] = ee({}), { sessionId: E } = Pe();
449
+ let P = !n;
450
+ const [w] = ee(n ?? ae()), q = F(h ?? ae()), R = F(
384
451
  null
385
452
  );
386
- Y.current = W(w) ?? null, q(() => {
387
- if (t && t.stateKey === w && t.path?.[0]) {
388
- const g = `${t.stateKey}:${t.path.join(".")}`;
389
- He(g, {
390
- timeStamp: t.timeStamp,
391
- userId: t.userId
392
- });
393
- }
394
- }, [t]);
395
- const a = we(
396
- (g) => {
397
- const p = g ? { ...W(w), ...g } : W(w), I = p?.defaultState || l || e;
398
- if (p?.serverState?.status === "success" && p?.serverState?.data !== void 0)
453
+ R.current = W(w) ?? null;
454
+ const y = Te(
455
+ (o) => {
456
+ const d = o ? { ...W(w), ...o } : W(w), f = d?.defaultState || l || e;
457
+ if (d?.serverState?.status === "success" && d?.serverState?.data !== void 0)
399
458
  return {
400
- value: p.serverState.data,
459
+ value: d.serverState.data,
401
460
  source: "server",
402
- timestamp: p.serverState.timestamp || Date.now()
461
+ timestamp: d.serverState.timestamp || Date.now()
403
462
  };
404
- if (p?.localStorage?.key && U) {
405
- const b = z(p.localStorage.key) ? p.localStorage.key(I) : p.localStorage.key, _ = fe(
406
- `${U}-${w}-${b}`
463
+ if (d?.localStorage?.key && E) {
464
+ const v = G(d.localStorage.key) ? d.localStorage.key(f) : d.localStorage.key, p = Se(
465
+ `${E}-${w}-${v}`
407
466
  );
408
- if (_ && _.lastUpdated > (p?.serverState?.timestamp || 0))
467
+ if (p && p.lastUpdated > (d?.serverState?.timestamp || 0))
409
468
  return {
410
- value: _.state,
469
+ value: p.state,
411
470
  source: "localStorage",
412
- timestamp: _.lastUpdated
471
+ timestamp: p.lastUpdated
413
472
  };
414
473
  }
415
474
  return {
416
- value: I || e,
475
+ value: f || e,
417
476
  source: "default",
418
477
  timestamp: Date.now()
419
478
  };
420
479
  },
421
- [w, l, e, U]
480
+ [w, l, e, E]
422
481
  );
423
- q(() => {
424
- u && u.status === "success" && u.data !== void 0 && Ee(w, u);
425
- }, [u, w]), q(() => T.getState().subscribeToPath(w, (A) => {
426
- if (A?.type === "SERVER_STATE_UPDATE") {
427
- const p = A.serverState;
428
- if (p?.status !== "success" || p.data === void 0)
482
+ z(() => {
483
+ a && a.status === "success" && a.data !== void 0 && $e(w, a);
484
+ }, [a, w]), z(() => T.getState().subscribeToPath(w, (c) => {
485
+ if (c?.type === "SERVER_STATE_UPDATE") {
486
+ const d = c.serverState;
487
+ if (d?.status !== "success" || d.data === void 0)
429
488
  return;
430
- he(w, { serverState: p });
431
- const I = typeof p.merge == "object" ? p.merge : p.merge === !0 ? { strategy: "append", key: "id" } : null, M = N(w, []), b = p.data;
432
- if (I && I.strategy === "append" && "key" in I && Array.isArray(M) && Array.isArray(b)) {
433
- const _ = I.key;
434
- if (!_) {
489
+ we(w, { serverState: d });
490
+ const f = typeof d.merge == "object" ? d.merge : d.merge === !0 ? { strategy: "append", key: "id" } : null, m = x(w, []), v = d.data;
491
+ if (f && f.strategy === "append" && "key" in f && Array.isArray(m) && Array.isArray(v)) {
492
+ const p = f.key;
493
+ if (!p) {
435
494
  console.error(
436
495
  "CogsState: Merge strategy 'append' requires a 'key' field."
437
496
  );
438
497
  return;
439
498
  }
440
- const E = new Set(
441
- M.map((Z) => Z[_])
442
- ), $ = b.filter(
443
- (Z) => !E.has(Z[_])
499
+ const A = new Set(
500
+ m.map((U) => U[p])
501
+ ), $ = v.filter(
502
+ (U) => !A.has(U[p])
444
503
  );
445
- $.length > 0 && Le(w, [], $);
446
- const F = N(w, []);
447
- ue(
504
+ $.length > 0 && Ce(w, [], $);
505
+ const D = x(w, []);
506
+ de(
448
507
  w,
449
508
  [],
450
- F,
451
- p.timestamp || Date.now()
509
+ D,
510
+ d.timestamp || Date.now()
452
511
  );
453
512
  } else
454
- se(w, b), ue(
513
+ se(w, v), de(
455
514
  w,
456
515
  [],
457
- b,
458
- p.timestamp || Date.now()
516
+ v,
517
+ d.timestamp || Date.now()
459
518
  );
460
- ie(w);
519
+ K(w);
461
520
  }
462
- }), [w]), q(() => {
463
- const g = T.getState().getShadowMetadata(w, []);
464
- if (g && g.stateSource)
521
+ }), [w]), z(() => {
522
+ const o = T.getState().getShadowMetadata(w, []);
523
+ if (o && o.stateSource)
465
524
  return;
466
- const A = W(w), p = {
467
- syncEnabled: !!y && !!v,
468
- validationEnabled: !!(A?.validation?.zodSchemaV4 || A?.validation?.zodSchemaV3),
469
- localStorageEnabled: !!A?.localStorage?.key
525
+ const c = W(w), d = {
526
+ localStorageEnabled: !!c?.localStorage?.key
470
527
  };
471
528
  if (J(w, [], {
472
- ...g,
473
- features: p
474
- }), A?.defaultState !== void 0 || l !== void 0) {
475
- const _ = A?.defaultState || l;
476
- A?.defaultState || he(w, {
477
- defaultState: _
529
+ ...o,
530
+ features: d
531
+ }), c?.defaultState !== void 0 || l !== void 0) {
532
+ const p = c?.defaultState || l;
533
+ c?.defaultState || we(w, {
534
+ defaultState: p
478
535
  });
479
536
  }
480
- const { value: I, source: M, timestamp: b } = a();
481
- se(w, I), J(w, [], {
482
- stateSource: M,
483
- lastServerSync: M === "server" ? b : void 0,
484
- isDirty: M === "server" ? !1 : void 0,
485
- baseServerState: M === "server" ? I : void 0
486
- }), M === "server" && u && Ee(w, u), ie(w);
487
- }, [w, ...n || []]), ce(() => {
488
- G && he(w, {
537
+ const { value: f, source: m, timestamp: v } = y();
538
+ se(w, f), J(w, [], {
539
+ stateSource: m,
540
+ lastServerSync: m === "server" ? v : void 0,
541
+ isDirty: m === "server" ? !1 : void 0,
542
+ baseServerState: m === "server" ? f : void 0
543
+ }), m === "server" && a && $e(w, a), K(w);
544
+ }, [w, ...t || []]), ce(() => {
545
+ P && we(w, {
489
546
  formElements: s,
490
547
  defaultState: l,
491
- localStorage: d,
492
- middleware: Y.current?.middleware
548
+ localStorage: u,
549
+ middleware: R.current?.middleware
493
550
  });
494
- const g = `${w}////${m.current}`, A = V(w, []), p = A?.components || /* @__PURE__ */ new Map();
495
- return p.set(g, {
496
- forceUpdate: () => O({}),
551
+ const o = `${w}////${q.current}`, c = b(w, []), d = c?.components || /* @__PURE__ */ new Map();
552
+ return d.set(o, {
553
+ forceUpdate: () => M({}),
497
554
  reactiveType: i ?? ["component"],
498
555
  paths: /* @__PURE__ */ new Set(),
499
- depsFunction: f || void 0,
500
- deps: f ? f(N(w, [])) : [],
501
- prevDeps: f ? f(N(w, [])) : []
556
+ depsFunction: g || void 0,
557
+ deps: g ? g(x(w, [])) : [],
558
+ prevDeps: g ? g(x(w, [])) : []
502
559
  }), J(w, [], {
503
- ...A,
504
- components: p
505
- }), O({}), () => {
506
- const I = V(w, []), M = I?.components?.get(g);
507
- M?.paths && M.paths.forEach((b) => {
508
- const E = b.split(".").slice(1), $ = T.getState().getShadowMetadata(w, E);
509
- $?.pathComponents && $.pathComponents.size === 0 && (delete $.pathComponents, T.getState().setShadowMetadata(w, E, $));
510
- }), I?.components && J(w, [], I);
560
+ ...c,
561
+ components: d
562
+ }), M({}), () => {
563
+ const f = b(w, []), m = f?.components?.get(o);
564
+ m?.paths && m.paths.forEach((v) => {
565
+ const A = v.split(".").slice(1), $ = T.getState().getShadowMetadata(w, A);
566
+ $?.pathComponents && $.pathComponents.size === 0 && (delete $.pathComponents, T.getState().setShadowMetadata(w, A, $));
567
+ }), f?.components && J(w, [], f);
511
568
  };
512
569
  }, []);
513
- const o = R(null), c = st(
570
+ const te = St(
514
571
  w,
515
- o,
516
- U,
517
- Y
572
+ E,
573
+ R
518
574
  );
519
- T.getState().initialStateGlobal[w] || ke(w, e);
520
- const h = pe(() => Ve(
575
+ return T.getState().initialStateGlobal[w] || ke(w, e), Me(() => De(
521
576
  w,
522
- c,
523
- m.current,
524
- U
525
- ), [w, U]), y = C, v = Y.current?.syncOptions;
526
- return console.log("syncOpt", v), y && (o.current = y(
527
- h,
528
- v ?? {}
529
- )), h;
577
+ te,
578
+ q.current,
579
+ E
580
+ ), [w, E]);
530
581
  }
531
- const ct = (e, r, d) => {
532
- let s = V(e, r)?.arrayKeys || [];
533
- const f = d?.transforms;
534
- if (!f || f.length === 0)
582
+ const mt = (e, n, u) => {
583
+ let s = b(e, n)?.arrayKeys || [];
584
+ const g = u?.transforms;
585
+ if (!g || g.length === 0)
535
586
  return s;
536
- for (const i of f)
587
+ for (const i of g)
537
588
  if (i.type === "filter") {
538
- const S = [];
589
+ const h = [];
539
590
  s.forEach((l, t) => {
540
- const n = N(e, [...r, l]);
541
- i.fn(n, t) && S.push(l);
542
- }), s = S;
543
- } else i.type === "sort" && s.sort((S, l) => {
544
- const t = N(e, [...r, S]), n = N(e, [...r, l]);
545
- return i.fn(t, n);
591
+ const a = x(e, [...n, l]);
592
+ i.fn(a, t) && h.push(l);
593
+ }), s = h;
594
+ } else i.type === "sort" && s.sort((h, l) => {
595
+ const t = x(e, [...n, h]), a = x(e, [...n, l]);
596
+ return i.fn(t, a);
546
597
  });
547
598
  return s;
548
- }, ve = (e, r, d) => {
549
- const s = `${e}////${r}`, i = V(e, [])?.components?.get(s);
550
- !i || i.reactiveType === "none" || !(Array.isArray(i.reactiveType) ? i.reactiveType : [i.reactiveType]).includes("component") || qe(e, d, s);
551
- }, ae = (e, r, d) => {
552
- const s = V(e, []), f = /* @__PURE__ */ new Set();
553
- s?.components && s.components.forEach((S, l) => {
554
- (Array.isArray(S.reactiveType) ? S.reactiveType : [S.reactiveType || "component"]).includes("all") && (S.forceUpdate(), f.add(l));
555
- }), V(e, [
556
- ...r,
599
+ }, pe = (e, n, u) => {
600
+ const s = `${e}////${n}`, i = b(e, [])?.components?.get(s);
601
+ !i || i.reactiveType === "none" || !(Array.isArray(i.reactiveType) ? i.reactiveType : [i.reactiveType]).includes("component") || Je(e, u, s);
602
+ }, oe = (e, n, u) => {
603
+ const s = b(e, []), g = /* @__PURE__ */ new Set();
604
+ s?.components && s.components.forEach((h, l) => {
605
+ (Array.isArray(h.reactiveType) ? h.reactiveType : [h.reactiveType || "component"]).includes("all") && (h.forceUpdate(), g.add(l));
606
+ }), b(e, [
607
+ ...n,
557
608
  "getSelected"
558
- ])?.pathComponents?.forEach((S) => {
559
- s?.components?.get(S)?.forceUpdate();
609
+ ])?.pathComponents?.forEach((h) => {
610
+ s?.components?.get(h)?.forceUpdate();
560
611
  });
561
- const i = V(e, r);
562
- for (let S of i?.arrayKeys || []) {
563
- const l = S + ".selected", t = V(e, l.split(".").slice(1));
564
- S == d && t?.pathComponents?.forEach((n) => {
565
- s?.components?.get(n)?.forceUpdate();
612
+ const i = b(e, n);
613
+ for (let h of i?.arrayKeys || []) {
614
+ const l = h + ".selected", t = b(e, l.split(".").slice(1));
615
+ h == u && t?.pathComponents?.forEach((a) => {
616
+ s?.components?.get(a)?.forceUpdate();
566
617
  });
567
618
  }
568
619
  };
569
- function K(e, r, d) {
570
- const s = V(e, r), f = r.length > 0 ? r.join(".") : "root", i = d?.arrayViews?.[f];
620
+ function X(e, n, u) {
621
+ const s = b(e, n), g = n.length > 0 ? n.join(".") : "root", i = u?.arrayViews?.[g];
571
622
  if (Array.isArray(i) && i.length === 0)
572
623
  return {
573
624
  shadowMeta: s,
574
625
  value: [],
575
626
  arrayKeys: s?.arrayKeys
576
627
  };
577
- const S = N(e, r, i);
628
+ const h = x(e, n, i);
578
629
  return {
579
630
  shadowMeta: s,
580
- value: S,
631
+ value: h,
581
632
  arrayKeys: s?.arrayKeys
582
633
  };
583
634
  }
584
- function Ve(e, r, d, s) {
585
- const f = /* @__PURE__ */ new Map();
635
+ function De(e, n, u, s) {
636
+ const g = /* @__PURE__ */ new Map();
586
637
  function i({
587
638
  path: t = [],
588
- meta: n,
589
- componentId: u
639
+ meta: a,
640
+ componentId: S
590
641
  }) {
591
- const C = n ? JSON.stringify(n.arrayViews || n.transforms) : "", x = t.join(".") + ":" + u + ":" + C;
592
- if (f.has(x))
593
- return f.get(x);
594
- const O = [e, ...t].join("."), U = {
595
- get(w, m) {
596
- if (t.length === 0 && m in S)
597
- return S[m];
598
- if (!m.startsWith("$")) {
599
- const a = [...t, m];
642
+ const M = a ? JSON.stringify(a.arrayViews || a.transforms) : "", E = t.join(".") + ":" + S + ":" + M;
643
+ if (g.has(E))
644
+ return g.get(E);
645
+ const P = [e, ...t].join("."), w = {
646
+ get(R, y) {
647
+ if (typeof y != "string")
648
+ return Reflect.get(R, y);
649
+ if (t.length === 0 && y in h)
650
+ return h[y];
651
+ if (typeof y == "string" && !y.startsWith("$")) {
652
+ const r = [...t, y];
600
653
  return i({
601
- path: a,
602
- componentId: u,
603
- meta: n
654
+ path: r,
655
+ componentId: S,
656
+ meta: a
604
657
  });
605
658
  }
606
- if (m === "$_rebuildStateShape")
659
+ if (y === "$_rebuildStateShape")
607
660
  return i;
608
- if (m === "$sync" && t.length === 0)
661
+ if (y === "$sync" && t.length === 0)
609
662
  return async function() {
610
- const a = T.getState().getInitialOptions(e), o = a?.sync;
663
+ const r = T.getState().getInitialOptions(e), o = r?.sync;
611
664
  if (!o)
612
665
  return console.error(`No mutation defined for state key "${e}"`), { success: !1, error: "No mutation defined" };
613
- const c = T.getState().getShadowValue(e, []), h = a?.validation?.key;
666
+ const c = T.getState().getShadowValue(e, []), d = r?.validation?.key;
614
667
  try {
615
- const y = await o.action(c);
616
- if (y && !y.success && y.errors, y?.success) {
617
- const v = T.getState().getShadowMetadata(e, []);
668
+ const f = await o.action(c);
669
+ if (f && !f.success && f.errors, f?.success) {
670
+ const m = T.getState().getShadowMetadata(e, []);
618
671
  J(e, [], {
619
- ...v,
672
+ ...m,
620
673
  isDirty: !1,
621
674
  lastServerSync: Date.now(),
622
675
  stateSource: "server",
623
676
  baseServerState: c
624
677
  // Update base server state
625
- }), o.onSuccess && o.onSuccess(y.data);
626
- } else !y?.success && o.onError && o.onError(y.error);
627
- return y;
628
- } catch (y) {
629
- return o.onError && o.onError(y), { success: !1, error: y };
678
+ }), o.onSuccess && o.onSuccess(f.data);
679
+ } else !f?.success && o.onError && o.onError(f.error);
680
+ return f;
681
+ } catch (f) {
682
+ return o.onError && o.onError(f), { success: !1, error: f };
630
683
  }
631
684
  };
632
- if (m === "$_status" || m === "$getStatus") {
633
- const a = () => {
634
- const { shadowMeta: o, value: c } = K(e, t, n);
685
+ if (y === "$_status" || y === "$getStatus") {
686
+ const r = () => {
687
+ const { shadowMeta: o, value: c } = X(e, t, a);
635
688
  return console.log("getStatusFunc", t, o, c), o?.isDirty === !0 ? "dirty" : o?.stateSource === "server" || o?.isDirty === !1 ? "synced" : o?.stateSource === "localStorage" ? "restored" : o?.stateSource === "default" || c !== void 0 ? "fresh" : "unknown";
636
689
  };
637
- return m === "$_status" ? a() : a;
690
+ return y === "$_status" ? r() : r;
638
691
  }
639
- if (m === "$removeStorage")
692
+ if (y === "$removeStorage")
640
693
  return () => {
641
- const a = T.getState().initialStateGlobal[e], o = W(e), c = z(o?.localStorage?.key) ? o.localStorage.key(a) : o?.localStorage?.key, h = `${s}-${e}-${c}`;
642
- h && localStorage.removeItem(h);
694
+ const r = T.getState().initialStateGlobal[e], o = W(e), c = G(o?.localStorage?.key) ? o.localStorage.key(r) : o?.localStorage?.key, d = `${s}-${e}-${c}`;
695
+ d && localStorage.removeItem(d);
643
696
  };
644
- if (m === "$showValidationErrors")
697
+ if (y === "$showValidationErrors")
645
698
  return () => {
646
- const { shadowMeta: a } = K(e, t, n);
647
- return a?.validation?.status === "INVALID" && a.validation.errors.length > 0 ? a.validation.errors.filter((o) => o.severity === "error").map((o) => o.message) : [];
699
+ const { shadowMeta: r } = X(e, t, a);
700
+ return r?.validation?.status === "INVALID" && r.validation.errors.length > 0 ? r.validation.errors.filter((o) => o.severity === "error").map((o) => o.message) : [];
648
701
  };
649
- if (m === "$getSelected")
702
+ if (y === "$getSelected")
650
703
  return () => {
651
- const a = [e, ...t].join(".");
652
- ve(e, u, [
704
+ const r = [e, ...t].join(".");
705
+ pe(e, S, [
653
706
  ...t,
654
707
  "getSelected"
655
708
  ]);
656
- const o = T.getState().selectedIndicesMap.get(a);
709
+ const o = T.getState().selectedIndicesMap.get(r);
657
710
  if (!o)
658
711
  return;
659
- const c = t.join("."), h = n?.arrayViews?.[c], y = o.split(".").pop();
660
- if (!(h && !h.includes(y) || N(
712
+ const c = t.join("."), d = a?.arrayViews?.[c], f = o.split(".").pop();
713
+ if (!(d && !d.includes(f) || x(
661
714
  e,
662
715
  o.split(".").slice(1)
663
716
  ) === void 0))
664
717
  return i({
665
718
  path: o.split(".").slice(1),
666
- componentId: u,
667
- meta: n
719
+ componentId: S,
720
+ meta: a
668
721
  });
669
722
  };
670
- if (m === "$getSelectedIndex")
723
+ if (y === "$getSelectedIndex")
671
724
  return () => {
672
- const a = e + "." + t.join(".");
725
+ const r = e + "." + t.join(".");
673
726
  t.join(".");
674
- const o = T.getState().selectedIndicesMap.get(a);
727
+ const o = T.getState().selectedIndicesMap.get(r);
675
728
  if (!o)
676
729
  return -1;
677
- const { keys: c } = B(e, t, n);
730
+ const { keys: c } = L(e, t, a);
678
731
  if (!c)
679
732
  return -1;
680
- const h = o.split(".").pop();
681
- return c.indexOf(h);
733
+ const d = o.split(".").pop();
734
+ return c.indexOf(d);
682
735
  };
683
- if (m === "$clearSelected")
684
- return ae(e, t), () => {
685
- ze({
736
+ if (y === "$clearSelected")
737
+ return oe(e, t), () => {
738
+ Qe({
686
739
  arrayKey: e + "." + t.join(".")
687
740
  });
688
741
  };
689
- if (m === "$useVirtualView")
690
- return (a) => {
742
+ if (y === "$useVirtualView")
743
+ return (r) => {
691
744
  const {
692
745
  itemHeight: o = 50,
693
746
  overscan: c = 6,
694
- stickToBottom: h = !1,
695
- scrollStickTolerance: y = 75
696
- } = a, v = R(null), [g, A] = te({
747
+ stickToBottom: d = !1,
748
+ scrollStickTolerance: f = 75
749
+ } = r, m = F(null), [v, p] = ee({
697
750
  startIndex: 0,
698
751
  endIndex: 10
699
- }), [p, I] = te({}), M = R(!0);
700
- q(() => {
701
- const P = setInterval(() => {
702
- I({});
752
+ }), [A, $] = ee({}), D = F(!0);
753
+ z(() => {
754
+ const C = setInterval(() => {
755
+ $({});
703
756
  }, 1e3);
704
- return () => clearInterval(P);
757
+ return () => clearInterval(C);
705
758
  }, []);
706
- const b = R({
759
+ const U = F({
707
760
  isUserScrolling: !1,
708
761
  lastScrollTop: 0,
709
762
  scrollUpCount: 0,
710
763
  isNearBottom: !0
711
- }), _ = R(
764
+ }), O = F(
712
765
  /* @__PURE__ */ new Map()
713
- ), { keys: E } = B(e, t, n);
714
- q(() => {
715
- const P = [e, ...t].join("."), k = T.getState().subscribeToPath(P, (j) => {
766
+ ), { keys: I } = L(e, t, a);
767
+ z(() => {
768
+ const C = [e, ...t].join("."), V = T.getState().subscribeToPath(C, (j) => {
716
769
  j.type !== "GET_SELECTED" && j.type;
717
770
  });
718
771
  return () => {
719
- k();
772
+ V();
720
773
  };
721
- }, [u, e, t.join(".")]), ce(() => {
722
- if (h && E.length > 0 && v.current && !b.current.isUserScrolling && M.current) {
723
- const P = v.current, k = () => {
724
- if (P.clientHeight > 0) {
774
+ }, [S, e, t.join(".")]), ce(() => {
775
+ if (d && I.length > 0 && m.current && !U.current.isUserScrolling && D.current) {
776
+ const C = m.current, V = () => {
777
+ if (C.clientHeight > 0) {
725
778
  const j = Math.ceil(
726
- P.clientHeight / o
727
- ), L = E.length - 1, D = Math.max(
779
+ C.clientHeight / o
780
+ ), B = I.length - 1, _ = Math.max(
728
781
  0,
729
- L - j - c
782
+ B - j - c
730
783
  );
731
- A({ startIndex: D, endIndex: L }), requestAnimationFrame(() => {
732
- X("instant"), M.current = !1;
784
+ p({ startIndex: _, endIndex: B }), requestAnimationFrame(() => {
785
+ Z("instant"), D.current = !1;
733
786
  });
734
787
  } else
735
- requestAnimationFrame(k);
788
+ requestAnimationFrame(V);
736
789
  };
737
- k();
790
+ V();
738
791
  }
739
- }, [E.length, h, o, c]);
740
- const $ = R(g);
792
+ }, [I.length, d, o, c]);
793
+ const k = F(v);
741
794
  ce(() => {
742
- $.current = g;
743
- }, [g]);
744
- const F = R(E);
795
+ k.current = v;
796
+ }, [v]);
797
+ const N = F(I);
745
798
  ce(() => {
746
- F.current = E;
747
- }, [E]);
748
- const Z = we(() => {
749
- const P = v.current;
750
- if (!P) return;
751
- const k = P.scrollTop, { scrollHeight: j, clientHeight: L } = P, D = b.current, re = j - (k + L), Se = D.isNearBottom;
752
- D.isNearBottom = re <= y, k < D.lastScrollTop ? (D.scrollUpCount++, D.scrollUpCount > 3 && Se && (D.isUserScrolling = !0, console.log("User scrolled away from bottom"))) : D.isNearBottom && (D.isUserScrolling = !1, D.scrollUpCount = 0), D.lastScrollTop = k;
753
- let Q = 0;
754
- for (let H = 0; H < E.length; H++) {
755
- const ge = E[H], ye = _.current.get(ge);
756
- if (ye && ye.offset + ye.height > k) {
757
- Q = H;
799
+ N.current = I;
800
+ }, [I]);
801
+ const ie = Te(() => {
802
+ const C = m.current;
803
+ if (!C) return;
804
+ const V = C.scrollTop, { scrollHeight: j, clientHeight: B } = C, _ = U.current, re = j - (V + B), ye = _.isNearBottom;
805
+ _.isNearBottom = re <= f, V < _.lastScrollTop ? (_.scrollUpCount++, _.scrollUpCount > 3 && ye && (_.isUserScrolling = !0, console.log("User scrolled away from bottom"))) : _.isNearBottom && (_.isUserScrolling = !1, _.scrollUpCount = 0), _.lastScrollTop = V;
806
+ let Y = 0;
807
+ for (let H = 0; H < I.length; H++) {
808
+ const me = I[H], he = O.current.get(me);
809
+ if (he && he.offset + he.height > V) {
810
+ Y = H;
758
811
  break;
759
812
  }
760
813
  }
761
814
  if (console.log(
762
815
  "hadnlescroll ",
763
- _.current,
764
- Q,
765
- g
766
- ), Q !== g.startIndex && g.startIndex != 0) {
767
- const H = Math.ceil(L / o);
768
- A({
769
- startIndex: Math.max(0, Q - c),
816
+ O.current,
817
+ Y,
818
+ v
819
+ ), Y !== v.startIndex && v.startIndex != 0) {
820
+ const H = Math.ceil(B / o);
821
+ p({
822
+ startIndex: Math.max(0, Y - c),
770
823
  endIndex: Math.min(
771
- E.length - 1,
772
- Q + H + c
824
+ I.length - 1,
825
+ Y + H + c
773
826
  )
774
827
  });
775
828
  }
776
829
  }, [
777
- E.length,
778
- g.startIndex,
830
+ I.length,
831
+ v.startIndex,
779
832
  o,
780
833
  c,
781
- y
834
+ f
782
835
  ]);
783
- q(() => {
784
- const P = v.current;
785
- if (P)
786
- return P.addEventListener("scroll", Z, {
836
+ z(() => {
837
+ const C = m.current;
838
+ if (C)
839
+ return C.addEventListener("scroll", ie, {
787
840
  passive: !0
788
841
  }), () => {
789
- P.removeEventListener("scroll", Z);
842
+ C.removeEventListener("scroll", ie);
790
843
  };
791
- }, [Z, h]);
792
- const X = we(
793
- (P = "smooth") => {
794
- const k = v.current;
795
- if (!k) return;
796
- b.current.isUserScrolling = !1, b.current.isNearBottom = !0, b.current.scrollUpCount = 0;
844
+ }, [ie, d]);
845
+ const Z = Te(
846
+ (C = "smooth") => {
847
+ const V = m.current;
848
+ if (!V) return;
849
+ U.current.isUserScrolling = !1, U.current.isNearBottom = !0, U.current.scrollUpCount = 0;
797
850
  const j = () => {
798
- const L = (D = 0) => {
799
- if (D > 5) return;
800
- const re = k.scrollHeight, Se = k.scrollTop, Q = k.clientHeight;
801
- Se + Q >= re - 1 || (k.scrollTo({
851
+ const B = (_ = 0) => {
852
+ if (_ > 5) return;
853
+ const re = V.scrollHeight, ye = V.scrollTop, Y = V.clientHeight;
854
+ ye + Y >= re - 1 || (V.scrollTo({
802
855
  top: re,
803
- behavior: P
856
+ behavior: C
804
857
  }), setTimeout(() => {
805
- const H = k.scrollHeight, ge = k.scrollTop;
806
- (H !== re || ge + Q < H - 1) && L(D + 1);
858
+ const H = V.scrollHeight, me = V.scrollTop;
859
+ (H !== re || me + Y < H - 1) && B(_ + 1);
807
860
  }, 50));
808
861
  };
809
- L();
862
+ B();
810
863
  };
811
864
  "requestIdleCallback" in window ? requestIdleCallback(j, { timeout: 100 }) : requestAnimationFrame(() => {
812
865
  requestAnimationFrame(j);
@@ -814,45 +867,45 @@ function Ve(e, r, d, s) {
814
867
  },
815
868
  []
816
869
  );
817
- return q(() => {
818
- if (!h || !v.current) return;
819
- const P = v.current, k = b.current;
870
+ return z(() => {
871
+ if (!d || !m.current) return;
872
+ const C = m.current, V = U.current;
820
873
  let j;
821
- const L = () => {
874
+ const B = () => {
822
875
  clearTimeout(j), j = setTimeout(() => {
823
- !k.isUserScrolling && k.isNearBottom && X(
824
- M.current ? "instant" : "smooth"
876
+ !V.isUserScrolling && V.isNearBottom && Z(
877
+ D.current ? "instant" : "smooth"
825
878
  );
826
879
  }, 100);
827
- }, D = new MutationObserver(() => {
828
- k.isUserScrolling || L();
880
+ }, _ = new MutationObserver(() => {
881
+ V.isUserScrolling || B();
829
882
  });
830
- return D.observe(P, {
883
+ return _.observe(C, {
831
884
  childList: !0,
832
885
  subtree: !0,
833
886
  attributes: !0,
834
887
  attributeFilter: ["style", "class"]
835
- }), M.current ? setTimeout(() => {
836
- X("instant");
837
- }, 0) : L(), () => {
838
- clearTimeout(j), D.disconnect();
888
+ }), D.current ? setTimeout(() => {
889
+ Z("instant");
890
+ }, 0) : B(), () => {
891
+ clearTimeout(j), _.disconnect();
839
892
  };
840
- }, [h, E.length, X]), {
841
- virtualState: pe(() => {
842
- const P = Array.isArray(E) ? E.slice(g.startIndex, g.endIndex + 1) : [], k = t.length > 0 ? t.join(".") : "root";
893
+ }, [d, I.length, Z]), {
894
+ virtualState: Me(() => {
895
+ const C = Array.isArray(I) ? I.slice(v.startIndex, v.endIndex + 1) : [], V = t.length > 0 ? t.join(".") : "root";
843
896
  return i({
844
897
  path: t,
845
- componentId: u,
898
+ componentId: S,
846
899
  meta: {
847
- ...n,
848
- arrayViews: { [k]: P },
900
+ ...a,
901
+ arrayViews: { [V]: C },
849
902
  serverStateIsUpStream: !0
850
903
  }
851
904
  });
852
- }, [g.startIndex, g.endIndex, E, n]),
905
+ }, [v.startIndex, v.endIndex, I, a]),
853
906
  virtualizerProps: {
854
907
  outer: {
855
- ref: v,
908
+ ref: m,
856
909
  style: {
857
910
  overflowY: "auto",
858
911
  height: "100%",
@@ -866,423 +919,446 @@ function Ve(e, r, d, s) {
866
919
  },
867
920
  list: {
868
921
  style: {
869
- transform: `translateY(${_.current.get(E[g.startIndex])?.offset || 0}px)`
922
+ transform: `translateY(${O.current.get(I[v.startIndex])?.offset || 0}px)`
870
923
  }
871
924
  }
872
925
  },
873
- scrollToBottom: X,
874
- scrollToIndex: (P, k = "smooth") => {
875
- if (v.current && E[P]) {
876
- const j = _.current.get(E[P])?.offset || 0;
877
- v.current.scrollTo({ top: j, behavior: k });
926
+ scrollToBottom: Z,
927
+ scrollToIndex: (C, V = "smooth") => {
928
+ if (m.current && I[C]) {
929
+ const j = O.current.get(I[C])?.offset || 0;
930
+ m.current.scrollTo({ top: j, behavior: V });
878
931
  }
879
932
  }
880
933
  };
881
934
  };
882
- if (m === "$stateMap")
883
- return (a) => {
884
- const { value: o, keys: c } = B(
935
+ if (y === "$stateMap")
936
+ return (r) => {
937
+ const { value: o, keys: c } = L(
885
938
  e,
886
939
  t,
887
- n
940
+ a
888
941
  );
889
- if (ve(e, u, t), !c || !Array.isArray(o))
942
+ if (pe(e, S, t), !c || !Array.isArray(o))
890
943
  return [];
891
- const h = i({
944
+ const d = i({
892
945
  path: t,
893
- componentId: u,
894
- meta: n
946
+ componentId: S,
947
+ meta: a
895
948
  });
896
- return o.map((y, v) => {
897
- const g = c[v];
898
- if (!g) return;
899
- const A = [...t, g], p = i({
900
- path: A,
949
+ return o.map((f, m) => {
950
+ const v = c[m];
951
+ if (!v) return;
952
+ const p = [...t, v], A = i({
953
+ path: p,
901
954
  // This now correctly points to the item in the shadow store.
902
- componentId: u,
903
- meta: n
955
+ componentId: S,
956
+ meta: a
904
957
  });
905
- return a(p, v, h);
958
+ return r(A, m, d);
906
959
  });
907
960
  };
908
- if (m === "$stateFilter")
909
- return (a) => {
910
- const o = t.length > 0 ? t.join(".") : "root", { keys: c, value: h } = B(
961
+ if (y === "$stateFilter")
962
+ return (r) => {
963
+ const o = t.length > 0 ? t.join(".") : "root", { keys: c, value: d } = L(
911
964
  e,
912
965
  t,
913
- n
966
+ a
914
967
  );
915
- if (!Array.isArray(h))
968
+ if (!Array.isArray(d))
916
969
  throw new Error("stateFilter can only be used on arrays");
917
- const y = [];
918
- return h.forEach((v, g) => {
919
- if (a(v, g)) {
920
- const A = c[g];
921
- A && y.push(A);
970
+ const f = [];
971
+ return d.forEach((m, v) => {
972
+ if (r(m, v)) {
973
+ const p = c[v];
974
+ p && f.push(p);
922
975
  }
923
976
  }), i({
924
977
  path: t,
925
- componentId: u,
978
+ componentId: S,
926
979
  meta: {
927
- ...n,
980
+ ...a,
928
981
  arrayViews: {
929
- ...n?.arrayViews || {},
930
- [o]: y
982
+ ...a?.arrayViews || {},
983
+ [o]: f
931
984
  },
932
985
  transforms: [
933
- ...n?.transforms || [],
934
- { type: "filter", fn: a, path: t }
986
+ ...a?.transforms || [],
987
+ { type: "filter", fn: r, path: t }
935
988
  ]
936
989
  }
937
990
  });
938
991
  };
939
- if (m === "$stateSort")
940
- return (a) => {
941
- const o = t.length > 0 ? t.join(".") : "root", { value: c, keys: h } = B(
992
+ if (y === "$stateSort")
993
+ return (r) => {
994
+ const o = t.length > 0 ? t.join(".") : "root", { value: c, keys: d } = L(
942
995
  e,
943
996
  t,
944
- n
997
+ a
945
998
  );
946
- if (!Array.isArray(c) || !h)
999
+ if (!Array.isArray(c) || !d)
947
1000
  throw new Error("No array keys found for sorting");
948
- const y = c.map((g, A) => ({
949
- item: g,
950
- key: h[A]
1001
+ const f = c.map((v, p) => ({
1002
+ item: v,
1003
+ key: d[p]
951
1004
  }));
952
- y.sort((g, A) => a(g.item, A.item));
953
- const v = y.map((g) => g.key);
1005
+ f.sort((v, p) => r(v.item, p.item));
1006
+ const m = f.map((v) => v.key);
954
1007
  return i({
955
1008
  path: t,
956
- componentId: u,
1009
+ componentId: S,
957
1010
  meta: {
958
- ...n,
1011
+ ...a,
959
1012
  arrayViews: {
960
- ...n?.arrayViews || {},
961
- [o]: v
1013
+ ...a?.arrayViews || {},
1014
+ [o]: m
962
1015
  },
963
1016
  transforms: [
964
- ...n?.transforms || [],
965
- { type: "sort", fn: a, path: t }
1017
+ ...a?.transforms || [],
1018
+ { type: "sort", fn: r, path: t }
966
1019
  ]
967
1020
  }
968
1021
  });
969
1022
  };
970
- if (m === "$stream")
971
- return function(a = {}) {
1023
+ if (y === "$stream")
1024
+ return function(r = {}) {
972
1025
  const {
973
1026
  bufferSize: o = 100,
974
1027
  flushInterval: c = 100,
975
- bufferStrategy: h = "accumulate",
976
- store: y,
977
- onFlush: v
978
- } = a;
979
- let g = [], A = !1, p = null;
980
- const I = ($) => {
981
- if (!A) {
982
- if (h === "sliding" && g.length >= o)
983
- g.shift();
984
- else if (h === "dropping" && g.length >= o)
1028
+ bufferStrategy: d = "accumulate",
1029
+ store: f,
1030
+ onFlush: m
1031
+ } = r;
1032
+ let v = [], p = !1, A = null;
1033
+ const $ = (k) => {
1034
+ if (!p) {
1035
+ if (d === "sliding" && v.length >= o)
1036
+ v.shift();
1037
+ else if (d === "dropping" && v.length >= o)
985
1038
  return;
986
- g.push($), g.length >= o && M();
1039
+ v.push(k), v.length >= o && D();
987
1040
  }
988
- }, M = () => {
989
- if (g.length === 0) return;
990
- const $ = [...g];
991
- if (g = [], y) {
992
- const F = y($);
993
- F !== void 0 && (Array.isArray(F) ? F : [F]).forEach((X) => {
994
- r(X, t, {
1041
+ }, D = () => {
1042
+ if (v.length === 0) return;
1043
+ const k = [...v];
1044
+ if (v = [], f) {
1045
+ const N = f(k);
1046
+ N !== void 0 && (Array.isArray(N) ? N : [N]).forEach((Z) => {
1047
+ n(Z, t, {
995
1048
  updateType: "insert"
996
1049
  });
997
1050
  });
998
1051
  } else
999
- $.forEach((F) => {
1000
- r(F, t, {
1052
+ k.forEach((N) => {
1053
+ n(N, t, {
1001
1054
  updateType: "insert"
1002
1055
  });
1003
1056
  });
1004
- v?.($);
1057
+ m?.(k);
1005
1058
  };
1006
- c > 0 && (p = setInterval(M, c));
1007
- const b = ee(), _ = V(e, t) || {}, E = _.streams || /* @__PURE__ */ new Map();
1008
- return E.set(b, { buffer: g, flushTimer: p }), J(e, t, {
1009
- ..._,
1010
- streams: E
1059
+ c > 0 && (A = setInterval(D, c));
1060
+ const U = ae(), O = b(e, t) || {}, I = O.streams || /* @__PURE__ */ new Map();
1061
+ return I.set(U, { buffer: v, flushTimer: A }), J(e, t, {
1062
+ ...O,
1063
+ streams: I
1011
1064
  }), {
1012
- write: ($) => I($),
1013
- writeMany: ($) => $.forEach(I),
1014
- flush: () => M(),
1065
+ write: (k) => $(k),
1066
+ writeMany: (k) => k.forEach($),
1067
+ flush: () => D(),
1015
1068
  pause: () => {
1016
- A = !0;
1069
+ p = !0;
1017
1070
  },
1018
1071
  resume: () => {
1019
- A = !1, g.length > 0 && M();
1072
+ p = !1, v.length > 0 && D();
1020
1073
  },
1021
1074
  close: () => {
1022
- M(), p && clearInterval(p);
1023
- const $ = T.getState().getShadowMetadata(e, t);
1024
- $?.streams && $.streams.delete(b);
1075
+ D(), A && clearInterval(A);
1076
+ const k = T.getState().getShadowMetadata(e, t);
1077
+ k?.streams && k.streams.delete(U);
1025
1078
  }
1026
1079
  };
1027
1080
  };
1028
- if (m === "$stateList")
1029
- return (a) => /* @__PURE__ */ ne(() => {
1030
- const c = R(/* @__PURE__ */ new Map()), [h, y] = te({}), v = t.length > 0 ? t.join(".") : "root", g = ct(e, t, n), A = pe(() => ({
1031
- ...n,
1081
+ if (y === "$stateList")
1082
+ return (r) => /* @__PURE__ */ ne(() => {
1083
+ const c = F(/* @__PURE__ */ new Map()), [d, f] = ee({}), m = t.length > 0 ? t.join(".") : "root", v = mt(e, t, a), p = Me(() => ({
1084
+ ...a,
1032
1085
  arrayViews: {
1033
- ...n?.arrayViews || {},
1034
- [v]: g
1086
+ ...a?.arrayViews || {},
1087
+ [m]: v
1035
1088
  }
1036
- }), [n, v, g]), { value: p } = B(
1089
+ }), [a, m, v]), { value: A } = L(
1037
1090
  e,
1038
1091
  t,
1039
- A
1092
+ p
1040
1093
  );
1041
- if (q(() => {
1042
- const b = T.getState().subscribeToPath(O, (_) => {
1043
- if (_.type === "GET_SELECTED")
1094
+ if (z(() => {
1095
+ const U = T.getState().subscribeToPath(P, (O) => {
1096
+ if (O.type === "GET_SELECTED")
1044
1097
  return;
1045
- const $ = T.getState().getShadowMetadata(e, t)?.transformCaches;
1046
- if ($)
1047
- for (const F of $.keys())
1048
- F.startsWith(u) && $.delete(F);
1049
- (_.type === "INSERT" || _.type === "INSERT_MANY" || _.type === "REMOVE" || _.type === "CLEAR_SELECTION" || _.type === "SERVER_STATE_UPDATE" && !n?.serverStateIsUpStream) && y({});
1098
+ const k = T.getState().getShadowMetadata(e, t)?.transformCaches;
1099
+ if (k)
1100
+ for (const N of k.keys())
1101
+ N.startsWith(S) && k.delete(N);
1102
+ (O.type === "INSERT" || O.type === "INSERT_MANY" || O.type === "REMOVE" || O.type === "CLEAR_SELECTION" || O.type === "SERVER_STATE_UPDATE" && !a?.serverStateIsUpStream) && f({});
1050
1103
  });
1051
1104
  return () => {
1052
- b();
1105
+ U();
1053
1106
  };
1054
- }, [u, O]), !Array.isArray(p))
1107
+ }, [S, P]), !Array.isArray(A))
1055
1108
  return null;
1056
- const I = i({
1109
+ const $ = i({
1057
1110
  path: t,
1058
- componentId: u,
1059
- meta: A
1111
+ componentId: S,
1112
+ meta: p
1060
1113
  // Use updated meta here
1061
- }), M = p.map((b, _) => {
1062
- const E = g[_];
1063
- if (!E)
1114
+ }), D = A.map((U, O) => {
1115
+ const I = v[O];
1116
+ if (!I)
1064
1117
  return null;
1065
- let $ = c.current.get(E);
1066
- $ || ($ = ee(), c.current.set(E, $));
1067
- const F = [...t, E];
1068
- return Ae(je, {
1069
- key: E,
1118
+ let k = c.current.get(I);
1119
+ k || (k = ae(), c.current.set(I, k));
1120
+ const N = [...t, I];
1121
+ return Ee(Be, {
1122
+ key: I,
1070
1123
  stateKey: e,
1071
- itemComponentId: $,
1072
- itemPath: F,
1073
- localIndex: _,
1074
- arraySetter: I,
1124
+ itemComponentId: k,
1125
+ itemPath: N,
1126
+ localIndex: O,
1127
+ arraySetter: $,
1075
1128
  rebuildStateShape: i,
1076
- renderFn: a
1129
+ renderFn: r
1077
1130
  });
1078
1131
  });
1079
- return /* @__PURE__ */ ne(Pe, { children: M });
1132
+ return /* @__PURE__ */ ne(Ue, { children: D });
1080
1133
  }, {});
1081
- if (m === "$stateFlattenOn")
1082
- return (a) => {
1083
- const o = t.length > 0 ? t.join(".") : "root", c = n?.arrayViews?.[o], h = T.getState().getShadowValue(e, t, c);
1084
- return Array.isArray(h) ? i({
1085
- path: [...t, "[*]", a],
1086
- componentId: u,
1087
- meta: n
1134
+ if (y === "$stateFlattenOn")
1135
+ return (r) => {
1136
+ const o = t.length > 0 ? t.join(".") : "root", c = a?.arrayViews?.[o], d = T.getState().getShadowValue(e, t, c);
1137
+ return Array.isArray(d) ? i({
1138
+ path: [...t, "[*]", r],
1139
+ componentId: S,
1140
+ meta: a
1088
1141
  }) : [];
1089
1142
  };
1090
- if (m === "$index")
1091
- return (a) => {
1092
- const o = t.length > 0 ? t.join(".") : "root", c = n?.arrayViews?.[o];
1143
+ if (y === "$index")
1144
+ return (r) => {
1145
+ const o = t.length > 0 ? t.join(".") : "root", c = a?.arrayViews?.[o];
1093
1146
  if (c) {
1094
- const v = c[a];
1095
- return v ? i({
1096
- path: [...t, v],
1097
- componentId: u,
1098
- meta: n
1147
+ const m = c[r];
1148
+ return m ? i({
1149
+ path: [...t, m],
1150
+ componentId: S,
1151
+ meta: a
1099
1152
  }) : void 0;
1100
1153
  }
1101
- const h = V(e, t);
1102
- if (!h?.arrayKeys) return;
1103
- const y = h.arrayKeys[a];
1104
- if (y)
1154
+ const d = b(e, t);
1155
+ if (!d?.arrayKeys) return;
1156
+ const f = d.arrayKeys[r];
1157
+ if (f)
1105
1158
  return i({
1106
- path: [...t, y],
1107
- componentId: u,
1108
- meta: n
1159
+ path: [...t, f],
1160
+ componentId: S,
1161
+ meta: a
1109
1162
  });
1110
1163
  };
1111
- if (m === "$last")
1164
+ if (y === "$last")
1112
1165
  return () => {
1113
- const { keys: a } = B(e, t, n);
1114
- if (!a || a.length === 0)
1166
+ const { keys: r } = L(e, t, a);
1167
+ if (!r || r.length === 0)
1115
1168
  return;
1116
- const o = a[a.length - 1];
1169
+ const o = r[r.length - 1];
1117
1170
  if (!o)
1118
1171
  return;
1119
1172
  const c = [...t, o];
1120
1173
  return i({
1121
1174
  path: c,
1122
- componentId: u,
1123
- meta: n
1175
+ componentId: S,
1176
+ meta: a
1177
+ });
1178
+ };
1179
+ if (y === "$insert")
1180
+ return (r, o) => {
1181
+ n(r, t, {
1182
+ updateType: "insert",
1183
+ index: o
1124
1184
  });
1125
1185
  };
1126
- if (m === "$insert")
1127
- return (a, o) => {
1128
- r(a, t, { updateType: "insert" });
1186
+ if (y === "$insertMany")
1187
+ return (r) => {
1188
+ n(r, t, {
1189
+ updateType: "insert_many"
1190
+ });
1129
1191
  };
1130
- if (m === "$uniqueInsert")
1131
- return (a, o, c) => {
1132
- const { value: h } = K(
1192
+ if (y === "$uniqueInsert")
1193
+ return (r, o, c) => {
1194
+ const { value: d } = X(
1133
1195
  e,
1134
1196
  t,
1135
- n
1136
- ), y = z(a) ? a(h) : a;
1137
- let v = null;
1138
- if (!h.some((A) => {
1139
- const p = o ? o.every(
1140
- (I) => oe(A[I], y[I])
1141
- ) : oe(A, y);
1142
- return p && (v = A), p;
1197
+ a
1198
+ ), f = G(r) ? r(d) : r;
1199
+ let m = null;
1200
+ if (!d.some((p) => {
1201
+ const A = o ? o.every(
1202
+ ($) => le(p[$], f[$])
1203
+ ) : le(p, f);
1204
+ return A && (m = p), A;
1143
1205
  }))
1144
- r(y, t, { updateType: "insert" });
1145
- else if (c && v) {
1146
- const A = c(v), p = h.map(
1147
- (I) => oe(I, v) ? A : I
1206
+ n(f, t, { updateType: "insert" });
1207
+ else if (c && m) {
1208
+ const p = c(m), A = d.map(
1209
+ ($) => le($, m) ? p : $
1148
1210
  );
1149
- r(p, t, {
1211
+ n(A, t, {
1150
1212
  updateType: "update"
1151
1213
  });
1152
1214
  }
1153
1215
  };
1154
- if (m === "$cut")
1155
- return (a, o) => {
1156
- const c = V(e, t);
1157
- if (!c?.arrayKeys || c.arrayKeys.length === 0)
1216
+ if (y === "$cut")
1217
+ return (r, o) => {
1218
+ const c = b(e, t);
1219
+ if (console.log("shadowMeta ->>>>>>>>>>>>>>>>", c), !c?.arrayKeys || c.arrayKeys.length === 0)
1158
1220
  return;
1159
- const h = a === -1 ? c.arrayKeys.length - 1 : a !== void 0 ? a : c.arrayKeys.length - 1, y = c.arrayKeys[h];
1160
- y && r(null, [...t, y], {
1221
+ const d = r === -1 ? c.arrayKeys.length - 1 : r !== void 0 ? r : c.arrayKeys.length - 1;
1222
+ console.log("indexToCut ->>>>>>>>>>>>>>>>", d);
1223
+ const f = c.arrayKeys[d];
1224
+ f && (console.log("idToCut ->>>>>>>>>>>>>>>>", f), n(null, [...t, f], {
1161
1225
  updateType: "cut"
1162
- });
1226
+ }));
1163
1227
  };
1164
- if (m === "$cutSelected")
1228
+ if (y === "$cutSelected")
1165
1229
  return () => {
1166
- const a = [e, ...t].join("."), { keys: o } = B(e, t, n);
1230
+ const r = [e, ...t].join("."), { keys: o } = L(e, t, a);
1167
1231
  if (!o || o.length === 0)
1168
1232
  return;
1169
- const c = T.getState().selectedIndicesMap.get(a);
1233
+ const c = T.getState().selectedIndicesMap.get(r);
1170
1234
  if (!c)
1171
1235
  return;
1172
- const h = c.split(".").pop();
1173
- if (!o.includes(h))
1236
+ const d = c.split(".").pop();
1237
+ if (!o.includes(d))
1174
1238
  return;
1175
- const y = c.split(".").slice(1);
1176
- T.getState().clearSelectedIndex({ arrayKey: a });
1177
- const v = y.slice(0, -1);
1178
- ae(e, v), r(null, y, {
1239
+ const f = c.split(".").slice(1);
1240
+ T.getState().clearSelectedIndex({ arrayKey: r });
1241
+ const m = f.slice(0, -1);
1242
+ oe(e, m), n(null, f, {
1179
1243
  updateType: "cut"
1180
1244
  });
1181
1245
  };
1182
- if (m === "$cutByValue")
1183
- return (a) => {
1246
+ if (y === "$cutByValue")
1247
+ return (r) => {
1184
1248
  const {
1185
1249
  isArray: o,
1186
1250
  value: c,
1187
- keys: h
1188
- } = B(e, t, n);
1251
+ keys: d
1252
+ } = L(e, t, a);
1189
1253
  if (!o) return;
1190
- const y = me(c, h, (v) => v === a);
1191
- y && r(null, [...t, y.key], {
1254
+ const f = ve(c, d, (m) => m === r);
1255
+ f && n(null, [...t, f.key], {
1192
1256
  updateType: "cut"
1193
1257
  });
1194
1258
  };
1195
- if (m === "$toggleByValue")
1196
- return (a) => {
1259
+ if (y === "$toggleByValue")
1260
+ return (r) => {
1197
1261
  const {
1198
1262
  isArray: o,
1199
1263
  value: c,
1200
- keys: h
1201
- } = B(e, t, n);
1264
+ keys: d
1265
+ } = L(e, t, a);
1202
1266
  if (!o) return;
1203
- const y = me(c, h, (v) => v === a);
1204
- if (y) {
1205
- const v = [...t, y.key];
1206
- r(null, v, {
1267
+ const f = ve(c, d, (m) => m === r);
1268
+ if (f) {
1269
+ const m = [...t, f.key];
1270
+ n(null, m, {
1207
1271
  updateType: "cut"
1208
1272
  });
1209
1273
  } else
1210
- r(a, t, { updateType: "insert" });
1274
+ n(r, t, { updateType: "insert" });
1211
1275
  };
1212
- if (m === "$findWith")
1213
- return (a, o) => {
1214
- const { isArray: c, value: h, keys: y } = B(e, t, n);
1276
+ if (y === "$findWith")
1277
+ return (r, o) => {
1278
+ const { isArray: c, value: d, keys: f } = L(e, t, a);
1215
1279
  if (!c)
1216
1280
  throw new Error("findWith can only be used on arrays");
1217
- const v = me(
1218
- h,
1219
- y,
1220
- (g) => g?.[a] === o
1281
+ const m = ve(
1282
+ d,
1283
+ f,
1284
+ (v) => v?.[r] === o
1221
1285
  );
1222
- return i(v ? {
1223
- path: [...t, v.key],
1224
- componentId: u,
1225
- meta: n
1226
- } : {
1227
- path: [...t, `not_found_${ee()}`],
1228
- componentId: u,
1229
- meta: n
1230
- });
1286
+ return m ? i({
1287
+ path: [...t, m.key],
1288
+ componentId: S,
1289
+ meta: a
1290
+ }) : null;
1231
1291
  };
1232
- if (m === "$cutThis") {
1233
- const { value: a } = K(e, t, n), o = t.slice(0, -1);
1234
- return ae(e, o), () => {
1235
- r(a, t, { updateType: "cut" });
1292
+ if (y === "$cutThis") {
1293
+ const { value: r } = X(e, t, a), o = t.slice(0, -1);
1294
+ return oe(e, o), () => {
1295
+ n(r, t, { updateType: "cut" });
1236
1296
  };
1237
1297
  }
1238
- if (m === "$get")
1298
+ if (y === "$get")
1239
1299
  return () => {
1240
- ve(e, u, t);
1241
- const { value: a } = K(e, t, n);
1242
- return a;
1300
+ pe(e, S, t);
1301
+ const { value: r } = X(e, t, a);
1302
+ return r;
1243
1303
  };
1244
- if (m === "$$derive")
1245
- return (a) => be({
1304
+ if (y === "$$derive")
1305
+ return (r) => Ve({
1246
1306
  _stateKey: e,
1247
1307
  _path: t,
1248
- _effect: a.toString(),
1249
- _meta: n
1308
+ _effect: r.toString(),
1309
+ _meta: a
1250
1310
  });
1251
- if (m === "$$get")
1252
- return () => be({ _stateKey: e, _path: t, _meta: n });
1253
- if (m === "$lastSynced") {
1254
- const a = `${e}:${t.join(".")}`;
1255
- return Je(a);
1311
+ if (y === "$$get")
1312
+ return () => Ve({ _stateKey: e, _path: t, _meta: a });
1313
+ if (y === "$lastSynced") {
1314
+ const r = `${e}:${t.join(".")}`;
1315
+ return Xe(r);
1256
1316
  }
1257
- if (m == "getLocalStorage")
1258
- return (a) => fe(s + "-" + e + "-" + a);
1259
- if (m === "$isSelected") {
1260
- const a = t.slice(0, -1);
1261
- if (V(e, a)?.arrayKeys) {
1262
- const c = e + "." + a.join("."), h = T.getState().selectedIndicesMap.get(c), y = e + "." + t.join(".");
1263
- return h === y;
1317
+ if (y == "getLocalStorage")
1318
+ return (r) => Se(s + "-" + e + "-" + r);
1319
+ if (y === "$isSelected") {
1320
+ const r = t.slice(0, -1);
1321
+ if (b(e, r)?.arrayKeys) {
1322
+ const c = e + "." + r.join("."), d = T.getState().selectedIndicesMap.get(c), f = e + "." + t.join(".");
1323
+ return d === f;
1264
1324
  }
1265
1325
  return;
1266
1326
  }
1267
- if (m === "$setSelected")
1268
- return (a) => {
1269
- const o = t.slice(0, -1), c = e + "." + o.join("."), h = e + "." + t.join(".");
1270
- ae(e, o, void 0), T.getState().selectedIndicesMap.get(c), a && T.getState().setSelectedIndex(c, h);
1327
+ if (y === "$setSelected")
1328
+ return (r) => {
1329
+ const o = t.slice(0, -1), c = e + "." + o.join("."), d = e + "." + t.join(".");
1330
+ oe(e, o, void 0), T.getState().selectedIndicesMap.get(c), r && T.getState().setSelectedIndex(c, d);
1271
1331
  };
1272
- if (m === "$toggleSelected")
1332
+ if (y === "$toggleSelected")
1273
1333
  return () => {
1274
- const a = t.slice(0, -1), o = e + "." + a.join("."), c = e + "." + t.join(".");
1275
- T.getState().selectedIndicesMap.get(o) === c ? T.getState().clearSelectedIndex({ arrayKey: o }) : T.getState().setSelectedIndex(o, c), ae(e, a);
1334
+ const r = t.slice(0, -1), o = e + "." + r.join("."), c = e + "." + t.join(".");
1335
+ T.getState().selectedIndicesMap.get(o) === c ? T.getState().clearSelectedIndex({ arrayKey: o }) : T.getState().setSelectedIndex(o, c), oe(e, r);
1276
1336
  };
1277
- if (m === "$_componentId")
1278
- return u;
1337
+ if (y === "$_componentId")
1338
+ return S;
1279
1339
  if (t.length == 0) {
1280
- if (m === "$addZodValidation")
1281
- return (a, o) => {
1282
- a.forEach((c) => {
1283
- const h = T.getState().getShadowMetadata(e, c.path) || {};
1340
+ if (y === "$setOptions")
1341
+ return (r) => {
1342
+ Ae({ stateKey: e, options: r, initialOptionsPart: {} });
1343
+ };
1344
+ if (y === "$_applyUpdate")
1345
+ return (r, o, c = "update") => {
1346
+ n(r, o, { updateType: c });
1347
+ };
1348
+ if (y === "$_getEffectiveSetState")
1349
+ return n;
1350
+ if (y === "$getPluginMetaData")
1351
+ return (r) => et(e, t)?.get(r);
1352
+ if (y === "$addPluginMetaData")
1353
+ return console.log("$addPluginMetaDat"), (r, o) => tt(e, t, r, o);
1354
+ if (y === "$removePluginMetaData")
1355
+ return (r) => rt(e, t, r);
1356
+ if (y === "$addZodValidation")
1357
+ return (r, o) => {
1358
+ r.forEach((c) => {
1359
+ const d = T.getState().getShadowMetadata(e, c.path) || {};
1284
1360
  T.getState().setShadowMetadata(e, c.path, {
1285
- ...h,
1361
+ ...d,
1286
1362
  validation: {
1287
1363
  status: "INVALID",
1288
1364
  errors: [
@@ -1299,12 +1375,12 @@ function Ve(e, r, d, s) {
1299
1375
  });
1300
1376
  });
1301
1377
  };
1302
- if (m === "$clearZodValidation")
1303
- return (a) => {
1304
- if (!a)
1378
+ if (y === "$clearZodValidation")
1379
+ return (r) => {
1380
+ if (!r)
1305
1381
  throw new Error("clearZodValidation requires a path");
1306
- const o = V(e, a) || {};
1307
- J(e, a, {
1382
+ const o = b(e, r) || {};
1383
+ J(e, r, {
1308
1384
  ...o,
1309
1385
  validation: {
1310
1386
  status: "NOT_VALIDATED",
@@ -1313,108 +1389,100 @@ function Ve(e, r, d, s) {
1313
1389
  }
1314
1390
  });
1315
1391
  };
1316
- if (m === "$applyOperation")
1317
- return (a) => {
1318
- const o = a.validation || [];
1319
- if (!a || !a.path) {
1320
- console.error(
1321
- "Invalid operation received by $applyOperation:",
1322
- a
1323
- );
1324
- return;
1325
- }
1326
- const c = a.path, h = T.getState().getShadowMetadata(e, c) || {}, y = o.map((v) => ({
1327
- source: "sync_engine",
1328
- message: v.message,
1329
- severity: "warning",
1330
- code: v.code
1331
- }));
1332
- console.log("updatePath", c), T.getState().setShadowMetadata(e, c, {
1333
- ...h,
1334
- validation: {
1335
- status: y.length > 0 ? "INVALID" : "VALID",
1336
- errors: y,
1337
- lastValidated: Date.now()
1392
+ if (y === "$applyOperation")
1393
+ return (r, o) => {
1394
+ console.log(
1395
+ "getGlobalStore",
1396
+ T.getState().getShadowMetadata(e, r.path)
1397
+ );
1398
+ let c;
1399
+ if (r.insertAfterId && r.updateType === "insert") {
1400
+ const d = b(e, r.path);
1401
+ if (d?.arrayKeys) {
1402
+ const f = d.arrayKeys.indexOf(
1403
+ r.insertAfterId
1404
+ );
1405
+ f !== -1 && (c = f + 1);
1338
1406
  }
1339
- }), r(a.newValue, c, {
1340
- updateType: a.updateType,
1341
- sync: !1
1407
+ }
1408
+ n(r.newValue, r.path, {
1409
+ updateType: r.updateType,
1410
+ itemId: r.itemId,
1411
+ index: c,
1412
+ // Pass the calculated index
1413
+ metaData: o
1342
1414
  });
1343
1415
  };
1344
- if (m === "$applyJsonPatch")
1345
- return (a) => {
1416
+ if (y === "$applyJsonPatch")
1417
+ return (r) => {
1346
1418
  const o = T.getState(), c = o.getShadowMetadata(e, []);
1347
1419
  if (!c?.components) return;
1348
- const h = (v) => !v || v === "/" ? [] : v.split("/").slice(1).map((g) => g.replace(/~1/g, "/").replace(/~0/g, "~")), y = /* @__PURE__ */ new Set();
1349
- for (const v of a) {
1350
- const g = h(v.path);
1351
- switch (v.op) {
1420
+ const d = (m) => !m || m === "/" ? [] : m.split("/").slice(1).map((v) => v.replace(/~1/g, "/").replace(/~0/g, "~")), f = /* @__PURE__ */ new Set();
1421
+ for (const m of r) {
1422
+ const v = d(m.path);
1423
+ switch (m.op) {
1352
1424
  case "add":
1353
1425
  case "replace": {
1354
- const { value: A } = v;
1355
- o.updateShadowAtPath(e, g, A), o.markAsDirty(e, g, { bubble: !0 });
1356
- let p = [...g];
1426
+ const { value: p } = m;
1427
+ o.updateShadowAtPath(e, v, p), o.markAsDirty(e, v, { bubble: !0 });
1428
+ let A = [...v];
1357
1429
  for (; ; ) {
1358
- const I = o.getShadowMetadata(
1430
+ const $ = o.getShadowMetadata(
1359
1431
  e,
1360
- p
1432
+ A
1361
1433
  );
1362
- if (I?.pathComponents && I.pathComponents.forEach((M) => {
1363
- if (!y.has(M)) {
1364
- const b = c.components?.get(M);
1365
- b && (b.forceUpdate(), y.add(M));
1434
+ if ($?.pathComponents && $.pathComponents.forEach((D) => {
1435
+ if (!f.has(D)) {
1436
+ const U = c.components?.get(D);
1437
+ U && (U.forceUpdate(), f.add(D));
1366
1438
  }
1367
- }), p.length === 0) break;
1368
- p.pop();
1439
+ }), A.length === 0) break;
1440
+ A.pop();
1369
1441
  }
1370
1442
  break;
1371
1443
  }
1372
1444
  case "remove": {
1373
- const A = g.slice(0, -1);
1374
- o.removeShadowArrayElement(e, g), o.markAsDirty(e, A, { bubble: !0 });
1375
- let p = [...A];
1445
+ const p = v.slice(0, -1);
1446
+ o.removeShadowArrayElement(e, v), o.markAsDirty(e, p, { bubble: !0 });
1447
+ let A = [...p];
1376
1448
  for (; ; ) {
1377
- const I = o.getShadowMetadata(
1449
+ const $ = o.getShadowMetadata(
1378
1450
  e,
1379
- p
1451
+ A
1380
1452
  );
1381
- if (I?.pathComponents && I.pathComponents.forEach((M) => {
1382
- if (!y.has(M)) {
1383
- const b = c.components?.get(M);
1384
- b && (b.forceUpdate(), y.add(M));
1453
+ if ($?.pathComponents && $.pathComponents.forEach((D) => {
1454
+ if (!f.has(D)) {
1455
+ const U = c.components?.get(D);
1456
+ U && (U.forceUpdate(), f.add(D));
1385
1457
  }
1386
- }), p.length === 0) break;
1387
- p.pop();
1458
+ }), A.length === 0) break;
1459
+ A.pop();
1388
1460
  }
1389
1461
  break;
1390
1462
  }
1391
1463
  }
1392
1464
  }
1393
1465
  };
1394
- if (m === "$getComponents")
1395
- return () => V(e, [])?.components;
1396
- if (m === "$getAllFormRefs")
1397
- return () => Me.getState().getFormRefsByStateKey(e);
1466
+ if (y === "$getComponents")
1467
+ return () => b(e, [])?.components;
1398
1468
  }
1399
- if (m === "$getFormRef")
1400
- return () => Me.getState().getFormRef(e + "." + t.join("."));
1401
- if (m === "$validationWrapper")
1469
+ if (y === "$validationWrapper")
1402
1470
  return ({
1403
- children: a,
1471
+ children: r,
1404
1472
  hideMessage: o
1405
1473
  }) => /* @__PURE__ */ ne(
1406
- Oe,
1474
+ Fe,
1407
1475
  {
1408
1476
  formOpts: o ? { validation: { message: "" } } : void 0,
1409
1477
  path: t,
1410
1478
  stateKey: e,
1411
- children: a
1479
+ children: r
1412
1480
  }
1413
1481
  );
1414
- if (m === "$_stateKey") return e;
1415
- if (m === "$_path") return t;
1416
- if (m === "$update")
1417
- return (a) => (r(a, t, { updateType: "update" }), {
1482
+ if (y === "$_stateKey") return e;
1483
+ if (y === "$_path") return t;
1484
+ if (y === "$update")
1485
+ return (r) => (n(r, t, { updateType: "update" }), {
1418
1486
  synced: () => {
1419
1487
  const o = T.getState().getShadowMetadata(e, t);
1420
1488
  J(e, t, {
@@ -1424,153 +1492,155 @@ function Ve(e, r, d, s) {
1424
1492
  lastServerSync: Date.now()
1425
1493
  });
1426
1494
  const c = [e, ...t].join(".");
1427
- Ye(c, {
1495
+ Ke(c, {
1428
1496
  type: "SYNC_STATUS_CHANGE",
1429
1497
  isDirty: !1
1430
1498
  });
1431
1499
  }
1432
1500
  });
1433
- if (m === "$toggle") {
1434
- const { value: a } = K(
1501
+ if (y === "$toggle") {
1502
+ const { value: r } = X(
1435
1503
  e,
1436
1504
  t,
1437
- n
1505
+ a
1438
1506
  );
1439
- if (typeof a != "boolean")
1507
+ if (typeof r != "boolean")
1440
1508
  throw new Error("toggle() can only be used on boolean values");
1441
1509
  return () => {
1442
- r(!a, t, {
1510
+ n(!r, t, {
1443
1511
  updateType: "update"
1444
1512
  });
1445
1513
  };
1446
1514
  }
1447
- if (m === "$isolate")
1448
- return (a) => /* @__PURE__ */ ne(
1449
- Ue,
1515
+ if (y === "$isolate")
1516
+ return (r) => /* @__PURE__ */ ne(
1517
+ xe,
1450
1518
  {
1451
1519
  stateKey: e,
1452
1520
  path: t,
1453
1521
  rebuildStateShape: i,
1454
- renderFn: a
1522
+ renderFn: r
1455
1523
  }
1456
1524
  );
1457
- if (m === "$formElement")
1458
- return (a, o) => /* @__PURE__ */ ne(
1459
- Fe,
1525
+ if (y === "$formElement")
1526
+ return (r, o) => /* @__PURE__ */ ne(
1527
+ Re,
1460
1528
  {
1461
1529
  stateKey: e,
1462
1530
  path: t,
1463
1531
  rebuildStateShape: i,
1464
- setState: r,
1532
+ setState: n,
1465
1533
  formOpts: o,
1466
- renderFn: a
1534
+ renderFn: r
1467
1535
  }
1468
1536
  );
1469
- const Y = [...t, m];
1537
+ const te = [...t, y];
1470
1538
  return i({
1471
- path: Y,
1472
- componentId: u,
1473
- meta: n
1539
+ path: te,
1540
+ componentId: S,
1541
+ meta: a
1474
1542
  });
1475
1543
  }
1476
- }, G = new Proxy({}, U);
1477
- return f.set(x, G), G;
1544
+ }, q = new Proxy({}, w);
1545
+ return g.set(E, q), q;
1478
1546
  }
1479
- const S = {
1547
+ const h = {
1480
1548
  $revertToInitialState: (t) => {
1481
- const n = T.getState().getShadowMetadata(e, []);
1482
- let u;
1483
- n?.stateSource === "server" && n.baseServerState ? u = n.baseServerState : u = T.getState().initialStateGlobal[e], We(e), se(e, u), i({
1549
+ const a = T.getState().getShadowMetadata(e, []);
1550
+ let S;
1551
+ a?.stateSource === "server" && a.baseServerState ? S = a.baseServerState : S = T.getState().initialStateGlobal[e], Ye(e), se(e, S), i({
1484
1552
  path: [],
1485
- componentId: d
1553
+ componentId: u
1486
1554
  });
1487
- const C = W(e), x = z(C?.localStorage?.key) ? C?.localStorage?.key(u) : C?.localStorage?.key, O = `${s}-${e}-${x}`;
1488
- return O && localStorage.removeItem(O), ie(e), u;
1555
+ const M = W(e), E = G(M?.localStorage?.key) ? M?.localStorage?.key(S) : M?.localStorage?.key, P = `${s}-${e}-${E}`;
1556
+ return P && localStorage.removeItem(P), K(e), S;
1557
+ },
1558
+ $initializeAndMergeShadowState: (t) => {
1559
+ We(e, t), K(e);
1489
1560
  },
1490
1561
  $updateInitialState: (t) => {
1491
- const n = Ve(
1562
+ const a = De(
1492
1563
  e,
1493
- r,
1494
- d,
1564
+ n,
1565
+ u,
1495
1566
  s
1496
- ), u = T.getState().initialStateGlobal[e], C = W(e), x = z(C?.localStorage?.key) ? C?.localStorage?.key(u) : C?.localStorage?.key, O = `${s}-${e}-${x}`;
1497
- return localStorage.getItem(O) && localStorage.removeItem(O), Ce(() => {
1567
+ ), S = T.getState().initialStateGlobal[e], M = W(e), E = G(M?.localStorage?.key) ? M?.localStorage?.key(S) : M?.localStorage?.key, P = `${s}-${e}-${E}`;
1568
+ return localStorage.getItem(P) && localStorage.removeItem(P), _e(() => {
1498
1569
  ke(e, t), se(e, t);
1499
- const U = T.getState().getShadowMetadata(e, []);
1500
- U && U?.components?.forEach((G) => {
1501
- G.forceUpdate();
1570
+ const w = T.getState().getShadowMetadata(e, []);
1571
+ w && w?.components?.forEach((q) => {
1572
+ q.forceUpdate();
1502
1573
  });
1503
1574
  }), {
1504
- fetchId: (U) => n.$get()[U]
1575
+ fetchId: (w) => a.$get()[w]
1505
1576
  };
1506
1577
  }
1507
1578
  };
1508
1579
  return i({
1509
- componentId: d,
1580
+ componentId: u,
1510
1581
  path: []
1511
1582
  });
1512
1583
  }
1513
- function be(e) {
1514
- return Ae(lt, { proxy: e });
1584
+ function Ve(e) {
1585
+ return Ee(ht, { proxy: e });
1515
1586
  }
1516
- function lt({
1587
+ function ht({
1517
1588
  proxy: e
1518
1589
  }) {
1519
- const r = R(null), d = R(null), s = R(!1), f = `${e._stateKey}-${e._path.join(".")}`, i = e._path.length > 0 ? e._path.join(".") : "root", S = e._meta?.arrayViews?.[i], l = N(e._stateKey, e._path, S);
1520
- return q(() => {
1521
- const t = r.current;
1590
+ const n = F(null), u = F(null), s = F(!1), g = `${e._stateKey}-${e._path.join(".")}`, i = e._path.length > 0 ? e._path.join(".") : "root", h = e._meta?.arrayViews?.[i], l = x(e._stateKey, e._path, h);
1591
+ return z(() => {
1592
+ const t = n.current;
1522
1593
  if (!t || s.current) return;
1523
- const n = setTimeout(() => {
1594
+ const a = setTimeout(() => {
1524
1595
  if (!t.parentElement) {
1525
- console.warn("Parent element not found for signal", f);
1596
+ console.warn("Parent element not found for signal", g);
1526
1597
  return;
1527
1598
  }
1528
- const u = t.parentElement, x = Array.from(u.childNodes).indexOf(t);
1529
- let O = u.getAttribute("data-parent-id");
1530
- O || (O = `parent-${crypto.randomUUID()}`, u.setAttribute("data-parent-id", O)), d.current = `instance-${crypto.randomUUID()}`;
1531
- const U = T.getState().getShadowMetadata(e._stateKey, e._path) || {}, G = U.signals || [];
1532
- G.push({
1533
- instanceId: d.current,
1534
- parentId: O,
1535
- position: x,
1599
+ const S = t.parentElement, E = Array.from(S.childNodes).indexOf(t);
1600
+ let P = S.getAttribute("data-parent-id");
1601
+ P || (P = `parent-${crypto.randomUUID()}`, S.setAttribute("data-parent-id", P)), u.current = `instance-${crypto.randomUUID()}`;
1602
+ const w = T.getState().getShadowMetadata(e._stateKey, e._path) || {}, q = w.signals || [];
1603
+ q.push({
1604
+ instanceId: u.current,
1605
+ parentId: P,
1606
+ position: E,
1536
1607
  effect: e._effect
1537
1608
  }), T.getState().setShadowMetadata(e._stateKey, e._path, {
1538
- ...U,
1539
- signals: G
1609
+ ...w,
1610
+ signals: q
1540
1611
  });
1541
- let w = l;
1612
+ let R = l;
1542
1613
  if (e._effect)
1543
1614
  try {
1544
- w = new Function(
1615
+ R = new Function(
1545
1616
  "state",
1546
1617
  `return (${e._effect})(state)`
1547
1618
  )(l);
1548
- } catch (Y) {
1549
- console.error("Error evaluating effect function:", Y);
1619
+ } catch (te) {
1620
+ console.error("Error evaluating effect function:", te);
1550
1621
  }
1551
- w !== null && typeof w == "object" && (w = JSON.stringify(w));
1552
- const m = document.createTextNode(String(w ?? ""));
1553
- t.replaceWith(m), s.current = !0;
1622
+ R !== null && typeof R == "object" && (R = JSON.stringify(R));
1623
+ const y = document.createTextNode(String(R ?? ""));
1624
+ t.replaceWith(y), s.current = !0;
1554
1625
  }, 0);
1555
1626
  return () => {
1556
- if (clearTimeout(n), d.current) {
1557
- const u = T.getState().getShadowMetadata(e._stateKey, e._path) || {};
1558
- u.signals && (u.signals = u.signals.filter(
1559
- (C) => C.instanceId !== d.current
1560
- ), T.getState().setShadowMetadata(e._stateKey, e._path, u));
1627
+ if (clearTimeout(a), u.current) {
1628
+ const S = T.getState().getShadowMetadata(e._stateKey, e._path) || {};
1629
+ S.signals && (S.signals = S.signals.filter(
1630
+ (M) => M.instanceId !== u.current
1631
+ ), T.getState().setShadowMetadata(e._stateKey, e._path, S));
1561
1632
  }
1562
1633
  };
1563
- }, []), Ae("span", {
1564
- ref: r,
1634
+ }, []), Ee("span", {
1635
+ ref: n,
1565
1636
  style: { display: "contents" },
1566
- "data-signal-id": f
1637
+ "data-signal-id": g
1567
1638
  });
1568
1639
  }
1569
1640
  export {
1570
- be as $cogsSignal,
1571
- wt as addStateOptions,
1572
- Ze as createCogsState,
1573
- pt as createCogsStateFromSync,
1574
- it as useCogsStateFn
1641
+ Ve as $cogsSignal,
1642
+ kt as addStateOptions,
1643
+ Ct as createCogsState,
1644
+ yt as useCogsStateFn
1575
1645
  };
1576
1646
  //# sourceMappingURL=CogsState.jsx.map