cogsbox-state 0.5.50 → 0.5.52

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