cogsbox-state 0.5.46 → 0.5.48

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