cogsbox-state 0.5.222 → 0.5.224

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,16 +1,16 @@
1
1
  "use client";
2
2
  import { jsx as ye } from "react/jsx-runtime";
3
3
  import { useState as de, useRef as te, useEffect as ue, useLayoutEffect as $e, useMemo as Te, createElement as ae, useSyncExternalStore as Ve, startTransition as Ne } from "react";
4
- import { transformStateFunc as Ae, isDeepEqual as W, isFunction as J, getNestedValue as G, getDifferences as fe, debounce as be } from "./utility.js";
4
+ import { transformStateFunc as Ae, isDeepEqual as W, isFunction as J, getNestedValue as G, getDifferences as ge, debounce as be } from "./utility.js";
5
5
  import { pushFunc as ce, updateFn as Q, cutFunc as ee, ValidationWrapper as Pe, FormControlComponent as _e } from "./Functions.jsx";
6
6
  import xe from "superjson";
7
- import { v4 as ge } from "uuid";
7
+ import { v4 as fe } from "uuid";
8
8
  import "zod";
9
- import { getGlobalStore as n, formRefStore as ve } from "./store.js";
9
+ import { getGlobalStore as r, formRefStore as ve } from "./store.js";
10
10
  import { useCogsConfig as we } from "./CogsStateClient.jsx";
11
11
  import { applyPatch as Ce } from "fast-json-patch";
