cogsbox-state 0.5.280 → 0.5.282

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.
@@ -1,18 +1,18 @@
1
1
  "use client";
2
- import { jsx as St } from "react/jsx-runtime";
3
- import { useState as K, useRef as H, useEffect as st, useLayoutEffect as it, useMemo as Tt, createElement as rt, useSyncExternalStore as Ct, startTransition as Vt, useCallback as vt } from "react";
4
- import { transformStateFunc as bt, isDeepEqual as L, isFunction as J, getNestedValue as B, getDifferences as mt, debounce as xt } from "./utility.js";
5
- import { pushFunc as ft, updateFn as nt, cutFunc as at, ValidationWrapper as Pt, FormControlComponent as _t } from "./Functions.jsx";
2
+ import { jsx as mt } from "react/jsx-runtime";
3
+ import { useState as K, useRef as q, useEffect as st, useLayoutEffect as it, useMemo as Tt, createElement as rt, useSyncExternalStore as Ct, startTransition as Vt, useCallback as ft } from "react";
4
+ import { transformStateFunc as bt, isDeepEqual as L, isFunction as J, getNestedValue as z, getDifferences as ht, debounce as xt } from "./utility.js";
5
+ import { pushFunc as St, updateFn as nt, cutFunc as at, ValidationWrapper as Pt, FormControlComponent as _t } from "./Functions.jsx";
6
6
  import Mt from "superjson";
7
- import { v4 as ht } from "uuid";
7
+ import { v4 as yt } from "uuid";
8
8
  import "zod";
9
- import { getGlobalStore as r, formRefStore as It } from "./store.js";
9
+ import { getGlobalStore as o, formRefStore as It } from "./store.js";
10
10
  import { useCogsConfig as $t } from "./CogsStateClient.jsx";
11
11
  import { applyPatch as jt } from "fast-json-patch";
12
12
  import Ot from "react-use-measure";
