cogsbox-state 0.5.71 → 0.5.73

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.
@@ -1,14 +1,14 @@
1
1
  "use client";
2
2
  import { jsx as ct } from "react/jsx-runtime";
3
- import { useState as et, useRef as Z, useEffect as nt, useLayoutEffect as It, useMemo as _t, createElement as Q, useSyncExternalStore as Et, startTransition as H } from "react";
4
- import { transformStateFunc as wt, isFunction as G, getNestedValue as U, isDeepEqual as M, debounce as Nt } from "./utility.js";
5
- import { pushFunc as K, updateFn as B, cutFunc as J, ValidationWrapper as Vt, FormControlComponent as $t } from "./Functions.jsx";
3
+ import { useState as et, useRef as J, useEffect as nt, useLayoutEffect as It, useMemo as _t, createElement as Q, useSyncExternalStore as Et, startTransition as Z } from "react";
4
+ import { transformStateFunc as $t, isFunction as L, getNestedValue as U, isDeepEqual as M, debounce as wt } from "./utility.js";
5
+ import { pushFunc as K, updateFn as z, cutFunc as B, ValidationWrapper as Nt, FormControlComponent as Vt } from "./Functions.jsx";
6
6
  import "zod";
7
7
  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, y = r.getState().setInitialStateOptions, g = S(t) || {};