12
12
  function he(e, i) {
13
- const v = n.getState().getInitialOptions, u = n.getState().setInitialStateOptions, S = v(e) || {};
13
+ const v = r.getState().getInitialOptions, u = r.getState().setInitialStateOptions, S = v(e) || {};
14
14
  u(e, {
15
15
  ...S,
16
16
  ...i
@@ -21,7 +21,7 @@ function Ie({
21
21
  options: i,
22
22
  initialOptionsPart: v
23
23
  }) {
24
- const u = Y(e) || {}, S = v[e] || {}, E = n.getState().setInitialStateOptions, p = { ...S, ...u };
24
+ const u = Y(e) || {}, S = v[e] || {}, E = r.getState().setInitialStateOptions, p = { ...S, ...u };
25
25
  let h = !1;
26
26
  if (i)
27
27
  for (const s in i)
@@ -42,16 +42,16 @@ const et = (e, i) => {
42
42
  ...i?.validation,
43
43
  ...S[h].formElements || {}
44
44
  // State-specific overrides
45
- }, Y(h) || n.getState().setInitialStateOptions(h, S[h]);
46
- }), n.getState().setInitialStates(u), n.getState().setCreatedState(u);
45
+ }, Y(h) || r.getState().setInitialStateOptions(h, S[h]);
46
+ }), r.getState().setInitialStates(u), r.getState().setCreatedState(u);
47
47
  const E = (h, s) => {
48
- const [m] = de(s?.componentId ?? ge());
48
+ const [m] = de(s?.componentId ?? fe());
49
49
  Ie({
50
50
  stateKey: h,
51
51
  options: s,
52
52
  initialOptionsPart: S
53
53
  });
54
- const r = n.getState().cogsStateStore[h] || u[h], y = s?.modifyState ? s.modifyState(r) : r, [j, _] = De(
54
+ const n = r.getState().cogsStateStore[h] || u[h], y = s?.modifyState ? s.modifyState(n) : n, [j, _] = De(
55
55
  y,
56
56
  {
57
57
  stateKey: h,
@@ -84,7 +84,7 @@ const et = (e, i) => {
84
84
  addValidationError: Re,
85
85
  removeValidationError: B,
86
86
  setServerSyncActions: je
87
- } = n.getState(), pe = (e, i, v, u, S) => {
87
+ } = r.getState(), pe = (e, i, v, u, S) => {
88
88
  v?.log && console.log(
89
89
  "saving to localstorage",
90
90
  i,
@@ -118,7 +118,7 @@ const et = (e, i) => {
118
118
  return console.error("Error loading from localStorage:", i), null;
119
119
  }
120
120
  }, Ue = (e, i) => {
121
- const v = n.getState().cogsStateStore[e], { sessionId: u } = we(), S = J(i?.localStorage?.key) ? i.localStorage.key(v) : i?.localStorage?.key;
121
+ const v = r.getState().cogsStateStore[e], { sessionId: u } = we(), S = J(i?.localStorage?.key) ? i.localStorage.key(v) : i?.localStorage?.key;
122
122
  if (S && u) {
123
123
  const E = oe(
124
124
  `${u}-${e}-${S}`
@@ -140,7 +140,7 @@ const et = (e, i) => {
140
140
  };
141
141
  Se(e, p.initialState), ne(e, p.updaterState), H(e, p.state);
142
142
  }, ie = (e) => {
143
- const i = n.getState().stateComponents.get(e);
143
+ const i = r.getState().stateComponents.get(e);
144
144
  if (!i) return;
145
145
  const v = /* @__PURE__ */ new Set();
146
146
  i.components.forEach((u) => {
@@ -149,7 +149,7 @@ const et = (e, i) => {
149
149
  v.forEach((u) => u());
150
150
  });
151
151
  }, tt = (e, i) => {
152
- const v = n.getState().stateComponents.get(e);
152
+ const v = r.getState().stateComponents.get(e);
153
153
  if (v) {
154
154
  const u = `${e}////${i}`, S = v.components.get(u);
155
155
  if ((S ? Array.isArray(S.reactiveType) ? S.reactiveType : [S.reactiveType || "component"] : null)?.includes("none"))
@@ -189,62 +189,62 @@ function De(e, {
189
189
  componentId: h,
190
190
  initialState: s,
191
191
  syncUpdate: m,
192
- dependencies: r,
192
+ dependencies: n,
193
193
  serverState: y
194
194
  } = {}) {
195
195
  const [j, _] = de({}), { sessionId: x } = we();
196
196
  let U = !i;
197
- const [g] = de(i ?? ge()), c = n.getState().stateLog[g], K = te(/* @__PURE__ */ new Set()), L = te(h ?? ge()), A = te(
197
+ const [f] = de(i ?? fe()), c = r.getState().stateLog[f], K = te(/* @__PURE__ */ new Set()), L = te(h ?? fe()), A = te(
198
198
  null
199
199
  );
200
- A.current = Y(g) ?? null, ue(() => {
201
- if (m && m.stateKey === g && m.path?.[0]) {
202
- H(g, (a) => ({
200
+ A.current = Y(f) ?? null, ue(() => {
201
+ if (m && m.stateKey === f && m.path?.[0]) {
202
+ H(f, (a) => ({
203
203
  ...a,
204
204
  [m.path[0]]: m.newValue
205
205
  }));
206
206
  const t = `${m.stateKey}:${m.path.join(".")}`;
207
- n.getState().setSyncInfo(t, {
207
+ r.getState().setSyncInfo(t, {
208
208
  timeStamp: m.timeStamp,
209
209
  userId: m.userId
210
210
  });
211
211
  }
212
212
  }, [m]), ue(() => {
213
213
  if (s) {
214
- he(g, {
214
+ he(f, {
215
215
  initialState: s
216
216
  });
217
- const t = A.current, o = t?.serverState?.id !== void 0 && t?.serverState?.status === "success" && t?.serverState?.data, d = n.getState().initialStateGlobal[g];
217
+ const t = A.current, o = t?.serverState?.id !== void 0 && t?.serverState?.status === "success" && t?.serverState?.data, d = r.getState().initialStateGlobal[f];
218
218
  if (!(d && !W(d, s) || !d) && !o)
219
219
  return;
220
- let f = null;
220
+ let g = null;
221
221
  const N = J(t?.localStorage?.key) ? t?.localStorage?.key(s) : t?.localStorage?.key;
222
- N && x && (f = oe(`${x}-${g}-${N}`));
222
+ N && x && (g = oe(`${x}-${f}-${N}`));
223
223
  let w = s, $ = !1;
224
- const F = o ? Date.now() : 0, b = f?.lastUpdated || 0, M = f?.lastSyncedWithServer || 0;
225
- o && F > b ? (w = t.serverState.data, $ = !0) : f && b > M && (w = f.state, t?.localStorage?.onChange && t?.localStorage?.onChange(w)), ke(
226
- g,
224
+ const F = o ? Date.now() : 0, b = g?.lastUpdated || 0, M = g?.lastSyncedWithServer || 0;
225
+ o && F > b ? (w = t.serverState.data, $ = !0) : g && b > M && (w = g.state, t?.localStorage?.onChange && t?.localStorage?.onChange(w)), ke(
226
+ f,
227
227
  s,
228
228
  w,
229
229
  X,
230
230
  L.current,
231
231
  x
232
- ), $ && N && x && pe(w, g, t, x, Date.now()), ie(g), (Array.isArray(p) ? p : [p || "component"]).includes("none") || _({});
232
+ ), $ && N && x && pe(w, f, t, x, Date.now()), ie(f), (Array.isArray(p) ? p : [p || "component"]).includes("none") || _({});
233
233
  }
234
234
  }, [
235
235
  s,
236
236
  y?.status,
237
237
  y?.data,
238
- ...r || []
238
+ ...n || []
239
239
  ]), $e(() => {
240
- U && he(g, {
240
+ U && he(f, {
241
241
  serverSync: v,
242
242
  formElements: S,
243
243
  initialState: s,
244
244
  localStorage: u,
245
245
  middleware: A.current?.middleware
246
246
  });
247
- const t = `${g}////${L.current}`, a = n.getState().stateComponents.get(g) || {
247
+ const t = `${f}////${L.current}`, a = r.getState().stateComponents.get(f) || {
248
248
  components: /* @__PURE__ */ new Map()
249
249
  };
250
250
  return a.components.set(t, {
@@ -253,30 +253,30 @@ function De(e, {
253
253
  deps: [],
254
254
  depsFunction: E || void 0,
255
255
  reactiveType: p ?? ["component", "deps"]
256
- }), n.getState().stateComponents.set(g, a), _({}), () => {
257
- const o = `${g}////${L.current}`;
258
- a && (a.components.delete(o), a.components.size === 0 && n.getState().stateComponents.delete(g));
256
+ }), r.getState().stateComponents.set(f, a), _({}), () => {
257
+ const o = `${f}////${L.current}`;
258
+ a && (a.components.delete(o), a.components.size === 0 && r.getState().stateComponents.delete(f));
259
259
  };
260
260
  }, []);
261
261
  const X = (t, a, o, d) => {
262
262
  if (Array.isArray(a)) {
263
- const I = `${g}-${a.join(".")}`;
263
+ const I = `${f}-${a.join(".")}`;
264
264
  K.current.add(I);
265
265
  }
266
- H(g, (I) => {
267
- const f = J(t) ? t(I) : t, N = `${g}-${a.join(".")}`;
266
+ H(f, (I) => {
267
+ const g = J(t) ? t(I) : t, N = `${f}-${a.join(".")}`;
268
268
  if (N) {
269
- let R = !1, k = n.getState().signalDomElements.get(N);
269
+ let R = !1, k = r.getState().signalDomElements.get(N);
270
270
  if ((!k || k.size === 0) && (o.updateType === "insert" || o.updateType === "cut")) {
271
- const O = a.slice(0, -1), C = G(f, O);
271
+ const O = a.slice(0, -1), C = G(g, O);
272
272
  if (Array.isArray(C)) {
273
273
  R = !0;
274
- const P = `${g}-${O.join(".")}`;
275
- k = n.getState().signalDomElements.get(P);
274
+ const P = `${f}-${O.join(".")}`;
275
+ k = r.getState().signalDomElements.get(P);
276
276
  }
277
277
  }
278
278
  if (k) {
279
- const O = R ? G(f, a.slice(0, -1)) : G(f, a);
279
+ const O = R ? G(g, a.slice(0, -1)) : G(g, a);
280
280
  k.forEach(({ parentId: C, position: P, effect: T }) => {
281
281
  const V = document.querySelector(
282
282
  `[data-parent-id="${C}"]`
@@ -306,9 +306,9 @@ function De(e, {
306
306
  B(k), Re(P, O);
307
307
  }
308
308
  });
309
- const $ = n.getState().stateComponents.get(g);
309
+ const $ = r.getState().stateComponents.get(f);
310
310
  if ($) {
311
- const R = fe(I, f), k = new Set(R), O = o.updateType === "update" ? a.join(".") : a.slice(0, -1).join(".") || "", C = [];
311
+ const R = ge(I, g), k = new Set(R), O = o.updateType === "update" ? a.join(".") : a.slice(0, -1).join(".") || "", C = [];
312
312
  for (const [
313
313
  P,
314
314
  T
@@ -349,7 +349,7 @@ function De(e, {
349
349
  if (V) break;
350
350
  }
351
351
  if (!V && Z.includes("deps") && T.depsFunction) {
352
- const z = T.depsFunction(f);
352
+ const z = T.depsFunction(g);
353
353
  let D = !1;
354
354
  typeof z == "boolean" ? z && (D = !0) : W(T.deps, z) || (T.deps = z, D = !0), D && (V = !0);
355
355
  }
@@ -362,132 +362,132 @@ function De(e, {
362
362
  }
363
363
  const F = Date.now();
364
364
  a = a.map((R, k) => {
365
- const O = a.slice(0, -1), C = G(f, O);
365
+ const O = a.slice(0, -1), C = G(g, O);
366
366
  return k === a.length - 1 && ["insert", "cut"].includes(o.updateType) ? (C.length - 1).toString() : R;
367
367
  });
368
368
  const { oldValue: b, newValue: M } = Me(
369
369
  o.updateType,
370
370
  I,
371
- f,
371
+ g,
372
372
  a
373
373
  ), q = {
374
374
  timeStamp: F,
375
- stateKey: g,
375
+ stateKey: f,
376
376
  path: a,
377
377
  updateType: o.updateType,
378
378
  status: "new",
379
379
  oldValue: b,
380
380
  newValue: M
381
381
  };
382
- if (Fe(g, (R) => {
382
+ if (Fe(f, (R) => {
383
383
  const O = [...R ?? [], q].reduce((C, P) => {
384
384
  const T = `${P.stateKey}:${JSON.stringify(P.path)}`, V = C.get(T);
385
385
  return V ? (V.timeStamp = Math.max(V.timeStamp, P.timeStamp), V.newValue = P.newValue, V.oldValue = V.oldValue ?? P.oldValue, V.updateType = P.updateType) : C.set(T, { ...P }), C;
386
386
  }, /* @__PURE__ */ new Map());
387
387
  return Array.from(O.values());
388
388
  }), pe(
389
- f,
390
389
  g,
390
+ f,
391
391
  A.current,
392
392
  x
393
393
  ), A.current?.middleware && A.current.middleware({
394
394
  updateLog: c,
395
395
  update: q
396
396
  }), A.current?.serverSync) {
397
- const R = n.getState().serverState[g], k = A.current?.serverSync;
398
- je(g, {
399
- syncKey: typeof k.syncKey == "string" ? k.syncKey : k.syncKey({ state: f }),
397
+ const R = r.getState().serverState[f], k = A.current?.serverSync;
398
+ je(f, {
399
+ syncKey: typeof k.syncKey == "string" ? k.syncKey : k.syncKey({ state: g }),
400
400
  rollBackState: R,
401
401
  actionTimeStamp: Date.now() + (k.debounce ?? 3e3),
402
402
  status: "waiting"
403
403
  });
404
404
  }
405
- return f;
405
+ return g;
406
406
  });
407
407
  };
408
- n.getState().updaterState[g] || (ne(
409
- g,
408
+ r.getState().updaterState[f] || (ne(
409
+ f,
410
410
  re(
411
- g,
411
+ f,
412
412
  X,
413
413
  L.current,
414
414
  x
415
415
  )
416
- ), n.getState().cogsStateStore[g] || H(g, e), n.getState().initialStateGlobal[g] || Se(g, e));
416
+ ), r.getState().cogsStateStore[f] || H(f, e), r.getState().initialStateGlobal[f] || Se(f, e));
417
417
  const l = Te(() => re(
418
- g,
418
+ f,
419
419
  X,
420
420
  L.current,
421
421
  x
422
- ), [g, x]);
423
- return [Ee(g), l];
422
+ ), [f, x]);
423
+ return [Ee(f), l];
424
424
  }
425
425
  function re(e, i, v, u) {
426
426
  const S = /* @__PURE__ */ new Map();
427
427
  let E = 0;
428
428
  const p = (m) => {
429
- const r = m.join(".");
429
+ const n = m.join(".");
430
430
  for (const [y] of S)
431
- (y === r || y.startsWith(r + ".")) && S.delete(y);
431
+ (y === n || y.startsWith(n + ".")) && S.delete(y);
432
432
  E++;
433
433
  }, h = {
434
434
  removeValidation: (m) => {
435
435
  m?.validationKey && B(m.validationKey);
436
436
  },
437
437
  revertToInitialState: (m) => {
438
- const r = n.getState().getInitialOptions(e)?.validation;
439
- r?.key && B(r?.key), m?.validationKey && B(m.validationKey);
440
- const y = n.getState().initialStateGlobal[e];
441
- n.getState().clearSelectedIndexesForState(e), S.clear(), E++;
438
+ const n = r.getState().getInitialOptions(e)?.validation;
439
+ n?.key && B(n?.key), m?.validationKey && B(m.validationKey);
440
+ const y = r.getState().initialStateGlobal[e];
441
+ r.getState().clearSelectedIndexesForState(e), S.clear(), E++;
442
442
  const j = s(y, []), _ = Y(e), x = J(_?.localStorage?.key) ? _?.localStorage?.key(y) : _?.localStorage?.key, U = `${u}-${e}-${x}`;
443
443
  U && localStorage.removeItem(U), ne(e, j), H(e, y);
444
- const g = n.getState().stateComponents.get(e);
445
- return g && g.components.forEach((c) => {
444
+ const f = r.getState().stateComponents.get(e);
445
+ return f && f.components.forEach((c) => {
446
446
  c.forceUpdate();
447
447
  }), y;
448
448
  },
449
449
  updateInitialState: (m) => {
450
450
  S.clear(), E++;
451
- const r = re(
451
+ const n = re(
452
452
  e,
453
453
  i,
454
454
  v,
455
455
  u
456
- ), y = n.getState().initialStateGlobal[e], j = Y(e), _ = J(j?.localStorage?.key) ? j?.localStorage?.key(y) : j?.localStorage?.key, x = `${u}-${e}-${_}`;
456
+ ), y = r.getState().initialStateGlobal[e], j = Y(e), _ = J(j?.localStorage?.key) ? j?.localStorage?.key(y) : j?.localStorage?.key, x = `${u}-${e}-${_}`;
457
457
  return localStorage.getItem(x) && localStorage.removeItem(x), Ne(() => {
458
- Se(e, m), ne(e, r), H(e, m);
459
- const U = n.getState().stateComponents.get(e);
460
- U && U.components.forEach((g) => {
461
- g.forceUpdate();
458
+ Se(e, m), ne(e, n), H(e, m);
459
+ const U = r.getState().stateComponents.get(e);
460
+ U && U.components.forEach((f) => {
461
+ f.forceUpdate();
462
462
  });
463
463
  }), {
464
- fetchId: (U) => r.get()[U]
464
+ fetchId: (U) => n.get()[U]
465
465
  };
466
466
  },
467
- _initialState: n.getState().initialStateGlobal[e],
468
- _serverState: n.getState().serverState[e],
469
- _isLoading: n.getState().isLoadingGlobal[e],
467
+ _initialState: r.getState().initialStateGlobal[e],
468
+ _serverState: r.getState().serverState[e],
469
+ _isLoading: r.getState().isLoadingGlobal[e],
470
470
  _isServerSynced: () => {
471
- const m = n.getState().serverState[e];
471
+ const m = r.getState().serverState[e];
472
472
  return !!(m && W(m, Ee(e)));
473
473
  }
474
474
  };
475
- function s(m, r = [], y) {
476
- const j = r.map(String).join(".");
475
+ function s(m, n = [], y) {
476
+ const j = n.map(String).join(".");
477
477
  S.get(j);
478
478
  const _ = function() {
479
- return n().getNestedState(e, r);
479
+ return r().getNestedState(e, n);
480
480
  };
481
- Object.keys(h).forEach((g) => {
482
- _[g] = h[g];
481
+ Object.keys(h).forEach((f) => {
482
+ _[f] = h[f];
483
483
  });
484
484
  const x = {
485
- apply(g, c, K) {
485
+ apply(f, c, K) {
486
486
  return console.log(
487
- `PROXY APPLY TRAP HIT: stateKey=${e}, path=${r.join(".")}`
488
- ), console.trace("Apply trap stack trace"), n().getNestedState(e, r);
487
+ `PROXY APPLY TRAP HIT: stateKey=${e}, path=${n.join(".")}`
488
+ ), console.trace("Apply trap stack trace"), r().getNestedState(e, n);
489
489
  },
490
- get(g, c) {
490
+ get(f, c) {
491
491
  y?.validIndices && !Array.isArray(m) && (y = { ...y, validIndices: void 0 });
492
492
  const K = /* @__PURE__ */ new Set([
493
493
  "insert",
@@ -510,42 +510,49 @@ function re(e, i, v, u) {
510
510
  "middleware"
511
511
  ]);
512
512
  if (c !== "then" && !c.startsWith("$") && c !== "stateMapNoRender" && !K.has(c)) {
513
- const l = `${e}////${v}`, t = n.getState().stateComponents.get(e);
513
+ const l = `${e}////${v}`;
514
+ console.log("adding path", l, n, c);
515
+ const t = r.getState().stateComponents.get(e);
514
516
  if (t) {
515
517
  const a = t.components.get(l);
516
518
  if (a && !a.pathsInitialized && (a.pathsInitialized = !0, !a.paths.has(""))) {
517
- const o = r.join(".");
519
+ const o = n.join(".");
518
520
  let d = !0;
519
521
  for (const I of a.paths)
520
522
  if (o.startsWith(I) && (o === I || o[I.length] === ".")) {
521
523
  d = !1;
522
524
  break;
523
525
  }
524
- d && a.paths.add(o);
526
+ d && (console.log(
527
+ "adding path actualyl adding",
528
+ l,
529
+ n,
530
+ c
531
+ ), a.paths.add(o));
525
532
  }
526
533
  }
527
534
  }
528
535
  if (c === "getDifferences")
529
- return () => fe(
530
- n.getState().cogsStateStore[e],
531
- n.getState().initialStateGlobal[e]
536
+ return () => ge(
537
+ r.getState().cogsStateStore[e],
538
+ r.getState().initialStateGlobal[e]
532
539
  );
533
- if (c === "sync" && r.length === 0)
540
+ if (c === "sync" && n.length === 0)
534
541
  return async function() {
535
- const l = n.getState().getInitialOptions(e), t = l?.sync;
542
+ const l = r.getState().getInitialOptions(e), t = l?.sync;
536
543
  if (!t)
537
544
  return console.error(`No mutation defined for state key "${e}"`), { success: !1, error: "No mutation defined" };
538
- const a = n.getState().getNestedState(e, []), o = l?.validation?.key;
545
+ const a = r.getState().getNestedState(e, []), o = l?.validation?.key;
539
546
  try {
540
547
  const d = await t.action(a);
541
548
  if (d && !d.success && d.errors && o) {
542
- n.getState().removeValidationError(o), d.errors.forEach((f) => {
543
- const N = [o, ...f.path].join(".");
544
- n.getState().addValidationError(N, f.message);
549
+ r.getState().removeValidationError(o), d.errors.forEach((g) => {
550
+ const N = [o, ...g.path].join(".");
551
+ r.getState().addValidationError(N, g.message);
545
552
  });
546
- const I = n.getState().stateComponents.get(e);
547
- I && I.components.forEach((f) => {
548
- f.forceUpdate();
553
+ const I = r.getState().stateComponents.get(e);
554
+ I && I.components.forEach((g) => {
555
+ g.forceUpdate();
549
556
  });
550
557
  }
551
558
  return d?.success && t.onSuccess ? t.onSuccess(d.data) : !d?.success && t.onError && t.onError(d.error), d;
@@ -554,95 +561,95 @@ function re(e, i, v, u) {
554
561
  }
555
562
  };
556
563
  if (c === "_status") {
557
- const l = n.getState().getNestedState(e, r), t = n.getState().initialStateGlobal[e], a = G(t, r);
564
+ const l = r.getState().getNestedState(e, n), t = r.getState().initialStateGlobal[e], a = G(t, n);
558
565
  return W(l, a) ? "fresh" : "stale";
559
566
  }
560
567
  if (c === "getStatus")
561
568
  return function() {
562
- const l = n().getNestedState(
569
+ const l = r().getNestedState(
563
570
  e,
564
- r
565
- ), t = n.getState().initialStateGlobal[e], a = G(t, r);
571
+ n
572
+ ), t = r.getState().initialStateGlobal[e], a = G(t, n);
566
573
  return W(l, a) ? "fresh" : "stale";
567
574
  };
568
575
  if (c === "removeStorage")
569
576
  return () => {
570
- const l = n.getState().initialStateGlobal[e], t = Y(e), a = J(t?.localStorage?.key) ? t?.localStorage?.key(l) : t?.localStorage?.key, o = `${u}-${e}-${a}`;
577
+ const l = r.getState().initialStateGlobal[e], t = Y(e), a = J(t?.localStorage?.key) ? t?.localStorage?.key(l) : t?.localStorage?.key, o = `${u}-${e}-${a}`;
571
578
  o && localStorage.removeItem(o);
572
579
  };
573
580
  if (c === "showValidationErrors")
574
581
  return () => {
575
- const l = n.getState().getInitialOptions(e)?.validation;
582
+ const l = r.getState().getInitialOptions(e)?.validation;
576
583
  if (!l?.key)
577
584
  throw new Error("Validation key not found");
578
- return n.getState().getValidationErrors(l.key + "." + r.join("."));
585
+ return r.getState().getValidationErrors(l.key + "." + n.join("."));
579
586
  };
580
587
  if (Array.isArray(m)) {
581
588
  const l = () => y?.validIndices ? m.map((a, o) => ({
582
589
  item: a,
583
590
  originalIndex: y.validIndices[o]
584
- })) : n.getState().getNestedState(e, r).map((a, o) => ({
591
+ })) : r.getState().getNestedState(e, n).map((a, o) => ({
585
592
  item: a,
586
593
  originalIndex: o
587
594
  }));
588
595
  if (c === "getSelected")
589
596
  return () => {
590
- const t = n.getState().getSelectedIndex(e, r.join("."));
597
+ const t = r.getState().getSelectedIndex(e, n.join("."));
591
598
  if (t !== void 0)
592
599
  return s(
593
600
  m[t],
594
- [...r, t.toString()],
601
+ [...n, t.toString()],
595
602
  y
596
603
  );
597
604
  };
598
605
  if (c === "clearSelected")
599
606
  return () => {
600
- n.getState().clearSelectedIndex({ stateKey: e, path: r });
607
+ r.getState().clearSelectedIndex({ stateKey: e, path: n });
601
608
  };
602
609
  if (c === "getSelectedIndex")
603
- return () => n.getState().getSelectedIndex(e, r.join(".")) ?? -1;
610
+ return () => r.getState().getSelectedIndex(e, n.join(".")) ?? -1;
604
611
  if (c === "stateSort")
605
612
  return (t) => {
606
613
  const o = [...l()].sort(
607
- (f, N) => t(f.item, N.item)
608
- ), d = o.map(({ item: f }) => f), I = {
614
+ (g, N) => t(g.item, N.item)
615
+ ), d = o.map(({ item: g }) => g), I = {
609
616
  ...y,
610
617
  validIndices: o.map(
611
- ({ originalIndex: f }) => f
618
+ ({ originalIndex: g }) => g
612
619
  )
613
620
  };
614
- return s(d, r, I);
621
+ return s(d, n, I);
615
622
  };
616
623
  if (c === "stateFilter")
617
624
  return (t) => {
618
625
  const o = l().filter(
619
- ({ item: f }, N) => t(f, N)
620
- ), d = o.map(({ item: f }) => f), I = {
626
+ ({ item: g }, N) => t(g, N)
627
+ ), d = o.map(({ item: g }) => g), I = {
621
628
  ...y,
622
629
  validIndices: o.map(
623
- ({ originalIndex: f }) => f
630
+ ({ originalIndex: g }) => g
624
631
  )
625
632
  };
626
- return s(d, r, I);
633
+ return s(d, n, I);
627
634
  };
628
635
  if (c === "stateMap" || c === "stateMapNoRender")
629
636
  return (t) => m.map((o, d) => {
630
637
  let I;
631
638
  y?.validIndices && y.validIndices[d] !== void 0 ? I = y.validIndices[d] : I = d;
632
- const f = [...r, I.toString()], N = s(o, f, y);
639
+ const g = [...n, I.toString()], N = s(o, g, y);
633
640
  return t(
634
641
  o,
635
642
  N,
636
643
  d,
637
644
  m,
638
- s(m, r, y)
645
+ s(m, n, y)
639
646
  );
640
647
  });
641
648
  if (c === "$stateMap")
642
649
  return (t) => ae(We, {
643
650
  proxy: {
644
651
  _stateKey: e,
645
- _path: r,
652
+ _path: n,
646
653
  _mapFn: t
647
654
  // Pass the actual function, not string
648
655
  },
@@ -657,89 +664,89 @@ function re(e, i, v, u) {
657
664
  );
658
665
  return s(
659
666
  o,
660
- [...r, "[*]", t],
667
+ [...n, "[*]", t],
661
668
  y
662
669
  );
663
670
  };
664
671
  if (c === "index")
665
672
  return (t) => {
666
673
  const a = m[t];
667
- return s(a, [...r, t.toString()]);
674
+ return s(a, [...n, t.toString()]);
668
675
  };
669
676
  if (c === "last")
670
677
  return () => {
671
- const t = n.getState().getNestedState(e, r);
678
+ const t = r.getState().getNestedState(e, n);
672
679
  if (t.length === 0) return;
673
- const a = t.length - 1, o = t[a], d = [...r, a.toString()];
680
+ const a = t.length - 1, o = t[a], d = [...n, a.toString()];
674
681
  return s(o, d);
675
682
  };
676
683
  if (c === "insert")
677
- return (t) => (p(r), ce(i, t, r, e), s(
678
- n.getState().getNestedState(e, r),
679
- r
684
+ return (t) => (p(n), ce(i, t, n, e), s(
685
+ r.getState().getNestedState(e, n),
686
+ n
680
687
  ));
681
688
  if (c === "uniqueInsert")
682
689
  return (t, a, o) => {
683
- const d = n.getState().getNestedState(e, r), I = J(t) ? t(d) : t;
684
- let f = null;
690
+ const d = r.getState().getNestedState(e, n), I = J(t) ? t(d) : t;
691
+ let g = null;
685
692
  if (!d.some((w) => {
686
693
  if (a) {
687
694
  const F = a.every(
688
695
  (b) => W(w[b], I[b])
689
696
  );
690
- return F && (f = w), F;
697
+ return F && (g = w), F;
691
698
  }
692
699
  const $ = W(w, I);
693
- return $ && (f = w), $;
700
+ return $ && (g = w), $;
694
701
  }))
695
- p(r), ce(i, I, r, e);
696
- else if (o && f) {
697
- const w = o(f), $ = d.map(
698
- (F) => W(F, f) ? w : F
702
+ p(n), ce(i, I, n, e);
703
+ else if (o && g) {
704
+ const w = o(g), $ = d.map(
705
+ (F) => W(F, g) ? w : F
699
706
  );
700
- p(r), Q(i, $, r);
707
+ p(n), Q(i, $, n);
701
708
  }
702
709
  };
703
710
  if (c === "cut")
704
711
  return (t, a) => {
705
712
  if (!a?.waitForSync)
706
- return p(r), ee(i, r, e, t), s(
707
- n.getState().getNestedState(e, r),
708
- r
713
+ return p(n), ee(i, n, e, t), s(
714
+ r.getState().getNestedState(e, n),
715
+ n
709
716
  );
710
717
  };
711
718
  if (c === "cutByValue")
712
719
  return (t) => {
713
720
  for (let a = 0; a < m.length; a++)
714
- m[a] === t && ee(i, r, e, a);
721
+ m[a] === t && ee(i, n, e, a);
715
722
  };
716
723
  if (c === "toggleByValue")
717
724
  return (t) => {
718
725
  const a = m.findIndex((o) => o === t);
719
- a > -1 ? ee(i, r, e, a) : ce(i, t, r, e);
726
+ a > -1 ? ee(i, n, e, a) : ce(i, t, n, e);
720
727
  };
721
728
  if (c === "stateFind")
722
729
  return (t) => {
723
730
  const o = l().find(
724
- ({ item: I }, f) => t(I, f)
731
+ ({ item: I }, g) => t(I, g)
725
732
  );
726
733
  if (!o) return;
727
- const d = [...r, o.originalIndex.toString()];
734
+ const d = [...n, o.originalIndex.toString()];
728
735
  return s(o.item, d, y);
729
736
  };
730
737
  if (c === "findWith")
731
738
  return (t, a) => {
732
739
  const d = l().find(
733
- ({ item: f }) => f[t] === a
740
+ ({ item: g }) => g[t] === a
734
741
  );
735
742
  if (!d) return;
736
- const I = [...r, d.originalIndex.toString()];
743
+ const I = [...n, d.originalIndex.toString()];
737
744
  return s(d.item, I, y);
738
745
  };
739
746
  }
740
- const L = r[r.length - 1];
747
+ const L = n[n.length - 1];
741
748
  if (!isNaN(Number(L))) {
742
- const l = r.slice(0, -1), t = n.getState().getNestedState(e, l);
749
+ const l = n.slice(0, -1), t = r.getState().getNestedState(e, l);
743
750
  if (Array.isArray(t) && c === "cut")
744
751
  return () => ee(
745
752
  i,
@@ -749,67 +756,67 @@ function re(e, i, v, u) {
749
756
  );
750
757
  }
751
758
  if (c === "get")
752
- return () => n.getState().getNestedState(e, r);
759
+ return () => r.getState().getNestedState(e, n);
753
760
  if (c === "$derive")
754
761
  return (l) => le({
755
762
  _stateKey: e,
756
- _path: r,
763
+ _path: n,
757
764
  _effect: l.toString()
758
765
  });
759
766
  if (c === "$derive")
760
767
  return (l) => le({
761
768
  _stateKey: e,
762
- _path: r,
769
+ _path: n,
763
770
  _effect: l.toString()
764
771
  });
765
772
  if (c === "$get")
766
773
  return () => le({
767
774
  _stateKey: e,
768
- _path: r
775
+ _path: n
769
776
  });
770
777
  if (c === "lastSynced") {
771
- const l = `${e}:${r.join(".")}`;
772
- return n.getState().getSyncInfo(l);
778
+ const l = `${e}:${n.join(".")}`;
779
+ return r.getState().getSyncInfo(l);
773
780
  }
774
781
  if (c == "getLocalStorage")
775
782
  return (l) => oe(u + "-" + e + "-" + l);
776
783
  if (c === "_selected") {
777
- const l = r.slice(0, -1), t = l.join("."), a = n.getState().getNestedState(e, l);
778
- return Array.isArray(a) ? Number(r[r.length - 1]) === n.getState().getSelectedIndex(e, t) : void 0;
784
+ const l = n.slice(0, -1), t = l.join("."), a = r.getState().getNestedState(e, l);
785
+ return Array.isArray(a) ? Number(n[n.length - 1]) === r.getState().getSelectedIndex(e, t) : void 0;
779
786
  }
780
787
  if (c === "setSelected")
781
788
  return (l) => {
782
- const t = r.slice(0, -1), a = Number(r[r.length - 1]), o = t.join(".");
783
- l ? n.getState().setSelectedIndex(e, o, a) : n.getState().setSelectedIndex(e, o, void 0);
784
- const d = n.getState().getNestedState(e, [...t]);
789
+ const t = n.slice(0, -1), a = Number(n[n.length - 1]), o = t.join(".");
790
+ l ? r.getState().setSelectedIndex(e, o, a) : r.getState().setSelectedIndex(e, o, void 0);
791
+ const d = r.getState().getNestedState(e, [...t]);
785
792
  Q(i, d, t), p(t);
786
793
  };
787
794
  if (c === "toggleSelected")
788
795
  return () => {
789
- const l = r.slice(0, -1), t = Number(r[r.length - 1]), a = l.join("."), o = n.getState().getSelectedIndex(e, a);
790
- n.getState().setSelectedIndex(
796
+ const l = n.slice(0, -1), t = Number(n[n.length - 1]), a = l.join("."), o = r.getState().getSelectedIndex(e, a);
797
+ r.getState().setSelectedIndex(
791
798
  e,
792
799
  a,
793
800
  o === t ? void 0 : t
794
801
  );
795
- const d = n.getState().getNestedState(e, [...l]);
802
+ const d = r.getState().getNestedState(e, [...l]);
796
803
  Q(i, d, l), p(l);
797
804
  };
798
- if (r.length == 0) {
805
+ if (n.length == 0) {
799
806
  if (c === "applyJsonPatch")
800
807
  return (l) => {
801
- const t = n.getState().cogsStateStore[e], o = Ce(t, l).newDocument;
808
+ const t = r.getState().cogsStateStore[e], o = Ce(t, l).newDocument;
802
809
  ke(
803
810
  e,
804
- n.getState().initialStateGlobal[e],
811
+ r.getState().initialStateGlobal[e],
805
812
  o,
806
813
  i,
807
814
  v,
808
815
  u
809
816
  );
810
- const d = n.getState().stateComponents.get(e);
817
+ const d = r.getState().stateComponents.get(e);
811
818
  if (d) {
812
- const I = fe(t, o), f = new Set(I);
819
+ const I = ge(t, o), g = new Set(I);
813
820
  for (const [
814
821
  N,
815
822
  w
@@ -822,7 +829,7 @@ function re(e, i, v, u) {
822
829
  continue;
823
830
  }
824
831
  if (F.includes("component") && (w.paths.has("") && ($ = !0), !$))
825
- for (const b of f) {
832
+ for (const b of g) {
826
833
  if (w.paths.has(b)) {
827
834
  $ = !0;
828
835
  break;
@@ -866,21 +873,21 @@ function re(e, i, v, u) {
866
873
  };
867
874
  if (c === "validateZodSchema")
868
875
  return () => {
869
- const l = n.getState().getInitialOptions(e)?.validation, t = n.getState().addValidationError;
876
+ const l = r.getState().getInitialOptions(e)?.validation, t = r.getState().addValidationError;
870
877
  if (!l?.zodSchema)
871
878
  throw new Error("Zod schema not found");
872
879
  if (!l?.key)
873
880
  throw new Error("Validation key not found");
874
881
  B(l.key);
875
- const a = n.getState().cogsStateStore[e];
882
+ const a = r.getState().cogsStateStore[e];
876
883
  try {
877
- const o = n.getState().getValidationErrors(l.key);
884
+ const o = r.getState().getValidationErrors(l.key);
878
885
  o && o.length > 0 && o.forEach(([I]) => {
879
886
  I && I.startsWith(l.key) && B(I);
880
887
  });
881
888
  const d = l.zodSchema.safeParse(a);
882
- return d.success ? !0 : (d.error.errors.forEach((f) => {
883
- const N = f.path, w = f.message, $ = [l.key, ...N].join(".");
889
+ return d.success ? !0 : (d.error.errors.forEach((g) => {
890
+ const N = g.path, w = g.message, $ = [l.key, ...N].join(".");
884
891
  t($, w);
885
892
  }), ie(e), !1);
886
893
  } catch (o) {
@@ -889,22 +896,22 @@ function re(e, i, v, u) {
889
896
  };
890
897
  if (c === "_componentId") return v;
891
898
  if (c === "getComponents")
892
- return () => n().stateComponents.get(e);
899
+ return () => r().stateComponents.get(e);
893
900
  if (c === "getAllFormRefs")
894
901
  return () => ve.getState().getFormRefsByStateKey(e);
895
902
  if (c === "_initialState")
896
- return n.getState().initialStateGlobal[e];
903
+ return r.getState().initialStateGlobal[e];
897
904
  if (c === "_serverState")
898
- return n.getState().serverState[e];
905
+ return r.getState().serverState[e];
899
906
  if (c === "_isLoading")
900
- return n.getState().isLoadingGlobal[e];
907
+ return r.getState().isLoadingGlobal[e];
901
908
  if (c === "revertToInitialState")
902
909
  return h.revertToInitialState;
903
910
  if (c === "updateInitialState") return h.updateInitialState;
904
911
  if (c === "removeValidation") return h.removeValidation;
905
912
  }
906
913
  if (c === "getFormRef")
907
- return () => ve.getState().getFormRef(e + "." + r.join("."));
914
+ return () => ve.getState().getFormRef(e + "." + n.join("."));
908
915
  if (c === "validationWrapper")
909
916
  return ({
910
917
  children: l,
@@ -913,30 +920,30 @@ function re(e, i, v, u) {
913
920
  Pe,
914
921
  {
915
922
  formOpts: t ? { validation: { message: "" } } : void 0,
916
- path: r,
917
- validationKey: n.getState().getInitialOptions(e)?.validation?.key || "",
923
+ path: n,
924
+ validationKey: r.getState().getInitialOptions(e)?.validation?.key || "",
918
925
  stateKey: e,
919
926
  validIndices: y?.validIndices,
920
927
  children: l
921
928
  }
922
929
  );
923
930
  if (c === "_stateKey") return e;
924
- if (c === "_path") return r;
931
+ if (c === "_path") return n;
925
932
  if (c === "_isServerSynced") return h._isServerSynced;
926
933
  if (c === "update")
927
934
  return (l, t) => {
928
935
  if (t?.debounce)
929
936
  be(() => {
930
- Q(i, l, r, "");
931
- const a = n.getState().getNestedState(e, r);
937
+ Q(i, l, n, "");
938
+ const a = r.getState().getNestedState(e, n);
932
939
  t?.afterUpdate && t.afterUpdate(a);
933
940
  }, t.debounce);
934
941
  else {
935
- Q(i, l, r, "");
936
- const a = n.getState().getNestedState(e, r);
942
+ Q(i, l, n, "");
943
+ const a = r.getState().getNestedState(e, n);
937
944
  t?.afterUpdate && t.afterUpdate(a);
938
945
  }
939
- p(r);
946
+ p(n);
940
947
  };
941
948
  if (c === "formElement")
942
949
  return (l, t) => /* @__PURE__ */ ye(
@@ -944,12 +951,12 @@ function re(e, i, v, u) {
944
951
  {
945
952
  setState: i,
946
953
  stateKey: e,
947
- path: r,
954
+ path: n,
948
955
  child: l,
949
956
  formOpts: t
950
957
  }
951
958
  );
952
- const A = [...r, c], X = n.getState().getNestedState(e, A);
959
+ const A = [...n, c], X = r.getState().getNestedState(e, A);
953
960
  return s(X, A, y);
954
961
  }
955
962
  }, U = new Proxy(_, x);
@@ -959,7 +966,7 @@ function re(e, i, v, u) {
959
966
  }), U;
960
967
  }
961
968
  return s(
962
- n.getState().getNestedState(e, [])
969
+ r.getState().getNestedState(e, [])
963
970
  );
964
971
  }
965
972
  function le(e) {
@@ -969,7 +976,7 @@ function We({
969
976
  proxy: e,
970
977
  rebuildStateShape: i
971
978
  }) {
972
- const v = n().getNestedState(e._stateKey, e._path);
979
+ const v = r().getNestedState(e._stateKey, e._path);
973
980
  return Array.isArray(v) ? i(
974
981
  v,
975
982
  e._path
@@ -993,20 +1000,20 @@ function Ge({
993
1000
  position: p,
994
1001
  effect: e._effect
995
1002
  };
996
- n.getState().addSignalElement(v, m);
997
- const r = n.getState().getNestedState(e._stateKey, e._path);
1003
+ r.getState().addSignalElement(v, m);
1004
+ const n = r.getState().getNestedState(e._stateKey, e._path);
998
1005
  let y;
999
1006
  if (e._effect)
1000
1007
  try {
1001
1008
  y = new Function(
1002
1009
  "state",
1003
1010
  `return (${e._effect})(state)`
1004
- )(r);
1011
+ )(n);
1005
1012
  } catch (_) {
1006
- console.error("Error evaluating effect function during mount:", _), y = r;
1013
+ console.error("Error evaluating effect function during mount:", _), y = n;
1007
1014
  }
1008
1015
  else
1009
- y = r;
1016
+ y = n;
1010
1017
  y !== null && typeof y == "object" && (y = JSON.stringify(y));
1011
1018
  const j = document.createTextNode(String(y));
1012
1019
  u.replaceWith(j);
@@ -1019,7 +1026,7 @@ function Ge({
1019
1026
  function nt(e) {
1020
1027
  const i = Ve(
1021
1028
  (v) => {
1022
- const u = n.getState().stateComponents.get(e._stateKey) || {
1029
+ const u = r.getState().stateComponents.get(e._stateKey) || {
1023
1030
  components: /* @__PURE__ */ new Map()
1024
1031
  };
1025
1032
  return u.components.set(e._stateKey, {
@@ -1027,7 +1034,7 @@ function nt(e) {
1027
1034
  paths: /* @__PURE__ */ new Set([e._path.join(".")])
1028
1035
  }), () => u.components.delete(e._stateKey);
1029
1036
  },
1030
- () => n.getState().getNestedState(e._stateKey, e._path)
1037
+ () => r.getState().getNestedState(e._stateKey, e._path)
1031
1038
  );
1032
1039
  return ae("text", {}, String(i));
1033
1040
  }