cogsbox-state 0.5.42 → 0.5.44

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.
@@ -2,14 +2,14 @@
2
2
  import { jsx as it } from "react/jsx-runtime";
3
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
4
  import { transformStateFunc as _t, isFunction as ft, getNestedValue as L, isDeepEqual as G, debounce as pt } from "./utility.js";
5
- import { pushFunc as st, updateFn as q, cutFunc as ct, ValidationWrapper as wt, FormControlComponent as Vt } from "./Functions.jsx";
5
+ import { pushFunc as st, updateFn as q, cutFunc as ct, ValidationWrapper as wt, FormControlComponent as Nt } from "./Functions.jsx";
6
6
  import "zod";
7
7
  import { getGlobalStore as o, formRefStore as lt } from "./store.js";
8
- import { useCogsConfig as Nt } from "./CogsStateClient.jsx";
8
+ import { useCogsConfig as Vt } 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 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;
25
+ const u = et(t) || {}, d = f[t] || {}, _ = o.getState().setInitialStateOptions, w = { ...d, ...u };
26
+ let y = !1;
27
27
  if (a)
28
- for (const m in a)
29
- E.hasOwnProperty(m) || (w = !0, E[m] = a[m]);
30
- w && o.getState().setInitialStateOptions(t, E);
28
+ for (const S in a)
29
+ w.hasOwnProperty(S) || (y = !0, w[S] = a[S]);
30
+ y && _(t, w);
31
31
  }
