cogsbox-state 0.5.39 → 0.5.41

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