13
13
  function pt(t, c) {
14
- const h = r.getState().getInitialOptions, g = r.getState().setInitialStateOptions, v = h(t) || {};
15
- g(t, {
14
+ const h = o.getState().getInitialOptions, f = o.getState().setInitialStateOptions, v = h(t) || {};
15
+ f(t, {
16
16
  ...v,
17
17
  ...c
18
18
  });
@@ -22,20 +22,20 @@ function wt({
22
22
  options: c,
23
23
  initialOptionsPart: h
24
24
  }) {
25
- const g = Q(t) || {}, v = h[t] || {}, $ = r.getState().setInitialStateOptions, p = { ...v, ...g };
25
+ const f = Q(t) || {}, v = h[t] || {}, k = o.getState().setInitialStateOptions, p = { ...v, ...f };
26
26
  let I = !1;
27
27
  if (c)
28
28
  for (const a in c)
29
29
  p.hasOwnProperty(a) ? (a == "localStorage" && c[a] && p[a].key !== c[a]?.key && (I = !0, p[a] = c[a]), a == "initialState" && c[a] && p[a] !== c[a] && // Different references
30
30
  !L(p[a], c[a]) && (I = !0, p[a] = c[a])) : (I = !0, p[a] = c[a]);
31
- I && $(t, p);
31
+ I && k(t, p);
32
32
  }
33
33
  function oe(t, { formElements: c, validation: h }) {
34
34
  return { initialState: t, formElements: c, validation: h };
35
35
  }
36
36
  const ae = (t, c) => {
37
37
  let h = t;
38
- const [g, v] = bt(h);
38
+ const [f, v] = bt(h);
39
39
  (Object.keys(v).length > 0 || c && Object.keys(c).length > 0) && Object.keys(v).forEach((I) => {
40
40
  v[I] = v[I] || {}, v[I].formElements = {
41
41
  ...c?.formElements,
@@ -43,16 +43,16 @@ const ae = (t, c) => {
43
43
  ...c?.validation,
44
44
  ...v[I].formElements || {}
45
45
  // State-specific overrides
46
- }, Q(I) || r.getState().setInitialStateOptions(I, v[I]);
47
- }), r.getState().setInitialStates(g), r.getState().setCreatedState(g);
48
- const $ = (I, a) => {
49
- const [y] = K(a?.componentId ?? ht());
46
+ }, Q(I) || o.getState().setInitialStateOptions(I, v[I]);
47
+ }), o.getState().setInitialStates(f), o.getState().setCreatedState(f);
48
+ const k = (I, a) => {
49
+ const [y] = K(a?.componentId ?? yt());
50
50
  wt({
51
51
  stateKey: I,
52
52
  options: a,
53
53
  initialOptionsPart: v
54
54
  });
55
- const o = r.getState().cogsStateStore[I] || g[I], S = a?.modifyState ? a.modifyState(o) : o, [D, O] = Lt(
55
+ const r = o.getState().cogsStateStore[I] || f[I], S = a?.modifyState ? a.modifyState(r) : r, [D, O] = Lt(
56
56
  S,
57
57
  {
58
58
  stateKey: I,
@@ -73,28 +73,28 @@ const ae = (t, c) => {
73
73
  function p(I, a) {
74
74
  wt({ stateKey: I, options: a, initialOptionsPart: v }), a.localStorage && Wt(I, a), ut(I);
75
75
  }
76
- return { useCogsState: $, setCogsOptions: p };
76
+ return { useCogsState: k, setCogsOptions: p };
77
77
  }, {
78
78
  setUpdaterState: ct,
79
79
  setState: Z,
80
80
  getInitialOptions: Q,
81
81
  getKeyState: kt,
82
- getValidationErrors: Ut,
83
- setStateLog: Rt,
84
- updateInitialStateGlobal: yt,
85
- addValidationError: Ft,
86
- removeValidationError: q,
82
+ getValidationErrors: Rt,
83
+ setStateLog: Ft,
84
+ updateInitialStateGlobal: vt,
85
+ addValidationError: Ut,
86
+ removeValidationError: H,
87
87
  setServerSyncActions: Dt
88
- } = r.getState(), Et = (t, c, h, g, v) => {
88
+ } = o.getState(), Et = (t, c, h, f, v) => {
89
89
  h?.log && console.log(
90
90
  "saving to localstorage",
91
91
  c,
92
92
  h.localStorage?.key,
93
- g
93
+ f
94
94
  );
95
- const $ = J(h?.localStorage?.key) ? h.localStorage?.key(t) : h?.localStorage?.key;
96
- if ($ && g) {
97
- const p = `${g}-${c}-${$}`;
95
+ const k = J(h?.localStorage?.key) ? h.localStorage?.key(t) : h?.localStorage?.key;
96
+ if (k && f) {
97
+ const p = `${f}-${c}-${k}`;
98
98
  let I;
99
99
  try {
100
100
  I = dt(p)?.lastSyncedWithServer;
@@ -119,60 +119,60 @@ const ae = (t, c) => {
119
119
  return console.error("Error loading from localStorage:", c), null;
120
120
  }
121
121
  }, Wt = (t, c) => {
122
- const h = r.getState().cogsStateStore[t], { sessionId: g } = $t(), v = J(c?.localStorage?.key) ? c.localStorage.key(h) : c?.localStorage?.key;
123
- if (v && g) {
124
- const $ = dt(
125
- `${g}-${t}-${v}`
122
+ const h = o.getState().cogsStateStore[t], { sessionId: f } = $t(), v = J(c?.localStorage?.key) ? c.localStorage.key(h) : c?.localStorage?.key;
123
+ if (v && f) {
124
+ const k = dt(
125
+ `${f}-${t}-${v}`
126
126
  );
127
- if ($ && $.lastUpdated > ($.lastSyncedWithServer || 0))
128
- return Z(t, $.state), ut(t), !0;
127
+ if (k && k.lastUpdated > (k.lastSyncedWithServer || 0))
128
+ return Z(t, k.state), ut(t), !0;
129
129
  }
130
130
  return !1;
131
- }, Nt = (t, c, h, g, v, $) => {
131
+ }, Nt = (t, c, h, f, v, k) => {
132
132
  const p = {
133
133
  initialState: c,
134
134
  updaterState: lt(
135
135
  t,
136
- g,
136
+ f,
137
137
  v,
138
- $
138
+ k
139
139
  ),
140
140
  state: h
141
141
  };
142
- yt(t, p.initialState), ct(t, p.updaterState), Z(t, p.state);
142
+ vt(t, p.initialState), ct(t, p.updaterState), Z(t, p.state);
143
143
  }, ut = (t) => {
144
- const c = r.getState().stateComponents.get(t);
144
+ const c = o.getState().stateComponents.get(t);
145
145
  if (!c) return;
146
146
  const h = /* @__PURE__ */ new Set();
147
- c.components.forEach((g) => {
148
- (g ? Array.isArray(g.reactiveType) ? g.reactiveType : [g.reactiveType || "component"] : null)?.includes("none") || h.add(() => g.forceUpdate());
147
+ c.components.forEach((f) => {
148
+ (f ? Array.isArray(f.reactiveType) ? f.reactiveType : [f.reactiveType || "component"] : null)?.includes("none") || h.add(() => f.forceUpdate());
149
149
  }), queueMicrotask(() => {
150
- h.forEach((g) => g());
150
+ h.forEach((f) => f());
151
151
  });
152
152
  }, se = (t, c) => {
153
- const h = r.getState().stateComponents.get(t);
153
+ const h = o.getState().stateComponents.get(t);
154
154
  if (h) {
155
- const g = `${t}////${c}`, v = h.components.get(g);
155
+ const f = `${t}////${c}`, v = h.components.get(f);
156
156
  if ((v ? Array.isArray(v.reactiveType) ? v.reactiveType : [v.reactiveType || "component"] : null)?.includes("none"))
157
157
  return;
158
158
  v && v.forceUpdate();
159
159
  }
160
- }, Gt = (t, c, h, g) => {
160
+ }, Gt = (t, c, h, f) => {
161
161
  switch (t) {
162
162
  case "update":
163
163
  return {
164
- oldValue: B(c, g),
165
- newValue: B(h, g)
164
+ oldValue: z(c, f),
165
+ newValue: z(h, f)
166
166
  };
167
167
  case "insert":
168
168
  return {
169
169
  oldValue: null,
170
170
  // or undefined
171
- newValue: B(h, g)
171
+ newValue: z(h, f)
172
172
  };
173
173
  case "cut":
174
174
  return {
175
- oldValue: B(c, g),
175
+ oldValue: z(c, f),
176
176
  newValue: null
177
177
  // or undefined
178
178
  };
@@ -183,19 +183,19 @@ const ae = (t, c) => {
183
183
  function Lt(t, {
184
184
  stateKey: c,
185
185
  serverSync: h,
186
- localStorage: g,
186
+ localStorage: f,
187
187
  formElements: v,
188
- reactiveDeps: $,
188
+ reactiveDeps: k,
189
189
  reactiveType: p,
190
190
  componentId: I,
191
191
  initialState: a,
192
192
  syncUpdate: y,
193
- dependencies: o,
193
+ dependencies: r,
194
194
  serverState: S
195
195
  } = {}) {
196
- const [D, O] = K({}), { sessionId: U } = $t();
196
+ const [D, O] = K({}), { sessionId: R } = $t();
197
197
  let W = !c;
198
- const [m] = K(c ?? ht()), l = r.getState().stateLog[m], ot = H(/* @__PURE__ */ new Set()), Y = H(I ?? ht()), j = H(
198
+ const [m] = K(c ?? yt()), l = o.getState().stateLog[m], ot = q(/* @__PURE__ */ new Set()), Y = q(I ?? yt()), j = q(
199
199
  null
200
200
  );
201
201
  j.current = Q(m) ?? null, st(() => {
@@ -205,7 +205,7 @@ function Lt(t, {
205
205
  [y.path[0]]: y.newValue
206
206
  }));
207
207
  const e = `${y.stateKey}:${y.path.join(".")}`;
208
- r.getState().setSyncInfo(e, {
208
+ o.getState().setSyncInfo(e, {
209
209
  timeStamp: y.timeStamp,
210
210
  userId: y.userId
211
211
  });
@@ -215,174 +215,160 @@ function Lt(t, {
215
215
  pt(m, {
216
216
  initialState: a
217
217
  });
218
- const e = j.current, s = e?.serverState?.id !== void 0 && e?.serverState?.status === "success" && e?.serverState?.data, i = r.getState().initialStateGlobal[m];
218
+ const e = j.current, s = e?.serverState?.id !== void 0 && e?.serverState?.status === "success" && e?.serverState?.data, i = o.getState().initialStateGlobal[m];
219
219
  if (!(i && !L(i, a) || !i) && !s)
220
220
  return;
221
- let d = null;
222
- const k = J(e?.localStorage?.key) ? e?.localStorage?.key(a) : e?.localStorage?.key;
223
- k && U && (d = dt(`${U}-${m}-${k}`));
224
- let w = a, A = !1;
225
- const x = s ? Date.now() : 0, C = d?.lastUpdated || 0, V = d?.lastSyncedWithServer || 0;
226
- s && x > C ? (w = e.serverState.data, A = !0) : d && C > V && (w = d.state, e?.localStorage?.onChange && e?.localStorage?.onChange(w)), r.getState().initializeShadowState(m, a), Nt(
221
+ let u = null;
222
+ const E = J(e?.localStorage?.key) ? e?.localStorage?.key(a) : e?.localStorage?.key;
223
+ E && R && (u = dt(`${R}-${m}-${E}`));
224
+ let w = a, $ = !1;
225
+ const x = s ? Date.now() : 0, C = u?.lastUpdated || 0, V = u?.lastSyncedWithServer || 0;
226
+ s && x > C ? (w = e.serverState.data, $ = !0) : u && C > V && (w = u.state, e?.localStorage?.onChange && e?.localStorage?.onChange(w)), o.getState().initializeShadowState(m, a), Nt(
227
227
  m,
228
228
  a,
229
229
  w,
230
230
  tt,
231
231
  Y.current,
232
- U
233
- ), A && k && U && Et(w, m, e, U, Date.now()), ut(m), (Array.isArray(p) ? p : [p || "component"]).includes("none") || O({});
232
+ R
233
+ ), $ && E && R && Et(w, m, e, R, Date.now()), ut(m), (Array.isArray(p) ? p : [p || "component"]).includes("none") || O({});
234
234
  }
235
235
  }, [
236
236
  a,
237
237
  S?.status,
238
238
  S?.data,
239
- ...o || []
239
+ ...r || []
240
240
  ]), it(() => {
241
241
  W && pt(m, {
242
242
  serverSync: h,
243
243
  formElements: v,
244
244
  initialState: a,
245
- localStorage: g,
245
+ localStorage: f,
246
246
  middleware: j.current?.middleware
247
247
  });
248
- const e = `${m}////${Y.current}`, n = r.getState().stateComponents.get(m) || {
248
+ const e = `${m}////${Y.current}`, n = o.getState().stateComponents.get(m) || {
249
249
  components: /* @__PURE__ */ new Map()
250
250
  };
251
251
  return n.components.set(e, {
252
252
  forceUpdate: () => O({}),
253
253
  paths: /* @__PURE__ */ new Set(),
254
254
  deps: [],
255
- depsFunction: $ || void 0,
255
+ depsFunction: k || void 0,
256
256
  reactiveType: p ?? ["component", "deps"]
257
- }), r.getState().stateComponents.set(m, n), O({}), () => {
258
- n && (n.components.delete(e), n.components.size === 0 && r.getState().stateComponents.delete(m));
257
+ }), o.getState().stateComponents.set(m, n), O({}), () => {
258
+ n && (n.components.delete(e), n.components.size === 0 && o.getState().stateComponents.delete(m));
259
259
  };
260
260
  }, []);
261
261
  const tt = (e, n, s, i) => {
262
262
  if (Array.isArray(n)) {
263
- const f = `${m}-${n.join(".")}`;
264
- ot.current.add(f);
263
+ const u = `${m}-${n.join(".")}`;
264
+ ot.current.add(u);
265
265
  }
266
- Z(m, (f) => {
267
- const d = J(e) ? e(f) : e, k = `${m}-${n.join(".")}`;
268
- if (k) {
269
- let P = !1, N = r.getState().signalDomElements.get(k);
266
+ const g = o.getState();
267
+ Z(m, (u) => {
268
+ const E = J(e) ? e(u) : e, w = `${m}-${n.join(".")}`;
269
+ if (w) {
270
+ let M = !1, N = g.signalDomElements.get(w);
270
271
  if ((!N || N.size === 0) && (s.updateType === "insert" || s.updateType === "cut")) {
271
- const T = n.slice(0, -1), _ = B(d, T);
272
+ const T = n.slice(0, -1), _ = z(E, T);
272
273
  if (Array.isArray(_)) {
273
- P = !0;
274
- const E = `${m}-${T.join(".")}`;
275
- N = r.getState().signalDomElements.get(E);
274
+ M = !0;
275
+ const A = `${m}-${T.join(".")}`;
276
+ N = g.signalDomElements.get(A);
276
277
  }
277
278
  }
278
279
  if (N) {
279
- const T = P ? B(d, n.slice(0, -1)) : B(d, n);
280
- N.forEach(({ parentId: _, position: E, effect: M }) => {
280
+ const T = M ? z(E, n.slice(0, -1)) : z(E, n);
281
+ N.forEach(({ parentId: _, position: A, effect: P }) => {
281
282
  const b = document.querySelector(
282
283
  `[data-parent-id="${_}"]`
283
284
  );
284
285
  if (b) {
285
- const F = Array.from(b.childNodes);
286
- if (F[E]) {
287
- const R = M ? new Function("state", `return (${M})(state)`)(T) : T;
288
- F[E].textContent = String(R);
286
+ const U = Array.from(b.childNodes);
287
+ if (U[A]) {
288
+ const F = P ? new Function("state", `return (${P})(state)`)(T) : T;
289
+ U[A].textContent = String(F);
289
290
  }
290
291
  }
291
292
  });
292
293
  }
293
294
  }
294
- (() => {
295
- const P = r.getState();
296
- switch (s.updateType) {
297
- case "update":
298
- P.updateShadowAtPath(m, n, d);
299
- break;
300
- case "insert":
301
- const N = n.slice(0, -1);
302
- P.insertShadowArrayElement(m, N);
303
- break;
304
- case "cut":
305
- const T = n.slice(0, -1), _ = parseInt(n[n.length - 1]);
306
- P.removeShadowArrayElement(m, T, _);
307
- break;
308
- }
309
- })(), console.log("shadowState", r.getState().shadowStateStore), s.updateType === "update" && (i || j.current?.validation?.key) && n && q(
295
+ console.log("shadowState", g.shadowStateStore), s.updateType === "update" && (i || j.current?.validation?.key) && n && H(
310
296
  (i || j.current?.validation?.key) + "." + n.join(".")
311
297
  );
312
- const A = n.slice(0, n.length - 1);
313
- s.updateType === "cut" && j.current?.validation?.key && q(
314
- j.current?.validation?.key + "." + A.join(".")
315
- ), s.updateType === "insert" && j.current?.validation?.key && Ut(
316
- j.current?.validation?.key + "." + A.join(".")
298
+ const $ = n.slice(0, n.length - 1);
299
+ s.updateType === "cut" && j.current?.validation?.key && H(
300
+ j.current?.validation?.key + "." + $.join(".")
301
+ ), s.updateType === "insert" && j.current?.validation?.key && Rt(
302
+ j.current?.validation?.key + "." + $.join(".")
317
303
  ).filter(([N, T]) => {
318
304
  let _ = N?.split(".").length;
319
- if (N == A.join(".") && _ == A.length - 1) {
320
- let E = N + "." + A;
321
- q(N), Ft(E, T);
305
+ if (N == $.join(".") && _ == $.length - 1) {
306
+ let A = N + "." + $;
307
+ H(N), Ut(A, T);
322
308
  }
323
309
  });
324
- const x = r.getState().stateComponents.get(m);
310
+ const x = g.stateComponents.get(m);
325
311
  if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", x), x) {
326
- const P = mt(f, d), N = new Set(P), T = s.updateType === "update" ? n.join(".") : n.slice(0, -1).join(".") || "";
312
+ const M = ht(u, E), N = new Set(M), T = s.updateType === "update" ? n.join(".") : n.slice(0, -1).join(".") || "";
327
313
  for (const [
328
314
  _,
329
- E
315
+ A
330
316
  ] of x.components.entries()) {
331
- let M = !1;
332
- const b = Array.isArray(E.reactiveType) ? E.reactiveType : [E.reactiveType || "component"];
333
- if (console.log("component", E), !b.includes("none")) {
317
+ let P = !1;
318
+ const b = Array.isArray(A.reactiveType) ? A.reactiveType : [A.reactiveType || "component"];
319
+ if (console.log("component", A), !b.includes("none")) {
334
320
  if (b.includes("all")) {
335
- E.forceUpdate();
321
+ A.forceUpdate();
336
322
  continue;
337
323
  }
338
- if (b.includes("component") && ((E.paths.has(T) || E.paths.has("")) && (M = !0), !M))
339
- for (const F of N) {
340
- let R = F;
324
+ if (b.includes("component") && ((A.paths.has(T) || A.paths.has("")) && (P = !0), !P))
325
+ for (const U of N) {
326
+ let F = U;
341
327
  for (; ; ) {
342
- if (E.paths.has(R)) {
343
- M = !0;
328
+ if (A.paths.has(F)) {
329
+ P = !0;
344
330
  break;
345
331
  }
346
- const X = R.lastIndexOf(".");
332
+ const X = F.lastIndexOf(".");
347
333
  if (X !== -1) {
348
- const et = R.substring(
334
+ const et = F.substring(
349
335
  0,
350
336
  X
351
337
  );
352
338
  if (!isNaN(
353
- Number(R.substring(X + 1))
354
- ) && E.paths.has(et)) {
355
- M = !0;
339
+ Number(F.substring(X + 1))
340
+ ) && A.paths.has(et)) {
341
+ P = !0;
356
342
  break;
357
343
  }
358
- R = et;
344
+ F = et;
359
345
  } else
360
- R = "";
361
- if (R === "")
346
+ F = "";
347
+ if (F === "")
362
348
  break;
363
349
  }
364
- if (M) break;
350
+ if (P) break;
365
351
  }
366
- if (!M && b.includes("deps") && E.depsFunction) {
367
- const F = E.depsFunction(d);
368
- let R = !1;
369
- typeof F == "boolean" ? F && (R = !0) : L(E.deps, F) || (E.deps = F, R = !0), R && (M = !0);
352
+ if (!P && b.includes("deps") && A.depsFunction) {
353
+ const U = A.depsFunction(E);
354
+ let F = !1;
355
+ typeof U == "boolean" ? U && (F = !0) : L(A.deps, U) || (A.deps = U, F = !0), F && (P = !0);
370
356
  }
371
- M && E.forceUpdate();
357
+ P && A.forceUpdate();
372
358
  }
373
359
  }
374
360
  }
375
361
  const C = Date.now();
376
- n = n.map((P, N) => {
377
- const T = n.slice(0, -1), _ = B(d, T);
378
- return N === n.length - 1 && ["insert", "cut"].includes(s.updateType) ? (_.length - 1).toString() : P;
362
+ n = n.map((M, N) => {
363
+ const T = n.slice(0, -1), _ = z(E, T);
364
+ return N === n.length - 1 && ["insert", "cut"].includes(s.updateType) ? (_.length - 1).toString() : M;
379
365
  });
380
366
  const { oldValue: V, newValue: G } = Gt(
381
367
  s.updateType,
382
- f,
383
- d,
368
+ u,
369
+ E,
384
370
  n
385
- ), z = {
371
+ ), B = {
386
372
  timeStamp: C,
387
373
  stateKey: m,
388
374
  path: n,
@@ -391,113 +377,126 @@ function Lt(t, {
391
377
  oldValue: V,
392
378
  newValue: G
393
379
  };
394
- if (Rt(m, (P) => {
395
- const T = [...P ?? [], z].reduce((_, E) => {
396
- const M = `${E.stateKey}:${JSON.stringify(E.path)}`, b = _.get(M);
397
- return b ? (b.timeStamp = Math.max(b.timeStamp, E.timeStamp), b.newValue = E.newValue, b.oldValue = b.oldValue ?? E.oldValue, b.updateType = E.updateType) : _.set(M, { ...E }), _;
380
+ switch (s.updateType) {
381
+ case "update":
382
+ g.updateShadowAtPath(m, n, E);
383
+ break;
384
+ case "insert":
385
+ const M = n.slice(0, -1);
386
+ g.insertShadowArrayElement(m, M);
387
+ break;
388
+ case "cut":
389
+ const N = n.slice(0, -1), T = parseInt(n[n.length - 1]);
390
+ g.removeShadowArrayElement(m, N, T);
391
+ break;
392
+ }
393
+ if (Ft(m, (M) => {
394
+ const T = [...M ?? [], B].reduce((_, A) => {
395
+ const P = `${A.stateKey}:${JSON.stringify(A.path)}`, b = _.get(P);
396
+ return b ? (b.timeStamp = Math.max(b.timeStamp, A.timeStamp), b.newValue = A.newValue, b.oldValue = b.oldValue ?? A.oldValue, b.updateType = A.updateType) : _.set(P, { ...A }), _;
398
397
  }, /* @__PURE__ */ new Map());
399
398
  return Array.from(T.values());
400
399
  }), Et(
401
- d,
400
+ E,
402
401
  m,
403
402
  j.current,
404
- U
403
+ R
405
404
  ), j.current?.middleware && j.current.middleware({
406
405
  updateLog: l,
407
- update: z
406
+ update: B
408
407
  }), j.current?.serverSync) {
409
- const P = r.getState().serverState[m], N = j.current?.serverSync;
408
+ const M = g.serverState[m], N = j.current?.serverSync;
410
409
  Dt(m, {
411
- syncKey: typeof N.syncKey == "string" ? N.syncKey : N.syncKey({ state: d }),
412
- rollBackState: P,
410
+ syncKey: typeof N.syncKey == "string" ? N.syncKey : N.syncKey({ state: E }),
411
+ rollBackState: M,
413
412
  actionTimeStamp: Date.now() + (N.debounce ?? 3e3),
414
413
  status: "waiting"
415
414
  });
416
415
  }
417
- return d;
416
+ return E;
418
417
  });
419
418
  };
420
- r.getState().updaterState[m] || (ct(
419
+ o.getState().updaterState[m] || (ct(
421
420
  m,
422
421
  lt(
423
422
  m,
424
423
  tt,
425
424
  Y.current,
426
- U
425
+ R
427
426
  )
428
- ), r.getState().cogsStateStore[m] || Z(m, t), r.getState().initialStateGlobal[m] || yt(m, t));
429
- const u = Tt(() => lt(
427
+ ), o.getState().cogsStateStore[m] || Z(m, t), o.getState().initialStateGlobal[m] || vt(m, t));
428
+ const d = Tt(() => lt(
430
429
  m,
431
430
  tt,
432
431
  Y.current,
433
- U
434
- ), [m, U]);
435
- return [kt(m), u];
432
+ R
433
+ ), [m, R]);
434
+ return [kt(m), d];
436
435
  }
437
- function lt(t, c, h, g) {
436
+ function lt(t, c, h, f) {
438
437
  const v = /* @__PURE__ */ new Map();
439
- let $ = 0;
438
+ let k = 0;
440
439
  const p = (y) => {
441
- const o = y.join(".");
440
+ const r = y.join(".");
442
441
  for (const [S] of v)
443
- (S === o || S.startsWith(o + ".")) && v.delete(S);
444
- $++;
442
+ (S === r || S.startsWith(r + ".")) && v.delete(S);
443
+ k++;
445
444
  }, I = {
446
445
  removeValidation: (y) => {
447
- y?.validationKey && q(y.validationKey);
446
+ y?.validationKey && H(y.validationKey);
448
447
  },
449
448
  revertToInitialState: (y) => {
450
- const o = r.getState().getInitialOptions(t)?.validation;
451
- o?.key && q(o?.key), y?.validationKey && q(y.validationKey);
452
- const S = r.getState().initialStateGlobal[t];
453
- r.getState().clearSelectedIndexesForState(t), v.clear(), $++;
454
- const D = a(S, []), O = Q(t), U = J(O?.localStorage?.key) ? O?.localStorage?.key(S) : O?.localStorage?.key, W = `${g}-${t}-${U}`;
449
+ const r = o.getState().getInitialOptions(t)?.validation;
450
+ r?.key && H(r?.key), y?.validationKey && H(y.validationKey);
451
+ const S = o.getState().initialStateGlobal[t];
452
+ o.getState().clearSelectedIndexesForState(t), v.clear(), k++;
453
+ const D = a(S, []), O = Q(t), R = J(O?.localStorage?.key) ? O?.localStorage?.key(S) : O?.localStorage?.key, W = `${f}-${t}-${R}`;
455
454
  W && localStorage.removeItem(W), ct(t, D), Z(t, S);
456
- const m = r.getState().stateComponents.get(t);
455
+ const m = o.getState().stateComponents.get(t);
457
456
  return m && m.components.forEach((l) => {
458
457
  l.forceUpdate();
459
458
  }), S;
460
459
  },
461
460
  updateInitialState: (y) => {
462
- v.clear(), $++;
463
- const o = lt(
461
+ v.clear(), k++;
462
+ const r = lt(
464
463
  t,
465
464
  c,
466
465
  h,
467
- g
468
- ), S = r.getState().initialStateGlobal[t], D = Q(t), O = J(D?.localStorage?.key) ? D?.localStorage?.key(S) : D?.localStorage?.key, U = `${g}-${t}-${O}`;
469
- return localStorage.getItem(U) && localStorage.removeItem(U), Vt(() => {
470
- yt(t, y), r.getState().initializeShadowState(t, y), ct(t, o), Z(t, y);
471
- const W = r.getState().stateComponents.get(t);
466
+ f
467
+ ), S = o.getState().initialStateGlobal[t], D = Q(t), O = J(D?.localStorage?.key) ? D?.localStorage?.key(S) : D?.localStorage?.key, R = `${f}-${t}-${O}`;
468
+ return localStorage.getItem(R) && localStorage.removeItem(R), Vt(() => {
469
+ vt(t, y), o.getState().initializeShadowState(t, y), ct(t, r), Z(t, y);
470
+ const W = o.getState().stateComponents.get(t);
472
471
  W && W.components.forEach((m) => {
473
472
  m.forceUpdate();
474
473
  });
475
474
  }), {
476
- fetchId: (W) => o.get()[W]
475
+ fetchId: (W) => r.get()[W]
477
476
  };
478
477
  },
479
- _initialState: r.getState().initialStateGlobal[t],
480
- _serverState: r.getState().serverState[t],
481
- _isLoading: r.getState().isLoadingGlobal[t],
478
+ _initialState: o.getState().initialStateGlobal[t],
479
+ _serverState: o.getState().serverState[t],
480
+ _isLoading: o.getState().isLoadingGlobal[t],
482
481
  _isServerSynced: () => {
483
- const y = r.getState().serverState[t];
482
+ const y = o.getState().serverState[t];
484
483
  return !!(y && L(y, kt(t)));
485
484
  }
486
485
  };
487
- function a(y, o = [], S) {
488
- const D = o.map(String).join(".");
486
+ function a(y, r = [], S) {
487
+ const D = r.map(String).join(".");
489
488
  v.get(D);
490
489
  const O = function() {
491
- return r().getNestedState(t, o);
490
+ return o().getNestedState(t, r);
492
491
  };
493
492
  Object.keys(I).forEach((m) => {
494
493
  O[m] = I[m];
495
494
  });
496
- const U = {
495
+ const R = {
497
496
  apply(m, l, ot) {
498
497
  return console.log(
499
- `PROXY APPLY TRAP HIT: stateKey=${t}, path=${o.join(".")}`
500
- ), console.trace("Apply trap stack trace"), r().getNestedState(t, o);
498
+ `PROXY APPLY TRAP HIT: stateKey=${t}, path=${r.join(".")}`
499
+ ), console.trace("Apply trap stack trace"), o().getNestedState(t, r);
501
500
  },
502
501
  get(m, l) {
503
502
  S?.validIndices && !Array.isArray(y) && (S = { ...S, validIndices: void 0 });
@@ -525,14 +524,14 @@ function lt(t, c, h, g) {
525
524
  "getComponents"
526
525
  ]);
527
526
  if (l !== "then" && !l.startsWith("$") && l !== "stateMapNoRender" && !ot.has(l)) {
528
- const u = `${t}////${h}`, e = r.getState().stateComponents.get(t);
527
+ const d = `${t}////${h}`, e = o.getState().stateComponents.get(t);
529
528
  if (e) {
530
- const n = e.components.get(u);
529
+ const n = e.components.get(d);
531
530
  if (n && !n.paths.has("")) {
532
- const s = o.join(".");
531
+ const s = r.join(".");
533
532
  let i = !0;
534
- for (const f of n.paths)
535
- if (s.startsWith(f) && (s === f || s[f.length] === ".")) {
533
+ for (const g of n.paths)
534
+ if (s.startsWith(g) && (s === g || s[g.length] === ".")) {
536
535
  i = !1;
537
536
  break;
538
537
  }
@@ -541,26 +540,26 @@ function lt(t, c, h, g) {
541
540
  }
542
541
  }
543
542
  if (l === "getDifferences")
544
- return () => mt(
545
- r.getState().cogsStateStore[t],
546
- r.getState().initialStateGlobal[t]
543
+ return () => ht(
544
+ o.getState().cogsStateStore[t],
545
+ o.getState().initialStateGlobal[t]
547
546
  );
548
- if (l === "sync" && o.length === 0)
547
+ if (l === "sync" && r.length === 0)
549
548
  return async function() {
550
- const u = r.getState().getInitialOptions(t), e = u?.sync;
549
+ const d = o.getState().getInitialOptions(t), e = d?.sync;
551
550
  if (!e)
552
551
  return console.error(`No mutation defined for state key "${t}"`), { success: !1, error: "No mutation defined" };
553
- const n = r.getState().getNestedState(t, []), s = u?.validation?.key;
552
+ const n = o.getState().getNestedState(t, []), s = d?.validation?.key;
554
553
  try {
555
554
  const i = await e.action(n);
556
555
  if (i && !i.success && i.errors && s) {
557
- r.getState().removeValidationError(s), i.errors.forEach((d) => {
558
- const k = [s, ...d.path].join(".");
559
- r.getState().addValidationError(k, d.message);
556
+ o.getState().removeValidationError(s), i.errors.forEach((u) => {
557
+ const E = [s, ...u.path].join(".");
558
+ o.getState().addValidationError(E, u.message);
560
559
  });
561
- const f = r.getState().stateComponents.get(t);
562
- f && f.components.forEach((d) => {
563
- d.forceUpdate();
560
+ const g = o.getState().stateComponents.get(t);
561
+ g && g.components.forEach((u) => {
562
+ u.forceUpdate();
564
563
  });
565
564
  }
566
565
  return i?.success && e.onSuccess ? e.onSuccess(i.data) : !i?.success && e.onError && e.onError(i.error), i;
@@ -569,115 +568,117 @@ function lt(t, c, h, g) {
569
568
  }
570
569
  };
571
570
  if (l === "_status") {
572
- const u = r.getState().getNestedState(t, o), e = r.getState().initialStateGlobal[t], n = B(e, o);
573
- return L(u, n) ? "fresh" : "stale";
571
+ const d = o.getState().getNestedState(t, r), e = o.getState().initialStateGlobal[t], n = z(e, r);
572
+ return L(d, n) ? "fresh" : "stale";
574
573
  }
575
574
  if (l === "getStatus")
576
575
  return function() {
577
- const u = r().getNestedState(
576
+ const d = o().getNestedState(
578
577
  t,
579
- o
580
- ), e = r.getState().initialStateGlobal[t], n = B(e, o);
581
- return L(u, n) ? "fresh" : "stale";
578
+ r
579
+ ), e = o.getState().initialStateGlobal[t], n = z(e, r);
580
+ return L(d, n) ? "fresh" : "stale";
582
581
  };
583
582
  if (l === "removeStorage")
584
583
  return () => {
585
- const u = r.getState().initialStateGlobal[t], e = Q(t), n = J(e?.localStorage?.key) ? e?.localStorage?.key(u) : e?.localStorage?.key, s = `${g}-${t}-${n}`;
584
+ const d = o.getState().initialStateGlobal[t], e = Q(t), n = J(e?.localStorage?.key) ? e?.localStorage?.key(d) : e?.localStorage?.key, s = `${f}-${t}-${n}`;
586
585
  s && localStorage.removeItem(s);
587
586
  };
588
587
  if (l === "showValidationErrors")
589
588
  return () => {
590
- const u = r.getState().getInitialOptions(t)?.validation;
591
- if (!u?.key)
589
+ const d = o.getState().getInitialOptions(t)?.validation;
590
+ if (!d?.key)
592
591
  throw new Error("Validation key not found");
593
- return r.getState().getValidationErrors(u.key + "." + o.join("."));
592
+ return o.getState().getValidationErrors(d.key + "." + r.join("."));
594
593
  };
595
594
  if (Array.isArray(y)) {
596
- const u = () => S?.validIndices ? y.map((n, s) => ({
595
+ const d = () => S?.validIndices ? y.map((n, s) => ({
597
596
  item: n,
598
597
  originalIndex: S.validIndices[s]
599
- })) : r.getState().getNestedState(t, o).map((n, s) => ({
598
+ })) : o.getState().getNestedState(t, r).map((n, s) => ({
600
599
  item: n,
601
600
  originalIndex: s
602
601
  }));
603
602
  if (l === "getSelected")
604
603
  return () => {
605
- const e = r.getState().getSelectedIndex(t, o.join("."));
604
+ const e = o.getState().getSelectedIndex(t, r.join("."));
606
605
  if (e !== void 0)
607
606
  return a(
608
607
  y[e],
609
- [...o, e.toString()],
608
+ [...r, e.toString()],
610
609
  S
611
610
  );
612
611
  };
613
612
  if (l === "clearSelected")
614
613
  return () => {
615
- r.getState().clearSelectedIndex({ stateKey: t, path: o });
614
+ o.getState().clearSelectedIndex({ stateKey: t, path: r });
616
615
  };
617
616
  if (l === "getSelectedIndex")
618
- return () => r.getState().getSelectedIndex(t, o.join(".")) ?? -1;
617
+ return () => o.getState().getSelectedIndex(t, r.join(".")) ?? -1;
619
618
  if (l === "useVirtualView")
620
619
  return (e) => {
621
620
  const {
622
621
  itemHeight: n,
623
622
  overscan: s = 5,
624
623
  stickToBottom: i = !1
625
- } = e, f = H(null), [d, k] = K({
624
+ } = e, g = q(null), [u, E] = K({
626
625
  startIndex: 0,
627
626
  endIndex: 10
628
- }), w = H(i), A = H(0), x = H(!0), C = r().getNestedState(
627
+ });
628
+ ft((T) => o.getState().getShadowMetadata(t, [...r, T.toString()])?.virtualizer?.itemHeight || e.itemHeight, []);
629
+ const w = q(i), $ = q(0), x = q(!0), C = o().getNestedState(
629
630
  t,
630
- o
631
+ r
631
632
  ), V = C.length, G = Tt(() => {
632
- const T = Math.max(0, d.startIndex), _ = Math.min(V, d.endIndex), E = Array.from(
633
+ const T = Math.max(0, u.startIndex), _ = Math.min(V, u.endIndex), A = Array.from(
633
634
  { length: _ - T },
634
- (b, F) => T + F
635
- ), M = E.map((b) => C[b]);
636
- return a(M, o, {
635
+ (b, U) => T + U
636
+ ), P = A.map((b) => C[b]);
637
+ return a(P, r, {
637
638
  ...S,
638
- validIndices: E
639
+ validIndices: A
639
640
  });
640
- }, [d.startIndex, d.endIndex, C, V]);
641
+ }, [u.startIndex, u.endIndex, C, V]);
641
642
  it(() => {
642
- const T = f.current;
643
+ const T = g.current;
643
644
  if (!T) return;
644
- const _ = w.current, E = V > A.current;
645
- A.current = V;
646
- const M = () => {
647
- const { scrollTop: b, clientHeight: F, scrollHeight: R } = T;
648
- w.current = R - b - F < 10;
645
+ const _ = w.current, A = V > $.current;
646
+ $.current = V;
647
+ const P = () => {
648
+ const { scrollTop: b, clientHeight: U, scrollHeight: F } = T;
649
+ w.current = F - b - U < 10;
649
650
  const X = Math.max(
650
651
  0,
651
652
  Math.floor(b / n) - s
652
653
  ), et = Math.min(
653
654
  V,
654
- Math.ceil((b + F) / n) + s
655
+ Math.ceil((b + U) / n) + s
655
656
  );
656
- k((gt) => gt.startIndex !== X || gt.endIndex !== et ? { startIndex: X, endIndex: et } : gt);
657
+ E((gt) => gt.startIndex !== X || gt.endIndex !== et ? { startIndex: X, endIndex: et } : gt);
657
658
  };
658
- return T.addEventListener("scroll", M, {
659
+ return T.addEventListener("scroll", P, {
659
660
  passive: !0
660
661
  }), i && (x.current ? T.scrollTo({
661
662
  top: T.scrollHeight,
662
663
  behavior: "auto"
663
- }) : _ && E && requestAnimationFrame(() => {
664
+ }) : _ && A && requestAnimationFrame(() => {
664
665
  T.scrollTo({
665
666
  top: T.scrollHeight,
666
667
  behavior: "smooth"
667
668
  });
668
- })), x.current = !1, M(), () => T.removeEventListener("scroll", M);
669
+ })), x.current = !1, P(), () => T.removeEventListener("scroll", P);
669
670
  }, [V, n, s, i]);
670
- const z = vt(
671
+ const B = ft(
671
672
  (T = "smooth") => {
672
- f.current && f.current.scrollTo({
673
- top: f.current.scrollHeight,
673
+ g.current && g.current.scrollTo({
674
+ top: g.current.scrollHeight,
674
675
  behavior: T
675
676
  });
676
677
  },
677
678
  []
678
- ), P = vt(
679
+ ), M = ft(
679
680
  (T, _ = "smooth") => {
680
- f.current && f.current.scrollTo({
681
+ g.current && g.current.scrollTo({
681
682
  top: T * n,
682
683
  behavior: _
683
684
  });
@@ -685,7 +686,7 @@ function lt(t, c, h, g) {
685
686
  [n]
686
687
  ), N = {
687
688
  outer: {
688
- ref: f,
689
+ ref: g,
689
690
  style: { overflowY: "auto", height: "100%" }
690
691
  },
691
692
  inner: {
@@ -696,88 +697,88 @@ function lt(t, c, h, g) {
696
697
  },
697
698
  list: {
698
699
  style: {
699
- transform: `translateY(${d.startIndex * n}px)`
700
+ transform: `translateY(${u.startIndex * n}px)`
700
701
  }
701
702
  }
702
703
  };
703
704
  return {
704
705
  virtualState: G,
705
706
  virtualizerProps: N,
706
- scrollToBottom: z,
707
- scrollToIndex: P
707
+ scrollToBottom: B,
708
+ scrollToIndex: M
708
709
  };
709
710
  };
710
711
  if (l === "stateSort")
711
712
  return (e) => {
712
- const s = [...u()].sort(
713
- (d, k) => e(d.item, k.item)
714
- ), i = s.map(({ item: d }) => d), f = {
713
+ const s = [...d()].sort(
714
+ (u, E) => e(u.item, E.item)
715
+ ), i = s.map(({ item: u }) => u), g = {
715
716
  ...S,
716
717
  validIndices: s.map(
717
- ({ originalIndex: d }) => d
718
+ ({ originalIndex: u }) => u
718
719
  )
719
720
  };
720
- return a(i, o, f);
721
+ return a(i, r, g);
721
722
  };
722
723
  if (l === "stateFilter")
723
724
  return (e) => {
724
- const s = u().filter(
725
- ({ item: d }, k) => e(d, k)
726
- ), i = s.map(({ item: d }) => d), f = {
725
+ const s = d().filter(
726
+ ({ item: u }, E) => e(u, E)
727
+ ), i = s.map(({ item: u }) => u), g = {
727
728
  ...S,
728
729
  validIndices: s.map(
729
- ({ originalIndex: d }) => d
730
+ ({ originalIndex: u }) => u
730
731
  )
731
732
  };
732
- return a(i, o, f);
733
+ return a(i, r, g);
733
734
  };
734
735
  if (l === "stateMap")
735
736
  return (e) => {
736
- const n = r.getState().getNestedState(t, o);
737
- return Array.isArray(n) ? (S?.validIndices || Array.from({ length: n.length }, (i, f) => f)).map((i, f) => {
738
- const d = n[i], k = [...o, i.toString()], w = a(d, k, S);
739
- return e(d, w, {
737
+ const n = o.getState().getNestedState(t, r);
738
+ return Array.isArray(n) ? (S?.validIndices || Array.from({ length: n.length }, (i, g) => g)).map((i, g) => {
739
+ const u = n[i], E = [...r, i.toString()], w = a(u, E, S);
740
+ return e(u, w, {
740
741
  register: () => {
741
- const [, x] = K({}), C = `${h}-${o.join(".")}-${i}`;
742
+ const [, x] = K({}), C = `${h}-${r.join(".")}-${i}`;
742
743
  it(() => {
743
- const V = `${t}////${C}`, G = r.getState().stateComponents.get(t) || {
744
+ const V = `${t}////${C}`, G = o.getState().stateComponents.get(t) || {
744
745
  components: /* @__PURE__ */ new Map()
745
746
  };
746
747
  return G.components.set(V, {
747
748
  forceUpdate: () => x({}),
748
- paths: /* @__PURE__ */ new Set([k.join(".")])
749
- }), r.getState().stateComponents.set(t, G), () => {
750
- const z = r.getState().stateComponents.get(t);
751
- z && z.components.delete(V);
749
+ paths: /* @__PURE__ */ new Set([E.join(".")])
750
+ }), o.getState().stateComponents.set(t, G), () => {
751
+ const B = o.getState().stateComponents.get(t);
752
+ B && B.components.delete(V);
752
753
  };
753
754
  }, [t, C]);
754
755
  },
755
- index: f,
756
+ index: g,
756
757
  originalIndex: i
757
758
  });
758
759
  }) : (console.warn(
759
- `stateMap called on a non-array value at path: ${o.join(".")}. The current value is:`,
760
+ `stateMap called on a non-array value at path: ${r.join(".")}. The current value is:`,
760
761
  n
761
762
  ), null);
762
763
  };
763
764
  if (l === "stateMapNoRender")
764
765
  return (e) => y.map((s, i) => {
765
- let f;
766
- S?.validIndices && S.validIndices[i] !== void 0 ? f = S.validIndices[i] : f = i;
767
- const d = [...o, f.toString()], k = a(s, d, S);
766
+ let g;
767
+ S?.validIndices && S.validIndices[i] !== void 0 ? g = S.validIndices[i] : g = i;
768
+ const u = [...r, g.toString()], E = a(s, u, S);
768
769
  return e(
769
770
  s,
770
- k,
771
+ E,
771
772
  i,
772
773
  y,
773
- a(y, o, S)
774
+ a(y, r, S)
774
775
  );
775
776
  });
776
777
  if (l === "$stateMap")
777
- return (e) => rt(Bt, {
778
+ return (e) => rt(zt, {
778
779
  proxy: {
779
780
  _stateKey: t,
780
- _path: o,
781
+ _path: r,
781
782
  _mapFn: e
782
783
  // Pass the actual function, not string
783
784
  },
@@ -785,122 +786,122 @@ function lt(t, c, h, g) {
785
786
  });
786
787
  if (l === "stateList")
787
788
  return (e) => {
788
- const n = r.getState().getNestedState(t, o);
789
- return Array.isArray(n) ? (S?.validIndices || Array.from({ length: n.length }, (i, f) => f)).map((i, f) => {
790
- const d = n[i], k = [...o, i.toString()], w = a(d, k, S), A = `${h}-${o.join(".")}-${i}`;
791
- return rt(qt, {
789
+ const n = o.getState().getNestedState(t, r);
790
+ return Array.isArray(n) ? (S?.validIndices || Array.from({ length: n.length }, (i, g) => g)).map((i, g) => {
791
+ const u = n[i], E = [...r, i.toString()], w = a(u, E, S), $ = `${h}-${r.join(".")}-${i}`;
792
+ return rt(Ht, {
792
793
  key: i,
793
794
  stateKey: t,
794
- itemComponentId: A,
795
- itemPath: k,
795
+ itemComponentId: $,
796
+ itemPath: E,
796
797
  children: e(
797
- d,
798
+ u,
798
799
  w,
799
- f,
800
+ g,
800
801
  n,
801
- a(n, o, S)
802
+ a(n, r, S)
802
803
  )
803
804
  });
804
805
  }) : (console.warn(
805
- `stateList called on a non-array value at path: ${o.join(".")}.`
806
+ `stateList called on a non-array value at path: ${r.join(".")}.`
806
807
  ), null);
807
808
  };
808
809
  if (l === "stateFlattenOn")
809
810
  return (e) => {
810
811
  const n = y;
811
- v.clear(), $++;
812
+ v.clear(), k++;
812
813
  const s = n.flatMap(
813
814
  (i) => i[e] ?? []
814
815
  );
815
816
  return a(
816
817
  s,
817
- [...o, "[*]", e],
818
+ [...r, "[*]", e],
818
819
  S
819
820
  );
820
821
  };
821
822
  if (l === "index")
822
823
  return (e) => {
823
824
  const n = y[e];
824
- return a(n, [...o, e.toString()]);
825
+ return a(n, [...r, e.toString()]);
825
826
  };
826
827
  if (l === "last")
827
828
  return () => {
828
- const e = r.getState().getNestedState(t, o);
829
+ const e = o.getState().getNestedState(t, r);
829
830
  if (e.length === 0) return;
830
- const n = e.length - 1, s = e[n], i = [...o, n.toString()];
831
+ const n = e.length - 1, s = e[n], i = [...r, n.toString()];
831
832
  return a(s, i);
832
833
  };
833
834
  if (l === "insert")
834
- return (e) => (p(o), ft(c, e, o, t), a(
835
- r.getState().getNestedState(t, o),
836
- o
835
+ return (e) => (p(r), St(c, e, r, t), a(
836
+ o.getState().getNestedState(t, r),
837
+ r
837
838
  ));
838
839
  if (l === "uniqueInsert")
839
840
  return (e, n, s) => {
840
- const i = r.getState().getNestedState(t, o), f = J(e) ? e(i) : e;
841
- let d = null;
841
+ const i = o.getState().getNestedState(t, r), g = J(e) ? e(i) : e;
842
+ let u = null;
842
843
  if (!i.some((w) => {
843
844
  if (n) {
844
845
  const x = n.every(
845
- (C) => L(w[C], f[C])
846
+ (C) => L(w[C], g[C])
846
847
  );
847
- return x && (d = w), x;
848
+ return x && (u = w), x;
848
849
  }
849
- const A = L(w, f);
850
- return A && (d = w), A;
850
+ const $ = L(w, g);
851
+ return $ && (u = w), $;
851
852
  }))
852
- p(o), ft(c, f, o, t);
853
- else if (s && d) {
854
- const w = s(d), A = i.map(
855
- (x) => L(x, d) ? w : x
853
+ p(r), St(c, g, r, t);
854
+ else if (s && u) {
855
+ const w = s(u), $ = i.map(
856
+ (x) => L(x, u) ? w : x
856
857
  );
857
- p(o), nt(c, A, o);
858
+ p(r), nt(c, $, r);
858
859
  }
859
860
  };
860
861
  if (l === "cut")
861
862
  return (e, n) => {
862
863
  if (!n?.waitForSync)
863
- return p(o), at(c, o, t, e), a(
864
- r.getState().getNestedState(t, o),
865
- o
864
+ return p(r), at(c, r, t, e), a(
865
+ o.getState().getNestedState(t, r),
866
+ r
866
867
  );
867
868
  };
868
869
  if (l === "cutByValue")
869
870
  return (e) => {
870
871
  for (let n = 0; n < y.length; n++)
871
- y[n] === e && at(c, o, t, n);
872
+ y[n] === e && at(c, r, t, n);
872
873
  };
873
874
  if (l === "toggleByValue")
874
875
  return (e) => {
875
876
  const n = y.findIndex((s) => s === e);
876
- n > -1 ? at(c, o, t, n) : ft(c, e, o, t);
877
+ n > -1 ? at(c, r, t, n) : St(c, e, r, t);
877
878
  };
878
879
  if (l === "stateFind")
879
880
  return (e) => {
880
- const s = u().find(
881
- ({ item: f }, d) => e(f, d)
881
+ const s = d().find(
882
+ ({ item: g }, u) => e(g, u)
882
883
  );
883
884
  if (!s) return;
884
- const i = [...o, s.originalIndex.toString()];
885
+ const i = [...r, s.originalIndex.toString()];
885
886
  return a(s.item, i, S);
886
887
  };
887
888
  if (l === "findWith")
888
889
  return (e, n) => {
889
- const i = u().find(
890
- ({ item: d }) => d[e] === n
890
+ const i = d().find(
891
+ ({ item: u }) => u[e] === n
891
892
  );
892
893
  if (!i) return;
893
- const f = [...o, i.originalIndex.toString()];
894
- return a(i.item, f, S);
894
+ const g = [...r, i.originalIndex.toString()];
895
+ return a(i.item, g, S);
895
896
  };
896
897
  }
897
- const Y = o[o.length - 1];
898
+ const Y = r[r.length - 1];
898
899
  if (!isNaN(Number(Y))) {
899
- const u = o.slice(0, -1), e = r.getState().getNestedState(t, u);
900
+ const d = r.slice(0, -1), e = o.getState().getNestedState(t, d);
900
901
  if (Array.isArray(e) && l === "cut")
901
902
  return () => at(
902
903
  c,
903
- u,
904
+ d,
904
905
  t,
905
906
  Number(Y)
906
907
  );
@@ -908,137 +909,137 @@ function lt(t, c, h, g) {
908
909
  if (l === "get")
909
910
  return () => {
910
911
  if (S?.validIndices && Array.isArray(y)) {
911
- const u = r.getState().getNestedState(t, o);
912
- return S.validIndices.map((e) => u[e]);
912
+ const d = o.getState().getNestedState(t, r);
913
+ return S.validIndices.map((e) => d[e]);
913
914
  }
914
- return r.getState().getNestedState(t, o);
915
+ return o.getState().getNestedState(t, r);
915
916
  };
916
917
  if (l === "$derive")
917
- return (u) => At({
918
+ return (d) => At({
918
919
  _stateKey: t,
919
- _path: o,
920
- _effect: u.toString()
920
+ _path: r,
921
+ _effect: d.toString()
921
922
  });
922
923
  if (l === "$get")
923
924
  return () => At({
924
925
  _stateKey: t,
925
- _path: o
926
+ _path: r
926
927
  });
927
928
  if (l === "lastSynced") {
928
- const u = `${t}:${o.join(".")}`;
929
- return r.getState().getSyncInfo(u);
929
+ const d = `${t}:${r.join(".")}`;
930
+ return o.getState().getSyncInfo(d);
930
931
  }
931
932
  if (l == "getLocalStorage")
932
- return (u) => dt(g + "-" + t + "-" + u);
933
+ return (d) => dt(f + "-" + t + "-" + d);
933
934
  if (l === "_selected") {
934
- const u = o.slice(0, -1), e = u.join("."), n = r.getState().getNestedState(t, u);
935
- return Array.isArray(n) ? Number(o[o.length - 1]) === r.getState().getSelectedIndex(t, e) : void 0;
935
+ const d = r.slice(0, -1), e = d.join("."), n = o.getState().getNestedState(t, d);
936
+ return Array.isArray(n) ? Number(r[r.length - 1]) === o.getState().getSelectedIndex(t, e) : void 0;
936
937
  }
937
938
  if (l === "setSelected")
938
- return (u) => {
939
- const e = o.slice(0, -1), n = Number(o[o.length - 1]), s = e.join(".");
940
- u ? r.getState().setSelectedIndex(t, s, n) : r.getState().setSelectedIndex(t, s, void 0);
941
- const i = r.getState().getNestedState(t, [...e]);
939
+ return (d) => {
940
+ const e = r.slice(0, -1), n = Number(r[r.length - 1]), s = e.join(".");
941
+ d ? o.getState().setSelectedIndex(t, s, n) : o.getState().setSelectedIndex(t, s, void 0);
942
+ const i = o.getState().getNestedState(t, [...e]);
942
943
  nt(c, i, e), p(e);
943
944
  };
944
945
  if (l === "toggleSelected")
945
946
  return () => {
946
- const u = o.slice(0, -1), e = Number(o[o.length - 1]), n = u.join("."), s = r.getState().getSelectedIndex(t, n);
947
- r.getState().setSelectedIndex(
947
+ const d = r.slice(0, -1), e = Number(r[r.length - 1]), n = d.join("."), s = o.getState().getSelectedIndex(t, n);
948
+ o.getState().setSelectedIndex(
948
949
  t,
949
950
  n,
950
951
  s === e ? void 0 : e
951
952
  );
952
- const i = r.getState().getNestedState(t, [...u]);
953
- nt(c, i, u), p(u);
953
+ const i = o.getState().getNestedState(t, [...d]);
954
+ nt(c, i, d), p(d);
954
955
  };
955
- if (o.length == 0) {
956
+ if (r.length == 0) {
956
957
  if (l === "applyJsonPatch")
957
- return (u) => {
958
- const e = r.getState().cogsStateStore[t], s = jt(e, u).newDocument;
958
+ return (d) => {
959
+ const e = o.getState().cogsStateStore[t], s = jt(e, d).newDocument;
959
960
  Nt(
960
961
  t,
961
- r.getState().initialStateGlobal[t],
962
+ o.getState().initialStateGlobal[t],
962
963
  s,
963
964
  c,
964
965
  h,
965
- g
966
+ f
966
967
  );
967
- const i = r.getState().stateComponents.get(t);
968
+ const i = o.getState().stateComponents.get(t);
968
969
  if (i) {
969
- const f = mt(e, s), d = new Set(f);
970
+ const g = ht(e, s), u = new Set(g);
970
971
  for (const [
971
- k,
972
+ E,
972
973
  w
973
974
  ] of i.components.entries()) {
974
- let A = !1;
975
+ let $ = !1;
975
976
  const x = Array.isArray(w.reactiveType) ? w.reactiveType : [w.reactiveType || "component"];
976
977
  if (!x.includes("none")) {
977
978
  if (x.includes("all")) {
978
979
  w.forceUpdate();
979
980
  continue;
980
981
  }
981
- if (x.includes("component") && (w.paths.has("") && (A = !0), !A))
982
- for (const C of d) {
982
+ if (x.includes("component") && (w.paths.has("") && ($ = !0), !$))
983
+ for (const C of u) {
983
984
  if (w.paths.has(C)) {
984
- A = !0;
985
+ $ = !0;
985
986
  break;
986
987
  }
987
988
  let V = C.lastIndexOf(".");
988
989
  for (; V !== -1; ) {
989
990
  const G = C.substring(0, V);
990
991
  if (w.paths.has(G)) {
991
- A = !0;
992
+ $ = !0;
992
993
  break;
993
994
  }
994
- const z = C.substring(
995
+ const B = C.substring(
995
996
  V + 1
996
997
  );
997
- if (!isNaN(Number(z))) {
998
- const P = G.lastIndexOf(".");
999
- if (P !== -1) {
998
+ if (!isNaN(Number(B))) {
999
+ const M = G.lastIndexOf(".");
1000
+ if (M !== -1) {
1000
1001
  const N = G.substring(
1001
1002
  0,
1002
- P
1003
+ M
1003
1004
  );
1004
1005
  if (w.paths.has(N)) {
1005
- A = !0;
1006
+ $ = !0;
1006
1007
  break;
1007
1008
  }
1008
1009
  }
1009
1010
  }
1010
1011
  V = G.lastIndexOf(".");
1011
1012
  }
1012
- if (A) break;
1013
+ if ($) break;
1013
1014
  }
1014
- if (!A && x.includes("deps") && w.depsFunction) {
1015
+ if (!$ && x.includes("deps") && w.depsFunction) {
1015
1016
  const C = w.depsFunction(s);
1016
1017
  let V = !1;
1017
- typeof C == "boolean" ? C && (V = !0) : L(w.deps, C) || (w.deps = C, V = !0), V && (A = !0);
1018
+ typeof C == "boolean" ? C && (V = !0) : L(w.deps, C) || (w.deps = C, V = !0), V && ($ = !0);
1018
1019
  }
1019
- A && w.forceUpdate();
1020
+ $ && w.forceUpdate();
1020
1021
  }
1021
1022
  }
1022
1023
  }
1023
1024
  };
1024
1025
  if (l === "validateZodSchema")
1025
1026
  return () => {
1026
- const u = r.getState().getInitialOptions(t)?.validation, e = r.getState().addValidationError;
1027
- if (!u?.zodSchema)
1027
+ const d = o.getState().getInitialOptions(t)?.validation, e = o.getState().addValidationError;
1028
+ if (!d?.zodSchema)
1028
1029
  throw new Error("Zod schema not found");
1029
- if (!u?.key)
1030
+ if (!d?.key)
1030
1031
  throw new Error("Validation key not found");
1031
- q(u.key);
1032
- const n = r.getState().cogsStateStore[t];
1032
+ H(d.key);
1033
+ const n = o.getState().cogsStateStore[t];
1033
1034
  try {
1034
- const s = r.getState().getValidationErrors(u.key);
1035
- s && s.length > 0 && s.forEach(([f]) => {
1036
- f && f.startsWith(u.key) && q(f);
1035
+ const s = o.getState().getValidationErrors(d.key);
1036
+ s && s.length > 0 && s.forEach(([g]) => {
1037
+ g && g.startsWith(d.key) && H(g);
1037
1038
  });
1038
- const i = u.zodSchema.safeParse(n);
1039
- return i.success ? !0 : (i.error.errors.forEach((d) => {
1040
- const k = d.path, w = d.message, A = [u.key, ...k].join(".");
1041
- e(A, w);
1039
+ const i = d.zodSchema.safeParse(n);
1040
+ return i.success ? !0 : (i.error.errors.forEach((u) => {
1041
+ const E = u.path, w = u.message, $ = [d.key, ...E].join(".");
1042
+ e($, w);
1042
1043
  }), ut(t), !1);
1043
1044
  } catch (s) {
1044
1045
  return console.error("Zod schema validation failed", s), !1;
@@ -1046,102 +1047,102 @@ function lt(t, c, h, g) {
1046
1047
  };
1047
1048
  if (l === "_componentId") return h;
1048
1049
  if (l === "getComponents")
1049
- return () => r().stateComponents.get(t);
1050
+ return () => o().stateComponents.get(t);
1050
1051
  if (l === "getAllFormRefs")
1051
1052
  return () => It.getState().getFormRefsByStateKey(t);
1052
1053
  if (l === "_initialState")
1053
- return r.getState().initialStateGlobal[t];
1054
+ return o.getState().initialStateGlobal[t];
1054
1055
  if (l === "_serverState")
1055
- return r.getState().serverState[t];
1056
+ return o.getState().serverState[t];
1056
1057
  if (l === "_isLoading")
1057
- return r.getState().isLoadingGlobal[t];
1058
+ return o.getState().isLoadingGlobal[t];
1058
1059
  if (l === "revertToInitialState")
1059
1060
  return I.revertToInitialState;
1060
1061
  if (l === "updateInitialState") return I.updateInitialState;
1061
1062
  if (l === "removeValidation") return I.removeValidation;
1062
1063
  }
1063
1064
  if (l === "getFormRef")
1064
- return () => It.getState().getFormRef(t + "." + o.join("."));
1065
+ return () => It.getState().getFormRef(t + "." + r.join("."));
1065
1066
  if (l === "validationWrapper")
1066
1067
  return ({
1067
- children: u,
1068
+ children: d,
1068
1069
  hideMessage: e
1069
- }) => /* @__PURE__ */ St(
1070
+ }) => /* @__PURE__ */ mt(
1070
1071
  Pt,
1071
1072
  {
1072
1073
  formOpts: e ? { validation: { message: "" } } : void 0,
1073
- path: o,
1074
- validationKey: r.getState().getInitialOptions(t)?.validation?.key || "",
1074
+ path: r,
1075
+ validationKey: o.getState().getInitialOptions(t)?.validation?.key || "",
1075
1076
  stateKey: t,
1076
1077
  validIndices: S?.validIndices,
1077
- children: u
1078
+ children: d
1078
1079
  }
1079
1080
  );
1080
1081
  if (l === "_stateKey") return t;
1081
- if (l === "_path") return o;
1082
+ if (l === "_path") return r;
1082
1083
  if (l === "_isServerSynced") return I._isServerSynced;
1083
1084
  if (l === "update")
1084
- return (u, e) => {
1085
+ return (d, e) => {
1085
1086
  if (e?.debounce)
1086
1087
  xt(() => {
1087
- nt(c, u, o, "");
1088
- const n = r.getState().getNestedState(t, o);
1088
+ nt(c, d, r, "");
1089
+ const n = o.getState().getNestedState(t, r);
1089
1090
  e?.afterUpdate && e.afterUpdate(n);
1090
1091
  }, e.debounce);
1091
1092
  else {
1092
- nt(c, u, o, "");
1093
- const n = r.getState().getNestedState(t, o);
1093
+ nt(c, d, r, "");
1094
+ const n = o.getState().getNestedState(t, r);
1094
1095
  e?.afterUpdate && e.afterUpdate(n);
1095
1096
  }
1096
- p(o);
1097
+ p(r);
1097
1098
  };
1098
1099
  if (l === "formElement")
1099
- return (u, e) => /* @__PURE__ */ St(
1100
+ return (d, e) => /* @__PURE__ */ mt(
1100
1101
  _t,
1101
1102
  {
1102
1103
  setState: c,
1103
1104
  stateKey: t,
1104
- path: o,
1105
- child: u,
1105
+ path: r,
1106
+ child: d,
1106
1107
  formOpts: e
1107
1108
  }
1108
1109
  );
1109
- const j = [...o, l], tt = r.getState().getNestedState(t, j);
1110
+ const j = [...r, l], tt = o.getState().getNestedState(t, j);
1110
1111
  return a(tt, j, S);
1111
1112
  }
1112
- }, W = new Proxy(O, U);
1113
+ }, W = new Proxy(O, R);
1113
1114
  return v.set(D, {
1114
1115
  proxy: W,
1115
- stateVersion: $
1116
+ stateVersion: k
1116
1117
  }), W;
1117
1118
  }
1118
1119
  return a(
1119
- r.getState().getNestedState(t, [])
1120
+ o.getState().getNestedState(t, [])
1120
1121
  );
1121
1122
  }
1122
1123
  function At(t) {
1123
- return rt(zt, { proxy: t });
1124
+ return rt(Bt, { proxy: t });
1124
1125
  }
1125
- function Bt({
1126
+ function zt({
1126
1127
  proxy: t,
1127
1128
  rebuildStateShape: c
1128
1129
  }) {
1129
- const h = r().getNestedState(t._stateKey, t._path);
1130
+ const h = o().getNestedState(t._stateKey, t._path);
1130
1131
  return Array.isArray(h) ? c(
1131
1132
  h,
1132
1133
  t._path
1133
1134
  ).stateMapNoRender(
1134
- (v, $, p, I, a) => t._mapFn(v, $, p, I, a)
1135
+ (v, k, p, I, a) => t._mapFn(v, k, p, I, a)
1135
1136
  ) : null;
1136
1137
  }
1137
- function zt({
1138
+ function Bt({
1138
1139
  proxy: t
1139
1140
  }) {
1140
- const c = H(null), h = `${t._stateKey}-${t._path.join(".")}`;
1141
+ const c = q(null), h = `${t._stateKey}-${t._path.join(".")}`;
1141
1142
  return st(() => {
1142
- const g = c.current;
1143
- if (!g || !g.parentElement) return;
1144
- const v = g.parentElement, p = Array.from(v.childNodes).indexOf(g);
1143
+ const f = c.current;
1144
+ if (!f || !f.parentElement) return;
1145
+ const v = f.parentElement, p = Array.from(v.childNodes).indexOf(f);
1145
1146
  let I = v.getAttribute("data-parent-id");
1146
1147
  I || (I = `parent-${crypto.randomUUID()}`, v.setAttribute("data-parent-id", I));
1147
1148
  const y = {
@@ -1150,23 +1151,23 @@ function zt({
1150
1151
  position: p,
1151
1152
  effect: t._effect
1152
1153
  };
1153
- r.getState().addSignalElement(h, y);
1154
- const o = r.getState().getNestedState(t._stateKey, t._path);
1154
+ o.getState().addSignalElement(h, y);
1155
+ const r = o.getState().getNestedState(t._stateKey, t._path);
1155
1156
  let S;
1156
1157
  if (t._effect)
1157
1158
  try {
1158
1159
  S = new Function(
1159
1160
  "state",
1160
1161
  `return (${t._effect})(state)`
1161
- )(o);
1162
+ )(r);
1162
1163
  } catch (O) {
1163
- console.error("Error evaluating effect function during mount:", O), S = o;
1164
+ console.error("Error evaluating effect function during mount:", O), S = r;
1164
1165
  }
1165
1166
  else
1166
- S = o;
1167
+ S = r;
1167
1168
  S !== null && typeof S == "object" && (S = JSON.stringify(S));
1168
1169
  const D = document.createTextNode(String(S));
1169
- g.replaceWith(D);
1170
+ f.replaceWith(D);
1170
1171
  }, [t._stateKey, t._path.join("."), t._effect]), rt("span", {
1171
1172
  ref: c,
1172
1173
  style: { display: "none" },
@@ -1176,39 +1177,43 @@ function zt({
1176
1177
  function ie(t) {
1177
1178
  const c = Ct(
1178
1179
  (h) => {
1179
- const g = r.getState().stateComponents.get(t._stateKey) || {
1180
+ const f = o.getState().stateComponents.get(t._stateKey) || {
1180
1181
  components: /* @__PURE__ */ new Map()
1181
1182
  };
1182
- return g.components.set(t._stateKey, {
1183
+ return f.components.set(t._stateKey, {
1183
1184
  forceUpdate: h,
1184
1185
  paths: /* @__PURE__ */ new Set([t._path.join(".")])
1185
- }), () => g.components.delete(t._stateKey);
1186
+ }), () => f.components.delete(t._stateKey);
1186
1187
  },
1187
- () => r.getState().getNestedState(t._stateKey, t._path)
1188
+ () => o.getState().getNestedState(t._stateKey, t._path)
1188
1189
  );
1189
1190
  return rt("text", {}, String(c));
1190
1191
  }
1191
- function qt({
1192
+ function Ht({
1192
1193
  stateKey: t,
1193
1194
  itemComponentId: c,
1194
1195
  itemPath: h,
1195
- children: g
1196
+ children: f
1196
1197
  }) {
1197
- const [, v] = K({}), [$, p] = Ot();
1198
+ const [, v] = K({}), [k, p] = Ot();
1198
1199
  return st(() => {
1199
- p.height > 0 && r.getState().setShadowMetadata(t, h, { itemHeight: p.height });
1200
+ p.height > 0 && o.getState().setShadowMetadata(t, h, {
1201
+ virtualizer: {
1202
+ itemHeight: p.height
1203
+ }
1204
+ });
1200
1205
  }, [p.height]), it(() => {
1201
- const I = `${t}////${c}`, a = r.getState().stateComponents.get(t) || {
1206
+ const I = `${t}////${c}`, a = o.getState().stateComponents.get(t) || {
1202
1207
  components: /* @__PURE__ */ new Map()
1203
1208
  };
1204
1209
  return a.components.set(I, {
1205
1210
  forceUpdate: () => v({}),
1206
1211
  paths: /* @__PURE__ */ new Set([h.join(".")])
1207
- }), r.getState().stateComponents.set(t, a), () => {
1208
- const y = r.getState().stateComponents.get(t);
1212
+ }), o.getState().stateComponents.set(t, a), () => {
1213
+ const y = o.getState().stateComponents.get(t);
1209
1214
  y && y.components.delete(I);
1210
1215
  };
1211
- }, [t, c, h.join(".")]), /* @__PURE__ */ St("div", { ref: $, children: g });
1216
+ }, [t, c, h.join(".")]), /* @__PURE__ */ mt("div", { ref: k, children: f });
1212
1217
  }
1213
1218
  export {
1214
1219
  At as $cogsSignal,