cogsbox-state 0.5.85 → 0.5.87

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,52 +8,52 @@ import { getGlobalStore as r, formRefStore as lt } from "./store.js";
8
8
  import { useCogsConfig as At } from "./CogsStateClient.jsx";
9
9
  import rt from "./node_modules/uuid/dist/esm-browser/v4.js";
10
10
  function dt(t, i) {
11
- const S = r.getState().getInitialOptions, v = r.getState().setInitialStateOptions, d = S(t) || {};
12
- v(t, {
13
- ...d,
11
+ const m = r.getState().getInitialOptions, y = r.getState().setInitialStateOptions, u = m(t) || {};
12
+ y(t, {
13
+ ...u,
14
14
  ...i
15
15
  });
16
16
  }
17
17
  function ut({
18
18
  stateKey: t,
19
19
  options: i,
20
- initialOptionsPart: S
20
+ initialOptionsPart: m
21
21
  }) {
22
- const v = q(t) || {}, d = S[t] || {}, $ = r.getState().setInitialStateOptions, _ = { ...d, ...v };
23
- let y = !1;
22
+ const y = q(t) || {}, u = m[t] || {}, $ = r.getState().setInitialStateOptions, _ = { ...u, ...y };
23
+ let v = !1;
24
24
  if (i)
25
25
  for (const l in i)
26
- _.hasOwnProperty(l) ? l == "localStorage" && i[l] && _[l].key !== i[l]?.key && (y = !0, _[l] = i[l]) : (y = !0, _[l] = i[l]);
27
- console.log("existingOptions", y, i, S), y && $(t, _);
26
+ _.hasOwnProperty(l) ? l == "localStorage" && i[l] && _[l].key !== i[l]?.key && (v = !0, _[l] = i[l]) : (v = !0, _[l] = i[l]);
27
+ console.log("existingOptions-------", _), v && $(t, _);
28
28
  }
29
- function qt(t, { formElements: i, validation: S }) {
30
- return { initialState: t, formElements: i, validation: S };
29
+ function qt(t, { formElements: i, validation: m }) {
30
+ return { initialState: t, formElements: i, validation: m };
31
31
  }
32
32
  const zt = (t, i) => {
33
- let S = t;
34
- const [v, d] = $t(S);
35
- (Object.keys(d).length > 0 || i && Object.keys(i).length > 0) && Object.keys(d).forEach((y) => {
36
- d[y] = d[y] || {}, d[y].formElements = {
33
+ let m = t;
34
+ const [y, u] = $t(m);
35
+ (Object.keys(u).length > 0 || i && Object.keys(i).length > 0) && Object.keys(u).forEach((v) => {
36
+ u[v] = u[v] || {}, u[v].formElements = {
37
37
  ...i?.formElements,
38
38
  // Global defaults first
39
39
  ...i?.validation,
40
- ...d[y].formElements || {}
40
+ ...u[v].formElements || {}
41
41
  // State-specific overrides
42
42
  };
43
- const l = q(y);
44
- console.log("existingOptions", l, d[y]), l || r.getState().setInitialStateOptions(y, d[y]);
45
- }), r.getState().setInitialStates(v);
46
- const $ = (y, l) => {
43
+ const l = q(v);
44
+ console.log("existingOptions", l, u[v]), l || r.getState().setInitialStateOptions(v, u[v]);
45
+ }), r.getState().setInitialStates(y);
46
+ const $ = (v, l) => {
47
47
  const [f] = et(l?.componentId ?? rt());
48
48
  ut({
49
- stateKey: y,
49
+ stateKey: v,
50
50
  options: l,
51
- initialOptionsPart: d
51
+ initialOptionsPart: u
52
52
  });
53
- const e = r.getState().cogsStateStore[y] || v[y], m = l?.modifyState ? l.modifyState(e) : e, [C, A] = Ot(
54
- m,
53
+ const e = r.getState().cogsStateStore[v] || y[v], S = l?.modifyState ? l.modifyState(e) : e, [k, A] = Tt(
54
+ S,
55
55
  {
56
- stateKey: y,
56
+ stateKey: v,
57
57
  syncUpdate: l?.syncUpdate,
58
58
  componentId: f,
59
59
  localStorage: l?.localStorage,
@@ -67,8 +67,8 @@ const zt = (t, i) => {
67
67
  );
68
68
  return A;
69
69
  };
70
- function _(y, l) {
71
- ut({ stateKey: y, options: l, initialOptionsPart: d });
70
+ function _(v, l) {
71
+ ut({ stateKey: v, options: l, initialOptionsPart: u });
72
72
  }
73
73
  return { useCogsState: $, setCogsOptions: _ };
74
74
  }, {
@@ -76,12 +76,12 @@ const zt = (t, i) => {
76
76
  setState: D,
77
77
  getInitialOptions: q,
78
78
  getKeyState: gt,
79
- getValidationErrors: xt,
80
- setStateLog: ht,
79
+ getValidationErrors: pt,
80
+ setStateLog: xt,
81
81
  updateInitialStateGlobal: ot,
82
- addValidationError: pt,
82
+ addValidationError: ht,
83
83
  removeValidationError: P,
84
- setServerSyncActions: Ct
84
+ setServerSyncActions: kt
85
85
  } = r.getState(), ft = (t) => {
86
86
  if (!t) return null;
87
87
  try {
@@ -90,33 +90,33 @@ const zt = (t, i) => {
90
90
  } catch (i) {
91
91
  return console.error("Error loading from localStorage:", i), null;
92
92
  }
93
- }, Tt = (t, i, S, v) => {
94
- S?.log && console.log(
93
+ }, Ct = (t, i, m, y) => {
94
+ m?.log && console.log(
95
95
  "saving to localstorage",
96
96
  i,
97
- S.localStorage?.key,
98
- v
97
+ m.localStorage?.key,
98
+ y
99
99
  );
100
- const d = L(S?.localStorage?.key) ? S.localStorage?.key(t) : S?.localStorage?.key;
101
- if (d && v) {
100
+ const u = L(m?.localStorage?.key) ? m.localStorage?.key(t) : m?.localStorage?.key;
101
+ if (u && y) {
102
102
  const $ = {
103
103
  state: t,
104
104
  lastUpdated: Date.now(),
105
105
  lastSyncedWithServer: r.getState().serverSyncLog[i]?.[0]?.timeStamp,
106
106
  baseServerState: r.getState().serverState[i]
107
- }, _ = `${v}-${i}-${d}`;
107
+ }, _ = `${y}-${i}-${u}`;
108
108
  window.localStorage.setItem(_, JSON.stringify($));
109
109
  }
110
- }, kt = (t, i, S, v, d, $) => {
110
+ }, Ot = (t, i, m, y, u, $) => {
111
111
  const _ = {
112
112
  initialState: i,
113
113
  updaterState: X(
114
114
  t,
115
- v,
116
- d,
115
+ y,
116
+ u,
117
117
  $
118
118
  ),
119
- state: S
119
+ state: m
120
120
  };
121
121
  H(() => {
122
122
  ot(t, _.initialState), Y(t, _.updaterState), D(t, _.state);
@@ -124,38 +124,38 @@ const zt = (t, i) => {
124
124
  }, St = (t) => {
125
125
  const i = r.getState().stateComponents.get(t);
126
126
  if (!i) return;
127
- const S = /* @__PURE__ */ new Set();
128
- i.components.forEach((v) => {
129
- S.add(() => v.forceUpdate());
127
+ const m = /* @__PURE__ */ new Set();
128
+ i.components.forEach((y) => {
129
+ m.add(() => y.forceUpdate());
130
130
  }), queueMicrotask(() => {
131
131
  H(() => {
132
- S.forEach((v) => v());
132
+ m.forEach((y) => y());
133
133
  });
134
134
  });
135
135
  }, Bt = (t, i) => {
136
- const S = r.getState().stateComponents.get(t);
137
- if (S) {
138
- const v = `${t}////${i}`, d = S.components.get(v);
139
- d && d.forceUpdate();
136
+ const m = r.getState().stateComponents.get(t);
137
+ if (m) {
138
+ const y = `${t}////${i}`, u = m.components.get(y);
139
+ u && u.forceUpdate();
140
140
  }
141
141
  };
142
- function Ot(t, {
142
+ function Tt(t, {
143
143
  stateKey: i,
144
- serverSync: S,
145
- localStorage: v,
146
- formElements: d,
144
+ serverSync: m,
145
+ localStorage: y,
146
+ formElements: u,
147
147
  middleware: $,
148
148
  reactiveDeps: _,
149
- reactiveType: y,
149
+ reactiveType: v,
150
150
  componentId: l,
151
151
  initialState: f,
152
152
  syncUpdate: e,
153
- dependencies: m
153
+ dependencies: S
154
154
  } = {}) {
155
- const [C, A] = et({}), { sessionId: T } = At();
155
+ const [k, A] = et({}), { sessionId: C } = At();
156
156
  let R = !i;
157
- const [g] = et(i ?? rt()), s = r.getState().stateLog[g], G = Z(/* @__PURE__ */ new Set()), b = Z(l ?? rt()), h = Z(null);
158
- h.current = q(g), nt(() => {
157
+ const [g] = et(i ?? rt()), s = r.getState().stateLog[g], G = Z(/* @__PURE__ */ new Set()), b = Z(l ?? rt()), x = Z(null);
158
+ x.current = q(g), nt(() => {
159
159
  if (e && e.stateKey === g && e.path?.[0]) {
160
160
  D(g, (o) => ({
161
161
  ...o,
@@ -171,28 +171,27 @@ function Ot(t, {
171
171
  dt(g, {
172
172
  initialState: f
173
173
  });
174
- const c = h.current;
174
+ const c = x.current;
175
175
  let o = null;
176
- c?.log && console.log("newoptions", c);
177
- const u = L(c?.localStorage?.key) ? c?.localStorage?.key(f) : c?.localStorage?.key;
178
- u && T && (o = ft(
179
- T + "-" + g + "-" + u
176
+ const d = L(c?.localStorage?.key) ? c?.localStorage?.key(f) : c?.localStorage?.key;
177
+ c?.log && (console.log("newoptions", c), console.log("localkey", d), console.log("initialState", f)), d && C && (o = ft(
178
+ C + "-" + g + "-" + d
180
179
  ));
181
180
  let E = null;
182
- f && (E = f, o && o.lastUpdated > (o.lastSyncedWithServer || 0) && (E = o.state), kt(
181
+ f && (E = f, o && o.lastUpdated > (o.lastSyncedWithServer || 0) && (E = o.state), Ot(
183
182
  g,
184
183
  f,
185
184
  E,
186
185
  n,
187
186
  b.current,
188
- T
187
+ C
189
188
  ), St(g), A({}));
190
- }, [f, ...m || []]), It(() => {
189
+ }, [f, ...S || []]), It(() => {
191
190
  R && dt(g, {
192
- serverSync: S,
193
- formElements: d,
191
+ serverSync: m,
192
+ formElements: u,
194
193
  initialState: f,
195
- localStorage: v,
194
+ localStorage: y,
196
195
  middleware: $
197
196
  });
198
197
  const c = `${g}////${b.current}`, o = r.getState().stateComponents.get(g) || {
@@ -203,24 +202,24 @@ function Ot(t, {
203
202
  paths: /* @__PURE__ */ new Set(),
204
203
  deps: [],
205
204
  depsFunction: _ || void 0,
206
- reactiveType: y ?? ["component", "deps"]
205
+ reactiveType: v ?? ["component", "deps"]
207
206
  }), r.getState().stateComponents.set(g, o), A({}), () => {
208
- const u = `${g}////${b.current}`;
209
- o && (o.components.delete(u), o.components.size === 0 && r.getState().stateComponents.delete(g));
207
+ const d = `${g}////${b.current}`;
208
+ o && (o.components.delete(d), o.components.size === 0 && r.getState().stateComponents.delete(g));
210
209
  };
211
210
  }, []);
212
- const n = (c, o, u, E) => {
211
+ const n = (c, o, d, E) => {
213
212
  if (Array.isArray(o)) {
214
- const k = `${g}-${o.join(".")}`;
215
- G.current.add(k);
213
+ const O = `${g}-${o.join(".")}`;
214
+ G.current.add(O);
216
215
  }
217
- D(g, (k) => {
218
- const N = L(c) ? c(k) : c, j = `${g}-${o.join(".")}`;
216
+ D(g, (O) => {
217
+ const N = L(c) ? c(O) : c, j = `${g}-${o.join(".")}`;
219
218
  if (j) {
220
219
  let F = !1, I = r.getState().signalDomElements.get(j);
221
- if ((!I || I.size === 0) && (u.updateType === "insert" || u.updateType === "cut")) {
222
- const V = o.slice(0, -1), x = U(N, V);
223
- if (Array.isArray(x)) {
220
+ if ((!I || I.size === 0) && (d.updateType === "insert" || d.updateType === "cut")) {
221
+ const V = o.slice(0, -1), p = U(N, V);
222
+ if (Array.isArray(p)) {
224
223
  F = !0;
225
224
  const w = `${g}-${V.join(".")}`;
226
225
  I = r.getState().signalDomElements.get(w);
@@ -228,12 +227,12 @@ function Ot(t, {
228
227
  }
229
228
  if (I) {
230
229
  const V = F ? U(N, o.slice(0, -1)) : U(N, o);
231
- I.forEach(({ parentId: x, position: w, effect: W }) => {
232
- const O = document.querySelector(
233
- `[data-parent-id="${x}"]`
230
+ I.forEach(({ parentId: p, position: w, effect: W }) => {
231
+ const T = document.querySelector(
232
+ `[data-parent-id="${p}"]`
234
233
  );
235
- if (O) {
236
- const st = Array.from(O.childNodes);
234
+ if (T) {
235
+ const st = Array.from(T.childNodes);
237
236
  if (st[w]) {
238
237
  const vt = W ? new Function("state", `return (${W})(state)`)(V) : V;
239
238
  st[w].textContent = String(vt);
@@ -242,32 +241,32 @@ function Ot(t, {
242
241
  });
243
242
  }
244
243
  }
245
- u.updateType === "update" && (E || h.current?.validationKey) && o && P(
246
- (E || h.current?.validationKey) + "." + o.join(".")
244
+ d.updateType === "update" && (E || x.current?.validationKey) && o && P(
245
+ (E || x.current?.validationKey) + "." + o.join(".")
247
246
  );
248
- const p = o.slice(0, o.length - 1);
249
- u.updateType === "cut" && h.current?.validationKey && P(
250
- h.current?.validationKey + "." + p.join(".")
251
- ), u.updateType === "insert" && h.current?.validationKey && xt(
252
- h.current?.validationKey + "." + p.join(".")
247
+ const h = o.slice(0, o.length - 1);
248
+ d.updateType === "cut" && x.current?.validationKey && P(
249
+ x.current?.validationKey + "." + h.join(".")
250
+ ), d.updateType === "insert" && x.current?.validationKey && pt(
251
+ x.current?.validationKey + "." + h.join(".")
253
252
  ).filter(([I, V]) => {
254
- let x = I?.split(".").length;
255
- if (I == p.join(".") && x == p.length - 1) {
256
- let w = I + "." + p;
257
- P(I), pt(w, V);
253
+ let p = I?.split(".").length;
254
+ if (I == h.join(".") && p == h.length - 1) {
255
+ let w = I + "." + h;
256
+ P(I), ht(w, V);
258
257
  }
259
258
  });
260
- const z = U(k, o), mt = U(N, o), yt = u.updateType === "update" ? o.join(".") : [...o].slice(0, -1).join("."), at = r.getState().stateComponents.get(g);
259
+ const z = U(O, o), mt = U(N, o), yt = d.updateType === "update" ? o.join(".") : [...o].slice(0, -1).join("."), at = r.getState().stateComponents.get(g);
261
260
  if (at)
262
261
  for (const [F, I] of at.components.entries()) {
263
262
  let V = !1;
264
- const x = Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"];
265
- if (!x.includes("none")) {
266
- if (x.includes("all")) {
263
+ const p = Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"];
264
+ if (!p.includes("none")) {
265
+ if (p.includes("all")) {
267
266
  I.forceUpdate();
268
267
  continue;
269
268
  }
270
- if (x.includes("component") && I.paths && (I.paths.has(yt) || I.paths.has("")) && (V = !0), !V && x.includes("deps") && I.depsFunction) {
269
+ if (p.includes("component") && I.paths && (I.paths.has(yt) || I.paths.has("")) && (V = !0), !V && p.includes("deps") && I.depsFunction) {
271
270
  const w = I.depsFunction(N);
272
271
  typeof w == "boolean" ? w && (V = !0) : M(I.deps, w) || (I.deps = w, V = !0);
273
272
  }
@@ -278,28 +277,28 @@ function Ot(t, {
278
277
  timeStamp: Date.now(),
279
278
  stateKey: g,
280
279
  path: o,
281
- updateType: u.updateType,
280
+ updateType: d.updateType,
282
281
  status: "new",
283
282
  oldValue: z,
284
283
  newValue: mt
285
284
  };
286
- if (ht(g, (F) => {
287
- const V = [...F ?? [], it].reduce((x, w) => {
288
- const W = `${w.stateKey}:${JSON.stringify(w.path)}`, O = x.get(W);
289
- return O ? (O.timeStamp = Math.max(O.timeStamp, w.timeStamp), O.newValue = w.newValue, O.oldValue = O.oldValue ?? w.oldValue, O.updateType = w.updateType) : x.set(W, { ...w }), x;
285
+ if (xt(g, (F) => {
286
+ const V = [...F ?? [], it].reduce((p, w) => {
287
+ const W = `${w.stateKey}:${JSON.stringify(w.path)}`, T = p.get(W);
288
+ return T ? (T.timeStamp = Math.max(T.timeStamp, w.timeStamp), T.newValue = w.newValue, T.oldValue = T.oldValue ?? w.oldValue, T.updateType = w.updateType) : p.set(W, { ...w }), p;
290
289
  }, /* @__PURE__ */ new Map());
291
290
  return Array.from(V.values());
292
- }), Tt(
291
+ }), Ct(
293
292
  N,
294
293
  g,
295
- h.current,
296
- T
294
+ x.current,
295
+ C
297
296
  ), $ && $({
298
297
  updateLog: s,
299
298
  update: it
300
- }), h.current?.serverSync) {
301
- const F = r.getState().serverState[g], I = h.current?.serverSync;
302
- Ct(g, {
299
+ }), x.current?.serverSync) {
300
+ const F = r.getState().serverState[g], I = x.current?.serverSync;
301
+ kt(g, {
303
302
  syncKey: typeof I.syncKey == "string" ? I.syncKey : I.syncKey({ state: N }),
304
303
  rollBackState: F,
305
304
  actionTimeStamp: Date.now() + (I.debounce ?? 3e3),
@@ -315,59 +314,59 @@ function Ot(t, {
315
314
  g,
316
315
  n,
317
316
  b.current,
318
- T
317
+ C
319
318
  )
320
319
  ), r.getState().cogsStateStore[g] || D(g, t), r.getState().initialStateGlobal[g] || ot(g, t));
321
320
  const a = _t(() => X(
322
321
  g,
323
322
  n,
324
323
  b.current,
325
- T
324
+ C
326
325
  ), [g]);
327
326
  return [gt(g), a];
328
327
  }
329
- function X(t, i, S, v) {
330
- const d = /* @__PURE__ */ new Map();
328
+ function X(t, i, m, y) {
329
+ const u = /* @__PURE__ */ new Map();
331
330
  let $ = 0;
332
331
  const _ = (f) => {
333
332
  const e = f.join(".");
334
- for (const [m] of d)
335
- (m === e || m.startsWith(e + ".")) && d.delete(m);
333
+ for (const [S] of u)
334
+ (S === e || S.startsWith(e + ".")) && u.delete(S);
336
335
  $++;
337
- }, y = {
336
+ }, v = {
338
337
  removeValidation: (f) => {
339
338
  f?.validationKey && P(f.validationKey);
340
339
  },
341
340
  revertToInitialState: (f) => {
342
341
  const e = r.getState().getInitialOptions(t)?.validation;
343
342
  e?.key && P(e?.key), f?.validationKey && P(f.validationKey);
344
- const m = r.getState().initialStateGlobal[t];
345
- d.clear(), $++;
346
- const C = l(m, []), A = q(t), T = L(A?.localStorage?.key) ? A?.localStorage?.key(m) : A?.localStorage?.key, R = `${v}-${t}-${T}`;
343
+ const S = r.getState().initialStateGlobal[t];
344
+ u.clear(), $++;
345
+ const k = l(S, []), A = q(t), C = L(A?.localStorage?.key) ? A?.localStorage?.key(S) : A?.localStorage?.key, R = `${y}-${t}-${C}`;
347
346
  return R && localStorage.removeItem(R), H(() => {
348
- Y(t, C), D(t, m);
347
+ Y(t, k), D(t, S);
349
348
  const g = r.getState().stateComponents.get(t);
350
349
  g && g.components.forEach((s) => {
351
350
  s.forceUpdate();
352
351
  });
353
- }), m;
352
+ }), S;
354
353
  },
355
354
  updateInitialState: (f) => {
356
- d.clear(), $++;
355
+ u.clear(), $++;
357
356
  const e = X(
358
357
  t,
359
358
  i,
360
- S,
361
- v
359
+ m,
360
+ y
362
361
  );
363
362
  return H(() => {
364
363
  ot(t, f), Y(t, e), D(t, f);
365
- const m = r.getState().stateComponents.get(t);
366
- m && m.components.forEach((C) => {
367
- C.forceUpdate();
364
+ const S = r.getState().stateComponents.get(t);
365
+ S && S.components.forEach((k) => {
366
+ k.forceUpdate();
368
367
  });
369
368
  }), {
370
- fetchId: (m) => e.get()[m]
369
+ fetchId: (S) => e.get()[S]
371
370
  };
372
371
  },
373
372
  _initialState: r.getState().initialStateGlobal[t],
@@ -378,16 +377,16 @@ function X(t, i, S, v) {
378
377
  return !!(f && M(f, gt(t)));
379
378
  }
380
379
  };
381
- function l(f, e = [], m) {
382
- const C = e.map(String).join(".");
383
- d.get(C);
380
+ function l(f, e = [], S) {
381
+ const k = e.map(String).join(".");
382
+ u.get(k);
384
383
  const A = function() {
385
384
  return r().getNestedState(t, e);
386
385
  };
387
- Object.keys(y).forEach((g) => {
388
- A[g] = y[g];
386
+ Object.keys(v).forEach((g) => {
387
+ A[g] = v[g];
389
388
  });
390
- const T = {
389
+ const C = {
391
390
  apply(g, s, G) {
392
391
  return console.log(
393
392
  `PROXY APPLY TRAP HIT: stateKey=${t}, path=${e.join(".")}`
@@ -395,7 +394,7 @@ function X(t, i, S, v) {
395
394
  },
396
395
  get(g, s) {
397
396
  if (s !== "then" && !s.startsWith("$") && s !== "stateMapNoRender") {
398
- const n = e.join("."), a = `${t}////${S}`, c = r.getState().stateComponents.get(t);
397
+ const n = e.join("."), a = `${t}////${m}`, c = r.getState().stateComponents.get(t);
399
398
  if (c) {
400
399
  const o = c.components.get(a);
401
400
  o && (e.length > 0 || s === "get") && o.paths.add(n);
@@ -415,7 +414,7 @@ function X(t, i, S, v) {
415
414
  };
416
415
  if (s === "removeStorage")
417
416
  return () => {
418
- const n = r.getState().initialStateGlobal[t], a = q(t), c = L(a?.localStorage?.key) ? a?.localStorage?.key(n) : a?.localStorage?.key, o = `${v}-${t}-${c}`;
417
+ const n = r.getState().initialStateGlobal[t], a = q(t), c = L(a?.localStorage?.key) ? a?.localStorage?.key(n) : a?.localStorage?.key, o = `${y}-${t}-${c}`;
419
418
  console.log("removing storage", o), o && localStorage.removeItem(o);
420
419
  };
421
420
  if (s === "showValidationErrors")
@@ -433,41 +432,41 @@ function X(t, i, S, v) {
433
432
  return l(
434
433
  f[n],
435
434
  [...e, n.toString()],
436
- m
435
+ S
437
436
  );
438
437
  };
439
438
  if (s === "getSelectedIndex")
440
439
  return () => r.getState().getSelectedIndex(t, e.join(".")) ?? -1;
441
440
  if (s === "stateSort")
442
441
  return (n) => {
443
- const o = [...r.getState().getNestedState(t, e).map((u, E) => ({
444
- ...u,
442
+ const o = [...r.getState().getNestedState(t, e).map((d, E) => ({
443
+ ...d,
445
444
  __origIndex: E.toString()
446
445
  }))].sort(n);
447
- return d.clear(), $++, l(o, e, {
448
- filtered: [...m?.filtered || [], e],
446
+ return u.clear(), $++, l(o, e, {
447
+ filtered: [...S?.filtered || [], e],
449
448
  validIndices: o.map(
450
- (u) => parseInt(u.__origIndex)
449
+ (d) => parseInt(d.__origIndex)
451
450
  )
452
451
  });
453
452
  };
454
453
  if (s === "stateMap" || s === "stateMapNoRender")
455
454
  return (n) => {
456
- const a = m?.filtered?.some(
455
+ const a = S?.filtered?.some(
457
456
  (o) => o.join(".") === e.join(".")
458
457
  ), c = a ? f : r.getState().getNestedState(t, e);
459
- return s !== "stateMapNoRender" && (d.clear(), $++), c.map((o, u) => {
460
- const E = a && o.__origIndex ? o.__origIndex : u, k = l(
458
+ return s !== "stateMapNoRender" && (u.clear(), $++), c.map((o, d) => {
459
+ const E = a && o.__origIndex ? o.__origIndex : d, O = l(
461
460
  o,
462
461
  [...e, E.toString()],
463
- m
462
+ S
464
463
  );
465
464
  return n(
466
465
  o,
467
- k,
468
- u,
466
+ O,
467
+ d,
469
468
  f,
470
- l(f, e, m)
469
+ l(f, e, S)
471
470
  );
472
471
  });
473
472
  };
@@ -483,17 +482,17 @@ function X(t, i, S, v) {
483
482
  });
484
483
  if (s === "stateFlattenOn")
485
484
  return (n) => {
486
- const c = m?.filtered?.some(
487
- (u) => u.join(".") === e.join(".")
485
+ const c = S?.filtered?.some(
486
+ (d) => d.join(".") === e.join(".")
488
487
  ) ? f : r.getState().getNestedState(t, e);
489
- d.clear(), $++;
488
+ u.clear(), $++;
490
489
  const o = c.flatMap(
491
- (u, E) => u[n] ?? []
490
+ (d, E) => d[n] ?? []
492
491
  );
493
492
  return l(
494
493
  o,
495
494
  [...e, "[*]", n],
496
- m
495
+ S
497
496
  );
498
497
  };
499
498
  if (s === "findWith")
@@ -502,8 +501,8 @@ function X(t, i, S, v) {
502
501
  (E) => E[n] === a
503
502
  );
504
503
  if (c === -1) return;
505
- const o = f[c], u = [...e, c.toString()];
506
- return d.clear(), $++, d.clear(), $++, l(o, u);
504
+ const o = f[c], d = [...e, c.toString()];
505
+ return u.clear(), $++, u.clear(), $++, l(o, d);
507
506
  };
508
507
  if (s === "index")
509
508
  return (n) => {
@@ -517,22 +516,22 @@ function X(t, i, S, v) {
517
516
  ));
518
517
  if (s === "uniqueInsert")
519
518
  return (n, a, c) => {
520
- const o = r.getState().getNestedState(t, e), u = L(n) ? n(o) : n;
519
+ const o = r.getState().getNestedState(t, e), d = L(n) ? n(o) : n;
521
520
  let E = null;
522
521
  if (!o.some((N) => {
523
522
  if (a) {
524
- const p = a.every(
525
- (z) => M(N[z], u[z])
523
+ const h = a.every(
524
+ (z) => M(N[z], d[z])
526
525
  );
527
- return p && (E = N), p;
526
+ return h && (E = N), h;
528
527
  }
529
- const j = M(N, u);
528
+ const j = M(N, d);
530
529
  return j && (E = N), j;
531
530
  }))
532
- _(e), K(i, u, e, t);
531
+ _(e), K(i, d, e, t);
533
532
  else if (c && E) {
534
533
  const N = c(E), j = o.map(
535
- (p) => M(p, E) ? N : p
534
+ (h) => M(h, E) ? N : h
536
535
  );
537
536
  _(e), B(i, j, e);
538
537
  }
@@ -553,14 +552,14 @@ function X(t, i, S, v) {
553
552
  };
554
553
  if (s === "stateFilter")
555
554
  return (n) => {
556
- const a = f.map((u, E) => ({
557
- ...u,
555
+ const a = f.map((d, E) => ({
556
+ ...d,
558
557
  __origIndex: E.toString()
559
558
  })), c = [], o = [];
560
- for (let u = 0; u < a.length; u++)
561
- n(a[u], u) && (c.push(u), o.push(a[u]));
562
- return d.clear(), $++, l(o, e, {
563
- filtered: [...m?.filtered || [], e],
559
+ for (let d = 0; d < a.length; d++)
560
+ n(a[d], d) && (c.push(d), o.push(a[d]));
561
+ return u.clear(), $++, l(o, e, {
562
+ filtered: [...S?.filtered || [], e],
564
563
  validIndices: c
565
564
  // Always pass validIndices, even if empty
566
565
  });
@@ -601,7 +600,7 @@ function X(t, i, S, v) {
601
600
  return r.getState().getSyncInfo(n);
602
601
  }
603
602
  if (s == "getLocalStorage")
604
- return (n) => ft(v + "-" + t + "-" + n);
603
+ return (n) => ft(y + "-" + t + "-" + n);
605
604
  if (s === "_selected") {
606
605
  const n = e.slice(0, -1), a = n.join("."), c = r.getState().getNestedState(t, n);
607
606
  return Array.isArray(c) ? Number(e[e.length - 1]) === r.getState().getSelectedIndex(t, a) : void 0;
@@ -610,8 +609,8 @@ function X(t, i, S, v) {
610
609
  return (n) => {
611
610
  const a = e.slice(0, -1), c = Number(e[e.length - 1]), o = a.join(".");
612
611
  n ? r.getState().setSelectedIndex(t, o, c) : r.getState().setSelectedIndex(t, o, void 0);
613
- const u = r.getState().getNestedState(t, [...a]);
614
- B(i, u, a), _(a);
612
+ const d = r.getState().getNestedState(t, [...a]);
613
+ B(i, d, a), _(a);
615
614
  };
616
615
  if (e.length == 0) {
617
616
  if (s === "validateZodSchema")
@@ -628,16 +627,16 @@ function X(t, i, S, v) {
628
627
  o && o.length > 0 && o.forEach(([E]) => {
629
628
  E && E.startsWith(n.key) && P(E);
630
629
  });
631
- const u = n.zodSchema.safeParse(c);
632
- return u.success ? !0 : (u.error.errors.forEach((k) => {
633
- const N = k.path, j = k.message, p = [n.key, ...N].join(".");
634
- a(p, j);
630
+ const d = n.zodSchema.safeParse(c);
631
+ return d.success ? !0 : (d.error.errors.forEach((O) => {
632
+ const N = O.path, j = O.message, h = [n.key, ...N].join(".");
633
+ a(h, j);
635
634
  }), St(t), !1);
636
635
  } catch (o) {
637
636
  return console.error("Zod schema validation failed", o), !1;
638
637
  }
639
638
  };
640
- if (s === "_componentId") return S;
639
+ if (s === "_componentId") return m;
641
640
  if (s === "getComponents")
642
641
  return () => r().stateComponents.get(t);
643
642
  if (s === "getAllFormRefs")
@@ -649,9 +648,9 @@ function X(t, i, S, v) {
649
648
  if (s === "_isLoading")
650
649
  return r.getState().isLoadingGlobal[t];
651
650
  if (s === "revertToInitialState")
652
- return y.revertToInitialState;
653
- if (s === "updateInitialState") return y.updateInitialState;
654
- if (s === "removeValidation") return y.removeValidation;
651
+ return v.revertToInitialState;
652
+ if (s === "updateInitialState") return v.updateInitialState;
653
+ if (s === "removeValidation") return v.removeValidation;
655
654
  }
656
655
  if (s === "getFormRef")
657
656
  return () => lt.getState().getFormRef(t + "." + e.join("."));
@@ -666,13 +665,13 @@ function X(t, i, S, v) {
666
665
  path: e,
667
666
  validationKey: r.getState().getInitialOptions(t)?.validation?.key || "",
668
667
  stateKey: t,
669
- validIndices: m?.validIndices,
668
+ validIndices: S?.validIndices,
670
669
  children: n
671
670
  }
672
671
  );
673
672
  if (s === "_stateKey") return t;
674
673
  if (s === "_path") return e;
675
- if (s === "_isServerSynced") return y._isServerSynced;
674
+ if (s === "_isServerSynced") return v._isServerSynced;
676
675
  if (s === "update")
677
676
  return (n, a) => {
678
677
  if (a?.debounce)
@@ -699,11 +698,11 @@ function X(t, i, S, v) {
699
698
  formOpts: a
700
699
  }
701
700
  );
702
- const b = [...e, s], h = r.getState().getNestedState(t, b);
703
- return l(h, b, m);
701
+ const b = [...e, s], x = r.getState().getNestedState(t, b);
702
+ return l(x, b, S);
704
703
  }
705
- }, R = new Proxy(A, T);
706
- return d.set(C, {
704
+ }, R = new Proxy(A, C);
705
+ return u.set(k, {
707
706
  proxy: R,
708
707
  stateVersion: $
709
708
  }), R;
@@ -719,63 +718,63 @@ function jt({
719
718
  proxy: t,
720
719
  rebuildStateShape: i
721
720
  }) {
722
- const S = r().getNestedState(t._stateKey, t._path);
723
- return Array.isArray(S) ? i(
724
- S,
721
+ const m = r().getNestedState(t._stateKey, t._path);
722
+ return Array.isArray(m) ? i(
723
+ m,
725
724
  t._path
726
725
  ).stateMapNoRender(
727
- (d, $, _, y, l) => t._mapFn(d, $, _, y, l)
726
+ (u, $, _, v, l) => t._mapFn(u, $, _, v, l)
728
727
  ) : null;
729
728
  }
730
729
  function bt({
731
730
  proxy: t
732
731
  }) {
733
- const i = Z(null), S = `${t._stateKey}-${t._path.join(".")}`;
732
+ const i = Z(null), m = `${t._stateKey}-${t._path.join(".")}`;
734
733
  return nt(() => {
735
- const v = i.current;
736
- if (!v || !v.parentElement) return;
737
- const d = v.parentElement, _ = Array.from(d.childNodes).indexOf(v);
738
- let y = d.getAttribute("data-parent-id");
739
- y || (y = `parent-${crypto.randomUUID()}`, d.setAttribute("data-parent-id", y));
734
+ const y = i.current;
735
+ if (!y || !y.parentElement) return;
736
+ const u = y.parentElement, _ = Array.from(u.childNodes).indexOf(y);
737
+ let v = u.getAttribute("data-parent-id");
738
+ v || (v = `parent-${crypto.randomUUID()}`, u.setAttribute("data-parent-id", v));
740
739
  const f = {
741
740
  instanceId: `instance-${crypto.randomUUID()}`,
742
- parentId: y,
741
+ parentId: v,
743
742
  position: _,
744
743
  effect: t._effect
745
744
  };
746
- r.getState().addSignalElement(S, f);
745
+ r.getState().addSignalElement(m, f);
747
746
  const e = r.getState().getNestedState(t._stateKey, t._path);
748
- let m;
747
+ let S;
749
748
  if (t._effect)
750
749
  try {
751
- m = new Function(
750
+ S = new Function(
752
751
  "state",
753
752
  `return (${t._effect})(state)`
754
753
  )(e);
755
754
  } catch (A) {
756
- console.error("Error evaluating effect function during mount:", A), m = e;
755
+ console.error("Error evaluating effect function during mount:", A), S = e;
757
756
  }
758
757
  else
759
- m = e;
760
- m !== null && typeof m == "object" && (m = JSON.stringify(m));
761
- const C = document.createTextNode(String(m));
762
- v.replaceWith(C);
758
+ S = e;
759
+ S !== null && typeof S == "object" && (S = JSON.stringify(S));
760
+ const k = document.createTextNode(String(S));
761
+ y.replaceWith(k);
763
762
  }, [t._stateKey, t._path.join("."), t._effect]), Q("span", {
764
763
  ref: i,
765
764
  style: { display: "none" },
766
- "data-signal-id": S
765
+ "data-signal-id": m
767
766
  });
768
767
  }
769
768
  function Jt(t) {
770
769
  const i = Et(
771
- (S) => {
772
- const v = r.getState().stateComponents.get(t._stateKey) || {
770
+ (m) => {
771
+ const y = r.getState().stateComponents.get(t._stateKey) || {
773
772
  components: /* @__PURE__ */ new Map()
774
773
  };
775
- return v.components.set(t._stateKey, {
776
- forceUpdate: S,
774
+ return y.components.set(t._stateKey, {
775
+ forceUpdate: m,
777
776
  paths: /* @__PURE__ */ new Set([t._path.join(".")])
778
- }), () => v.components.delete(t._stateKey);
777
+ }), () => y.components.delete(t._stateKey);
779
778
  },
780
779
  () => r.getState().getNestedState(t._stateKey, t._path)
781
780
  );
@@ -787,6 +786,6 @@ export {
787
786
  qt as addStateOptions,
788
787
  zt as createCogsState,
789
788
  Bt as notifyComponent,
790
- Ot as useCogsStateFn
789
+ Tt as useCogsStateFn
791
790
  };
792
791
  //# sourceMappingURL=CogsState.jsx.map