cogsbox-state 0.5.41 → 0.5.43

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,15 +1,15 @@
1
1
  "use client";
2
2
  import { jsx as it } from "react/jsx-runtime";
3
- import { useState as Y, useRef as z, useEffect as K, useLayoutEffect as vt, useMemo as It, createElement as H, useSyncExternalStore as Et, startTransition as J } from "react";
4
- import { transformStateFunc as _t, isFunction as ft, getNestedValue as L, isDeepEqual as G, debounce as pt } from "./utility.js";
3
+ import { useState as Y, useRef as z, useEffect as K, useLayoutEffect as vt, useMemo as It, createElement as H, useSyncExternalStore as pt, startTransition as J } from "react";
4
+ import { transformStateFunc as Et, isFunction as ft, getNestedValue as L, isDeepEqual as G, debounce as _t } from "./utility.js";
5
5
  import { pushFunc as st, updateFn as q, cutFunc as ct, ValidationWrapper as wt, FormControlComponent as Vt } from "./Functions.jsx";
6
6
  import "zod";
7
7
  import { getGlobalStore as o, formRefStore as lt } from "./store.js";
8
8
  import { useCogsConfig as Nt } from "./CogsStateClient.jsx";
9
9
  import tt from "./node_modules/uuid/dist/esm-browser/v4.js";
