cogsbox-state 0.5.45 → 0.5.47

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