cogsbox-state 0.5.60 → 0.5.61

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