10
10
  function dt(t, a) {
11
- const u = o.getState().getInitialOptions, f = o.getState().setInitialStateOptions, d = u(t) || {};
12
- return a.log && (console.log("setAndMergeOptions", t, a), console.log("setAndMergeOptions oldValue", d)), f(t, {
11
+ const f = o.getState().getInitialOptions, u = o.getState().setInitialStateOptions, d = f(t) || {};
12
+ return a.log && (console.log("setAndMergeOptions", t, a), console.log("setAndMergeOptions oldValue", d)), u(t, {
13
13
  ...d,
14
14
  ...a
15
15
  }), {
@@ -20,58 +20,59 @@ function dt(t, a) {
20
20
  function ut({
21
21
  stateKey: t,
22
22
  options: a,
23
- initialOptionsPart: u
23
+ initialOptionsPart: f
24
24
  }) {
25
- const f = et(t) || {}, E = { ...u?.[t] || {}, ...f };
26
- let w = !1;
27
- a && Object.keys(a).forEach((m) => {
28
- const y = a[m];
29
- E[m] !== y && (E[m] = y, w = !0);
30
- }), w && o.getState().setInitialStateOptions(t, E);
25
+ const u = et(t) || {}, d = f?.[t] || {}, E = o.getState().setInitialStateOptions, _ = { ...d, ...u };
26
+ (_.log || u.log) && (console.log("setOptions mergedOptions", _), console.log("setOptions initialOptions", u));
27
+ let y = !1;
28
+ if (a)
29
+ for (const S in a)
30
+ _.hasOwnProperty(S) || (y = !0, _[S] = a[S]);
31
+ y && E(t, _);
31
32
  }
32
- function Wt(t, { formElements: a, validation: u }) {
33
- return { initialState: t, formElements: a, validation: u };
33
+ function Wt(t, { formElements: a, validation: f }) {
34
+ return { initialState: t, formElements: a, validation: f };
34
35
  }
35
36
  const qt = (t, a) => {
36
- let u = t;
37
- const [f, d] = _t(u);
38
- (a?.formElements || a?.validation) && Object.keys(d).forEach((m) => {
39
- d[m] = d[m] || {}, d[m].formElements = {
37
+ let f = t;
38
+ const [u, d] = Et(f);
39
+ (a?.formElements || a?.validation) && Object.keys(d).forEach((y) => {
40
+ d[y] = d[y] || {}, d[y].formElements = {
40
41
  ...a.formElements,
41
42
  // Global defaults first
42
43
  ...a?.validation,
43
- ...d[m].formElements || {}
44
+ ...d[y].formElements || {}
44
45
  // State-specific overrides
45
46
  };
46
- }), o.getState().setInitialStates(f);
47
- const E = (m, y) => {
48
- const [p] = Y(y?.componentId ?? tt());
47
+ }), o.getState().setInitialStates(u);
48
+ const E = (y, S) => {
49
+ const [w] = Y(S?.componentId ?? tt());
49
50
  ut({
50
- stateKey: m,
51
- options: y
51
+ stateKey: y,
52
+ options: S
52
53
  });
53
- const s = o.getState().cogsStateStore[m] || f[m], e = y?.modifyState ? y.modifyState(s) : s;
54
- y?.log && console.log("useCogsState", m, y);
55
- const [I, N] = jt(
54
+ const s = o.getState().cogsStateStore[y] || u[y], e = S?.modifyState ? S.modifyState(s) : s;
55
+ S?.log && console.log("useCogsState", y, S);
56
+ const [I, N] = Ft(
56
57
  e,
57
58
  {
58
- stateKey: m,
59
- syncUpdate: y?.syncUpdate,
60
- componentId: p,
61
- localStorage: y?.localStorage,
62
- middleware: y?.middleware,
63
- enabledSync: y?.enabledSync,
64
- reactiveType: y?.reactiveType,
65
- reactiveDeps: y?.reactiveDeps,
66
- initState: y?.initState
59
+ stateKey: y,
60
+ syncUpdate: S?.syncUpdate,
61
+ componentId: w,
62
+ localStorage: S?.localStorage,
63
+ middleware: S?.middleware,
64
+ enabledSync: S?.enabledSync,
65
+ reactiveType: S?.reactiveType,
66
+ reactiveDeps: S?.reactiveDeps,
67
+ initState: S?.initState
67
68
  }
68
69
  );
69
70
  return N;
70
71
  };
71
- function w(m, y) {
72
- ut({ stateKey: m, options: y, initialOptionsPart: d });
72
+ function _(y, S) {
73
+ ut({ stateKey: y, options: S, initialOptionsPart: d });
73
74
  }
74
- return { useCogsState: E, setCogsOptions: w };
75
+ return { useCogsState: E, setCogsOptions: _ };
75
76
  }, {
76
77
  setUpdaterState: B,
77
78
  setState: x,
@@ -91,68 +92,68 @@ const qt = (t, a) => {
91
92
  } catch (a) {
92
93
  return console.error("Error loading from localStorage:", a), null;
93
94
  }
94
- }, Tt = (t, a, u, f) => {
95
- if (u.log && console.log(
95
+ }, Ot = (t, a, f, u) => {
96
+ if (f.log && console.log(
96
97
  "saving to localstorage",
97
98
  a,
98
- u.localStorage?.key,
99
- f
100
- ), u.localStorage?.key && f) {
99
+ f.localStorage?.key,
100
+ u
101
+ ), f.localStorage?.key && u) {
101
102
  const d = {
102
103
  state: t,
103
104
  lastUpdated: Date.now(),
104
105
  lastSyncedWithServer: o.getState().serverSyncLog[a]?.[0]?.timeStamp,
105
106
  baseServerState: o.getState().serverState[a]
106
- }, E = `${f}-${a}-${u.localStorage?.key}`;
107
+ }, E = `${u}-${a}-${f.localStorage?.key}`;
107
108
  window.localStorage.setItem(E, JSON.stringify(d));
108
109
  }
109
- }, Ft = (t, a, u, f, d, E) => {
110
- const w = {
110
+ }, Tt = (t, a, f, u, d, E) => {
111
+ const _ = {
111
112
  initialState: a,
112
113
  updaterState: Z(
113
114
  t,
114
- f,
115
+ u,
115
116
  d,
116
117
  E
117
118
  ),
118
- state: u
119
+ state: f
119
120
  };
120
121
  J(() => {
121
- nt(t, w.initialState), B(t, w.updaterState), x(t, w.state);
122
+ nt(t, _.initialState), B(t, _.updaterState), x(t, _.state);
122
123
  });
123
124
  }, mt = (t) => {
124
125
  const a = o.getState().stateComponents.get(t);
125
126
  if (!a) return;
126
- const u = /* @__PURE__ */ new Set();
127
- a.components.forEach((f) => {
128
- u.add(() => f.forceUpdate());
127
+ const f = /* @__PURE__ */ new Set();
128
+ a.components.forEach((u) => {
129
+ f.add(() => u.forceUpdate());
129
130
  }), queueMicrotask(() => {
130
131
  J(() => {
131
- u.forEach((f) => f());
132
+ f.forEach((u) => u());
132
133
  });
133
134
  });
134
135
  }, zt = (t, a) => {
135
- const u = o.getState().stateComponents.get(t);
136
- if (u) {
137
- const f = `${t}////${a}`, d = u.components.get(f);
136
+ const f = o.getState().stateComponents.get(t);
137
+ if (f) {
138
+ const u = `${t}////${a}`, d = f.components.get(u);
138
139
  d && d.forceUpdate();
139
140
  }
140
141
  };
141
- function jt(t, {
142
+ function Ft(t, {
142
143
  stateKey: a,
143
- serverSync: u,
144
- localStorage: f,
144
+ serverSync: f,
145
+ localStorage: u,
145
146
  formElements: d,
146
147
  middleware: E,
147
- reactiveDeps: w,
148
- reactiveType: m,
149
- componentId: y,
150
- initState: p,
148
+ reactiveDeps: _,
149
+ reactiveType: y,
150
+ componentId: S,
151
+ initState: w,
151
152
  syncUpdate: s
152
153
  } = {}) {
153
154
  const [e, I] = Y({}), { sessionId: N } = Nt();
154
- let R = !a;
155
- const [c] = Y(a ?? tt()), U = o.getState().stateLog[c], k = z(/* @__PURE__ */ new Set()), i = z(y ?? tt()), $ = z(null);
155
+ let k = !a;
156
+ const [c] = Y(a ?? tt()), R = o.getState().stateLog[c], U = z(/* @__PURE__ */ new Set()), i = z(S ?? tt()), $ = z(null);
156
157
  $.current = et(c), K(() => {
157
158
  if (s && s.stateKey === c && s.path?.[0]) {
158
159
  x(c, (r) => ({
@@ -168,29 +169,30 @@ function jt(t, {
168
169
  }, [s]), $.current.log && (console.log(
169
170
  "latestInitialOptionsRef.current ",
170
171
  $.current
171
- ), console.log("latestInitialOptionsRef.current localStorage", f)), K(() => {
172
+ ), console.log("latestInitialOptionsRef.current localStorage", u)), K(() => {
172
173
  const n = dt(c, {
173
- initState: p
174
+ initState: w
174
175
  });
176
+ $.current = n;
175
177
  let r = null;
176
178
  n.log && console.log("newoptions", n), n.localStorage?.key && N && (r = St(
177
179
  N + "-" + c + "-" + n.localStorage?.key
178
180
  ));
179
181
  let l = null;
180
- p?.initialState && (l = p?.initialState, r && r.lastUpdated > (r.lastSyncedWithServer || 0) && (l = r.state), Ft(
182
+ w?.initialState && (l = w?.initialState, r && r.lastUpdated > (r.lastSyncedWithServer || 0) && (l = r.state), Tt(
181
183
  c,
182
- p?.initialState,
184
+ w?.initialState,
183
185
  l,
184
186
  P,
185
187
  i.current,
186
188
  N
187
189
  ), mt(c), I({}));
188
- }, [...p?.dependencies || []]), vt(() => {
189
- R && dt(c, {
190
- serverSync: u,
190
+ }, [...w?.dependencies || []]), vt(() => {
191
+ k && dt(c, {
192
+ serverSync: f,
191
193
  formElements: d,
192
- initState: p,
193
- localStorage: f,
194
+ initState: w,
195
+ localStorage: u,
194
196
  middleware: E
195
197
  });
196
198
  const n = `${c}////${i.current}`, r = o.getState().stateComponents.get(c) || {
@@ -200,8 +202,8 @@ function jt(t, {
200
202
  forceUpdate: () => I({}),
201
203
  paths: /* @__PURE__ */ new Set(),
202
204
  deps: [],
203
- depsFunction: w || void 0,
204
- reactiveType: m ?? ["component", "deps"]
205
+ depsFunction: _ || void 0,
206
+ reactiveType: y ?? ["component", "deps"]
205
207
  }), o.getState().stateComponents.set(c, r), I({}), () => {
206
208
  const l = `${c}////${i.current}`;
207
209
  r && (r.components.delete(l), r.components.size === 0 && o.getState().stateComponents.delete(c));
@@ -209,15 +211,15 @@ function jt(t, {
209
211
  }, []);
210
212
  const P = (n, r, l, g) => {
211
213
  if (Array.isArray(r)) {
212
- const S = `${c}-${r.join(".")}`;
213
- k.current.add(S);
214
+ const m = `${c}-${r.join(".")}`;
215
+ U.current.add(m);
214
216
  }
215
- x(c, (S) => {
216
- const _ = ft(n) ? n(S) : n, j = `${c}-${r.join(".")}`;
217
- if (j) {
218
- let M = !1, v = o.getState().signalDomElements.get(j);
217
+ x(c, (m) => {
218
+ const p = ft(n) ? n(m) : n, F = `${c}-${r.join(".")}`;
219
+ if (F) {
220
+ let M = !1, v = o.getState().signalDomElements.get(F);
219
221
  if ((!v || v.size === 0) && (l.updateType === "insert" || l.updateType === "cut")) {
220
- const h = r.slice(0, -1), C = L(_, h);
222
+ const h = r.slice(0, -1), C = L(p, h);
221
223
  if (Array.isArray(C)) {
222
224
  M = !0;
223
225
  const V = `${c}-${h.join(".")}`;
@@ -225,13 +227,13 @@ function jt(t, {
225
227
  }
226
228
  }
227
229
  if (v) {
228
- const h = M ? L(_, r.slice(0, -1)) : L(_, r);
230
+ const h = M ? L(p, r.slice(0, -1)) : L(p, r);
229
231
  v.forEach(({ parentId: C, position: V, effect: D }) => {
230
- const T = document.querySelector(
232
+ const O = document.querySelector(
231
233
  `[data-parent-id="${C}"]`
232
234
  );
233
- if (T) {
234
- const at = Array.from(T.childNodes);
235
+ if (O) {
236
+ const at = Array.from(O.childNodes);
235
237
  if (at[V]) {
236
238
  const yt = D ? new Function("state", `return (${D})(state)`)(h) : h;
237
239
  at[V].textContent = String(yt);
@@ -255,7 +257,7 @@ function jt(t, {
255
257
  b(v), At(V, h);
256
258
  }
257
259
  });
258
- const O = L(S, r), F = L(_, r), W = l.updateType === "update" ? r.join(".") : [...r].slice(0, -1).join("."), rt = o.getState().stateComponents.get(c);
260
+ const j = L(m, r), T = L(p, r), W = l.updateType === "update" ? r.join(".") : [...r].slice(0, -1).join("."), rt = o.getState().stateComponents.get(c);
259
261
  if (rt)
260
262
  for (const [M, v] of rt.components.entries()) {
261
263
  let h = !1;
@@ -266,7 +268,7 @@ function jt(t, {
266
268
  continue;
267
269
  }
268
270
  if (C.includes("component") && v.paths && (v.paths.has(W) || v.paths.has("")) && (h = !0), !h && C.includes("deps") && v.depsFunction) {
269
- const V = v.depsFunction(_);
271
+ const V = v.depsFunction(p);
270
272
  typeof V == "boolean" ? V && (h = !0) : G(v.deps, V) || (v.deps = V, h = !0);
271
273
  }
272
274
  h && v.forceUpdate();
@@ -278,33 +280,33 @@ function jt(t, {
278
280
  path: r,
279
281
  updateType: l.updateType,
280
282
  status: "new",
281
- oldValue: O,
282
- newValue: F
283
+ oldValue: j,
284
+ newValue: T
283
285
  };
284
286
  if (ht(c, (M) => {
285
287
  const h = [...M ?? [], ot].reduce((C, V) => {
286
- const D = `${V.stateKey}:${JSON.stringify(V.path)}`, T = C.get(D);
287
- return T ? (T.timeStamp = Math.max(T.timeStamp, V.timeStamp), T.newValue = V.newValue, T.oldValue = T.oldValue ?? V.oldValue, T.updateType = V.updateType) : C.set(D, { ...V }), C;
288
+ const D = `${V.stateKey}:${JSON.stringify(V.path)}`, O = C.get(D);
289
+ return O ? (O.timeStamp = Math.max(O.timeStamp, V.timeStamp), O.newValue = V.newValue, O.oldValue = O.oldValue ?? V.oldValue, O.updateType = V.updateType) : C.set(D, { ...V }), C;
288
290
  }, /* @__PURE__ */ new Map());
289
291
  return Array.from(h.values());
290
- }), Tt(
291
- _,
292
+ }), Ot(
293
+ p,
292
294
  c,
293
295
  $.current,
294
296
  N
295
297
  ), E && E({
296
- updateLog: U,
298
+ updateLog: R,
297
299
  update: ot
298
300
  }), $.current?.serverSync) {
299
301
  const M = o.getState().serverState[c], v = $.current?.serverSync;
300
302
  Ct(c, {
301
- syncKey: typeof v.syncKey == "string" ? v.syncKey : v.syncKey({ state: _ }),
303
+ syncKey: typeof v.syncKey == "string" ? v.syncKey : v.syncKey({ state: p }),
302
304
  rollBackState: M,
303
305
  actionTimeStamp: Date.now() + (v.debounce ?? 3e3),
304
306
  status: "waiting"
305
307
  });
306
308
  }
307
- return _;
309
+ return p;
308
310
  });
309
311
  };
310
312
  o.getState().updaterState[c] || (B(
@@ -324,15 +326,15 @@ function jt(t, {
324
326
  ), [c]);
325
327
  return [gt(c), Q];
326
328
  }
327
- function Z(t, a, u, f) {
329
+ function Z(t, a, f, u) {
328
330
  const d = /* @__PURE__ */ new Map();
329
331
  let E = 0;
330
- const w = (s) => {
332
+ const _ = (s) => {
331
333
  const e = s.join(".");
332
334
  for (const [I] of d)
333
335
  (I === e || I.startsWith(e + ".")) && d.delete(I);
334
336
  E++;
335
- }, m = /* @__PURE__ */ new Map(), y = {
337
+ }, y = /* @__PURE__ */ new Map(), S = {
336
338
  removeValidation: (s) => {
337
339
  s?.validationKey && b(s.validationKey);
338
340
  },
@@ -341,16 +343,16 @@ function Z(t, a, u, f) {
341
343
  e?.key && b(e?.key), s?.validationKey && b(s.validationKey);
342
344
  const I = o.getState().initialStateGlobal[t];
343
345
  d.clear(), E++;
344
- const N = p(I, []);
346
+ const N = w(I, []);
345
347
  J(() => {
346
348
  B(t, N), x(t, I);
347
- const R = o.getState().stateComponents.get(t);
348
- R && R.components.forEach((U) => {
349
- U.forceUpdate();
349
+ const k = o.getState().stateComponents.get(t);
350
+ k && k.components.forEach((R) => {
351
+ R.forceUpdate();
350
352
  });
351
353
  const c = et(t);
352
354
  c?.localStorage?.key && localStorage.removeItem(
353
- c?.initState ? f + "-" + t + "-" + c?.localStorage?.key : t
355
+ c?.initState ? u + "-" + t + "-" + c?.localStorage?.key : t
354
356
  ), localStorage.removeItem(t);
355
357
  });
356
358
  },
@@ -359,8 +361,8 @@ function Z(t, a, u, f) {
359
361
  const e = Z(
360
362
  t,
361
363
  a,
362
- u,
363
- f
364
+ f,
365
+ u
364
366
  );
365
367
  return J(() => {
366
368
  nt(t, s), B(t, e), x(t, s);
@@ -380,22 +382,22 @@ function Z(t, a, u, f) {
380
382
  return !!(s && G(s, gt(t)));
381
383
  }
382
384
  };
383
- function p(s, e = [], I) {
385
+ function w(s, e = [], I) {
384
386
  const N = e.map(String).join(".");
385
387
  d.get(N);
386
- const R = function() {
388
+ const k = function() {
387
389
  return o().getNestedState(t, e);
388
390
  };
389
- Object.keys(y).forEach((k) => {
390
- R[k] = y[k];
391
+ Object.keys(S).forEach((U) => {
392
+ k[U] = S[U];
391
393
  });
392
394
  const c = {
393
- apply(k, i, $) {
395
+ apply(U, i, $) {
394
396
  return o().getNestedState(t, e);
395
397
  },
396
- get(k, i) {
398
+ get(U, i) {
397
399
  if (i !== "then" && !i.startsWith("$") && i !== "stateMapNoRender") {
398
- const n = e.join("."), r = `${t}////${u}`, l = o.getState().stateComponents.get(t);
400
+ const n = e.join("."), r = `${t}////${f}`, l = o.getState().stateComponents.get(t);
399
401
  if (l) {
400
402
  const g = l.components.get(r);
401
403
  g && (e.length > 0 || i === "get") && g.paths.add(n);
@@ -411,9 +413,9 @@ function Z(t, a, u, f) {
411
413
  if (Array.isArray(s)) {
412
414
  if (i === "getSelected")
413
415
  return () => {
414
- const n = m.get(e.join("."));
416
+ const n = y.get(e.join("."));
415
417
  if (n !== void 0)
416
- return p(
418
+ return w(
417
419
  s[n],
418
420
  [...e, n.toString()],
419
421
  I
@@ -424,41 +426,41 @@ function Z(t, a, u, f) {
424
426
  const r = I?.filtered?.some(
425
427
  (g) => g.join(".") === e.join(".")
426
428
  ), l = r ? s : o.getState().getNestedState(t, e);
427
- return i !== "stateMapNoRender" && (d.clear(), E++), l.map((g, S) => {
428
- const _ = r && g.__origIndex ? g.__origIndex : S, j = p(
429
+ return i !== "stateMapNoRender" && (d.clear(), E++), l.map((g, m) => {
430
+ const p = r && g.__origIndex ? g.__origIndex : m, F = w(
429
431
  g,
430
- [...e, _.toString()],
432
+ [...e, p.toString()],
431
433
  I
432
434
  );
433
435
  return n(
434
436
  g,
435
- j,
436
- S,
437
+ F,
438
+ m,
437
439
  s,
438
- p(s, e, I)
440
+ w(s, e, I)
439
441
  );
440
442
  });
441
443
  };
442
444
  if (i === "$stateMap")
443
- return (n) => H(Ot, {
445
+ return (n) => H(jt, {
444
446
  proxy: {
445
447
  _stateKey: t,
446
448
  _path: e,
447
449
  _mapFn: n
448
450
  // Pass the actual function, not string
449
451
  },
450
- rebuildStateShape: p
452
+ rebuildStateShape: w
451
453
  });
452
454
  if (i === "stateFlattenOn")
453
455
  return (n) => {
454
456
  const l = I?.filtered?.some(
455
- (S) => S.join(".") === e.join(".")
457
+ (m) => m.join(".") === e.join(".")
456
458
  ) ? s : o.getState().getNestedState(t, e);
457
459
  d.clear(), E++;
458
460
  const g = l.flatMap(
459
- (S, _) => S[n] ?? []
461
+ (m, p) => m[n] ?? []
460
462
  );
461
- return p(
463
+ return w(
462
464
  g,
463
465
  [...e, "[*]", n],
464
466
  I
@@ -467,57 +469,57 @@ function Z(t, a, u, f) {
467
469
  if (i === "findWith")
468
470
  return (n, r) => {
469
471
  const l = s.findIndex(
470
- (_) => _[n] === r
472
+ (p) => p[n] === r
471
473
  );
472
474
  if (l === -1) return;
473
- const g = s[l], S = [...e, l.toString()];
474
- return d.clear(), E++, d.clear(), E++, p(g, S);
475
+ const g = s[l], m = [...e, l.toString()];
476
+ return d.clear(), E++, d.clear(), E++, w(g, m);
475
477
  };
476
478
  if (i === "index")
477
479
  return (n) => {
478
480
  const r = s[n];
479
- return p(r, [...e, n.toString()]);
481
+ return w(r, [...e, n.toString()]);
480
482
  };
481
483
  if (i === "insert")
482
- return (n) => (w(e), st(a, n, e, t), p(
484
+ return (n) => (_(e), st(a, n, e, t), w(
483
485
  o.getState().cogsStateStore[t],
484
486
  []
485
487
  ));
486
488
  if (i === "uniqueInsert")
487
489
  return (n, r, l) => {
488
- const g = o.getState().getNestedState(t, e), S = ft(n) ? n(g) : n;
489
- let _ = null;
490
+ const g = o.getState().getNestedState(t, e), m = ft(n) ? n(g) : n;
491
+ let p = null;
490
492
  if (!g.some((A) => {
491
493
  if (r) {
492
- const F = r.every(
493
- (W) => G(A[W], S[W])
494
+ const T = r.every(
495
+ (W) => G(A[W], m[W])
494
496
  );
495
- return F && (_ = A), F;
497
+ return T && (p = A), T;
496
498
  }
497
- const O = G(A, S);
498
- return O && (_ = A), O;
499
+ const j = G(A, m);
500
+ return j && (p = A), j;
499
501
  }))
500
- w(e), st(a, S, e, t);
501
- else if (l && _) {
502
- const A = l(_), O = g.map(
503
- (F) => G(F, _) ? A : F
502
+ _(e), st(a, m, e, t);
503
+ else if (l && p) {
504
+ const A = l(p), j = g.map(
505
+ (T) => G(T, p) ? A : T
504
506
  );
505
- w(e), q(a, O, e);
507
+ _(e), q(a, j, e);
506
508
  }
507
509
  };
508
510
  if (i === "cut")
509
511
  return (n, r) => {
510
- r?.waitForSync || (w(e), ct(a, e, t, n));
512
+ r?.waitForSync || (_(e), ct(a, e, t, n));
511
513
  };
512
514
  if (i === "stateFilter")
513
515
  return (n) => {
514
- const r = s.map((S, _) => ({
515
- ...S,
516
- __origIndex: _.toString()
516
+ const r = s.map((m, p) => ({
517
+ ...m,
518
+ __origIndex: p.toString()
517
519
  })), l = [], g = [];
518
- for (let S = 0; S < r.length; S++)
519
- n(r[S], S) && (l.push(S), g.push(r[S]));
520
- return d.clear(), E++, p(g, e, {
520
+ for (let m = 0; m < r.length; m++)
521
+ n(r[m], m) && (l.push(m), g.push(r[m]));
522
+ return d.clear(), E++, w(g, e, {
521
523
  filtered: [...I?.filtered || [], e],
522
524
  validIndices: l
523
525
  // Always pass validIndices, even if empty
@@ -560,16 +562,16 @@ function Z(t, a, u, f) {
560
562
  }
561
563
  if (i === "_selected") {
562
564
  const n = e.slice(0, -1), r = n.join("."), l = o.getState().getNestedState(t, n);
563
- return Array.isArray(l) ? Number(e[e.length - 1]) === m.get(r) : void 0;
565
+ return Array.isArray(l) ? Number(e[e.length - 1]) === y.get(r) : void 0;
564
566
  }
565
567
  if (i == "getLocalStorage")
566
- return (n) => St(f + "-" + t + "-" + n);
568
+ return (n) => St(u + "-" + t + "-" + n);
567
569
  if (i === "setSelected")
568
570
  return (n) => {
569
571
  const r = e.slice(0, -1), l = Number(e[e.length - 1]), g = r.join(".");
570
- n ? m.set(g, l) : m.delete(g);
571
- const S = o.getState().getNestedState(t, [...r]);
572
- q(a, S, r), w(r);
572
+ n ? y.set(g, l) : y.delete(g);
573
+ const m = o.getState().getNestedState(t, [...r]);
574
+ q(a, m, r), _(r);
573
575
  };
574
576
  if (e.length == 0) {
575
577
  if (i === "validateZodSchema")
@@ -583,19 +585,19 @@ function Z(t, a, u, f) {
583
585
  const l = o.getState().cogsStateStore[t];
584
586
  try {
585
587
  const g = o.getState().getValidationErrors(n.key);
586
- g && g.length > 0 && g.forEach(([_]) => {
587
- _ && _.startsWith(n.key) && b(_);
588
+ g && g.length > 0 && g.forEach(([p]) => {
589
+ p && p.startsWith(n.key) && b(p);
588
590
  });
589
- const S = n.zodSchema.safeParse(l);
590
- return S.success ? !0 : (S.error.errors.forEach((j) => {
591
- const A = j.path, O = j.message, F = [n.key, ...A].join(".");
592
- r(F, O);
591
+ const m = n.zodSchema.safeParse(l);
592
+ return m.success ? !0 : (m.error.errors.forEach((F) => {
593
+ const A = F.path, j = F.message, T = [n.key, ...A].join(".");
594
+ r(T, j);
593
595
  }), mt(t), !1);
594
596
  } catch (g) {
595
597
  return console.error("Zod schema validation failed", g), !1;
596
598
  }
597
599
  };
598
- if (i === "_componentId") return u;
600
+ if (i === "_componentId") return f;
599
601
  if (i === "getComponents")
600
602
  return () => o().stateComponents.get(t);
601
603
  if (i === "getAllFormRefs")
@@ -607,9 +609,9 @@ function Z(t, a, u, f) {
607
609
  if (i === "_isLoading")
608
610
  return o.getState().isLoadingGlobal[t];
609
611
  if (i === "revertToInitialState")
610
- return y.revertToInitialState;
611
- if (i === "updateInitialState") return y.updateInitialState;
612
- if (i === "removeValidation") return y.removeValidation;
612
+ return S.revertToInitialState;
613
+ if (i === "updateInitialState") return S.updateInitialState;
614
+ if (i === "removeValidation") return S.removeValidation;
613
615
  }
614
616
  if (i === "getFormRef")
615
617
  return () => lt.getState().getFormRef(t + "." + e.join("."));
@@ -630,11 +632,11 @@ function Z(t, a, u, f) {
630
632
  );
631
633
  if (i === "_stateKey") return t;
632
634
  if (i === "_path") return e;
633
- if (i === "_isServerSynced") return y._isServerSynced;
635
+ if (i === "_isServerSynced") return S._isServerSynced;
634
636
  if (i === "update")
635
637
  return (n, r) => {
636
638
  if (r?.debounce)
637
- pt(() => {
639
+ _t(() => {
638
640
  q(a, n, e, "");
639
641
  const l = o.getState().getNestedState(t, e);
640
642
  r?.afterUpdate && r.afterUpdate(l);
@@ -644,7 +646,7 @@ function Z(t, a, u, f) {
644
646
  const l = o.getState().getNestedState(t, e);
645
647
  r?.afterUpdate && r.afterUpdate(l);
646
648
  }
647
- w(e);
649
+ _(e);
648
650
  };
649
651
  if (i === "formElement")
650
652
  return (n, r) => /* @__PURE__ */ it(
@@ -658,50 +660,50 @@ function Z(t, a, u, f) {
658
660
  }
659
661
  );
660
662
  const P = [...e, i], Q = o.getState().getNestedState(t, P);
661
- return p(Q, P, I);
663
+ return w(Q, P, I);
662
664
  }
663
- }, U = new Proxy(R, c);
665
+ }, R = new Proxy(k, c);
664
666
  return d.set(N, {
665
- proxy: U,
667
+ proxy: R,
666
668
  stateVersion: E
667
- }), U;
669
+ }), R;
668
670
  }
669
- return p(
671
+ return w(
670
672
  o.getState().getNestedState(t, [])
671
673
  );
672
674
  }
673
675
  function X(t) {
674
676
  return H(Mt, { proxy: t });
675
677
  }
676
- function Ot({
678
+ function jt({
677
679
  proxy: t,
678
680
  rebuildStateShape: a
679
681
  }) {
680
- const u = o().getNestedState(t._stateKey, t._path);
681
- return Array.isArray(u) ? a(
682
- u,
682
+ const f = o().getNestedState(t._stateKey, t._path);
683
+ return Array.isArray(f) ? a(
684
+ f,
683
685
  t._path
684
686
  ).stateMapNoRender(
685
- (d, E, w, m, y) => t._mapFn(d, E, w, m, y)
687
+ (d, E, _, y, S) => t._mapFn(d, E, _, y, S)
686
688
  ) : null;
687
689
  }
688
690
  function Mt({
689
691
  proxy: t
690
692
  }) {
691
- const a = z(null), u = `${t._stateKey}-${t._path.join(".")}`;
693
+ const a = z(null), f = `${t._stateKey}-${t._path.join(".")}`;
692
694
  return K(() => {
693
- const f = a.current;
694
- if (!f || !f.parentElement) return;
695
- const d = f.parentElement, w = Array.from(d.childNodes).indexOf(f);
696
- let m = d.getAttribute("data-parent-id");
697
- m || (m = `parent-${crypto.randomUUID()}`, d.setAttribute("data-parent-id", m));
698
- const p = {
695
+ const u = a.current;
696
+ if (!u || !u.parentElement) return;
697
+ const d = u.parentElement, _ = Array.from(d.childNodes).indexOf(u);
698
+ let y = d.getAttribute("data-parent-id");
699
+ y || (y = `parent-${crypto.randomUUID()}`, d.setAttribute("data-parent-id", y));
700
+ const w = {
699
701
  instanceId: `instance-${crypto.randomUUID()}`,
700
- parentId: m,
701
- position: w,
702
+ parentId: y,
703
+ position: _,
702
704
  effect: t._effect
703
705
  };
704
- o.getState().addSignalElement(u, p);
706
+ o.getState().addSignalElement(f, w);
705
707
  const s = o.getState().getNestedState(t._stateKey, t._path);
706
708
  let e;
707
709
  if (t._effect)
@@ -717,23 +719,23 @@ function Mt({
717
719
  e = s;
718
720
  e !== null && typeof e == "object" && (e = JSON.stringify(e));
719
721
  const I = document.createTextNode(String(e));
720
- f.replaceWith(I);
722
+ u.replaceWith(I);
721
723
  }, [t._stateKey, t._path.join("."), t._effect]), H("span", {
722
724
  ref: a,
723
725
  style: { display: "none" },
724
- "data-signal-id": u
726
+ "data-signal-id": f
725
727
  });
726
728
  }
727
729
  function Jt(t) {
728
- const a = Et(
729
- (u) => {
730
- const f = o.getState().stateComponents.get(t._stateKey) || {
730
+ const a = pt(
731
+ (f) => {
732
+ const u = o.getState().stateComponents.get(t._stateKey) || {
731
733
  components: /* @__PURE__ */ new Map()
732
734
  };
733
- return f.components.set(t._stateKey, {
734
- forceUpdate: u,
735
+ return u.components.set(t._stateKey, {
736
+ forceUpdate: f,
735
737
  paths: /* @__PURE__ */ new Set([t._path.join(".")])
736
- }), () => f.components.delete(t._stateKey);
738
+ }), () => u.components.delete(t._stateKey);
737
739
  },
738
740
  () => o.getState().getNestedState(t._stateKey, t._path)
739
741
  );
@@ -745,6 +747,6 @@ export {
745
747
  Wt as addStateOptions,
746
748
  qt as createCogsState,
747
749
  zt as notifyComponent,
748
- jt as useCogsStateFn
750
+ Ft as useCogsStateFn
749
751
  };
750
752
  //# sourceMappingURL=CogsState.jsx.map