cogsbox-state 0.5.38 → 0.5.39

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