11
+ const m = r.getState().getInitialOptions, y = r.getState().setInitialStateOptions, g = m(t) || {};
12
12
  y(t, {
13
13
  ...g,
14
14
  ...i
@@ -17,21 +17,21 @@ function dt(t, i) {
17
17
  function ut({
18
18
  stateKey: t,
19
19
  options: i,
20
- initialOptionsPart: S
20
+ initialOptionsPart: m
21
21
  }) {
22
- const y = q(t) || {}, g = S[t] || {}, w = r.getState().setInitialStateOptions, _ = { ...g, ...y };
22
+ const y = Y(t) || {}, g = m[t] || {}, $ = r.getState().setInitialStateOptions, _ = { ...g, ...y };
23
23
  let v = !1;
24
24
  if (i)
25
25
  for (const l in i)
26
26
  _.hasOwnProperty(l) ? l == "localStorage" && i[l] && _[l].key !== i[l]?.key && (v = !0, _[l] = i[l]) : (v = !0, _[l] = i[l]);
27
- v && w(t, _);
27
+ 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 [y, g] = wt(S);
33
+ let m = t;
34
+ const [y, g] = $t(m);
35
35
  (i?.formElements || i?.validation) && Object.keys(g).forEach((v) => {
36
36
  g[v] = g[v] || {}, g[v].formElements = {
37
37
  ...i.formElements,
@@ -41,15 +41,15 @@ const zt = (t, i) => {
41
41
  // State-specific overrides
42
42
  };
43
43
  }), r.getState().setInitialStates(y);
44
- const w = (v, l) => {
44
+ const $ = (v, l) => {
45
45
  const [f] = et(l?.componentId ?? rt());
46
46
  ut({
47
47
  stateKey: v,
48
48
  options: l,
49
49
  initialOptionsPart: g
50
50
  });
51
- const e = r.getState().cogsStateStore[v] || y[v], m = l?.modifyState ? l.modifyState(e) : e, [h, V] = Ft(
52
- m,
51
+ const e = r.getState().cogsStateStore[v] || y[v], S = l?.modifyState ? l.modifyState(e) : e, [T, A] = bt(
52
+ S,
53
53
  {
54
54
  stateKey: v,
55
55
  syncUpdate: l?.syncUpdate,
@@ -63,22 +63,22 @@ const zt = (t, i) => {
63
63
  dependencies: l?.dependencies
64
64
  }
65
65
  );
66
- return V;
66
+ return A;
67
67
  };
68
68
  function _(v, l) {
69
69
  ut({ stateKey: v, options: l, initialOptionsPart: g });
70
70
  }
71
- return { useCogsState: w, setCogsOptions: _ };
71
+ return { useCogsState: $, setCogsOptions: _ };
72
72
  }, {
73
- setUpdaterState: Y,
74
- setState: L,
75
- getInitialOptions: q,
73
+ setUpdaterState: H,
74
+ setState: D,
75
+ getInitialOptions: Y,
76
76
  getKeyState: gt,
77
77
  getValidationErrors: kt,
78
78
  setStateLog: pt,
79
79
  updateInitialStateGlobal: ot,
80
80
  addValidationError: Ct,
81
- removeValidationError: R,
81
+ removeValidationError: P,
82
82
  setServerSyncActions: Tt
83
83
  } = r.getState(), ft = (t) => {
84
84
  if (!t) return null;
@@ -88,74 +88,74 @@ const zt = (t, i) => {
88
88
  } catch (i) {
89
89
  return console.error("Error loading from localStorage:", i), null;
90
90
  }
91
- }, ht = (t, i, S, y) => {
92
- S.log && console.log(
91
+ }, ht = (t, i, m, y) => {
92
+ m.log && console.log(
93
93
  "saving to localstorage",
94
94
  i,
95
- S.localStorage?.key,
95
+ m.localStorage?.key,
96
96
  y
97
97
  );
98
- const g = G(S.localStorage?.key) ? S.localStorage?.key(t) : S.localStorage?.key;
98
+ const g = L(m.localStorage?.key) ? m.localStorage?.key(t) : m.localStorage?.key;
99
99
  if (g && y) {
100
- const w = {
100
+ const $ = {
101
101
  state: t,
102
102
  lastUpdated: Date.now(),
103
103
  lastSyncedWithServer: r.getState().serverSyncLog[i]?.[0]?.timeStamp,
104
104
  baseServerState: r.getState().serverState[i]
105
105
  }, _ = `${y}-${i}-${g}`;
106
- window.localStorage.setItem(_, JSON.stringify(w));
106
+ window.localStorage.setItem(_, JSON.stringify($));
107
107
  }
108
- }, bt = (t, i, S, y, g, w) => {
108
+ }, Ft = (t, i, m, y, g, $) => {
109
109
  const _ = {
110
110
  initialState: i,
111
111
  updaterState: X(
112
112
  t,
113
113
  y,
114
114
  g,
115
- w
115
+ $
116
116
  ),
117
- state: S
117
+ state: m
118
118
  };
119
- H(() => {
120
- ot(t, _.initialState), Y(t, _.updaterState), L(t, _.state);
119
+ Z(() => {
120
+ ot(t, _.initialState), H(t, _.updaterState), D(t, _.state);
121
121
  });
122
122
  }, St = (t) => {
123
123
  const i = r.getState().stateComponents.get(t);
124
124
  if (!i) return;
125
- const S = /* @__PURE__ */ new Set();
125
+ const m = /* @__PURE__ */ new Set();
126
126
  i.components.forEach((y) => {
127
- S.add(() => y.forceUpdate());
127
+ m.add(() => y.forceUpdate());
128
128
  }), queueMicrotask(() => {
129
- H(() => {
130
- S.forEach((y) => y());
129
+ Z(() => {
130
+ m.forEach((y) => y());
131
131
  });
132
132
  });
133
133
  }, Bt = (t, i) => {
134
- const S = r.getState().stateComponents.get(t);
135
- if (S) {
136
- const y = `${t}////${i}`, g = S.components.get(y);
134
+ const m = r.getState().stateComponents.get(t);
135
+ if (m) {
136
+ const y = `${t}////${i}`, g = m.components.get(y);
137
137
  g && g.forceUpdate();
138
138
  }
139
139
  };
140
- function Ft(t, {
140
+ function bt(t, {
141
141
  stateKey: i,
142
- serverSync: S,
142
+ serverSync: m,
143
143
  localStorage: y,
144
144
  formElements: g,
145
- middleware: w,
145
+ middleware: $,
146
146
  reactiveDeps: _,
147
147
  reactiveType: v,
148
148
  componentId: l,
149
149
  initialState: f,
150
150
  syncUpdate: e,
151
- dependencies: m
151
+ dependencies: S
152
152
  } = {}) {
153
- const [h, V] = et({}), { sessionId: A } = At();
154
- let j = !i;
155
- const [u] = et(i ?? rt()), s = r.getState().stateLog[u], D = Z(/* @__PURE__ */ new Set()), x = Z(l ?? rt()), C = Z(null);
156
- C.current = q(u), nt(() => {
153
+ const [T, A] = et({}), { sessionId: h } = At();
154
+ let R = !i;
155
+ const [u] = et(i ?? rt()), s = r.getState().stateLog[u], G = J(/* @__PURE__ */ new Set()), x = J(l ?? rt()), p = J(null);
156
+ p.current = Y(u), nt(() => {
157
157
  if (e && e.stateKey === u && e.path?.[0]) {
158
- L(u, (o) => ({
158
+ D(u, (o) => ({
159
159
  ...o,
160
160
  [e.path[0]]: e.newValue
161
161
  }));
@@ -169,107 +169,107 @@ function Ft(t, {
169
169
  dt(u, {
170
170
  initialState: f
171
171
  });
172
- const c = C.current;
172
+ const c = p.current;
173
173
  let o = null;
174
174
  c.log && console.log("newoptions", c);
175
- const d = G(c.localStorage?.key) ? c.localStorage?.key(f) : c.localStorage?.key;
176
- d && A && (o = ft(
177
- A + "-" + u + "-" + d
175
+ const d = L(c.localStorage?.key) ? c.localStorage?.key(f) : c.localStorage?.key;
176
+ d && h && (o = ft(
177
+ h + "-" + u + "-" + d
178
178
  ));
179
179
  let E = null;
180
- f && (E = f, o && o.lastUpdated > (o.lastSyncedWithServer || 0) && (E = o.state), bt(
180
+ f && (E = f, o && o.lastUpdated > (o.lastSyncedWithServer || 0) && (E = o.state), Ft(
181
181
  u,
182
182
  f,
183
183
  E,
184
184
  n,
185
185
  x.current,
186
- A
187
- ), St(u), V({}));
188
- }, [f, ...m || []]), It(() => {
189
- j && dt(u, {
190
- serverSync: S,
186
+ h
187
+ ), St(u), A({}));
188
+ }, [f, ...S || []]), It(() => {
189
+ R && dt(u, {
190
+ serverSync: m,
191
191
  formElements: g,
192
192
  initialState: f,
193
193
  localStorage: y,
194
- middleware: w
194
+ middleware: $
195
195
  });
196
196
  const c = `${u}////${x.current}`, o = r.getState().stateComponents.get(u) || {
197
197
  components: /* @__PURE__ */ new Map()
198
198
  };
199
199
  return o.components.set(c, {
200
- forceUpdate: () => V({}),
200
+ forceUpdate: () => A({}),
201
201
  paths: /* @__PURE__ */ new Set(),
202
202
  deps: [],
203
203
  depsFunction: _ || void 0,
204
204
  reactiveType: v ?? ["component", "deps"]
205
- }), r.getState().stateComponents.set(u, o), V({}), () => {
205
+ }), r.getState().stateComponents.set(u, o), A({}), () => {
206
206
  const d = `${u}////${x.current}`;
207
207
  o && (o.components.delete(d), o.components.size === 0 && r.getState().stateComponents.delete(u));
208
208
  };
209
209
  }, []);
210
210
  const n = (c, o, d, E) => {
211
211
  if (Array.isArray(o)) {
212
- const b = `${u}-${o.join(".")}`;
213
- D.current.add(b);
212
+ const F = `${u}-${o.join(".")}`;
213
+ G.current.add(F);
214
214
  }
215
- L(u, (b) => {
216
- const $ = G(c) ? c(b) : c, O = `${u}-${o.join(".")}`;
217
- if (O) {
218
- let P = !1, I = r.getState().signalDomElements.get(O);
215
+ D(u, (F) => {
216
+ const N = L(c) ? c(F) : c, j = `${u}-${o.join(".")}`;
217
+ if (j) {
218
+ let O = !1, I = r.getState().signalDomElements.get(j);
219
219
  if ((!I || I.size === 0) && (d.updateType === "insert" || d.updateType === "cut")) {
220
- const k = o.slice(0, -1), p = U($, k);
221
- if (Array.isArray(p)) {
222
- P = !0;
223
- const N = `${u}-${k.join(".")}`;
224
- I = r.getState().signalDomElements.get(N);
220
+ const V = o.slice(0, -1), k = U(N, V);
221
+ if (Array.isArray(k)) {
222
+ O = !0;
223
+ const w = `${u}-${V.join(".")}`;
224
+ I = r.getState().signalDomElements.get(w);
225
225
  }
226
226
  }
227
227
  if (I) {
228
- const k = P ? U($, o.slice(0, -1)) : U($, o);
229
- I.forEach(({ parentId: p, position: N, effect: W }) => {
230
- const F = document.querySelector(
231
- `[data-parent-id="${p}"]`
228
+ const V = O ? U(N, o.slice(0, -1)) : U(N, o);
229
+ I.forEach(({ parentId: k, position: w, effect: W }) => {
230
+ const b = document.querySelector(
231
+ `[data-parent-id="${k}"]`
232
232
  );
233
- if (F) {
234
- const st = Array.from(F.childNodes);
235
- if (st[N]) {
236
- const vt = W ? new Function("state", `return (${W})(state)`)(k) : k;
237
- st[N].textContent = String(vt);
233
+ if (b) {
234
+ const st = Array.from(b.childNodes);
235
+ if (st[w]) {
236
+ const vt = W ? new Function("state", `return (${W})(state)`)(V) : V;
237
+ st[w].textContent = String(vt);
238
238
  }
239
239
  }
240
240
  });
241
241
  }
242
242
  }
243
- d.updateType === "update" && (E || C.current?.validationKey) && o && R(
244
- (E || C.current?.validationKey) + "." + o.join(".")
243
+ d.updateType === "update" && (E || p.current?.validationKey) && o && P(
244
+ (E || p.current?.validationKey) + "." + o.join(".")
245
245
  );
246
- const T = o.slice(0, o.length - 1);
247
- d.updateType === "cut" && C.current?.validationKey && R(
248
- C.current?.validationKey + "." + T.join(".")
249
- ), d.updateType === "insert" && C.current?.validationKey && kt(
250
- C.current?.validationKey + "." + T.join(".")
251
- ).filter(([I, k]) => {
252
- let p = I?.split(".").length;
253
- if (I == T.join(".") && p == T.length - 1) {
254
- let N = I + "." + T;
255
- R(I), Ct(N, k);
246
+ const C = o.slice(0, o.length - 1);
247
+ d.updateType === "cut" && p.current?.validationKey && P(
248
+ p.current?.validationKey + "." + C.join(".")
249
+ ), d.updateType === "insert" && p.current?.validationKey && kt(
250
+ p.current?.validationKey + "." + C.join(".")
251
+ ).filter(([I, V]) => {
252
+ let k = I?.split(".").length;
253
+ if (I == C.join(".") && k == C.length - 1) {
254
+ let w = I + "." + C;
255
+ P(I), Ct(w, V);
256
256
  }
257
257
  });
258
- const z = U(b, o), mt = U($, o), yt = d.updateType === "update" ? o.join(".") : [...o].slice(0, -1).join("."), at = r.getState().stateComponents.get(u);
258
+ const q = U(F, o), mt = U(N, o), yt = d.updateType === "update" ? o.join(".") : [...o].slice(0, -1).join("."), at = r.getState().stateComponents.get(u);
259
259
  if (at)
260
- for (const [P, I] of at.components.entries()) {
261
- let k = !1;
262
- const p = Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"];
263
- if (!p.includes("none")) {
264
- if (p.includes("all")) {
260
+ for (const [O, I] of at.components.entries()) {
261
+ let V = !1;
262
+ const k = Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"];
263
+ if (!k.includes("none")) {
264
+ if (k.includes("all")) {
265
265
  I.forceUpdate();
266
266
  continue;
267
267
  }
268
- if (p.includes("component") && I.paths && (I.paths.has(yt) || I.paths.has("")) && (k = !0), !k && p.includes("deps") && I.depsFunction) {
269
- const N = I.depsFunction($);
270
- typeof N == "boolean" ? N && (k = !0) : M(I.deps, N) || (I.deps = N, k = !0);
268
+ if (k.includes("component") && I.paths && (I.paths.has(yt) || I.paths.has("")) && (V = !0), !V && k.includes("deps") && I.depsFunction) {
269
+ const w = I.depsFunction(N);
270
+ typeof w == "boolean" ? w && (V = !0) : M(I.deps, w) || (I.deps = w, V = !0);
271
271
  }
272
- k && I.forceUpdate();
272
+ V && I.forceUpdate();
273
273
  }
274
274
  }
275
275
  const it = {
@@ -278,96 +278,94 @@ function Ft(t, {
278
278
  path: o,
279
279
  updateType: d.updateType,
280
280
  status: "new",
281
- oldValue: z,
281
+ oldValue: q,
282
282
  newValue: mt
283
283
  };
284
- if (pt(u, (P) => {
285
- const k = [...P ?? [], it].reduce((p, N) => {
286
- const W = `${N.stateKey}:${JSON.stringify(N.path)}`, F = p.get(W);
287
- return F ? (F.timeStamp = Math.max(F.timeStamp, N.timeStamp), F.newValue = N.newValue, F.oldValue = F.oldValue ?? N.oldValue, F.updateType = N.updateType) : p.set(W, { ...N }), p;
284
+ if (pt(u, (O) => {
285
+ const V = [...O ?? [], it].reduce((k, w) => {
286
+ const W = `${w.stateKey}:${JSON.stringify(w.path)}`, b = k.get(W);
287
+ return b ? (b.timeStamp = Math.max(b.timeStamp, w.timeStamp), b.newValue = w.newValue, b.oldValue = b.oldValue ?? w.oldValue, b.updateType = w.updateType) : k.set(W, { ...w }), k;
288
288
  }, /* @__PURE__ */ new Map());
289
- return Array.from(k.values());
289
+ return Array.from(V.values());
290
290
  }), ht(
291
- $,
291
+ N,
292
292
  u,
293
- C.current,
294
- A
295
- ), w && w({
293
+ p.current,
294
+ h
295
+ ), $ && $({
296
296
  updateLog: s,
297
297
  update: it
298
- }), C.current?.serverSync) {
299
- const P = r.getState().serverState[u], I = C.current?.serverSync;
298
+ }), p.current?.serverSync) {
299
+ const O = r.getState().serverState[u], I = p.current?.serverSync;
300
300
  Tt(u, {
301
- syncKey: typeof I.syncKey == "string" ? I.syncKey : I.syncKey({ state: $ }),
302
- rollBackState: P,
301
+ syncKey: typeof I.syncKey == "string" ? I.syncKey : I.syncKey({ state: N }),
302
+ rollBackState: O,
303
303
  actionTimeStamp: Date.now() + (I.debounce ?? 3e3),
304
304
  status: "waiting"
305
305
  });
306
306
  }
307
- return $;
307
+ return N;
308
308
  });
309
309
  };
310
- r.getState().updaterState[u] || (Y(
310
+ r.getState().updaterState[u] || (H(
311
311
  u,
312
312
  X(
313
313
  u,
314
314
  n,
315
315
  x.current,
316
- A
316
+ h
317
317
  )
318
- ), r.getState().cogsStateStore[u] || L(u, t), r.getState().initialStateGlobal[u] || ot(u, t));
318
+ ), r.getState().cogsStateStore[u] || D(u, t), r.getState().initialStateGlobal[u] || ot(u, t));
319
319
  const a = _t(() => X(
320
320
  u,
321
321
  n,
322
322
  x.current,
323
- A
323
+ h
324
324
  ), [u]);
325
325
  return [gt(u), a];
326
326
  }
327
- function X(t, i, S, y) {
327
+ function X(t, i, m, y) {
328
328
  const g = /* @__PURE__ */ new Map();
329
- let w = 0;
329
+ let $ = 0;
330
330
  const _ = (f) => {
331
331
  const e = f.join(".");
332
- for (const [m] of g)
333
- (m === e || m.startsWith(e + ".")) && g.delete(m);
334
- w++;
332
+ for (const [S] of g)
333
+ (S === e || S.startsWith(e + ".")) && g.delete(S);
334
+ $++;
335
335
  }, v = {
336
336
  removeValidation: (f) => {
337
- f?.validationKey && R(f.validationKey);
337
+ f?.validationKey && P(f.validationKey);
338
338
  },
339
339
  revertToInitialState: (f) => {
340
340
  const e = r.getState().getInitialOptions(t)?.validation;
341
- e?.key && R(e?.key), f?.validationKey && R(f.validationKey);
342
- const m = r.getState().initialStateGlobal[t];
343
- g.clear(), w++;
344
- const h = l(m, []), V = q(t), A = G(V?.localStorage?.key) ? V?.localStorage?.key(m) : V?.localStorage?.key;
345
- return A && localStorage.removeItem(A), H(() => {
346
- Y(t, h), L(t, m);
347
- const j = r.getState().stateComponents.get(t);
348
- j && j.components.forEach((u) => {
349
- u.forceUpdate();
341
+ e?.key && P(e?.key), f?.validationKey && P(f.validationKey);
342
+ const S = r.getState().initialStateGlobal[t];
343
+ g.clear(), $++;
344
+ const T = l(S, []), A = Y(t), h = L(A?.localStorage?.key) ? A?.localStorage?.key(S) : A?.localStorage?.key, R = `${y}-${t}-${h}`;
345
+ return R && localStorage.removeItem(R), Z(() => {
346
+ H(t, T), D(t, S);
347
+ const u = r.getState().stateComponents.get(t);
348
+ u && u.components.forEach((s) => {
349
+ s.forceUpdate();
350
350
  });
351
- }), m;
351
+ }), S;
352
352
  },
353
353
  updateInitialState: (f) => {
354
- g.clear(), w++;
355
- const e = r.getState().initialStateGlobal[t], m = q(t), h = G(m?.localStorage?.key) ? m?.localStorage?.key(e) : m?.localStorage?.key;
356
- h && localStorage.removeItem(h);
357
- const V = X(
354
+ g.clear(), $++;
355
+ const e = X(
358
356
  t,
359
357
  i,
360
- S,
358
+ m,
361
359
  y
362
360
  );
363
- return H(() => {
364
- ot(t, f), Y(t, V), L(t, f);
365
- const A = r.getState().stateComponents.get(t);
366
- A && A.components.forEach((j) => {
367
- j.forceUpdate();
361
+ return Z(() => {
362
+ ot(t, f), H(t, e), D(t, f);
363
+ const S = r.getState().stateComponents.get(t);
364
+ S && S.components.forEach((T) => {
365
+ T.forceUpdate();
368
366
  });
369
367
  }), {
370
- fetchId: (A) => V.get()[A]
368
+ fetchId: (S) => e.get()[S]
371
369
  };
372
370
  },
373
371
  _initialState: r.getState().initialStateGlobal[t],
@@ -378,24 +376,24 @@ function X(t, i, S, y) {
378
376
  return !!(f && M(f, gt(t)));
379
377
  }
380
378
  };
381
- function l(f, e = [], m) {
382
- const h = e.map(String).join(".");
383
- g.get(h);
384
- const V = function() {
379
+ function l(f, e = [], S) {
380
+ const T = e.map(String).join(".");
381
+ g.get(T);
382
+ const A = function() {
385
383
  return r().getNestedState(t, e);
386
384
  };
387
385
  Object.keys(v).forEach((u) => {
388
- V[u] = v[u];
386
+ A[u] = v[u];
389
387
  });
390
- const A = {
391
- apply(u, s, D) {
388
+ const h = {
389
+ apply(u, s, G) {
392
390
  return console.log(
393
391
  `PROXY APPLY TRAP HIT: stateKey=${t}, path=${e.join(".")}`
394
392
  ), console.trace("Apply trap stack trace"), r().getNestedState(t, e);
395
393
  },
396
394
  get(u, s) {
397
395
  if (s !== "then" && !s.startsWith("$") && s !== "stateMapNoRender") {
398
- const n = e.join("."), a = `${t}////${S}`, c = r.getState().stateComponents.get(t);
396
+ const n = e.join("."), a = `${t}////${m}`, c = r.getState().stateComponents.get(t);
399
397
  if (c) {
400
398
  const o = c.components.get(a);
401
399
  o && (e.length > 0 || s === "get") && o.paths.add(n);
@@ -415,8 +413,8 @@ function X(t, i, S, y) {
415
413
  };
416
414
  if (s === "removeStorage")
417
415
  return () => {
418
- const n = r.getState().initialStateGlobal[t], a = q(t), c = G(a?.localStorage?.key) ? a?.localStorage?.key(n) : a?.localStorage?.key;
419
- c && localStorage.removeItem(c);
416
+ const n = r.getState().initialStateGlobal[t], a = Y(t), c = L(a?.localStorage?.key) ? a?.localStorage?.key(n) : a?.localStorage?.key, o = `${y}-${t}-${c}`;
417
+ o && localStorage.removeItem(o);
420
418
  };
421
419
  if (s === "showValidationErrors")
422
420
  return () => {
@@ -433,7 +431,7 @@ function X(t, i, S, y) {
433
431
  return l(
434
432
  f[n],
435
433
  [...e, n.toString()],
436
- m
434
+ S
437
435
  );
438
436
  };
439
437
  if (s === "stateSort")
@@ -442,8 +440,8 @@ function X(t, i, S, y) {
442
440
  ...d,
443
441
  __origIndex: E.toString()
444
442
  }))].sort(n);
445
- return g.clear(), w++, l(o, e, {
446
- filtered: [...m?.filtered || [], e],
443
+ return g.clear(), $++, l(o, e, {
444
+ filtered: [...S?.filtered || [], e],
447
445
  validIndices: o.map(
448
446
  (d) => parseInt(d.__origIndex)
449
447
  )
@@ -451,21 +449,21 @@ function X(t, i, S, y) {
451
449
  };
452
450
  if (s === "stateMap" || s === "stateMapNoRender")
453
451
  return (n) => {
454
- const a = m?.filtered?.some(
452
+ const a = S?.filtered?.some(
455
453
  (o) => o.join(".") === e.join(".")
456
454
  ), c = a ? f : r.getState().getNestedState(t, e);
457
- return s !== "stateMapNoRender" && (g.clear(), w++), c.map((o, d) => {
458
- const E = a && o.__origIndex ? o.__origIndex : d, b = l(
455
+ return s !== "stateMapNoRender" && (g.clear(), $++), c.map((o, d) => {
456
+ const E = a && o.__origIndex ? o.__origIndex : d, F = l(
459
457
  o,
460
458
  [...e, E.toString()],
461
- m
459
+ S
462
460
  );
463
461
  return n(
464
462
  o,
465
- b,
463
+ F,
466
464
  d,
467
465
  f,
468
- l(f, e, m)
466
+ l(f, e, S)
469
467
  );
470
468
  });
471
469
  };
@@ -481,17 +479,17 @@ function X(t, i, S, y) {
481
479
  });
482
480
  if (s === "stateFlattenOn")
483
481
  return (n) => {
484
- const c = m?.filtered?.some(
482
+ const c = S?.filtered?.some(
485
483
  (d) => d.join(".") === e.join(".")
486
484
  ) ? f : r.getState().getNestedState(t, e);
487
- g.clear(), w++;
485
+ g.clear(), $++;
488
486
  const o = c.flatMap(
489
487
  (d, E) => d[n] ?? []
490
488
  );
491
489
  return l(
492
490
  o,
493
491
  [...e, "[*]", n],
494
- m
492
+ S
495
493
  );
496
494
  };
497
495
  if (s === "findWith")
@@ -501,7 +499,7 @@ function X(t, i, S, y) {
501
499
  );
502
500
  if (c === -1) return;
503
501
  const o = f[c], d = [...e, c.toString()];
504
- return g.clear(), w++, g.clear(), w++, l(o, d);
502
+ return g.clear(), $++, g.clear(), $++, l(o, d);
505
503
  };
506
504
  if (s === "index")
507
505
  return (n) => {
@@ -515,39 +513,39 @@ function X(t, i, S, y) {
515
513
  ));
516
514
  if (s === "uniqueInsert")
517
515
  return (n, a, c) => {
518
- const o = r.getState().getNestedState(t, e), d = G(n) ? n(o) : n;
516
+ const o = r.getState().getNestedState(t, e), d = L(n) ? n(o) : n;
519
517
  let E = null;
520
- if (!o.some(($) => {
518
+ if (!o.some((N) => {
521
519
  if (a) {
522
- const T = a.every(
523
- (z) => M($[z], d[z])
520
+ const C = a.every(
521
+ (q) => M(N[q], d[q])
524
522
  );
525
- return T && (E = $), T;
523
+ return C && (E = N), C;
526
524
  }
527
- const O = M($, d);
528
- return O && (E = $), O;
525
+ const j = M(N, d);
526
+ return j && (E = N), j;
529
527
  }))
530
528
  _(e), K(i, d, e, t);
531
529
  else if (c && E) {
532
- const $ = c(E), O = o.map(
533
- (T) => M(T, E) ? $ : T
530
+ const N = c(E), j = o.map(
531
+ (C) => M(C, E) ? N : C
534
532
  );
535
- _(e), B(i, O, e);
533
+ _(e), z(i, j, e);
536
534
  }
537
535
  };
538
536
  if (s === "cut")
539
537
  return (n, a) => {
540
- a?.waitForSync || (_(e), J(i, e, t, n));
538
+ a?.waitForSync || (_(e), B(i, e, t, n));
541
539
  };
542
540
  if (s === "cutByValue")
543
541
  return (n) => {
544
542
  for (let a = 0; a < f.length; a++)
545
- f[a] === n && J(i, e, t, a);
543
+ f[a] === n && B(i, e, t, a);
546
544
  };
547
545
  if (s === "toggleByValue")
548
546
  return (n) => {
549
547
  const a = f.findIndex((c) => c === n);
550
- a > -1 ? J(i, e, t, a) : K(i, n, e, t);
548
+ a > -1 ? B(i, e, t, a) : K(i, n, e, t);
551
549
  };
552
550
  if (s === "stateFilter")
553
551
  return (n) => {
@@ -557,22 +555,22 @@ function X(t, i, S, y) {
557
555
  })), c = [], o = [];
558
556
  for (let d = 0; d < a.length; d++)
559
557
  n(a[d], d) && (c.push(d), o.push(a[d]));
560
- return g.clear(), w++, l(o, e, {
561
- filtered: [...m?.filtered || [], e],
558
+ return g.clear(), $++, l(o, e, {
559
+ filtered: [...S?.filtered || [], e],
562
560
  validIndices: c
563
561
  // Always pass validIndices, even if empty
564
562
  });
565
563
  };
566
564
  }
567
- const D = e[e.length - 1];
568
- if (!isNaN(Number(D))) {
565
+ const G = e[e.length - 1];
566
+ if (!isNaN(Number(G))) {
569
567
  const n = e.slice(0, -1), a = r.getState().getNestedState(t, n);
570
568
  if (Array.isArray(a) && s === "cut")
571
- return () => J(
569
+ return () => B(
572
570
  i,
573
571
  n,
574
572
  t,
575
- Number(D)
573
+ Number(G)
576
574
  );
577
575
  }
578
576
  if (s === "get")
@@ -609,7 +607,7 @@ function X(t, i, S, y) {
609
607
  const a = e.slice(0, -1), c = Number(e[e.length - 1]), o = a.join(".");
610
608
  n ? r.getState().setSelectedIndex(t, o, c) : r.getState().setSelectedIndex(t, o, void 0);
611
609
  const d = r.getState().getNestedState(t, [...a]);
612
- B(i, d, a), _(a);
610
+ z(i, d, a), _(a);
613
611
  };
614
612
  if (e.length == 0) {
615
613
  if (s === "validateZodSchema")
@@ -619,23 +617,23 @@ function X(t, i, S, y) {
619
617
  throw new Error("Zod schema not found");
620
618
  if (!n?.key)
621
619
  throw new Error("Validation key not found");
622
- R(n.key);
620
+ P(n.key);
623
621
  const c = r.getState().cogsStateStore[t];
624
622
  try {
625
623
  const o = r.getState().getValidationErrors(n.key);
626
624
  o && o.length > 0 && o.forEach(([E]) => {
627
- E && E.startsWith(n.key) && R(E);
625
+ E && E.startsWith(n.key) && P(E);
628
626
  });
629
627
  const d = n.zodSchema.safeParse(c);
630
- return d.success ? !0 : (d.error.errors.forEach((b) => {
631
- const $ = b.path, O = b.message, T = [n.key, ...$].join(".");
632
- a(T, O);
628
+ return d.success ? !0 : (d.error.errors.forEach((F) => {
629
+ const N = F.path, j = F.message, C = [n.key, ...N].join(".");
630
+ a(C, j);
633
631
  }), St(t), !1);
634
632
  } catch (o) {
635
633
  return console.error("Zod schema validation failed", o), !1;
636
634
  }
637
635
  };
638
- if (s === "_componentId") return S;
636
+ if (s === "_componentId") return m;
639
637
  if (s === "getComponents")
640
638
  return () => r().stateComponents.get(t);
641
639
  if (s === "getAllFormRefs")
@@ -658,13 +656,13 @@ function X(t, i, S, y) {
658
656
  children: n,
659
657
  hideMessage: a
660
658
  }) => /* @__PURE__ */ ct(
661
- Vt,
659
+ Nt,
662
660
  {
663
661
  formOpts: a ? { validation: { message: "" } } : void 0,
664
662
  path: e,
665
663
  validationKey: r.getState().getInitialOptions(t)?.validation?.key || "",
666
664
  stateKey: t,
667
- validIndices: m?.validIndices,
665
+ validIndices: S?.validIndices,
668
666
  children: n
669
667
  }
670
668
  );
@@ -674,13 +672,13 @@ function X(t, i, S, y) {
674
672
  if (s === "update")
675
673
  return (n, a) => {
676
674
  if (a?.debounce)
677
- Nt(() => {
678
- B(i, n, e, "");
675
+ wt(() => {
676
+ z(i, n, e, "");
679
677
  const c = r.getState().getNestedState(t, e);
680
678
  a?.afterUpdate && a.afterUpdate(c);
681
679
  }, a.debounce);
682
680
  else {
683
- B(i, n, e, "");
681
+ z(i, n, e, "");
684
682
  const c = r.getState().getNestedState(t, e);
685
683
  a?.afterUpdate && a.afterUpdate(c);
686
684
  }
@@ -688,7 +686,7 @@ function X(t, i, S, y) {
688
686
  };
689
687
  if (s === "formElement")
690
688
  return (n, a) => /* @__PURE__ */ ct(
691
- $t,
689
+ Vt,
692
690
  {
693
691
  setState: i,
694
692
  stateKey: t,
@@ -697,38 +695,38 @@ function X(t, i, S, y) {
697
695
  formOpts: a
698
696
  }
699
697
  );
700
- const x = [...e, s], C = r.getState().getNestedState(t, x);
701
- return l(C, x, m);
698
+ const x = [...e, s], p = r.getState().getNestedState(t, x);
699
+ return l(p, x, S);
702
700
  }
703
- }, j = new Proxy(V, A);
704
- return g.set(h, {
705
- proxy: j,
706
- stateVersion: w
707
- }), j;
701
+ }, R = new Proxy(A, h);
702
+ return g.set(T, {
703
+ proxy: R,
704
+ stateVersion: $
705
+ }), R;
708
706
  }
709
707
  return l(
710
708
  r.getState().getNestedState(t, [])
711
709
  );
712
710
  }
713
711
  function tt(t) {
714
- return Q(Ot, { proxy: t });
712
+ return Q(xt, { proxy: t });
715
713
  }
716
714
  function jt({
717
715
  proxy: t,
718
716
  rebuildStateShape: i
719
717
  }) {
720
- const S = r().getNestedState(t._stateKey, t._path);
721
- return Array.isArray(S) ? i(
722
- S,
718
+ const m = r().getNestedState(t._stateKey, t._path);
719
+ return Array.isArray(m) ? i(
720
+ m,
723
721
  t._path
724
722
  ).stateMapNoRender(
725
- (g, w, _, v, l) => t._mapFn(g, w, _, v, l)
723
+ (g, $, _, v, l) => t._mapFn(g, $, _, v, l)
726
724
  ) : null;
727
725
  }
728
- function Ot({
726
+ function xt({
729
727
  proxy: t
730
728
  }) {
731
- const i = Z(null), S = `${t._stateKey}-${t._path.join(".")}`;
729
+ const i = J(null), m = `${t._stateKey}-${t._path.join(".")}`;
732
730
  return nt(() => {
733
731
  const y = i.current;
734
732
  if (!y || !y.parentElement) return;
@@ -741,37 +739,37 @@ function Ot({
741
739
  position: _,
742
740
  effect: t._effect
743
741
  };
744
- r.getState().addSignalElement(S, f);
742
+ r.getState().addSignalElement(m, f);
745
743
  const e = r.getState().getNestedState(t._stateKey, t._path);
746
- let m;
744
+ let S;
747
745
  if (t._effect)
748
746
  try {
749
- m = new Function(
747
+ S = new Function(
750
748
  "state",
751
749
  `return (${t._effect})(state)`
752
750
  )(e);
753
- } catch (V) {
754
- console.error("Error evaluating effect function during mount:", V), m = e;
751
+ } catch (A) {
752
+ console.error("Error evaluating effect function during mount:", A), S = e;
755
753
  }
756
754
  else
757
- m = e;
758
- m !== null && typeof m == "object" && (m = JSON.stringify(m));
759
- const h = document.createTextNode(String(m));
760
- y.replaceWith(h);
755
+ S = e;
756
+ S !== null && typeof S == "object" && (S = JSON.stringify(S));
757
+ const T = document.createTextNode(String(S));
758
+ y.replaceWith(T);
761
759
  }, [t._stateKey, t._path.join("."), t._effect]), Q("span", {
762
760
  ref: i,
763
761
  style: { display: "none" },
764
- "data-signal-id": S
762
+ "data-signal-id": m
765
763
  });
766
764
  }
767
765
  function Jt(t) {
768
766
  const i = Et(
769
- (S) => {
767
+ (m) => {
770
768
  const y = r.getState().stateComponents.get(t._stateKey) || {
771
769
  components: /* @__PURE__ */ new Map()
772
770
  };
773
771
  return y.components.set(t._stateKey, {
774
- forceUpdate: S,
772
+ forceUpdate: m,
775
773
  paths: /* @__PURE__ */ new Set([t._path.join(".")])
776
774
  }), () => y.components.delete(t._stateKey);
777
775
  },
@@ -785,6 +783,6 @@ export {
785
783
  qt as addStateOptions,
786
784
  zt as createCogsState,
787
785
  Bt as notifyComponent,
788
- Ft as useCogsStateFn
786
+ bt as useCogsStateFn
789
787
  };
790
788
  //# sourceMappingURL=CogsState.jsx.map