cogsbox-state 0.5.225 → 0.5.227

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