cogsbox-state 0.5.97 → 0.5.99

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