cogsbox-state 0.5.38 → 0.5.40

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