cogsbox-state 0.5.220 → 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,45 +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) {
518
- if (a.paths.has(""))
519
- return;
520
- const o = n.join(".");
516
+ if (a && !a.pathsInitialized && (a.pathsInitialized = !0, !a.paths.has(""))) {
517
+ const o = r.join(".");
521
518
  let d = !0;
522
519
  for (const I of a.paths)
523
520
  if (o.startsWith(I) && (o === I || o[I.length] === ".")) {
524
521
  d = !1;
525
522
  break;
526
523
  }
527
- d && (console.log("adding path addimg", o, c), a.paths.add(o));
524
+ d && a.paths.add(o);
528
525
  }
529
526
  }
530
527
  }
531
528
  if (c === "getDifferences")
532
- return () => ge(
533
- r.getState().cogsStateStore[e],
534
- r.getState().initialStateGlobal[e]
529
+ return () => fe(
530
+ n.getState().cogsStateStore[e],
531
+ n.getState().initialStateGlobal[e]
535
532
  );
536
- if (c === "sync" && n.length === 0)
533
+ if (c === "sync" && r.length === 0)
537
534
  return async function() {
538
- const l = r.getState().getInitialOptions(e), t = l?.sync;
535
+ const l = n.getState().getInitialOptions(e), t = l?.sync;
539
536
  if (!t)
540
537
  return console.error(`No mutation defined for state key "${e}"`), { success: !1, error: "No mutation defined" };
541
- const a = r.getState().getNestedState(e, []), o = l?.validation?.key;
538
+ const a = n.getState().getNestedState(e, []), o = l?.validation?.key;
542
539
  try {
543
540
  const d = await t.action(a);
544
541
  if (d && !d.success && d.errors && o) {
545
- r.getState().removeValidationError(o), d.errors.forEach((g) => {
546
- const N = [o, ...g.path].join(".");
547
- 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);
548
545
  });
549
- const I = r.getState().stateComponents.get(e);
550
- I && I.components.forEach((g) => {
551
- g.forceUpdate();
546
+ const I = n.getState().stateComponents.get(e);
547
+ I && I.components.forEach((f) => {
548
+ f.forceUpdate();
552
549
  });
553
550
  }
554
551
  return d?.success && t.onSuccess ? t.onSuccess(d.data) : !d?.success && t.onError && t.onError(d.error), d;
@@ -557,95 +554,95 @@ function re(e, i, v, u) {
557
554
  }
558
555
  };
559
556
  if (c === "_status") {
560
- 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);
561
558
  return W(l, a) ? "fresh" : "stale";
562
559
  }
563
560
  if (c === "getStatus")
564
561
  return function() {
565
- const l = r().getNestedState(
562
+ const l = n().getNestedState(
566
563
  e,
567
- n
568
- ), t = r.getState().initialStateGlobal[e], a = G(t, n);
564
+ r
565
+ ), t = n.getState().initialStateGlobal[e], a = G(t, r);
569
566
  return W(l, a) ? "fresh" : "stale";
570
567
  };
571
568
  if (c === "removeStorage")
572
569
  return () => {
573
- 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}`;
574
571
  o && localStorage.removeItem(o);
575
572
  };
576
573
  if (c === "showValidationErrors")
577
574
  return () => {
578
- const l = r.getState().getInitialOptions(e)?.validation;
575
+ const l = n.getState().getInitialOptions(e)?.validation;
579
576
  if (!l?.key)
580
577
  throw new Error("Validation key not found");
581
- return r.getState().getValidationErrors(l.key + "." + n.join("."));
578
+ return n.getState().getValidationErrors(l.key + "." + r.join("."));
582
579
  };
583
580
  if (Array.isArray(m)) {
584
581
  const l = () => y?.validIndices ? m.map((a, o) => ({
585
582
  item: a,
586
583
  originalIndex: y.validIndices[o]
587
- })) : r.getState().getNestedState(e, n).map((a, o) => ({
584
+ })) : n.getState().getNestedState(e, r).map((a, o) => ({
588
585
  item: a,
589
586
  originalIndex: o
590
587
  }));
591
588
  if (c === "getSelected")
592
589
  return () => {
593
- const t = r.getState().getSelectedIndex(e, n.join("."));
590
+ const t = n.getState().getSelectedIndex(e, r.join("."));
594
591
  if (t !== void 0)
595
592
  return s(
596
593
  m[t],
597
- [...n, t.toString()],
594
+ [...r, t.toString()],
598
595
  y
599
596
  );
600
597
  };
601
598
  if (c === "clearSelected")
602
599
  return () => {
603
- r.getState().clearSelectedIndex({ stateKey: e, path: n });
600
+ n.getState().clearSelectedIndex({ stateKey: e, path: r });
604
601
  };
605
602
  if (c === "getSelectedIndex")
606
- return () => r.getState().getSelectedIndex(e, n.join(".")) ?? -1;
603
+ return () => n.getState().getSelectedIndex(e, r.join(".")) ?? -1;
607
604
  if (c === "stateSort")
608
605
  return (t) => {
609
606
  const o = [...l()].sort(
610
- (g, N) => t(g.item, N.item)
611
- ), d = o.map(({ item: g }) => g), I = {
607
+ (f, N) => t(f.item, N.item)
608
+ ), d = o.map(({ item: f }) => f), I = {
612
609
  ...y,
613
610
  validIndices: o.map(
614
- ({ originalIndex: g }) => g
611
+ ({ originalIndex: f }) => f
615
612
  )
616
613
  };
617
- return s(d, n, I);
614
+ return s(d, r, I);
618
615
  };
619
616
  if (c === "stateFilter")
620
617
  return (t) => {
621
618
  const o = l().filter(
622
- ({ item: g }, N) => t(g, N)
623
- ), d = o.map(({ item: g }) => g), I = {
619
+ ({ item: f }, N) => t(f, N)
620
+ ), d = o.map(({ item: f }) => f), I = {
624
621
  ...y,
625
622
  validIndices: o.map(
626
- ({ originalIndex: g }) => g
623
+ ({ originalIndex: f }) => f
627
624
  )
628
625
  };
629
- return s(d, n, I);
626
+ return s(d, r, I);
630
627
  };
631
628
  if (c === "stateMap" || c === "stateMapNoRender")
632
629
  return (t) => m.map((o, d) => {
633
630
  let I;
634
631
  y?.validIndices && y.validIndices[d] !== void 0 ? I = y.validIndices[d] : I = d;
635
- const g = [...n, I.toString()], N = s(o, g, y);
632
+ const f = [...r, I.toString()], N = s(o, f, y);
636
633
  return t(
637
634
  o,
638
635
  N,
639
636
  d,
640
637
  m,
641
- s(m, n, y)
638
+ s(m, r, y)
642
639
  );
643
640
  });
644
641
  if (c === "$stateMap")
645
642
  return (t) => ae(We, {
646
643
  proxy: {
647
644
  _stateKey: e,
648
- _path: n,
645
+ _path: r,
649
646
  _mapFn: t
650
647
  // Pass the actual function, not string
651
648
  },
@@ -660,89 +657,89 @@ function re(e, i, v, u) {
660
657
  );
661
658
  return s(
662
659
  o,
663
- [...n, "[*]", t],
660
+ [...r, "[*]", t],
664
661
  y
665
662
  );
666
663
  };
667
664
  if (c === "index")
668
665
  return (t) => {
669
666
  const a = m[t];
670
- return s(a, [...n, t.toString()]);
667
+ return s(a, [...r, t.toString()]);
671
668
  };
672
669
  if (c === "last")
673
670
  return () => {
674
- const t = r.getState().getNestedState(e, n);
671
+ const t = n.getState().getNestedState(e, r);
675
672
  if (t.length === 0) return;
676
- 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()];
677
674
  return s(o, d);
678
675
  };
679
676
  if (c === "insert")
680
- return (t) => (p(n), ce(i, t, n, e), s(
681
- r.getState().getNestedState(e, n),
682
- n
677
+ return (t) => (p(r), ce(i, t, r, e), s(
678
+ n.getState().getNestedState(e, r),
679
+ r
683
680
  ));
684
681
  if (c === "uniqueInsert")
685
682
  return (t, a, o) => {
686
- const d = r.getState().getNestedState(e, n), I = J(t) ? t(d) : t;
687
- let g = null;
683
+ const d = n.getState().getNestedState(e, r), I = J(t) ? t(d) : t;
684
+ let f = null;
688
685
  if (!d.some((w) => {
689
686
  if (a) {
690
687
  const F = a.every(
691
688
  (b) => W(w[b], I[b])
692
689
  );
693
- return F && (g = w), F;
690
+ return F && (f = w), F;
694
691
  }
695
692
  const $ = W(w, I);
696
- return $ && (g = w), $;
693
+ return $ && (f = w), $;
697
694
  }))
698
- p(n), ce(i, I, n, e);
699
- else if (o && g) {
700
- const w = o(g), $ = d.map(
701
- (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
702
699
  );
703
- p(n), Q(i, $, n);
700
+ p(r), Q(i, $, r);
704
701
  }
705
702
  };
706
703
  if (c === "cut")
707
704
  return (t, a) => {
708
705
  if (!a?.waitForSync)
709
- return p(n), ee(i, n, e, t), s(
710
- r.getState().getNestedState(e, n),
711
- n
706
+ return p(r), ee(i, r, e, t), s(
707
+ n.getState().getNestedState(e, r),
708
+ r
712
709
  );
713
710
  };
714
711
  if (c === "cutByValue")
715
712
  return (t) => {
716
713
  for (let a = 0; a < m.length; a++)
717
- m[a] === t && ee(i, n, e, a);
714
+ m[a] === t && ee(i, r, e, a);
718
715
  };
719
716
  if (c === "toggleByValue")
720
717
  return (t) => {
721
718
  const a = m.findIndex((o) => o === t);
722
- 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);
723
720
  };
724
721
  if (c === "stateFind")
725
722
  return (t) => {
726
723
  const o = l().find(
727
- ({ item: I }, g) => t(I, g)
724
+ ({ item: I }, f) => t(I, f)
728
725
  );
729
726
  if (!o) return;
730
- const d = [...n, o.originalIndex.toString()];
727
+ const d = [...r, o.originalIndex.toString()];
731
728
  return s(o.item, d, y);
732
729
  };
733
730
  if (c === "findWith")
734
731
  return (t, a) => {
735
732
  const d = l().find(
736
- ({ item: g }) => g[t] === a
733
+ ({ item: f }) => f[t] === a
737
734
  );
738
735
  if (!d) return;
739
- const I = [...n, d.originalIndex.toString()];
736
+ const I = [...r, d.originalIndex.toString()];
740
737
  return s(d.item, I, y);
741
738
  };
742
739
  }
743
- const L = n[n.length - 1];
740
+ const L = r[r.length - 1];
744
741
  if (!isNaN(Number(L))) {
745
- 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);
746
743
  if (Array.isArray(t) && c === "cut")
747
744
  return () => ee(
748
745
  i,
@@ -752,67 +749,67 @@ function re(e, i, v, u) {
752
749
  );
753
750
  }
754
751
  if (c === "get")
755
- return () => r.getState().getNestedState(e, n);
752
+ return () => n.getState().getNestedState(e, r);
756
753
  if (c === "$derive")
757
754
  return (l) => le({
758
755
  _stateKey: e,
759
- _path: n,
756
+ _path: r,
760
757
  _effect: l.toString()
761
758
  });
762
759
  if (c === "$derive")
763
760
  return (l) => le({
764
761
  _stateKey: e,
765
- _path: n,
762
+ _path: r,
766
763
  _effect: l.toString()
767
764
  });
768
765
  if (c === "$get")
769
766
  return () => le({
770
767
  _stateKey: e,
771
- _path: n
768
+ _path: r
772
769
  });
773
770
  if (c === "lastSynced") {
774
- const l = `${e}:${n.join(".")}`;
775
- return r.getState().getSyncInfo(l);
771
+ const l = `${e}:${r.join(".")}`;
772
+ return n.getState().getSyncInfo(l);
776
773
  }
777
774
  if (c == "getLocalStorage")
778
775
  return (l) => oe(u + "-" + e + "-" + l);
779
776
  if (c === "_selected") {
780
- const l = n.slice(0, -1), t = l.join("."), a = r.getState().getNestedState(e, l);
781
- 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;
782
779
  }
783
780
  if (c === "setSelected")
784
781
  return (l) => {
785
- const t = n.slice(0, -1), a = Number(n[n.length - 1]), o = t.join(".");
786
- l ? r.getState().setSelectedIndex(e, o, a) : r.getState().setSelectedIndex(e, o, void 0);
787
- 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]);
788
785
  Q(i, d, t), p(t);
789
786
  };
790
787
  if (c === "toggleSelected")
791
788
  return () => {
792
- const l = n.slice(0, -1), t = Number(n[n.length - 1]), a = l.join("."), o = r.getState().getSelectedIndex(e, a);
793
- 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(
794
791
  e,
795
792
  a,
796
793
  o === t ? void 0 : t
797
794
  );
798
- const d = r.getState().getNestedState(e, [...l]);
795
+ const d = n.getState().getNestedState(e, [...l]);
799
796
  Q(i, d, l), p(l);
800
797
  };
801
- if (n.length == 0) {
798
+ if (r.length == 0) {
802
799
  if (c === "applyJsonPatch")
803
800
  return (l) => {
804
- const t = r.getState().cogsStateStore[e], o = Ce(t, l).newDocument;
801
+ const t = n.getState().cogsStateStore[e], o = Ce(t, l).newDocument;
805
802
  ke(
806
803
  e,
807
- r.getState().initialStateGlobal[e],
804
+ n.getState().initialStateGlobal[e],
808
805
  o,
809
806
  i,
810
807
  v,
811
808
  u
812
809
  );
813
- const d = r.getState().stateComponents.get(e);
810
+ const d = n.getState().stateComponents.get(e);
814
811
  if (d) {
815
- const I = ge(t, o), g = new Set(I);
812
+ const I = fe(t, o), f = new Set(I);
816
813
  for (const [
817
814
  N,
818
815
  w
@@ -825,7 +822,7 @@ function re(e, i, v, u) {
825
822
  continue;
826
823
  }
827
824
  if (F.includes("component") && (w.paths.has("") && ($ = !0), !$))
828
- for (const b of g) {
825
+ for (const b of f) {
829
826
  if (w.paths.has(b)) {
830
827
  $ = !0;
831
828
  break;
@@ -869,21 +866,21 @@ function re(e, i, v, u) {
869
866
  };
870
867
  if (c === "validateZodSchema")
871
868
  return () => {
872
- const l = r.getState().getInitialOptions(e)?.validation, t = r.getState().addValidationError;
869
+ const l = n.getState().getInitialOptions(e)?.validation, t = n.getState().addValidationError;
873
870
  if (!l?.zodSchema)
874
871
  throw new Error("Zod schema not found");
875
872
  if (!l?.key)
876
873
  throw new Error("Validation key not found");
877
874
  B(l.key);
878
- const a = r.getState().cogsStateStore[e];
875
+ const a = n.getState().cogsStateStore[e];
879
876
  try {
880
- const o = r.getState().getValidationErrors(l.key);
877
+ const o = n.getState().getValidationErrors(l.key);
881
878
  o && o.length > 0 && o.forEach(([I]) => {
882
879
  I && I.startsWith(l.key) && B(I);
883
880
  });
884
881
  const d = l.zodSchema.safeParse(a);
885
- return d.success ? !0 : (d.error.errors.forEach((g) => {
886
- 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(".");
887
884
  t($, w);
888
885
  }), ie(e), !1);
889
886
  } catch (o) {
@@ -892,22 +889,22 @@ function re(e, i, v, u) {
892
889
  };
893
890
  if (c === "_componentId") return v;
894
891
  if (c === "getComponents")
895
- return () => r().stateComponents.get(e);
892
+ return () => n().stateComponents.get(e);
896
893
  if (c === "getAllFormRefs")
897
894
  return () => ve.getState().getFormRefsByStateKey(e);
898
895
  if (c === "_initialState")
899
- return r.getState().initialStateGlobal[e];
896
+ return n.getState().initialStateGlobal[e];
900
897
  if (c === "_serverState")
901
- return r.getState().serverState[e];
898
+ return n.getState().serverState[e];
902
899
  if (c === "_isLoading")
903
- return r.getState().isLoadingGlobal[e];
900
+ return n.getState().isLoadingGlobal[e];
904
901
  if (c === "revertToInitialState")
905
902
  return h.revertToInitialState;
906
903
  if (c === "updateInitialState") return h.updateInitialState;
907
904
  if (c === "removeValidation") return h.removeValidation;
908
905
  }
909
906
  if (c === "getFormRef")
910
- return () => ve.getState().getFormRef(e + "." + n.join("."));
907
+ return () => ve.getState().getFormRef(e + "." + r.join("."));
911
908
  if (c === "validationWrapper")
912
909
  return ({
913
910
  children: l,
@@ -916,30 +913,30 @@ function re(e, i, v, u) {
916
913
  Pe,
917
914
  {
918
915
  formOpts: t ? { validation: { message: "" } } : void 0,
919
- path: n,
920
- validationKey: r.getState().getInitialOptions(e)?.validation?.key || "",
916
+ path: r,
917
+ validationKey: n.getState().getInitialOptions(e)?.validation?.key || "",
921
918
  stateKey: e,
922
919
  validIndices: y?.validIndices,
923
920
  children: l
924
921
  }
925
922
  );
926
923
  if (c === "_stateKey") return e;
927
- if (c === "_path") return n;
924
+ if (c === "_path") return r;
928
925
  if (c === "_isServerSynced") return h._isServerSynced;
929
926
  if (c === "update")
930
927
  return (l, t) => {
931
928
  if (t?.debounce)
932
929
  be(() => {
933
- Q(i, l, n, "");
934
- const a = r.getState().getNestedState(e, n);
930
+ Q(i, l, r, "");
931
+ const a = n.getState().getNestedState(e, r);
935
932
  t?.afterUpdate && t.afterUpdate(a);
936
933
  }, t.debounce);
937
934
  else {
938
- Q(i, l, n, "");
939
- const a = r.getState().getNestedState(e, n);
935
+ Q(i, l, r, "");
936
+ const a = n.getState().getNestedState(e, r);
940
937
  t?.afterUpdate && t.afterUpdate(a);
941
938
  }
942
- p(n);
939
+ p(r);
943
940
  };
944
941
  if (c === "formElement")
945
942
  return (l, t) => /* @__PURE__ */ ye(
@@ -947,12 +944,12 @@ function re(e, i, v, u) {
947
944
  {
948
945
  setState: i,
949
946
  stateKey: e,
950
- path: n,
947
+ path: r,
951
948
  child: l,
952
949
  formOpts: t
953
950
  }
954
951
  );
955
- const A = [...n, c], X = r.getState().getNestedState(e, A);
952
+ const A = [...r, c], X = n.getState().getNestedState(e, A);
956
953
  return s(X, A, y);
957
954
  }
958
955
  }, U = new Proxy(_, x);
@@ -962,7 +959,7 @@ function re(e, i, v, u) {
962
959
  }), U;
963
960
  }
964
961
  return s(
965
- r.getState().getNestedState(e, [])
962
+ n.getState().getNestedState(e, [])
966
963
  );
967
964
  }
968
965
  function le(e) {
@@ -972,7 +969,7 @@ function We({
972
969
  proxy: e,
973
970
  rebuildStateShape: i
974
971
  }) {
975
- const v = r().getNestedState(e._stateKey, e._path);
972
+ const v = n().getNestedState(e._stateKey, e._path);
976
973
  return Array.isArray(v) ? i(
977
974
  v,
978
975
  e._path
@@ -996,20 +993,20 @@ function Ge({
996
993
  position: p,
997
994
  effect: e._effect
998
995
  };
999
- r.getState().addSignalElement(v, m);
1000
- 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);
1001
998
  let y;
1002
999
  if (e._effect)
1003
1000
  try {
1004
1001
  y = new Function(
1005
1002
  "state",
1006
1003
  `return (${e._effect})(state)`
1007
- )(n);
1004
+ )(r);
1008
1005
  } catch (_) {
1009
- console.error("Error evaluating effect function during mount:", _), y = n;
1006
+ console.error("Error evaluating effect function during mount:", _), y = r;
1010
1007
  }
1011
1008
  else
1012
- y = n;
1009
+ y = r;
1013
1010
  y !== null && typeof y == "object" && (y = JSON.stringify(y));
1014
1011
  const j = document.createTextNode(String(y));
1015
1012
  u.replaceWith(j);
@@ -1022,7 +1019,7 @@ function Ge({
1022
1019
  function nt(e) {
1023
1020
  const i = Ve(
1024
1021
  (v) => {
1025
- const u = r.getState().stateComponents.get(e._stateKey) || {
1022
+ const u = n.getState().stateComponents.get(e._stateKey) || {
1026
1023
  components: /* @__PURE__ */ new Map()
1027
1024
  };
1028
1025
  return u.components.set(e._stateKey, {
@@ -1030,7 +1027,7 @@ function nt(e) {
1030
1027
  paths: /* @__PURE__ */ new Set([e._path.join(".")])
1031
1028
  }), () => u.components.delete(e._stateKey);
1032
1029
  },
1033
- () => r.getState().getNestedState(e._stateKey, e._path)
1030
+ () => n.getState().getNestedState(e._stateKey, e._path)
1034
1031
  );
1035
1032
  return ae("text", {}, String(i));
1036
1033
  }