cogsbox-state 0.5.221 → 0.5.222

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 ge, debounce as be } from "./utility.js";
4
+ import { transformStateFunc as Ae, isDeepEqual as W, isFunction as J, getNestedValue as G, getDifferences as fe, 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 fe } from "uuid";
7
+ import { v4 as ge } from "uuid";
8
8
  import "zod";
9
- import { getGlobalStore as r, formRefStore as ve } from "./store.js";
9
+ import { getGlobalStore as n, 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 = r.getState().getInitialOptions, u = r.getState().setInitialStateOptions, S = v(e) || {};
13
+ const v = n.getState().getInitialOptions, u = n.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 = r.getState().setInitialStateOptions, p = { ...S, ...u };
24
+ const u = Y(e) || {}, S = v[e] || {}, E = n.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) || r.getState().setInitialStateOptions(h, S[h]);
46
- }), r.getState().setInitialStates(u), r.getState().setCreatedState(u);
45
+ }, Y(h) || n.getState().setInitialStateOptions(h, S[h]);
46
+ }), n.getState().setInitialStates(u), n.getState().setCreatedState(u);
47
47
  const E = (h, s) => {
48
- const [m] = de(s?.componentId ?? fe());
48
+ const [m] = de(s?.componentId ?? ge());
49
49
  Ie({
50
50
  stateKey: h,
51
51
  options: s,
52
52
  initialOptionsPart: S
53
53
  });
54
- const n = r.getState().cogsStateStore[h] || u[h], y = s?.modifyState ? s.modifyState(n) : n, [j, _] = De(
54
+ const r = n.getState().cogsStateStore[h] || u[h], y = s?.modifyState ? s.modifyState(r) : r, [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
- } = r.getState(), pe = (e, i, v, u, S) => {
87
+ } = n.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 = r.getState().cogsStateStore[e], { sessionId: u } = we(), S = J(i?.localStorage?.key) ? i.localStorage.key(v) : i?.localStorage?.key;
121
+ const v = n.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 = r.getState().stateComponents.get(e);
143
+ const i = n.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 = r.getState().stateComponents.get(e);
152
+ const v = n.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: n,
192
+ dependencies: r,
193
193
  serverState: y
194
194
  } = {}) {
195
195
  const [j, _] = de({}), { sessionId: x } = we();
196
196
  let U = !i;
197
- const [f] = de(i ?? fe()), c = r.getState().stateLog[f], K = te(/* @__PURE__ */ new Set()), L = te(h ?? fe()), A = te(
197
+ const [g] = de(i ?? ge()), c = n.getState().stateLog[g], K = te(/* @__PURE__ */ new Set()), L = te(h ?? ge()), A = te(
198
198
  null
199
199
  );
200
- A.current = Y(f) ?? null, ue(() => {
201
- if (m && m.stateKey === f && m.path?.[0]) {
202
- H(f, (a) => ({
200
+ A.current = Y(g) ?? null, ue(() => {
201
+ if (m && m.stateKey === g && m.path?.[0]) {
202
+ H(g, (a) => ({
203
203
  ...a,
204
204
  [m.path[0]]: m.newValue
205
205
  }));
206
206
  const t = `${m.stateKey}:${m.path.join(".")}`;
207
- r.getState().setSyncInfo(t, {
207
+ n.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(f, {
214
+ he(g, {
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 = r.getState().initialStateGlobal[f];
217
+ const t = A.current, o = t?.serverState?.id !== void 0 && t?.serverState?.status === "success" && t?.serverState?.data, d = n.getState().initialStateGlobal[g];
218
218
  if (!(d && !W(d, s) || !d) && !o)
219
219
  return;
220
- let g = null;
220
+ let f = null;
221
221
  const N = J(t?.localStorage?.key) ? t?.localStorage?.key(s) : t?.localStorage?.key;
222
- N && x && (g = oe(`${x}-${f}-${N}`));
222
+ N && x && (f = oe(`${x}-${g}-${N}`));
223
223
  let w = s, $ = !1;
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,
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,
227
227
  s,
228
228
  w,
229
229
  X,
230
230
  L.current,
231
231
  x
232
- ), $ && N && x && pe(w, f, t, x, Date.now()), ie(f), (Array.isArray(p) ? p : [p || "component"]).includes("none") || _({});
232
+ ), $ && N && x && pe(w, g, t, x, Date.now()), ie(g), (Array.isArray(p) ? p : [p || "component"]).includes("none") || _({});
233
233
  }
234
234
  }, [
235
235
  s,
236
236
  y?.status,
237
237
  y?.data,
238
- ...n || []
238
+ ...r || []
239
239
  ]), $e(() => {
240
- U && he(f, {
240
+ U && he(g, {
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 = `${f}////${L.current}`, a = r.getState().stateComponents.get(f) || {
247
+ const t = `${g}////${L.current}`, a = n.getState().stateComponents.get(g) || {
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
- }), 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));
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));
259
259
  };
260
260
  }, []);
261
261
  const X = (t, a, o, d) => {
262
262
  if (Array.isArray(a)) {
263
- const I = `${f}-${a.join(".")}`;
263
+ const I = `${g}-${a.join(".")}`;
264
264
  K.current.add(I);
265
265
  }
266
- H(f, (I) => {
267
- const g = J(t) ? t(I) : t, N = `${f}-${a.join(".")}`;
266
+ H(g, (I) => {
267
+ const f = J(t) ? t(I) : t, N = `${g}-${a.join(".")}`;
268
268
  if (N) {
269
- let R = !1, k = r.getState().signalDomElements.get(N);
269
+ let R = !1, k = n.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(g, O);
271
+ const O = a.slice(0, -1), C = G(f, O);
272
272
  if (Array.isArray(C)) {
273
273
  R = !0;
274
- const P = `${f}-${O.join(".")}`;
275
- k = r.getState().signalDomElements.get(P);
274
+ const P = `${g}-${O.join(".")}`;
275
+ k = n.getState().signalDomElements.get(P);
276
276
  }
277
277
  }
278
278
  if (k) {
279
- const O = R ? G(g, a.slice(0, -1)) : G(g, a);
279
+ const O = R ? G(f, a.slice(0, -1)) : G(f, 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 $ = r.getState().stateComponents.get(f);
309
+ const $ = n.getState().stateComponents.get(g);
310
310
  if ($) {
311
- const R = ge(I, g), k = new Set(R), O = o.updateType === "update" ? a.join(".") : a.slice(0, -1).join(".") || "", C = [];
311
+ const R = fe(I, f), 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(g);
352
+ const z = T.depsFunction(f);
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(g, O);
365
+ const O = a.slice(0, -1), C = G(f, 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
- g,
371
+ f,
372
372
  a
373
373
  ), q = {
374
374
  timeStamp: F,
375
- stateKey: f,
375
+ stateKey: g,
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(f, (R) => {
382
+ if (Fe(g, (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
- g,
390
389
  f,
390
+ g,
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 = r.getState().serverState[f], k = A.current?.serverSync;
398
- je(f, {
399
- syncKey: typeof k.syncKey == "string" ? k.syncKey : k.syncKey({ state: g }),
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 }),
400
400
  rollBackState: R,
401
401
  actionTimeStamp: Date.now() + (k.debounce ?? 3e3),
402
402
  status: "waiting"
403
403
  });
404
404
  }
405
- return g;
405
+ return f;
406
406
  });
407
407
  };
408
- r.getState().updaterState[f] || (ne(
409
- f,
408
+ n.getState().updaterState[g] || (ne(
409
+ g,
410
410
  re(
411
- f,
411
+ g,
412
412
  X,
413
413
  L.current,
414
414
  x
415
415
  )
416
- ), r.getState().cogsStateStore[f] || H(f, e), r.getState().initialStateGlobal[f] || Se(f, e));
416
+ ), n.getState().cogsStateStore[g] || H(g, e), n.getState().initialStateGlobal[g] || Se(g, e));
417
417
  const l = Te(() => re(
418
- f,
418
+ g,
419
419
  X,
420
420
  L.current,
421
421
  x
422
- ), [f, x]);
423
- return [Ee(f), l];
422
+ ), [g, x]);
423
+ return [Ee(g), 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 n = m.join(".");
429
+ const r = m.join(".");
430
430
  for (const [y] of S)
431
- (y === n || y.startsWith(n + ".")) && S.delete(y);
431
+ (y === r || y.startsWith(r + ".")) && 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 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++;
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++;
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 f = r.getState().stateComponents.get(e);
445
- return f && f.components.forEach((c) => {
444
+ const g = n.getState().stateComponents.get(e);
445
+ return g && g.components.forEach((c) => {
446
446
  c.forceUpdate();
447
447
  }), y;
448
448
  },
449
449
  updateInitialState: (m) => {
450
450
  S.clear(), E++;
451
- const n = re(
451
+ const r = re(
452
452
  e,
453
453
  i,
454
454
  v,
455
455
  u
456
- ), y = r.getState().initialStateGlobal[e], j = Y(e), _ = J(j?.localStorage?.key) ? j?.localStorage?.key(y) : j?.localStorage?.key, x = `${u}-${e}-${_}`;
456
+ ), y = n.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, n), H(e, m);
459
- const U = r.getState().stateComponents.get(e);
460
- U && U.components.forEach((f) => {
461
- f.forceUpdate();
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();
462
462
  });
463
463
  }), {
464
- fetchId: (U) => n.get()[U]
464
+ fetchId: (U) => r.get()[U]
465
465
  };
466
466
  },
467
- _initialState: r.getState().initialStateGlobal[e],
468
- _serverState: r.getState().serverState[e],
469
- _isLoading: r.getState().isLoadingGlobal[e],
467
+ _initialState: n.getState().initialStateGlobal[e],
468
+ _serverState: n.getState().serverState[e],
469
+ _isLoading: n.getState().isLoadingGlobal[e],
470
470
  _isServerSynced: () => {
471
- const m = r.getState().serverState[e];
471
+ const m = n.getState().serverState[e];
472
472
  return !!(m && W(m, Ee(e)));
473
473
  }
474
474
  };
475
- function s(m, n = [], y) {
476
- const j = n.map(String).join(".");
475
+ function s(m, r = [], y) {
476
+ const j = r.map(String).join(".");
477
477
  S.get(j);
478
478
  const _ = function() {
479
- return r().getNestedState(e, n);
479
+ return n().getNestedState(e, r);
480
480
  };
481
- Object.keys(h).forEach((f) => {
482
- _[f] = h[f];
481
+ Object.keys(h).forEach((g) => {
482
+ _[g] = h[g];
483
483
  });
484
484
  const x = {
485
- apply(f, c, K) {
485
+ apply(g, c, K) {
486
486
  return console.log(
487
- `PROXY APPLY TRAP HIT: stateKey=${e}, path=${n.join(".")}`
488
- ), console.trace("Apply trap stack trace"), r().getNestedState(e, n);
487
+ `PROXY APPLY TRAP HIT: stateKey=${e}, path=${r.join(".")}`
488
+ ), console.trace("Apply trap stack trace"), n().getNestedState(e, r);
489
489
  },
490
- get(f, c) {
490
+ get(g, c) {
491
491
  y?.validIndices && !Array.isArray(m) && (y = { ...y, validIndices: void 0 });
492
492
  const K = /* @__PURE__ */ new Set([
493
493
  "insert",
@@ -510,43 +510,42 @@ function re(e, i, v, u) {
510
510
  "middleware"
511
511
  ]);
512
512
  if (c !== "then" && !c.startsWith("$") && c !== "stateMapNoRender" && !K.has(c)) {
513
- console.log("adding path start", n, c);
514
- const l = `${e}////${v}`, t = r.getState().stateComponents.get(e);
513
+ const l = `${e}////${v}`, t = n.getState().stateComponents.get(e);
515
514
  if (t) {
516
515
  const a = t.components.get(l);
517
- if (a && !a.paths.has("")) {
518
- const o = n.join(".");
516
+ if (a && !a.pathsInitialized && (a.pathsInitialized = !0, !a.paths.has(""))) {
517
+ const o = r.join(".");
519
518
  let d = !0;
520
519
  for (const I of a.paths)
521
520
  if (o.startsWith(I) && (o === I || o[I.length] === ".")) {
522
521
  d = !1;
523
522
  break;
524
523
  }
525
- d && (console.log("adding path addimg", o, c), a.paths.add(o));
524
+ d && a.paths.add(o);
526
525
  }
527
526
  }
528
527
  }
529
528
  if (c === "getDifferences")
530
- return () => ge(
531
- r.getState().cogsStateStore[e],
532
- r.getState().initialStateGlobal[e]
529
+ return () => fe(
530
+ n.getState().cogsStateStore[e],
531
+ n.getState().initialStateGlobal[e]
533
532
  );
534
- if (c === "sync" && n.length === 0)
533
+ if (c === "sync" && r.length === 0)
535
534
  return async function() {
536
- const l = r.getState().getInitialOptions(e), t = l?.sync;
535
+ const l = n.getState().getInitialOptions(e), t = l?.sync;
537
536
  if (!t)
538
537
  return console.error(`No mutation defined for state key "${e}"`), { success: !1, error: "No mutation defined" };
539
- const a = r.getState().getNestedState(e, []), o = l?.validation?.key;
538
+ const a = n.getState().getNestedState(e, []), o = l?.validation?.key;
540
539
  try {
541
540
  const d = await t.action(a);
542
541
  if (d && !d.success && d.errors && o) {
543
- r.getState().removeValidationError(o), d.errors.forEach((g) => {
544
- const N = [o, ...g.path].join(".");
545
- r.getState().addValidationError(N, g.message);
542
+ n.getState().removeValidationError(o), d.errors.forEach((f) => {
543
+ const N = [o, ...f.path].join(".");
544
+ n.getState().addValidationError(N, f.message);
546
545
  });
547
- const I = r.getState().stateComponents.get(e);
548
- I && I.components.forEach((g) => {
549
- g.forceUpdate();
546
+ const I = n.getState().stateComponents.get(e);
547
+ I && I.components.forEach((f) => {
548
+ f.forceUpdate();
550
549
  });
551
550
  }
552
551
  return d?.success && t.onSuccess ? t.onSuccess(d.data) : !d?.success && t.onError && t.onError(d.error), d;
@@ -555,95 +554,95 @@ function re(e, i, v, u) {
555
554
  }
556
555
  };
557
556
  if (c === "_status") {
558
- const l = r.getState().getNestedState(e, n), t = r.getState().initialStateGlobal[e], a = G(t, n);
557
+ const l = n.getState().getNestedState(e, r), t = n.getState().initialStateGlobal[e], a = G(t, r);
559
558
  return W(l, a) ? "fresh" : "stale";
560
559
  }
561
560
  if (c === "getStatus")
562
561
  return function() {
563
- const l = r().getNestedState(
562
+ const l = n().getNestedState(
564
563
  e,
565
- n
566
- ), t = r.getState().initialStateGlobal[e], a = G(t, n);
564
+ r
565
+ ), t = n.getState().initialStateGlobal[e], a = G(t, r);
567
566
  return W(l, a) ? "fresh" : "stale";
568
567
  };
569
568
  if (c === "removeStorage")
570
569
  return () => {
571
- 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}`;
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}`;
572
571
  o && localStorage.removeItem(o);
573
572
  };
574
573
  if (c === "showValidationErrors")
575
574
  return () => {
576
- const l = r.getState().getInitialOptions(e)?.validation;
575
+ const l = n.getState().getInitialOptions(e)?.validation;
577
576
  if (!l?.key)
578
577
  throw new Error("Validation key not found");
579
- return r.getState().getValidationErrors(l.key + "." + n.join("."));
578
+ return n.getState().getValidationErrors(l.key + "." + r.join("."));
580
579
  };
581
580
  if (Array.isArray(m)) {
582
581
  const l = () => y?.validIndices ? m.map((a, o) => ({
583
582
  item: a,
584
583
  originalIndex: y.validIndices[o]
585
- })) : r.getState().getNestedState(e, n).map((a, o) => ({
584
+ })) : n.getState().getNestedState(e, r).map((a, o) => ({
586
585
  item: a,
587
586
  originalIndex: o
588
587
  }));
589
588
  if (c === "getSelected")
590
589
  return () => {
591
- const t = r.getState().getSelectedIndex(e, n.join("."));
590
+ const t = n.getState().getSelectedIndex(e, r.join("."));
592
591
  if (t !== void 0)
593
592
  return s(
594
593
  m[t],
595
- [...n, t.toString()],
594
+ [...r, t.toString()],
596
595
  y
597
596
  );
598
597
  };
599
598
  if (c === "clearSelected")
600
599
  return () => {
601
- r.getState().clearSelectedIndex({ stateKey: e, path: n });
600
+ n.getState().clearSelectedIndex({ stateKey: e, path: r });
602
601
  };
603
602
  if (c === "getSelectedIndex")
604
- return () => r.getState().getSelectedIndex(e, n.join(".")) ?? -1;
603
+ return () => n.getState().getSelectedIndex(e, r.join(".")) ?? -1;
605
604
  if (c === "stateSort")
606
605
  return (t) => {
607
606
  const o = [...l()].sort(
608
- (g, N) => t(g.item, N.item)
609
- ), d = o.map(({ item: g }) => g), I = {
607
+ (f, N) => t(f.item, N.item)
608
+ ), d = o.map(({ item: f }) => f), I = {
610
609
  ...y,
611
610
  validIndices: o.map(
612
- ({ originalIndex: g }) => g
611
+ ({ originalIndex: f }) => f
613
612
  )
614
613
  };
615
- return s(d, n, I);
614
+ return s(d, r, I);
616
615
  };
617
616
  if (c === "stateFilter")
618
617
  return (t) => {
619
618
  const o = l().filter(
620
- ({ item: g }, N) => t(g, N)
621
- ), d = o.map(({ item: g }) => g), I = {
619
+ ({ item: f }, N) => t(f, N)
620
+ ), d = o.map(({ item: f }) => f), I = {
622
621
  ...y,
623
622
  validIndices: o.map(
624
- ({ originalIndex: g }) => g
623
+ ({ originalIndex: f }) => f
625
624
  )
626
625
  };
627
- return s(d, n, I);
626
+ return s(d, r, I);
628
627
  };
629
628
  if (c === "stateMap" || c === "stateMapNoRender")
630
629
  return (t) => m.map((o, d) => {
631
630
  let I;
632
631
  y?.validIndices && y.validIndices[d] !== void 0 ? I = y.validIndices[d] : I = d;
633
- const g = [...n, I.toString()], N = s(o, g, y);
632
+ const f = [...r, I.toString()], N = s(o, f, y);
634
633
  return t(
635
634
  o,
636
635
  N,
637
636
  d,
638
637
  m,
639
- s(m, n, y)
638
+ s(m, r, y)
640
639
  );
641
640
  });
642
641
  if (c === "$stateMap")
643
642
  return (t) => ae(We, {
644
643
  proxy: {
645
644
  _stateKey: e,
646
- _path: n,
645
+ _path: r,
647
646
  _mapFn: t
648
647
  // Pass the actual function, not string
649
648
  },
@@ -658,89 +657,89 @@ function re(e, i, v, u) {
658
657
  );
659
658
  return s(
660
659
  o,
661
- [...n, "[*]", t],
660
+ [...r, "[*]", t],
662
661
  y
663
662
  );
664
663
  };
665
664
  if (c === "index")
666
665
  return (t) => {
667
666
  const a = m[t];
668
- return s(a, [...n, t.toString()]);
667
+ return s(a, [...r, t.toString()]);
669
668
  };
670
669
  if (c === "last")
671
670
  return () => {
672
- const t = r.getState().getNestedState(e, n);
671
+ const t = n.getState().getNestedState(e, r);
673
672
  if (t.length === 0) return;
674
- const a = t.length - 1, o = t[a], d = [...n, a.toString()];
673
+ const a = t.length - 1, o = t[a], d = [...r, a.toString()];
675
674
  return s(o, d);
676
675
  };
677
676
  if (c === "insert")
678
- return (t) => (p(n), ce(i, t, n, e), s(
679
- r.getState().getNestedState(e, n),
680
- n
677
+ return (t) => (p(r), ce(i, t, r, e), s(
678
+ n.getState().getNestedState(e, r),
679
+ r
681
680
  ));
682
681
  if (c === "uniqueInsert")
683
682
  return (t, a, o) => {
684
- const d = r.getState().getNestedState(e, n), I = J(t) ? t(d) : t;
685
- let g = null;
683
+ const d = n.getState().getNestedState(e, r), I = J(t) ? t(d) : t;
684
+ let f = null;
686
685
  if (!d.some((w) => {
687
686
  if (a) {
688
687
  const F = a.every(
689
688
  (b) => W(w[b], I[b])
690
689
  );
691
- return F && (g = w), F;
690
+ return F && (f = w), F;
692
691
  }
693
692
  const $ = W(w, I);
694
- return $ && (g = w), $;
693
+ return $ && (f = w), $;
695
694
  }))
696
- p(n), ce(i, I, n, e);
697
- else if (o && g) {
698
- const w = o(g), $ = d.map(
699
- (F) => W(F, g) ? w : F
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
700
699
  );
701
- p(n), Q(i, $, n);
700
+ p(r), Q(i, $, r);
702
701
  }
703
702
  };
704
703
  if (c === "cut")
705
704
  return (t, a) => {
706
705
  if (!a?.waitForSync)
707
- return p(n), ee(i, n, e, t), s(
708
- r.getState().getNestedState(e, n),
709
- n
706
+ return p(r), ee(i, r, e, t), s(
707
+ n.getState().getNestedState(e, r),
708
+ r
710
709
  );
711
710
  };
712
711
  if (c === "cutByValue")
713
712
  return (t) => {
714
713
  for (let a = 0; a < m.length; a++)
715
- m[a] === t && ee(i, n, e, a);
714
+ m[a] === t && ee(i, r, e, a);
716
715
  };
717
716
  if (c === "toggleByValue")
718
717
  return (t) => {
719
718
  const a = m.findIndex((o) => o === t);
720
- a > -1 ? ee(i, n, e, a) : ce(i, t, n, e);
719
+ a > -1 ? ee(i, r, e, a) : ce(i, t, r, e);
721
720
  };
722
721
  if (c === "stateFind")
723
722
  return (t) => {
724
723
  const o = l().find(
725
- ({ item: I }, g) => t(I, g)
724
+ ({ item: I }, f) => t(I, f)
726
725
  );
727
726
  if (!o) return;
728
- const d = [...n, o.originalIndex.toString()];
727
+ const d = [...r, o.originalIndex.toString()];
729
728
  return s(o.item, d, y);
730
729
  };
731
730
  if (c === "findWith")
732
731
  return (t, a) => {
733
732
  const d = l().find(
734
- ({ item: g }) => g[t] === a
733
+ ({ item: f }) => f[t] === a
735
734
  );
736
735
  if (!d) return;
737
- const I = [...n, d.originalIndex.toString()];
736
+ const I = [...r, d.originalIndex.toString()];
738
737
  return s(d.item, I, y);
739
738
  };
740
739
  }
741
- const L = n[n.length - 1];
740
+ const L = r[r.length - 1];
742
741
  if (!isNaN(Number(L))) {
743
- const l = n.slice(0, -1), t = r.getState().getNestedState(e, l);
742
+ const l = r.slice(0, -1), t = n.getState().getNestedState(e, l);
744
743
  if (Array.isArray(t) && c === "cut")
745
744
  return () => ee(
746
745
  i,
@@ -750,67 +749,67 @@ function re(e, i, v, u) {
750
749
  );
751
750
  }
752
751
  if (c === "get")
753
- return () => r.getState().getNestedState(e, n);
752
+ return () => n.getState().getNestedState(e, r);
754
753
  if (c === "$derive")
755
754
  return (l) => le({
756
755
  _stateKey: e,
757
- _path: n,
756
+ _path: r,
758
757
  _effect: l.toString()
759
758
  });
760
759
  if (c === "$derive")
761
760
  return (l) => le({
762
761
  _stateKey: e,
763
- _path: n,
762
+ _path: r,
764
763
  _effect: l.toString()
765
764
  });
766
765
  if (c === "$get")
767
766
  return () => le({
768
767
  _stateKey: e,
769
- _path: n
768
+ _path: r
770
769
  });
771
770
  if (c === "lastSynced") {
772
- const l = `${e}:${n.join(".")}`;
773
- return r.getState().getSyncInfo(l);
771
+ const l = `${e}:${r.join(".")}`;
772
+ return n.getState().getSyncInfo(l);
774
773
  }
775
774
  if (c == "getLocalStorage")
776
775
  return (l) => oe(u + "-" + e + "-" + l);
777
776
  if (c === "_selected") {
778
- const l = n.slice(0, -1), t = l.join("."), a = r.getState().getNestedState(e, l);
779
- return Array.isArray(a) ? Number(n[n.length - 1]) === r.getState().getSelectedIndex(e, t) : void 0;
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;
780
779
  }
781
780
  if (c === "setSelected")
782
781
  return (l) => {
783
- const t = n.slice(0, -1), a = Number(n[n.length - 1]), o = t.join(".");
784
- l ? r.getState().setSelectedIndex(e, o, a) : r.getState().setSelectedIndex(e, o, void 0);
785
- const d = r.getState().getNestedState(e, [...t]);
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]);
786
785
  Q(i, d, t), p(t);
787
786
  };
788
787
  if (c === "toggleSelected")
789
788
  return () => {
790
- const l = n.slice(0, -1), t = Number(n[n.length - 1]), a = l.join("."), o = r.getState().getSelectedIndex(e, a);
791
- r.getState().setSelectedIndex(
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(
792
791
  e,
793
792
  a,
794
793
  o === t ? void 0 : t
795
794
  );
796
- const d = r.getState().getNestedState(e, [...l]);
795
+ const d = n.getState().getNestedState(e, [...l]);
797
796
  Q(i, d, l), p(l);
798
797
  };
799
- if (n.length == 0) {
798
+ if (r.length == 0) {
800
799
  if (c === "applyJsonPatch")
801
800
  return (l) => {
802
- const t = r.getState().cogsStateStore[e], o = Ce(t, l).newDocument;
801
+ const t = n.getState().cogsStateStore[e], o = Ce(t, l).newDocument;
803
802
  ke(
804
803
  e,
805
- r.getState().initialStateGlobal[e],
804
+ n.getState().initialStateGlobal[e],
806
805
  o,
807
806
  i,
808
807
  v,
809
808
  u
810
809
  );
811
- const d = r.getState().stateComponents.get(e);
810
+ const d = n.getState().stateComponents.get(e);
812
811
  if (d) {
813
- const I = ge(t, o), g = new Set(I);
812
+ const I = fe(t, o), f = new Set(I);
814
813
  for (const [
815
814
  N,
816
815
  w
@@ -823,7 +822,7 @@ function re(e, i, v, u) {
823
822
  continue;
824
823
  }
825
824
  if (F.includes("component") && (w.paths.has("") && ($ = !0), !$))
826
- for (const b of g) {
825
+ for (const b of f) {
827
826
  if (w.paths.has(b)) {
828
827
  $ = !0;
829
828
  break;
@@ -867,21 +866,21 @@ function re(e, i, v, u) {
867
866
  };
868
867
  if (c === "validateZodSchema")
869
868
  return () => {
870
- const l = r.getState().getInitialOptions(e)?.validation, t = r.getState().addValidationError;
869
+ const l = n.getState().getInitialOptions(e)?.validation, t = n.getState().addValidationError;
871
870
  if (!l?.zodSchema)
872
871
  throw new Error("Zod schema not found");
873
872
  if (!l?.key)
874
873
  throw new Error("Validation key not found");
875
874
  B(l.key);
876
- const a = r.getState().cogsStateStore[e];
875
+ const a = n.getState().cogsStateStore[e];
877
876
  try {
878
- const o = r.getState().getValidationErrors(l.key);
877
+ const o = n.getState().getValidationErrors(l.key);
879
878
  o && o.length > 0 && o.forEach(([I]) => {
880
879
  I && I.startsWith(l.key) && B(I);
881
880
  });
882
881
  const d = l.zodSchema.safeParse(a);
883
- return d.success ? !0 : (d.error.errors.forEach((g) => {
884
- const N = g.path, w = g.message, $ = [l.key, ...N].join(".");
882
+ return d.success ? !0 : (d.error.errors.forEach((f) => {
883
+ const N = f.path, w = f.message, $ = [l.key, ...N].join(".");
885
884
  t($, w);
886
885
  }), ie(e), !1);
887
886
  } catch (o) {
@@ -890,22 +889,22 @@ function re(e, i, v, u) {
890
889
  };
891
890
  if (c === "_componentId") return v;
892
891
  if (c === "getComponents")
893
- return () => r().stateComponents.get(e);
892
+ return () => n().stateComponents.get(e);
894
893
  if (c === "getAllFormRefs")
895
894
  return () => ve.getState().getFormRefsByStateKey(e);
896
895
  if (c === "_initialState")
897
- return r.getState().initialStateGlobal[e];
896
+ return n.getState().initialStateGlobal[e];
898
897
  if (c === "_serverState")
899
- return r.getState().serverState[e];
898
+ return n.getState().serverState[e];
900
899
  if (c === "_isLoading")
901
- return r.getState().isLoadingGlobal[e];
900
+ return n.getState().isLoadingGlobal[e];
902
901
  if (c === "revertToInitialState")
903
902
  return h.revertToInitialState;
904
903
  if (c === "updateInitialState") return h.updateInitialState;
905
904
  if (c === "removeValidation") return h.removeValidation;
906
905
  }
907
906
  if (c === "getFormRef")
908
- return () => ve.getState().getFormRef(e + "." + n.join("."));
907
+ return () => ve.getState().getFormRef(e + "." + r.join("."));
909
908
  if (c === "validationWrapper")
910
909
  return ({
911
910
  children: l,
@@ -914,30 +913,30 @@ function re(e, i, v, u) {
914
913
  Pe,
915
914
  {
916
915
  formOpts: t ? { validation: { message: "" } } : void 0,
917
- path: n,
918
- validationKey: r.getState().getInitialOptions(e)?.validation?.key || "",
916
+ path: r,
917
+ validationKey: n.getState().getInitialOptions(e)?.validation?.key || "",
919
918
  stateKey: e,
920
919
  validIndices: y?.validIndices,
921
920
  children: l
922
921
  }
923
922
  );
924
923
  if (c === "_stateKey") return e;
925
- if (c === "_path") return n;
924
+ if (c === "_path") return r;
926
925
  if (c === "_isServerSynced") return h._isServerSynced;
927
926
  if (c === "update")
928
927
  return (l, t) => {
929
928
  if (t?.debounce)
930
929
  be(() => {
931
- Q(i, l, n, "");
932
- const a = r.getState().getNestedState(e, n);
930
+ Q(i, l, r, "");
931
+ const a = n.getState().getNestedState(e, r);
933
932
  t?.afterUpdate && t.afterUpdate(a);
934
933
  }, t.debounce);
935
934
  else {
936
- Q(i, l, n, "");
937
- const a = r.getState().getNestedState(e, n);
935
+ Q(i, l, r, "");
936
+ const a = n.getState().getNestedState(e, r);
938
937
  t?.afterUpdate && t.afterUpdate(a);
939
938
  }
940
- p(n);
939
+ p(r);
941
940
  };
942
941
  if (c === "formElement")
943
942
  return (l, t) => /* @__PURE__ */ ye(
@@ -945,12 +944,12 @@ function re(e, i, v, u) {
945
944
  {
946
945
  setState: i,
947
946
  stateKey: e,
948
- path: n,
947
+ path: r,
949
948
  child: l,
950
949
  formOpts: t
951
950
  }
952
951
  );
953
- const A = [...n, c], X = r.getState().getNestedState(e, A);
952
+ const A = [...r, c], X = n.getState().getNestedState(e, A);
954
953
  return s(X, A, y);
955
954
  }
956
955
  }, U = new Proxy(_, x);
@@ -960,7 +959,7 @@ function re(e, i, v, u) {
960
959
  }), U;
961
960
  }
962
961
  return s(
963
- r.getState().getNestedState(e, [])
962
+ n.getState().getNestedState(e, [])
964
963
  );
965
964
  }
966
965
  function le(e) {
@@ -970,7 +969,7 @@ function We({
970
969
  proxy: e,
971
970
  rebuildStateShape: i
972
971
  }) {
973
- const v = r().getNestedState(e._stateKey, e._path);
972
+ const v = n().getNestedState(e._stateKey, e._path);
974
973
  return Array.isArray(v) ? i(
975
974
  v,
976
975
  e._path
@@ -994,20 +993,20 @@ function Ge({
994
993
  position: p,
995
994
  effect: e._effect
996
995
  };
997
- r.getState().addSignalElement(v, m);
998
- const n = r.getState().getNestedState(e._stateKey, e._path);
996
+ n.getState().addSignalElement(v, m);
997
+ const r = n.getState().getNestedState(e._stateKey, e._path);
999
998
  let y;
1000
999
  if (e._effect)
1001
1000
  try {
1002
1001
  y = new Function(
1003
1002
  "state",
1004
1003
  `return (${e._effect})(state)`
1005
- )(n);
1004
+ )(r);
1006
1005
  } catch (_) {
1007
- console.error("Error evaluating effect function during mount:", _), y = n;
1006
+ console.error("Error evaluating effect function during mount:", _), y = r;
1008
1007
  }
1009
1008
  else
1010
- y = n;
1009
+ y = r;
1011
1010
  y !== null && typeof y == "object" && (y = JSON.stringify(y));
1012
1011
  const j = document.createTextNode(String(y));
1013
1012
  u.replaceWith(j);
@@ -1020,7 +1019,7 @@ function Ge({
1020
1019
  function nt(e) {
1021
1020
  const i = Ve(
1022
1021
  (v) => {
1023
- const u = r.getState().stateComponents.get(e._stateKey) || {
1022
+ const u = n.getState().stateComponents.get(e._stateKey) || {
1024
1023
  components: /* @__PURE__ */ new Map()
1025
1024
  };
1026
1025
  return u.components.set(e._stateKey, {
@@ -1028,7 +1027,7 @@ function nt(e) {
1028
1027
  paths: /* @__PURE__ */ new Set([e._path.join(".")])
1029
1028
  }), () => u.components.delete(e._stateKey);
1030
1029
  },
1031
- () => r.getState().getNestedState(e._stateKey, e._path)
1030
+ () => n.getState().getNestedState(e._stateKey, e._path)
1032
1031
  );
1033
1032
  return ae("text", {}, String(i));
1034
1033
  }