32
- function Wt(t, { formElements: a, validation: u }) {
33
- return { initialState: t, formElements: a, validation: u };
32
+ function Wt(t, { formElements: a, validation: f }) {
33
+ return { initialState: t, formElements: a, validation: f };
34
34
  }
35
35
  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 = {
36
+ let f = t;
37
+ const [u, d] = _t(f);
38
+ (a?.formElements || a?.validation) && Object.keys(d).forEach((y) => {
39
+ d[y] = d[y] || {}, d[y].formElements = {
40
40
  ...a.formElements,
41
41
  // Global defaults first
42
42
  ...a?.validation,
43
- ...d[m].formElements || {}
43
+ ...d[y].formElements || {}
44
44
  // State-specific overrides
45
45
  };
46
- }), o.getState().setInitialStates(f);
47
- const E = (m, I) => {
48
- const [p] = Y(I?.componentId ?? tt());
46
+ }), o.getState().setInitialStates(u);
47
+ const _ = (y, S) => {
48
+ const [p] = Y(S?.componentId ?? tt());
49
49
  ut({
50
- stateKey: m,
51
- options: I
50
+ stateKey: y,
51
+ options: S,
52
+ initialOptionsPart: d
52
53
  });
53
- const s = o.getState().cogsStateStore[m] || f[m], e = I?.modifyState ? I.modifyState(s) : s;
54
- I?.log && console.log("useCogsState", m, I);
55
- const [v, N] = Ot(
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, V] = Ot(
56
57
  e,
57
58
  {
58
- stateKey: m,
59
- syncUpdate: I?.syncUpdate,
59
+ stateKey: y,
60
+ syncUpdate: S?.syncUpdate,
60
61
  componentId: p,
61
- localStorage: I?.localStorage,
62
- middleware: I?.middleware,
63
- enabledSync: I?.enabledSync,
64
- reactiveType: I?.reactiveType,
65
- reactiveDeps: I?.reactiveDeps,
66
- initState: I?.initState
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
- return N;
70
+ return V;
70
71
  };
71
- function w(m, I) {
72
- ut({ stateKey: m, options: I, initialOptionsPart: d });
72
+ function w(y, S) {
73
+ ut({ stateKey: y, options: S, initialOptionsPart: d });
73
74
  }
74
- return { useCogsState: E, setCogsOptions: w };
75
+ return { useCogsState: _, setCogsOptions: w };
75
76
  }, {
76
77
  setUpdaterState: B,
77
78
  setState: x,
@@ -80,9 +81,9 @@ const qt = (t, a) => {
80
81
  getValidationErrors: $t,
81
82
  setStateLog: ht,
82
83
  updateInitialStateGlobal: nt,
83
- addValidationError: At,
84
- removeValidationError: b,
85
- setServerSyncActions: Ct
84
+ addValidationError: Ct,
85
+ removeValidationError: M,
86
+ setServerSyncActions: At
86
87
  } = o.getState(), St = (t) => {
87
88
  if (!t) return null;
88
89
  try {
@@ -91,31 +92,31 @@ 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
+ }, Tt = (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
- window.localStorage.setItem(E, JSON.stringify(d));
107
+ }, _ = `${u}-${a}-${f.localStorage?.key}`;
108
+ window.localStorage.setItem(_, JSON.stringify(d));
108
109
  }
109
- }, Ft = (t, a, u, f, d, E) => {
110
+ }, Ft = (t, a, f, u, d, _) => {
110
111
  const w = {
111
112
  initialState: a,
112
113
  updaterState: Z(
113
114
  t,
114
- f,
115
+ u,
115
116
  d,
116
- E
117
+ _
117
118
  ),
118
- state: u
119
+ state: f
119
120
  };
120
121
  J(() => {
121
122
  nt(t, w.initialState), B(t, w.updaterState), x(t, w.state);
@@ -123,36 +124,36 @@ const qt = (t, a) => {
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
142
  function Ot(t, {
142
143
  stateKey: a,
143
- serverSync: u,
144
- localStorage: f,
144
+ serverSync: f,
145
+ localStorage: u,
145
146
  formElements: d,
146
- middleware: E,
147
+ middleware: _,
147
148
  reactiveDeps: w,
148
- reactiveType: m,
149
- componentId: I,
149
+ reactiveType: y,
150
+ componentId: S,
150
151
  initState: p,
151
152
  syncUpdate: s
152
153
  } = {}) {
153
- const [e, v] = Y({}), { sessionId: N } = Nt();
154
+ const [e, I] = Y({}), { sessionId: V } = Vt();
154
155
  let k = !a;
155
- const [c] = Y(a ?? tt()), R = o.getState().stateLog[c], P = z(/* @__PURE__ */ new Set()), i = z(I ?? tt()), $ = z(null);
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,108 +169,110 @@ function Ot(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: p,
175
+ localStorage: u
174
176
  });
177
+ $.current = n;
175
178
  let r = null;
176
- n.log && console.log("newoptions", n), n.localStorage?.key && N && (r = St(
177
- N + "-" + c + "-" + n.localStorage?.key
179
+ n.log && console.log("newoptions", n), n.localStorage?.key && V && (r = St(
180
+ V + "-" + c + "-" + n.localStorage?.key
178
181
  ));
179
182
  let l = null;
180
183
  p?.initialState && (l = p?.initialState, r && r.lastUpdated > (r.lastSyncedWithServer || 0) && (l = r.state), Ft(
181
184
  c,
182
185
  p?.initialState,
183
186
  l,
184
- U,
187
+ P,
185
188
  i.current,
186
- N
187
- ), mt(c), v({}));
188
- }, [...p?.dependencies || []]), vt(() => {
189
+ V
190
+ ), mt(c), I({}));
191
+ }, [u?.key, ...p?.dependencies || []]), vt(() => {
189
192
  k && dt(c, {
190
- serverSync: u,
193
+ serverSync: f,
191
194
  formElements: d,
192
195
  initState: p,
193
- localStorage: f,
194
- middleware: E
196
+ localStorage: u,
197
+ middleware: _
195
198
  });
196
199
  const n = `${c}////${i.current}`, r = o.getState().stateComponents.get(c) || {
197
200
  components: /* @__PURE__ */ new Map()
198
201
  };
199
202
  return r.components.set(n, {
200
- forceUpdate: () => v({}),
203
+ forceUpdate: () => I({}),
201
204
  paths: /* @__PURE__ */ new Set(),
202
205
  deps: [],
203
206
  depsFunction: w || void 0,
204
- reactiveType: m ?? ["component", "deps"]
205
- }), o.getState().stateComponents.set(c, r), v({}), () => {
207
+ reactiveType: y ?? ["component", "deps"]
208
+ }), o.getState().stateComponents.set(c, r), I({}), () => {
206
209
  const l = `${c}////${i.current}`;
207
210
  r && (r.components.delete(l), r.components.size === 0 && o.getState().stateComponents.delete(c));
208
211
  };
209
212
  }, []);
210
- const U = (n, r, l, g) => {
213
+ const P = (n, r, l, g) => {
211
214
  if (Array.isArray(r)) {
212
- const S = `${c}-${r.join(".")}`;
213
- P.current.add(S);
215
+ const m = `${c}-${r.join(".")}`;
216
+ U.current.add(m);
214
217
  }
215
- x(c, (S) => {
216
- const _ = ft(n) ? n(S) : n, O = `${c}-${r.join(".")}`;
218
+ x(c, (m) => {
219
+ const E = ft(n) ? n(m) : n, O = `${c}-${r.join(".")}`;
217
220
  if (O) {
218
- let M = !1, y = o.getState().signalDomElements.get(O);
219
- if ((!y || y.size === 0) && (l.updateType === "insert" || l.updateType === "cut")) {
220
- const h = r.slice(0, -1), C = L(_, h);
221
- if (Array.isArray(C)) {
222
- M = !0;
223
- const V = `${c}-${h.join(".")}`;
224
- y = o.getState().signalDomElements.get(V);
221
+ let b = !1, v = o.getState().signalDomElements.get(O);
222
+ if ((!v || v.size === 0) && (l.updateType === "insert" || l.updateType === "cut")) {
223
+ const h = r.slice(0, -1), A = L(E, h);
224
+ if (Array.isArray(A)) {
225
+ b = !0;
226
+ const N = `${c}-${h.join(".")}`;
227
+ v = o.getState().signalDomElements.get(N);
225
228
  }
226
229
  }
227
- if (y) {
228
- const h = M ? L(_, r.slice(0, -1)) : L(_, r);
229
- y.forEach(({ parentId: C, position: V, effect: D }) => {
230
+ if (v) {
231
+ const h = b ? L(E, r.slice(0, -1)) : L(E, r);
232
+ v.forEach(({ parentId: A, position: N, effect: D }) => {
230
233
  const T = document.querySelector(
231
- `[data-parent-id="${C}"]`
234
+ `[data-parent-id="${A}"]`
232
235
  );
233
236
  if (T) {
234
237
  const at = Array.from(T.childNodes);
235
- if (at[V]) {
238
+ if (at[N]) {
236
239
  const yt = D ? new Function("state", `return (${D})(state)`)(h) : h;
237
- at[V].textContent = String(yt);
240
+ at[N].textContent = String(yt);
238
241
  }
239
242
  }
240
243
  });
241
244
  }
242
245
  }
243
- l.updateType === "update" && (g || $.current?.validationKey) && r && b(
246
+ l.updateType === "update" && (g || $.current?.validationKey) && r && M(
244
247
  (g || $.current?.validationKey) + "." + r.join(".")
245
248
  );
246
- const A = r.slice(0, r.length - 1);
247
- l.updateType === "cut" && $.current?.validationKey && b(
248
- $.current?.validationKey + "." + A.join(".")
249
+ const C = r.slice(0, r.length - 1);
250
+ l.updateType === "cut" && $.current?.validationKey && M(
251
+ $.current?.validationKey + "." + C.join(".")
249
252
  ), l.updateType === "insert" && $.current?.validationKey && $t(
250
- $.current?.validationKey + "." + A.join(".")
251
- ).filter(([y, h]) => {
252
- let C = y?.split(".").length;
253
- if (y == A.join(".") && C == A.length - 1) {
254
- let V = y + "." + A;
255
- b(y), At(V, h);
253
+ $.current?.validationKey + "." + C.join(".")
254
+ ).filter(([v, h]) => {
255
+ let A = v?.split(".").length;
256
+ if (v == C.join(".") && A == C.length - 1) {
257
+ let N = v + "." + C;
258
+ M(v), Ct(N, h);
256
259
  }
257
260
  });
258
- const j = L(S, r), F = L(_, r), W = l.updateType === "update" ? r.join(".") : [...r].slice(0, -1).join("."), rt = o.getState().stateComponents.get(c);
261
+ const j = L(m, r), F = L(E, r), W = l.updateType === "update" ? r.join(".") : [...r].slice(0, -1).join("."), rt = o.getState().stateComponents.get(c);
259
262
  if (rt)
260
- for (const [M, y] of rt.components.entries()) {
263
+ for (const [b, v] of rt.components.entries()) {
261
264
  let h = !1;
262
- const C = Array.isArray(y.reactiveType) ? y.reactiveType : [y.reactiveType || "component"];
263
- if (!C.includes("none")) {
264
- if (C.includes("all")) {
265
- y.forceUpdate();
265
+ const A = Array.isArray(v.reactiveType) ? v.reactiveType : [v.reactiveType || "component"];
266
+ if (!A.includes("none")) {
267
+ if (A.includes("all")) {
268
+ v.forceUpdate();
266
269
  continue;
267
270
  }
268
- if (C.includes("component") && y.paths && (y.paths.has(W) || y.paths.has("")) && (h = !0), !h && C.includes("deps") && y.depsFunction) {
269
- const V = y.depsFunction(_);
270
- typeof V == "boolean" ? V && (h = !0) : G(y.deps, V) || (y.deps = V, h = !0);
271
+ if (A.includes("component") && v.paths && (v.paths.has(W) || v.paths.has("")) && (h = !0), !h && A.includes("deps") && v.depsFunction) {
272
+ const N = v.depsFunction(E);
273
+ typeof N == "boolean" ? N && (h = !0) : G(v.deps, N) || (v.deps = N, h = !0);
271
274
  }
272
- h && y.forceUpdate();
275
+ h && v.forceUpdate();
273
276
  }
274
277
  }
275
278
  const ot = {
@@ -281,95 +284,95 @@ function Ot(t, {
281
284
  oldValue: j,
282
285
  newValue: F
283
286
  };
284
- if (ht(c, (M) => {
285
- 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;
287
+ if (ht(c, (b) => {
288
+ const h = [...b ?? [], ot].reduce((A, N) => {
289
+ const D = `${N.stateKey}:${JSON.stringify(N.path)}`, T = A.get(D);
290
+ return T ? (T.timeStamp = Math.max(T.timeStamp, N.timeStamp), T.newValue = N.newValue, T.oldValue = T.oldValue ?? N.oldValue, T.updateType = N.updateType) : A.set(D, { ...N }), A;
288
291
  }, /* @__PURE__ */ new Map());
289
292
  return Array.from(h.values());
290
293
  }), Tt(
291
- _,
294
+ E,
292
295
  c,
293
296
  $.current,
294
- N
295
- ), E && E({
297
+ V
298
+ ), _ && _({
296
299
  updateLog: R,
297
300
  update: ot
298
301
  }), $.current?.serverSync) {
299
- const M = o.getState().serverState[c], y = $.current?.serverSync;
300
- Ct(c, {
301
- syncKey: typeof y.syncKey == "string" ? y.syncKey : y.syncKey({ state: _ }),
302
- rollBackState: M,
303
- actionTimeStamp: Date.now() + (y.debounce ?? 3e3),
302
+ const b = o.getState().serverState[c], v = $.current?.serverSync;
303
+ At(c, {
304
+ syncKey: typeof v.syncKey == "string" ? v.syncKey : v.syncKey({ state: E }),
305
+ rollBackState: b,
306
+ actionTimeStamp: Date.now() + (v.debounce ?? 3e3),
304
307
  status: "waiting"
305
308
  });
306
309
  }
307
- return _;
310
+ return E;
308
311
  });
309
312
  };
310
313
  o.getState().updaterState[c] || (B(
311
314
  c,
312
315
  Z(
313
316
  c,
314
- U,
317
+ P,
315
318
  i.current,
316
- N
319
+ V
317
320
  )
318
321
  ), o.getState().cogsStateStore[c] || x(c, t), o.getState().initialStateGlobal[c] || nt(c, t));
319
322
  const Q = It(() => Z(
320
323
  c,
321
- U,
324
+ P,
322
325
  i.current,
323
- N
326
+ V
324
327
  ), [c]);
325
328
  return [gt(c), Q];
326
329
  }
327
- function Z(t, a, u, f) {
330
+ function Z(t, a, f, u) {
328
331
  const d = /* @__PURE__ */ new Map();
329
- let E = 0;
332
+ let _ = 0;
330
333
  const w = (s) => {
331
334
  const e = s.join(".");
332
- for (const [v] of d)
333
- (v === e || v.startsWith(e + ".")) && d.delete(v);
334
- E++;
335
- }, m = /* @__PURE__ */ new Map(), I = {
335
+ for (const [I] of d)
336
+ (I === e || I.startsWith(e + ".")) && d.delete(I);
337
+ _++;
338
+ }, y = /* @__PURE__ */ new Map(), S = {
336
339
  removeValidation: (s) => {
337
- s?.validationKey && b(s.validationKey);
340
+ s?.validationKey && M(s.validationKey);
338
341
  },
339
342
  revertToInitialState: (s) => {
340
343
  const e = o.getState().getInitialOptions(t)?.validation;
341
- e?.key && b(e?.key), s?.validationKey && b(s.validationKey);
342
- const v = o.getState().initialStateGlobal[t];
343
- d.clear(), E++;
344
- const N = p(v, []);
344
+ e?.key && M(e?.key), s?.validationKey && M(s.validationKey);
345
+ const I = o.getState().initialStateGlobal[t];
346
+ d.clear(), _++;
347
+ const V = p(I, []);
345
348
  J(() => {
346
- B(t, N), x(t, v);
349
+ B(t, V), x(t, I);
347
350
  const k = o.getState().stateComponents.get(t);
348
351
  k && k.components.forEach((R) => {
349
352
  R.forceUpdate();
350
353
  });
351
354
  const c = et(t);
352
355
  c?.localStorage?.key && localStorage.removeItem(
353
- c?.initState ? f + "-" + t + "-" + c?.localStorage?.key : t
356
+ c?.initState ? u + "-" + t + "-" + c?.localStorage?.key : t
354
357
  ), localStorage.removeItem(t);
355
358
  });
356
359
  },
357
360
  updateInitialState: (s) => {
358
- d.clear(), E++;
361
+ d.clear(), _++;
359
362
  const e = Z(
360
363
  t,
361
364
  a,
362
- u,
363
- f
365
+ f,
366
+ u
364
367
  );
365
368
  return J(() => {
366
369
  nt(t, s), B(t, e), x(t, s);
367
- const v = o.getState().stateComponents.get(t);
368
- v && v.components.forEach((N) => {
369
- N.forceUpdate();
370
+ const I = o.getState().stateComponents.get(t);
371
+ I && I.components.forEach((V) => {
372
+ V.forceUpdate();
370
373
  }), localStorage.removeItem(t);
371
374
  }), {
372
- fetchId: (v) => e.get()[v]
375
+ fetchId: (I) => e.get()[I]
373
376
  };
374
377
  },
375
378
  _initialState: o.getState().initialStateGlobal[t],
@@ -380,22 +383,22 @@ function Z(t, a, u, f) {
380
383
  return !!(s && G(s, gt(t)));
381
384
  }
382
385
  };
383
- function p(s, e = [], v) {
384
- const N = e.map(String).join(".");
385
- d.get(N);
386
+ function p(s, e = [], I) {
387
+ const V = e.map(String).join(".");
388
+ d.get(V);
386
389
  const k = function() {
387
390
  return o().getNestedState(t, e);
388
391
  };
389
- Object.keys(I).forEach((P) => {
390
- k[P] = I[P];
392
+ Object.keys(S).forEach((U) => {
393
+ k[U] = S[U];
391
394
  });
392
395
  const c = {
393
- apply(P, i, $) {
396
+ apply(U, i, $) {
394
397
  return o().getNestedState(t, e);
395
398
  },
396
- get(P, i) {
399
+ get(U, i) {
397
400
  if (i !== "then" && !i.startsWith("$") && i !== "stateMapNoRender") {
398
- const n = e.join("."), r = `${t}////${u}`, l = o.getState().stateComponents.get(t);
401
+ const n = e.join("."), r = `${t}////${f}`, l = o.getState().stateComponents.get(t);
399
402
  if (l) {
400
403
  const g = l.components.get(r);
401
404
  g && (e.length > 0 || i === "get") && g.paths.add(n);
@@ -411,31 +414,31 @@ function Z(t, a, u, f) {
411
414
  if (Array.isArray(s)) {
412
415
  if (i === "getSelected")
413
416
  return () => {
414
- const n = m.get(e.join("."));
417
+ const n = y.get(e.join("."));
415
418
  if (n !== void 0)
416
419
  return p(
417
420
  s[n],
418
421
  [...e, n.toString()],
419
- v
422
+ I
420
423
  );
421
424
  };
422
425
  if (i === "stateMap" || i === "stateMapNoRender")
423
426
  return (n) => {
424
- const r = v?.filtered?.some(
427
+ const r = I?.filtered?.some(
425
428
  (g) => g.join(".") === e.join(".")
426
429
  ), 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, O = p(
430
+ return i !== "stateMapNoRender" && (d.clear(), _++), l.map((g, m) => {
431
+ const E = r && g.__origIndex ? g.__origIndex : m, O = p(
429
432
  g,
430
- [...e, _.toString()],
431
- v
433
+ [...e, E.toString()],
434
+ I
432
435
  );
433
436
  return n(
434
437
  g,
435
438
  O,
436
- S,
439
+ m,
437
440
  s,
438
- p(s, e, v)
441
+ p(s, e, I)
439
442
  );
440
443
  });
441
444
  };
@@ -451,27 +454,27 @@ function Z(t, a, u, f) {
451
454
  });
452
455
  if (i === "stateFlattenOn")
453
456
  return (n) => {
454
- const l = v?.filtered?.some(
455
- (S) => S.join(".") === e.join(".")
457
+ const l = I?.filtered?.some(
458
+ (m) => m.join(".") === e.join(".")
456
459
  ) ? s : o.getState().getNestedState(t, e);
457
- d.clear(), E++;
460
+ d.clear(), _++;
458
461
  const g = l.flatMap(
459
- (S, _) => S[n] ?? []
462
+ (m, E) => m[n] ?? []
460
463
  );
461
464
  return p(
462
465
  g,
463
466
  [...e, "[*]", n],
464
- v
467
+ I
465
468
  );
466
469
  };
467
470
  if (i === "findWith")
468
471
  return (n, r) => {
469
472
  const l = s.findIndex(
470
- (_) => _[n] === r
473
+ (E) => E[n] === r
471
474
  );
472
475
  if (l === -1) return;
473
- const g = s[l], S = [...e, l.toString()];
474
- return d.clear(), E++, d.clear(), E++, p(g, S);
476
+ const g = s[l], m = [...e, l.toString()];
477
+ return d.clear(), _++, d.clear(), _++, p(g, m);
475
478
  };
476
479
  if (i === "index")
477
480
  return (n) => {
@@ -485,22 +488,22 @@ function Z(t, a, u, f) {
485
488
  ));
486
489
  if (i === "uniqueInsert")
487
490
  return (n, r, l) => {
488
- const g = o.getState().getNestedState(t, e), S = ft(n) ? n(g) : n;
489
- let _ = null;
490
- if (!g.some((A) => {
491
+ const g = o.getState().getNestedState(t, e), m = ft(n) ? n(g) : n;
492
+ let E = null;
493
+ if (!g.some((C) => {
491
494
  if (r) {
492
495
  const F = r.every(
493
- (W) => G(A[W], S[W])
496
+ (W) => G(C[W], m[W])
494
497
  );
495
- return F && (_ = A), F;
498
+ return F && (E = C), F;
496
499
  }
497
- const j = G(A, S);
498
- return j && (_ = A), j;
500
+ const j = G(C, m);
501
+ return j && (E = C), j;
499
502
  }))
500
- w(e), st(a, S, e, t);
501
- else if (l && _) {
502
- const A = l(_), j = g.map(
503
- (F) => G(F, _) ? A : F
503
+ w(e), st(a, m, e, t);
504
+ else if (l && E) {
505
+ const C = l(E), j = g.map(
506
+ (F) => G(F, E) ? C : F
504
507
  );
505
508
  w(e), q(a, j, e);
506
509
  }
@@ -511,14 +514,14 @@ function Z(t, a, u, f) {
511
514
  };
512
515
  if (i === "stateFilter")
513
516
  return (n) => {
514
- const r = s.map((S, _) => ({
515
- ...S,
516
- __origIndex: _.toString()
517
+ const r = s.map((m, E) => ({
518
+ ...m,
519
+ __origIndex: E.toString()
517
520
  })), 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, {
521
- filtered: [...v?.filtered || [], e],
521
+ for (let m = 0; m < r.length; m++)
522
+ n(r[m], m) && (l.push(m), g.push(r[m]));
523
+ return d.clear(), _++, p(g, e, {
524
+ filtered: [...I?.filtered || [], e],
522
525
  validIndices: l
523
526
  // Always pass validIndices, even if empty
524
527
  });
@@ -560,16 +563,16 @@ function Z(t, a, u, f) {
560
563
  }
561
564
  if (i === "_selected") {
562
565
  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;
566
+ return Array.isArray(l) ? Number(e[e.length - 1]) === y.get(r) : void 0;
564
567
  }
565
568
  if (i == "getLocalStorage")
566
- return (n) => St(f + "-" + t + "-" + n);
569
+ return (n) => St(u + "-" + t + "-" + n);
567
570
  if (i === "setSelected")
568
571
  return (n) => {
569
572
  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);
573
+ n ? y.set(g, l) : y.delete(g);
574
+ const m = o.getState().getNestedState(t, [...r]);
575
+ q(a, m, r), w(r);
573
576
  };
574
577
  if (e.length == 0) {
575
578
  if (i === "validateZodSchema")
@@ -579,23 +582,23 @@ function Z(t, a, u, f) {
579
582
  throw new Error("Zod schema not found");
580
583
  if (!n?.key)
581
584
  throw new Error("Validation key not found");
582
- b(n.key);
585
+ M(n.key);
583
586
  const l = o.getState().cogsStateStore[t];
584
587
  try {
585
588
  const g = o.getState().getValidationErrors(n.key);
586
- g && g.length > 0 && g.forEach(([_]) => {
587
- _ && _.startsWith(n.key) && b(_);
589
+ g && g.length > 0 && g.forEach(([E]) => {
590
+ E && E.startsWith(n.key) && M(E);
588
591
  });
589
- const S = n.zodSchema.safeParse(l);
590
- return S.success ? !0 : (S.error.errors.forEach((O) => {
591
- const A = O.path, j = O.message, F = [n.key, ...A].join(".");
592
+ const m = n.zodSchema.safeParse(l);
593
+ return m.success ? !0 : (m.error.errors.forEach((O) => {
594
+ const C = O.path, j = O.message, F = [n.key, ...C].join(".");
592
595
  r(F, j);
593
596
  }), mt(t), !1);
594
597
  } catch (g) {
595
598
  return console.error("Zod schema validation failed", g), !1;
596
599
  }
597
600
  };
598
- if (i === "_componentId") return u;
601
+ if (i === "_componentId") return f;
599
602
  if (i === "getComponents")
600
603
  return () => o().stateComponents.get(t);
601
604
  if (i === "getAllFormRefs")
@@ -607,9 +610,9 @@ function Z(t, a, u, f) {
607
610
  if (i === "_isLoading")
608
611
  return o.getState().isLoadingGlobal[t];
609
612
  if (i === "revertToInitialState")
610
- return I.revertToInitialState;
611
- if (i === "updateInitialState") return I.updateInitialState;
612
- if (i === "removeValidation") return I.removeValidation;
613
+ return S.revertToInitialState;
614
+ if (i === "updateInitialState") return S.updateInitialState;
615
+ if (i === "removeValidation") return S.removeValidation;
613
616
  }
614
617
  if (i === "getFormRef")
615
618
  return () => lt.getState().getFormRef(t + "." + e.join("."));
@@ -624,13 +627,13 @@ function Z(t, a, u, f) {
624
627
  path: e,
625
628
  validationKey: o.getState().getInitialOptions(t)?.validation?.key || "",
626
629
  stateKey: t,
627
- validIndices: v?.validIndices,
630
+ validIndices: I?.validIndices,
628
631
  children: n
629
632
  }
630
633
  );
631
634
  if (i === "_stateKey") return t;
632
635
  if (i === "_path") return e;
633
- if (i === "_isServerSynced") return I._isServerSynced;
636
+ if (i === "_isServerSynced") return S._isServerSynced;
634
637
  if (i === "update")
635
638
  return (n, r) => {
636
639
  if (r?.debounce)
@@ -648,7 +651,7 @@ function Z(t, a, u, f) {
648
651
  };
649
652
  if (i === "formElement")
650
653
  return (n, r) => /* @__PURE__ */ it(
651
- Vt,
654
+ Nt,
652
655
  {
653
656
  setState: a,
654
657
  stateKey: t,
@@ -657,13 +660,13 @@ function Z(t, a, u, f) {
657
660
  formOpts: r
658
661
  }
659
662
  );
660
- const U = [...e, i], Q = o.getState().getNestedState(t, U);
661
- return p(Q, U, v);
663
+ const P = [...e, i], Q = o.getState().getNestedState(t, P);
664
+ return p(Q, P, I);
662
665
  }
663
666
  }, R = new Proxy(k, c);
664
- return d.set(N, {
667
+ return d.set(V, {
665
668
  proxy: R,
666
- stateVersion: E
669
+ stateVersion: _
667
670
  }), R;
668
671
  }
669
672
  return p(
@@ -671,37 +674,37 @@ function Z(t, a, u, f) {
671
674
  );
672
675
  }
673
676
  function X(t) {
674
- return H(Mt, { proxy: t });
677
+ return H(bt, { proxy: t });
675
678
  }
676
679
  function jt({
677
680
  proxy: t,
678
681
  rebuildStateShape: a
679
682
  }) {
680
- const u = o().getNestedState(t._stateKey, t._path);
681
- return Array.isArray(u) ? a(
682
- u,
683
+ const f = o().getNestedState(t._stateKey, t._path);
684
+ return Array.isArray(f) ? a(
685
+ f,
683
686
  t._path
684
687
  ).stateMapNoRender(
685
- (d, E, w, m, I) => t._mapFn(d, E, w, m, I)
688
+ (d, _, w, y, S) => t._mapFn(d, _, w, y, S)
686
689
  ) : null;
687
690
  }
688
- function Mt({
691
+ function bt({
689
692
  proxy: t
690
693
  }) {
691
- const a = z(null), u = `${t._stateKey}-${t._path.join(".")}`;
694
+ const a = z(null), f = `${t._stateKey}-${t._path.join(".")}`;
692
695
  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));
696
+ const u = a.current;
697
+ if (!u || !u.parentElement) return;
698
+ const d = u.parentElement, w = Array.from(d.childNodes).indexOf(u);
699
+ let y = d.getAttribute("data-parent-id");
700
+ y || (y = `parent-${crypto.randomUUID()}`, d.setAttribute("data-parent-id", y));
698
701
  const p = {
699
702
  instanceId: `instance-${crypto.randomUUID()}`,
700
- parentId: m,
703
+ parentId: y,
701
704
  position: w,
702
705
  effect: t._effect
703
706
  };
704
- o.getState().addSignalElement(u, p);
707
+ o.getState().addSignalElement(f, p);
705
708
  const s = o.getState().getNestedState(t._stateKey, t._path);
706
709
  let e;
707
710
  if (t._effect)
@@ -710,30 +713,30 @@ function Mt({
710
713
  "state",
711
714
  `return (${t._effect})(state)`
712
715
  )(s);
713
- } catch (N) {
714
- console.error("Error evaluating effect function during mount:", N), e = s;
716
+ } catch (V) {
717
+ console.error("Error evaluating effect function during mount:", V), e = s;
715
718
  }
716
719
  else
717
720
  e = s;
718
721
  e !== null && typeof e == "object" && (e = JSON.stringify(e));
719
- const v = document.createTextNode(String(e));
720
- f.replaceWith(v);
722
+ const I = document.createTextNode(String(e));
723
+ u.replaceWith(I);
721
724
  }, [t._stateKey, t._path.join("."), t._effect]), H("span", {
722
725
  ref: a,
723
726
  style: { display: "none" },
724
- "data-signal-id": u
727
+ "data-signal-id": f
725
728
  });
726
729
  }
727
730
  function Jt(t) {
728
731
  const a = Et(
729
- (u) => {
730
- const f = o.getState().stateComponents.get(t._stateKey) || {
732
+ (f) => {
733
+ const u = o.getState().stateComponents.get(t._stateKey) || {
731
734
  components: /* @__PURE__ */ new Map()
732
735
  };
733
- return f.components.set(t._stateKey, {
734
- forceUpdate: u,
736
+ return u.components.set(t._stateKey, {
737
+ forceUpdate: f,
735
738
  paths: /* @__PURE__ */ new Set([t._path.join(".")])
736
- }), () => f.components.delete(t._stateKey);
739
+ }), () => u.components.delete(t._stateKey);
737
740
  },
738
741
  () => o.getState().getNestedState(t._stateKey, t._path)
739
742
  );