cogsbox-state 0.5.339 → 0.5.340

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,28 +1,28 @@
1
1
  "use client";
2
- import { jsx as ht } from "react/jsx-runtime";
3
- import { useState as Z, useRef as X, useEffect as ot, useLayoutEffect as lt, useMemo as vt, createElement as at, useSyncExternalStore as Pt, startTransition as _t, useCallback as Tt } from "react";
4
- import { transformStateFunc as Mt, isDeepEqual as H, isFunction as q, getNestedValue as z, getDifferences as yt, debounce as jt } from "./utility.js";
5
- import { pushFunc as mt, updateFn as rt, cutFunc as ct, ValidationWrapper as Ot, FormControlComponent as Rt } from "./Functions.jsx";
6
- import Ut from "superjson";
7
- import { v4 as It } from "uuid";
2
+ import { jsx as vt } from "react/jsx-runtime";
3
+ import { useState as Z, useRef as X, useEffect as rt, useLayoutEffect as lt, useMemo as yt, createElement as ot, useSyncExternalStore as _t, startTransition as Mt, useCallback as Et } from "react";
4
+ import { transformStateFunc as jt, isDeepEqual as H, isFunction as J, getNestedValue as z, getDifferences as It, debounce as Ot } from "./utility.js";
5
+ import { pushFunc as ht, updateFn as nt, cutFunc as ct, ValidationWrapper as Rt, FormControlComponent as Ut } from "./Functions.jsx";
6
+ import Ft from "superjson";
7
+ import { v4 as pt } from "uuid";
8
8
  import "zod";
9
- import { getGlobalStore as o, formRefStore as Et } from "./store.js";
10
- import { useCogsConfig as Nt } from "./CogsStateClient.jsx";
11
- import { applyPatch as Ft } from "fast-json-patch";
12
- import Dt from "react-use-measure";
13
- function At(t, c) {
9
+ import { getGlobalStore as o, formRefStore as At } from "./store.js";
10
+ import { useCogsConfig as Vt } from "./CogsStateClient.jsx";
11
+ import { applyPatch as Dt } from "fast-json-patch";
12
+ import Wt from "react-use-measure";
13
+ function $t(t, c) {
14
14
  const h = o.getState().getInitialOptions, S = o.getState().setInitialStateOptions, I = h(t) || {};
15
15
  S(t, {
16
16
  ...I,
17
17
  ...c
18
18
  });
19
19
  }
20
- function $t({
20
+ function kt({
21
21
  stateKey: t,
22
22
  options: c,
23
23
  initialOptionsPart: h
24
24
  }) {
25
- const S = et(t) || {}, I = h[t] || {}, $ = o.getState().setInitialStateOptions, w = { ...I, ...S };
25
+ const S = tt(t) || {}, I = h[t] || {}, $ = o.getState().setInitialStateOptions, w = { ...I, ...S };
26
26
  let p = !1;
27
27
  if (c)
28
28
  for (const a in c)
@@ -30,12 +30,12 @@ function $t({
30
30
  !H(w[a], c[a]) && (p = !0, w[a] = c[a])) : (p = !0, w[a] = c[a]);
31
31
  p && $(t, w);
32
32
  }
33
- function ce(t, { formElements: c, validation: h }) {
33
+ function le(t, { formElements: c, validation: h }) {
34
34
  return { initialState: t, formElements: c, validation: h };
35
35
  }
36
- const le = (t, c) => {
36
+ const de = (t, c) => {
37
37
  let h = t;
38
- const [S, I] = Mt(h);
38
+ const [S, I] = jt(h);
39
39
  (Object.keys(I).length > 0 || c && Object.keys(c).length > 0) && Object.keys(I).forEach((p) => {
40
40
  I[p] = I[p] || {}, I[p].formElements = {
41
41
  ...c?.formElements,
@@ -43,16 +43,16 @@ const le = (t, c) => {
43
43
  ...c?.validation,
44
44
  ...I[p].formElements || {}
45
45
  // State-specific overrides
46
- }, et(p) || o.getState().setInitialStateOptions(p, I[p]);
46
+ }, tt(p) || o.getState().setInitialStateOptions(p, I[p]);
47
47
  }), o.getState().setInitialStates(S), o.getState().setCreatedState(S);
48
48
  const $ = (p, a) => {
49
- const [y] = Z(a?.componentId ?? It());
50
- $t({
49
+ const [y] = Z(a?.componentId ?? pt());
50
+ kt({
51
51
  stateKey: p,
52
52
  options: a,
53
53
  initialOptionsPart: I
54
54
  });
55
- const n = o.getState().cogsStateStore[p] || S[p], m = a?.modifyState ? a.modifyState(n) : n, [L, R] = qt(
55
+ const n = o.getState().cogsStateStore[p] || S[p], m = a?.modifyState ? a.modifyState(n) : n, [L, R] = Jt(
56
56
  m,
57
57
  {
58
58
  stateKey: p,
@@ -71,28 +71,28 @@ const le = (t, c) => {
71
71
  return R;
72
72
  };
73
73
  function w(p, a) {
74
- $t({ stateKey: p, options: a, initialOptionsPart: I }), a.localStorage && zt(p, a), ft(p);
74
+ kt({ stateKey: p, options: a, initialOptionsPart: I }), a.localStorage && Bt(p, a), ft(p);
75
75
  }
76
76
  return { useCogsState: $, setCogsOptions: w };
77
77
  }, {
78
78
  setUpdaterState: dt,
79
79
  setState: Q,
80
- getInitialOptions: et,
81
- getKeyState: Vt,
82
- getValidationErrors: Wt,
83
- setStateLog: Lt,
84
- updateInitialStateGlobal: pt,
85
- addValidationError: Gt,
86
- removeValidationError: B,
87
- setServerSyncActions: Ht
88
- } = o.getState(), kt = (t, c, h, S, I) => {
80
+ getInitialOptions: tt,
81
+ getKeyState: Ct,
82
+ getValidationErrors: Lt,
83
+ setStateLog: Gt,
84
+ updateInitialStateGlobal: wt,
85
+ addValidationError: Ht,
86
+ removeValidationError: q,
87
+ setServerSyncActions: zt
88
+ } = o.getState(), bt = (t, c, h, S, I) => {
89
89
  h?.log && console.log(
90
90
  "saving to localstorage",
91
91
  c,
92
92
  h.localStorage?.key,
93
93
  S
94
94
  );
95
- const $ = q(h?.localStorage?.key) ? h.localStorage?.key(t) : h?.localStorage?.key;
95
+ const $ = J(h?.localStorage?.key) ? h.localStorage?.key(t) : h?.localStorage?.key;
96
96
  if ($ && S) {
97
97
  const w = `${S}-${c}-${$}`;
98
98
  let p;
@@ -104,7 +104,7 @@ const le = (t, c) => {
104
104
  state: t,
105
105
  lastUpdated: Date.now(),
106
106
  lastSyncedWithServer: I ?? p
107
- }, y = Ut.serialize(a);
107
+ }, y = Ft.serialize(a);
108
108
  window.localStorage.setItem(
109
109
  w,
110
110
  JSON.stringify(y.json)
@@ -118,8 +118,8 @@ const le = (t, c) => {
118
118
  } catch (c) {
119
119
  return console.error("Error loading from localStorage:", c), null;
120
120
  }
121
- }, zt = (t, c) => {
122
- const h = o.getState().cogsStateStore[t], { sessionId: S } = Nt(), I = q(c?.localStorage?.key) ? c.localStorage.key(h) : c?.localStorage?.key;
121
+ }, Bt = (t, c) => {
122
+ const h = o.getState().cogsStateStore[t], { sessionId: S } = Vt(), I = J(c?.localStorage?.key) ? c.localStorage.key(h) : c?.localStorage?.key;
123
123
  if (I && S) {
124
124
  const $ = gt(
125
125
  `${S}-${t}-${I}`
@@ -128,7 +128,7 @@ const le = (t, c) => {
128
128
  return Q(t, $.state), ft(t), !0;
129
129
  }
130
130
  return !1;
131
- }, Ct = (t, c, h, S, I, $) => {
131
+ }, xt = (t, c, h, S, I, $) => {
132
132
  const w = {
133
133
  initialState: c,
134
134
  updaterState: ut(
@@ -139,7 +139,7 @@ const le = (t, c) => {
139
139
  ),
140
140
  state: h
141
141
  };
142
- pt(t, w.initialState), dt(t, w.updaterState), Q(t, w.state);
142
+ wt(t, w.initialState), dt(t, w.updaterState), Q(t, w.state);
143
143
  }, ft = (t) => {
144
144
  const c = o.getState().stateComponents.get(t);
145
145
  if (!c) return;
@@ -149,7 +149,7 @@ const le = (t, c) => {
149
149
  }), queueMicrotask(() => {
150
150
  h.forEach((S) => S());
151
151
  });
152
- }, de = (t, c) => {
152
+ }, ue = (t, c) => {
153
153
  const h = o.getState().stateComponents.get(t);
154
154
  if (h) {
155
155
  const S = `${t}////${c}`, I = h.components.get(S);
@@ -157,7 +157,7 @@ const le = (t, c) => {
157
157
  return;
158
158
  I && I.forceUpdate();
159
159
  }
160
- }, Bt = (t, c, h, S) => {
160
+ }, qt = (t, c, h, S) => {
161
161
  switch (t) {
162
162
  case "update":
163
163
  return {
@@ -180,7 +180,7 @@ const le = (t, c) => {
180
180
  return { oldValue: null, newValue: null };
181
181
  }
182
182
  };
183
- function qt(t, {
183
+ function Jt(t, {
184
184
  stateKey: c,
185
185
  serverSync: h,
186
186
  localStorage: S,
@@ -193,12 +193,12 @@ function qt(t, {
193
193
  dependencies: n,
194
194
  serverState: m
195
195
  } = {}) {
196
- const [L, R] = Z({}), { sessionId: U } = Nt();
196
+ const [L, R] = Z({}), { sessionId: U } = Vt();
197
197
  let G = !c;
198
- const [v] = Z(c ?? It()), l = o.getState().stateLog[v], st = X(/* @__PURE__ */ new Set()), J = X(p ?? It()), j = X(
198
+ const [v] = Z(c ?? pt()), l = o.getState().stateLog[v], at = X(/* @__PURE__ */ new Set()), Y = X(p ?? pt()), j = X(
199
199
  null
200
200
  );
201
- j.current = et(v) ?? null, ot(() => {
201
+ j.current = tt(v) ?? null, rt(() => {
202
202
  if (y && y.stateKey === v && y.path?.[0]) {
203
203
  Q(v, (r) => ({
204
204
  ...r,
@@ -210,27 +210,27 @@ function qt(t, {
210
210
  userId: y.userId
211
211
  });
212
212
  }
213
- }, [y]), ot(() => {
213
+ }, [y]), rt(() => {
214
214
  if (a) {
215
- At(v, {
215
+ $t(v, {
216
216
  initialState: a
217
217
  });
218
218
  const e = j.current, s = e?.serverState?.id !== void 0 && e?.serverState?.status === "success" && e?.serverState?.data, i = o.getState().initialStateGlobal[v];
219
219
  if (!(i && !H(i, a) || !i) && !s)
220
220
  return;
221
221
  let u = null;
222
- const E = q(e?.localStorage?.key) ? e?.localStorage?.key(a) : e?.localStorage?.key;
222
+ const E = J(e?.localStorage?.key) ? e?.localStorage?.key(a) : e?.localStorage?.key;
223
223
  E && U && (u = gt(`${U}-${v}-${E}`));
224
224
  let T = a, A = !1;
225
225
  const P = s ? Date.now() : 0, N = u?.lastUpdated || 0, V = u?.lastSyncedWithServer || 0;
226
- s && P > N ? (T = e.serverState.data, A = !0) : u && N > V && (T = u.state, e?.localStorage?.onChange && e?.localStorage?.onChange(T)), o.getState().initializeShadowState(v, a), Ct(
226
+ s && P > N ? (T = e.serverState.data, A = !0) : u && N > V && (T = u.state, e?.localStorage?.onChange && e?.localStorage?.onChange(T)), o.getState().initializeShadowState(v, a), xt(
227
227
  v,
228
228
  a,
229
229
  T,
230
- nt,
231
- J.current,
230
+ et,
231
+ Y.current,
232
232
  U
233
- ), A && E && U && kt(T, v, e, U, Date.now()), ft(v), (Array.isArray(w) ? w : [w || "component"]).includes("none") || R({});
233
+ ), A && E && U && bt(T, v, e, U, Date.now()), ft(v), (Array.isArray(w) ? w : [w || "component"]).includes("none") || R({});
234
234
  }
235
235
  }, [
236
236
  a,
@@ -238,14 +238,14 @@ function qt(t, {
238
238
  m?.data,
239
239
  ...n || []
240
240
  ]), lt(() => {
241
- G && At(v, {
241
+ G && $t(v, {
242
242
  serverSync: h,
243
243
  formElements: I,
244
244
  initialState: a,
245
245
  localStorage: S,
246
246
  middleware: j.current?.middleware
247
247
  });
248
- const e = `${v}////${J.current}`, r = o.getState().stateComponents.get(v) || {
248
+ const e = `${v}////${Y.current}`, r = o.getState().stateComponents.get(v) || {
249
249
  components: /* @__PURE__ */ new Map()
250
250
  };
251
251
  return r.components.set(e, {
@@ -258,14 +258,14 @@ function qt(t, {
258
258
  r && (r.components.delete(e), r.components.size === 0 && o.getState().stateComponents.delete(v));
259
259
  };
260
260
  }, []);
261
- const nt = (e, r, s, i) => {
261
+ const et = (e, r, s, i) => {
262
262
  if (Array.isArray(r)) {
263
263
  const u = `${v}-${r.join(".")}`;
264
- st.current.add(u);
264
+ at.current.add(u);
265
265
  }
266
266
  const g = o.getState();
267
267
  Q(v, (u) => {
268
- const E = q(e) ? e(u) : e, T = `${v}-${r.join(".")}`;
268
+ const E = J(e) ? e(u) : e, T = `${v}-${r.join(".")}`;
269
269
  if (T) {
270
270
  let M = !1, k = g.signalDomElements.get(T);
271
271
  if ((!k || k.size === 0) && (s.updateType === "insert" || s.updateType === "cut")) {
@@ -292,24 +292,24 @@ function qt(t, {
292
292
  });
293
293
  }
294
294
  }
295
- console.log("shadowState", g.shadowStateStore), s.updateType === "update" && (i || j.current?.validation?.key) && r && B(
295
+ console.log("shadowState", g.shadowStateStore), s.updateType === "update" && (i || j.current?.validation?.key) && r && q(
296
296
  (i || j.current?.validation?.key) + "." + r.join(".")
297
297
  );
298
298
  const A = r.slice(0, r.length - 1);
299
- s.updateType === "cut" && j.current?.validation?.key && B(
299
+ s.updateType === "cut" && j.current?.validation?.key && q(
300
300
  j.current?.validation?.key + "." + A.join(".")
301
- ), s.updateType === "insert" && j.current?.validation?.key && Wt(
301
+ ), s.updateType === "insert" && j.current?.validation?.key && Lt(
302
302
  j.current?.validation?.key + "." + A.join(".")
303
303
  ).filter(([k, O]) => {
304
304
  let W = k?.split(".").length;
305
305
  if (k == A.join(".") && W == A.length - 1) {
306
306
  let f = k + "." + A;
307
- B(k), Gt(f, O);
307
+ q(k), Ht(f, O);
308
308
  }
309
309
  });
310
310
  const P = g.stateComponents.get(v);
311
311
  if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", P), P) {
312
- const M = yt(u, E), k = new Set(M), O = s.updateType === "update" ? r.join(".") : r.slice(0, -1).join(".") || "";
312
+ const M = It(u, E), k = new Set(M), O = s.updateType === "update" ? r.join(".") : r.slice(0, -1).join(".") || "";
313
313
  for (const [
314
314
  W,
315
315
  f
@@ -329,19 +329,19 @@ function qt(t, {
329
329
  b = !0;
330
330
  break;
331
331
  }
332
- const Y = x.lastIndexOf(".");
333
- if (Y !== -1) {
334
- const K = x.substring(
332
+ const B = x.lastIndexOf(".");
333
+ if (B !== -1) {
334
+ const st = x.substring(
335
335
  0,
336
- Y
336
+ B
337
337
  );
338
338
  if (!isNaN(
339
- Number(x.substring(Y + 1))
340
- ) && f.paths.has(K)) {
339
+ Number(x.substring(B + 1))
340
+ ) && f.paths.has(st)) {
341
341
  b = !0;
342
342
  break;
343
343
  }
344
- x = K;
344
+ x = st;
345
345
  } else
346
346
  x = "";
347
347
  if (x === "")
@@ -363,7 +363,7 @@ function qt(t, {
363
363
  const O = r.slice(0, -1), W = z(E, O);
364
364
  return k === r.length - 1 && ["insert", "cut"].includes(s.updateType) ? (W.length - 1).toString() : M;
365
365
  });
366
- const { oldValue: V, newValue: D } = Bt(
366
+ const { oldValue: V, newValue: D } = qt(
367
367
  s.updateType,
368
368
  u,
369
369
  E,
@@ -390,13 +390,13 @@ function qt(t, {
390
390
  g.removeShadowArrayElement(v, k, O);
391
391
  break;
392
392
  }
393
- if (Lt(v, (M) => {
393
+ if (Gt(v, (M) => {
394
394
  const O = [...M ?? [], F].reduce((W, f) => {
395
395
  const b = `${f.stateKey}:${JSON.stringify(f.path)}`, C = W.get(b);
396
396
  return C ? (C.timeStamp = Math.max(C.timeStamp, f.timeStamp), C.newValue = f.newValue, C.oldValue = C.oldValue ?? f.oldValue, C.updateType = f.updateType) : W.set(b, { ...f }), W;
397
397
  }, /* @__PURE__ */ new Map());
398
398
  return Array.from(O.values());
399
- }), kt(
399
+ }), bt(
400
400
  E,
401
401
  v,
402
402
  j.current,
@@ -406,7 +406,7 @@ function qt(t, {
406
406
  update: F
407
407
  }), j.current?.serverSync) {
408
408
  const M = g.serverState[v], k = j.current?.serverSync;
409
- Ht(v, {
409
+ zt(v, {
410
410
  syncKey: typeof k.syncKey == "string" ? k.syncKey : k.syncKey({ state: E }),
411
411
  rollBackState: M,
412
412
  actionTimeStamp: Date.now() + (k.debounce ?? 3e3),
@@ -420,18 +420,18 @@ function qt(t, {
420
420
  v,
421
421
  ut(
422
422
  v,
423
- nt,
424
- J.current,
423
+ et,
424
+ Y.current,
425
425
  U
426
426
  )
427
- ), o.getState().cogsStateStore[v] || Q(v, t), o.getState().initialStateGlobal[v] || pt(v, t));
428
- const d = vt(() => ut(
427
+ ), o.getState().cogsStateStore[v] || Q(v, t), o.getState().initialStateGlobal[v] || wt(v, t));
428
+ const d = yt(() => ut(
429
429
  v,
430
- nt,
431
- J.current,
430
+ et,
431
+ Y.current,
432
432
  U
433
433
  ), [v, U]);
434
- return [Vt(v), d];
434
+ return [Ct(v), d];
435
435
  }
436
436
  function ut(t, c, h, S) {
437
437
  const I = /* @__PURE__ */ new Map();
@@ -443,14 +443,14 @@ function ut(t, c, h, S) {
443
443
  $++;
444
444
  }, p = {
445
445
  removeValidation: (y) => {
446
- y?.validationKey && B(y.validationKey);
446
+ y?.validationKey && q(y.validationKey);
447
447
  },
448
448
  revertToInitialState: (y) => {
449
449
  const n = o.getState().getInitialOptions(t)?.validation;
450
- n?.key && B(n?.key), y?.validationKey && B(y.validationKey);
450
+ n?.key && q(n?.key), y?.validationKey && q(y.validationKey);
451
451
  const m = o.getState().initialStateGlobal[t];
452
452
  o.getState().clearSelectedIndexesForState(t), I.clear(), $++;
453
- const L = a(m, []), R = et(t), U = q(R?.localStorage?.key) ? R?.localStorage?.key(m) : R?.localStorage?.key, G = `${S}-${t}-${U}`;
453
+ const L = a(m, []), R = tt(t), U = J(R?.localStorage?.key) ? R?.localStorage?.key(m) : R?.localStorage?.key, G = `${S}-${t}-${U}`;
454
454
  G && localStorage.removeItem(G), dt(t, L), Q(t, m);
455
455
  const v = o.getState().stateComponents.get(t);
456
456
  return v && v.components.forEach((l) => {
@@ -464,9 +464,9 @@ function ut(t, c, h, S) {
464
464
  c,
465
465
  h,
466
466
  S
467
- ), m = o.getState().initialStateGlobal[t], L = et(t), R = q(L?.localStorage?.key) ? L?.localStorage?.key(m) : L?.localStorage?.key, U = `${S}-${t}-${R}`;
468
- return localStorage.getItem(U) && localStorage.removeItem(U), _t(() => {
469
- pt(t, y), o.getState().initializeShadowState(t, y), dt(t, n), Q(t, y);
467
+ ), m = o.getState().initialStateGlobal[t], L = tt(t), R = J(L?.localStorage?.key) ? L?.localStorage?.key(m) : L?.localStorage?.key, U = `${S}-${t}-${R}`;
468
+ return localStorage.getItem(U) && localStorage.removeItem(U), Mt(() => {
469
+ wt(t, y), o.getState().initializeShadowState(t, y), dt(t, n), Q(t, y);
470
470
  const G = o.getState().stateComponents.get(t);
471
471
  G && G.components.forEach((v) => {
472
472
  v.forceUpdate();
@@ -480,7 +480,7 @@ function ut(t, c, h, S) {
480
480
  _isLoading: o.getState().isLoadingGlobal[t],
481
481
  _isServerSynced: () => {
482
482
  const y = o.getState().serverState[t];
483
- return !!(y && H(y, Vt(t)));
483
+ return !!(y && H(y, Ct(t)));
484
484
  }
485
485
  };
486
486
  function a(y, n = [], m) {
@@ -493,14 +493,14 @@ function ut(t, c, h, S) {
493
493
  R[v] = p[v];
494
494
  });
495
495
  const U = {
496
- apply(v, l, st) {
496
+ apply(v, l, at) {
497
497
  return console.log(
498
498
  `PROXY APPLY TRAP HIT: stateKey=${t}, path=${n.join(".")}`
499
499
  ), console.trace("Apply trap stack trace"), o().getNestedState(t, n);
500
500
  },
501
501
  get(v, l) {
502
502
  m?.validIndices && !Array.isArray(y) && (m = { ...m, validIndices: void 0 });
503
- const st = /* @__PURE__ */ new Set([
503
+ const at = /* @__PURE__ */ new Set([
504
504
  "insert",
505
505
  "cut",
506
506
  "cutByValue",
@@ -523,7 +523,7 @@ function ut(t, c, h, S) {
523
523
  "_stateKey",
524
524
  "getComponents"
525
525
  ]);
526
- if (l !== "then" && !l.startsWith("$") && l !== "stateMapNoRender" && !st.has(l)) {
526
+ if (l !== "then" && !l.startsWith("$") && l !== "stateMapNoRender" && !at.has(l)) {
527
527
  const d = `${t}////${h}`, e = o.getState().stateComponents.get(t);
528
528
  if (e) {
529
529
  const r = e.components.get(d);
@@ -540,7 +540,7 @@ function ut(t, c, h, S) {
540
540
  }
541
541
  }
542
542
  if (l === "getDifferences")
543
- return () => yt(
543
+ return () => It(
544
544
  o.getState().cogsStateStore[t],
545
545
  o.getState().initialStateGlobal[t]
546
546
  );
@@ -581,7 +581,7 @@ function ut(t, c, h, S) {
581
581
  };
582
582
  if (l === "removeStorage")
583
583
  return () => {
584
- const d = o.getState().initialStateGlobal[t], e = et(t), r = q(e?.localStorage?.key) ? e?.localStorage?.key(d) : e?.localStorage?.key, s = `${S}-${t}-${r}`;
584
+ const d = o.getState().initialStateGlobal[t], e = tt(t), r = J(e?.localStorage?.key) ? e?.localStorage?.key(d) : e?.localStorage?.key, s = `${S}-${t}-${r}`;
585
585
  s && localStorage.removeItem(s);
586
586
  };
587
587
  if (l === "showValidationErrors")
@@ -625,13 +625,13 @@ function ut(t, c, h, S) {
625
625
  startIndex: 0,
626
626
  endIndex: 10
627
627
  }), T = X(i), [A, P] = Z(0);
628
- ot(() => o.getState().subscribeToShadowState(t, () => {
628
+ rt(() => o.getState().subscribeToShadowState(t, () => {
629
629
  P((b) => b + 1);
630
630
  }), [t]);
631
631
  const N = o().getNestedState(
632
632
  t,
633
633
  n
634
- ), V = N.length, { totalHeight: D, positions: F } = vt(() => {
634
+ ), V = N.length, { totalHeight: D, positions: F } = yt(() => {
635
635
  const f = o.getState().getShadowMetadata(t, n) || [];
636
636
  let b = 0;
637
637
  const C = [];
@@ -647,10 +647,10 @@ function ut(t, c, h, S) {
647
647
  n.join("."),
648
648
  r,
649
649
  A
650
- ]), M = vt(() => {
650
+ ]), M = yt(() => {
651
651
  const f = Math.max(0, u.startIndex), b = Math.min(V, u.endIndex), C = Array.from(
652
652
  { length: b - f },
653
- (x, Y) => f + Y
653
+ (x, B) => f + B
654
654
  ), _ = C.map((x) => N[x]);
655
655
  return a(_, n, {
656
656
  ...m,
@@ -662,28 +662,41 @@ function ut(t, c, h, S) {
662
662
  if (!f) return;
663
663
  const b = f.scrollHeight - f.scrollTop - f.clientHeight < r, C = () => {
664
664
  if (!f) return;
665
- const { scrollTop: x, clientHeight: Y } = f;
666
- let K = 0, it = V - 1;
667
- for (; K <= it; ) {
668
- const St = Math.floor((K + it) / 2);
669
- F[St] < x ? K = St + 1 : it = St - 1;
665
+ const { scrollTop: B, clientHeight: st } = f;
666
+ let St = 0, it = V - 1;
667
+ for (; St <= it; ) {
668
+ const mt = Math.floor((St + it) / 2);
669
+ F[mt] < B ? St = mt + 1 : it = mt - 1;
670
670
  }
671
- const wt = Math.max(0, it - s);
672
- let tt = wt;
673
- const xt = x + Y;
674
- for (; tt < V && F[tt] < xt; )
675
- tt++;
676
- tt = Math.min(V, tt + s), E({ startIndex: wt, endIndex: tt });
671
+ const Tt = Math.max(0, it - s);
672
+ let K = Tt;
673
+ const Pt = B + st;
674
+ for (; K < V && F[K] < Pt; )
675
+ K++;
676
+ K = Math.min(V, K + s), E({ startIndex: Tt, endIndex: K });
677
677
  }, _ = () => {
678
- T.current = f.scrollHeight - f.scrollTop - f.clientHeight < 1, C();
678
+ T.current = f.scrollHeight - f.scrollTop - f.clientHeight < r, C();
679
679
  };
680
- return f.addEventListener("scroll", _, {
680
+ f.addEventListener("scroll", _, {
681
681
  passive: !0
682
- }), i && (T.current || b) && (f.scrollTop = f.scrollHeight), C(), () => {
683
- f.removeEventListener("scroll", _);
682
+ });
683
+ let x;
684
+ return i && (T.current || b) && (x = setTimeout(() => {
685
+ f.scrollTo({
686
+ top: f.scrollHeight,
687
+ behavior: "smooth"
688
+ });
689
+ }, 0)), C(), () => {
690
+ f.removeEventListener("scroll", _), x && clearTimeout(x);
684
691
  };
685
- }, [V, F, D, i]);
686
- const k = Tt(
692
+ }, [
693
+ V,
694
+ F,
695
+ D,
696
+ i,
697
+ r
698
+ ]);
699
+ const k = Et(
687
700
  (f = "smooth") => {
688
701
  g.current && (T.current = !0, g.current.scrollTo({
689
702
  top: g.current.scrollHeight,
@@ -691,7 +704,7 @@ function ut(t, c, h, S) {
691
704
  }));
692
705
  },
693
706
  []
694
- ), O = Tt(
707
+ ), O = Et(
695
708
  (f, b = "smooth") => {
696
709
  g.current && F[f] !== void 0 && (T.current = !1, g.current.scrollTo({
697
710
  top: F[f],
@@ -790,7 +803,7 @@ function ut(t, c, h, S) {
790
803
  );
791
804
  });
792
805
  if (l === "$stateMap")
793
- return (e) => at(Jt, {
806
+ return (e) => ot(Yt, {
794
807
  proxy: {
795
808
  _stateKey: t,
796
809
  _path: n,
@@ -804,7 +817,7 @@ function ut(t, c, h, S) {
804
817
  const r = o.getState().getNestedState(t, n);
805
818
  return Array.isArray(r) ? (m?.validIndices || Array.from({ length: r.length }, (i, g) => g)).map((i, g) => {
806
819
  const u = r[i], E = [...n, i.toString()], T = a(u, E, m), A = `${h}-${n.join(".")}-${i}`;
807
- return at(Zt, {
820
+ return ot(Xt, {
808
821
  key: i,
809
822
  stateKey: t,
810
823
  itemComponentId: A,
@@ -847,13 +860,13 @@ function ut(t, c, h, S) {
847
860
  return a(s, i);
848
861
  };
849
862
  if (l === "insert")
850
- return (e) => (w(n), mt(c, e, n, t), a(
863
+ return (e) => (w(n), ht(c, e, n, t), a(
851
864
  o.getState().getNestedState(t, n),
852
865
  n
853
866
  ));
854
867
  if (l === "uniqueInsert")
855
868
  return (e, r, s) => {
856
- const i = o.getState().getNestedState(t, n), g = q(e) ? e(i) : e;
869
+ const i = o.getState().getNestedState(t, n), g = J(e) ? e(i) : e;
857
870
  let u = null;
858
871
  if (!i.some((T) => {
859
872
  if (r) {
@@ -865,12 +878,12 @@ function ut(t, c, h, S) {
865
878
  const A = H(T, g);
866
879
  return A && (u = T), A;
867
880
  }))
868
- w(n), mt(c, g, n, t);
881
+ w(n), ht(c, g, n, t);
869
882
  else if (s && u) {
870
883
  const T = s(u), A = i.map(
871
884
  (P) => H(P, u) ? T : P
872
885
  );
873
- w(n), rt(c, A, n);
886
+ w(n), nt(c, A, n);
874
887
  }
875
888
  };
876
889
  if (l === "cut")
@@ -889,7 +902,7 @@ function ut(t, c, h, S) {
889
902
  if (l === "toggleByValue")
890
903
  return (e) => {
891
904
  const r = y.findIndex((s) => s === e);
892
- r > -1 ? ct(c, n, t, r) : mt(c, e, n, t);
905
+ r > -1 ? ct(c, n, t, r) : ht(c, e, n, t);
893
906
  };
894
907
  if (l === "stateFind")
895
908
  return (e) => {
@@ -910,15 +923,15 @@ function ut(t, c, h, S) {
910
923
  return a(i.item, g, m);
911
924
  };
912
925
  }
913
- const J = n[n.length - 1];
914
- if (!isNaN(Number(J))) {
926
+ const Y = n[n.length - 1];
927
+ if (!isNaN(Number(Y))) {
915
928
  const d = n.slice(0, -1), e = o.getState().getNestedState(t, d);
916
929
  if (Array.isArray(e) && l === "cut")
917
930
  return () => ct(
918
931
  c,
919
932
  d,
920
933
  t,
921
- Number(J)
934
+ Number(Y)
922
935
  );
923
936
  }
924
937
  if (l === "get")
@@ -930,13 +943,13 @@ function ut(t, c, h, S) {
930
943
  return o.getState().getNestedState(t, n);
931
944
  };
932
945
  if (l === "$derive")
933
- return (d) => bt({
946
+ return (d) => Nt({
934
947
  _stateKey: t,
935
948
  _path: n,
936
949
  _effect: d.toString()
937
950
  });
938
951
  if (l === "$get")
939
- return () => bt({
952
+ return () => Nt({
940
953
  _stateKey: t,
941
954
  _path: n
942
955
  });
@@ -955,7 +968,7 @@ function ut(t, c, h, S) {
955
968
  const e = n.slice(0, -1), r = Number(n[n.length - 1]), s = e.join(".");
956
969
  d ? o.getState().setSelectedIndex(t, s, r) : o.getState().setSelectedIndex(t, s, void 0);
957
970
  const i = o.getState().getNestedState(t, [...e]);
958
- rt(c, i, e), w(e);
971
+ nt(c, i, e), w(e);
959
972
  };
960
973
  if (l === "toggleSelected")
961
974
  return () => {
@@ -966,13 +979,13 @@ function ut(t, c, h, S) {
966
979
  s === e ? void 0 : e
967
980
  );
968
981
  const i = o.getState().getNestedState(t, [...d]);
969
- rt(c, i, d), w(d);
982
+ nt(c, i, d), w(d);
970
983
  };
971
984
  if (n.length == 0) {
972
985
  if (l === "applyJsonPatch")
973
986
  return (d) => {
974
- const e = o.getState().cogsStateStore[t], s = Ft(e, d).newDocument;
975
- Ct(
987
+ const e = o.getState().cogsStateStore[t], s = Dt(e, d).newDocument;
988
+ xt(
976
989
  t,
977
990
  o.getState().initialStateGlobal[t],
978
991
  s,
@@ -982,7 +995,7 @@ function ut(t, c, h, S) {
982
995
  );
983
996
  const i = o.getState().stateComponents.get(t);
984
997
  if (i) {
985
- const g = yt(e, s), u = new Set(g);
998
+ const g = It(e, s), u = new Set(g);
986
999
  for (const [
987
1000
  E,
988
1001
  T
@@ -1044,12 +1057,12 @@ function ut(t, c, h, S) {
1044
1057
  throw new Error("Zod schema not found");
1045
1058
  if (!d?.key)
1046
1059
  throw new Error("Validation key not found");
1047
- B(d.key);
1060
+ q(d.key);
1048
1061
  const r = o.getState().cogsStateStore[t];
1049
1062
  try {
1050
1063
  const s = o.getState().getValidationErrors(d.key);
1051
1064
  s && s.length > 0 && s.forEach(([g]) => {
1052
- g && g.startsWith(d.key) && B(g);
1065
+ g && g.startsWith(d.key) && q(g);
1053
1066
  });
1054
1067
  const i = d.zodSchema.safeParse(r);
1055
1068
  return i.success ? !0 : (i.error.errors.forEach((u) => {
@@ -1064,7 +1077,7 @@ function ut(t, c, h, S) {
1064
1077
  if (l === "getComponents")
1065
1078
  return () => o().stateComponents.get(t);
1066
1079
  if (l === "getAllFormRefs")
1067
- return () => Et.getState().getFormRefsByStateKey(t);
1080
+ return () => At.getState().getFormRefsByStateKey(t);
1068
1081
  if (l === "_initialState")
1069
1082
  return o.getState().initialStateGlobal[t];
1070
1083
  if (l === "_serverState")
@@ -1077,13 +1090,13 @@ function ut(t, c, h, S) {
1077
1090
  if (l === "removeValidation") return p.removeValidation;
1078
1091
  }
1079
1092
  if (l === "getFormRef")
1080
- return () => Et.getState().getFormRef(t + "." + n.join("."));
1093
+ return () => At.getState().getFormRef(t + "." + n.join("."));
1081
1094
  if (l === "validationWrapper")
1082
1095
  return ({
1083
1096
  children: d,
1084
1097
  hideMessage: e
1085
- }) => /* @__PURE__ */ ht(
1086
- Ot,
1098
+ }) => /* @__PURE__ */ vt(
1099
+ Rt,
1087
1100
  {
1088
1101
  formOpts: e ? { validation: { message: "" } } : void 0,
1089
1102
  path: n,
@@ -1099,21 +1112,21 @@ function ut(t, c, h, S) {
1099
1112
  if (l === "update")
1100
1113
  return (d, e) => {
1101
1114
  if (e?.debounce)
1102
- jt(() => {
1103
- rt(c, d, n, "");
1115
+ Ot(() => {
1116
+ nt(c, d, n, "");
1104
1117
  const r = o.getState().getNestedState(t, n);
1105
1118
  e?.afterUpdate && e.afterUpdate(r);
1106
1119
  }, e.debounce);
1107
1120
  else {
1108
- rt(c, d, n, "");
1121
+ nt(c, d, n, "");
1109
1122
  const r = o.getState().getNestedState(t, n);
1110
1123
  e?.afterUpdate && e.afterUpdate(r);
1111
1124
  }
1112
1125
  w(n);
1113
1126
  };
1114
1127
  if (l === "formElement")
1115
- return (d, e) => /* @__PURE__ */ ht(
1116
- Rt,
1128
+ return (d, e) => /* @__PURE__ */ vt(
1129
+ Ut,
1117
1130
  {
1118
1131
  setState: c,
1119
1132
  stateKey: t,
@@ -1122,8 +1135,8 @@ function ut(t, c, h, S) {
1122
1135
  formOpts: e
1123
1136
  }
1124
1137
  );
1125
- const j = [...n, l], nt = o.getState().getNestedState(t, j);
1126
- return a(nt, j, m);
1138
+ const j = [...n, l], et = o.getState().getNestedState(t, j);
1139
+ return a(et, j, m);
1127
1140
  }
1128
1141
  }, G = new Proxy(R, U);
1129
1142
  return I.set(L, {
@@ -1135,10 +1148,10 @@ function ut(t, c, h, S) {
1135
1148
  o.getState().getNestedState(t, [])
1136
1149
  );
1137
1150
  }
1138
- function bt(t) {
1139
- return at(Yt, { proxy: t });
1151
+ function Nt(t) {
1152
+ return ot(Zt, { proxy: t });
1140
1153
  }
1141
- function Jt({
1154
+ function Yt({
1142
1155
  proxy: t,
1143
1156
  rebuildStateShape: c
1144
1157
  }) {
@@ -1150,11 +1163,11 @@ function Jt({
1150
1163
  (I, $, w, p, a) => t._mapFn(I, $, w, p, a)
1151
1164
  ) : null;
1152
1165
  }
1153
- function Yt({
1166
+ function Zt({
1154
1167
  proxy: t
1155
1168
  }) {
1156
1169
  const c = X(null), h = `${t._stateKey}-${t._path.join(".")}`;
1157
- return ot(() => {
1170
+ return rt(() => {
1158
1171
  const S = c.current;
1159
1172
  if (!S || !S.parentElement) return;
1160
1173
  const I = S.parentElement, w = Array.from(I.childNodes).indexOf(S);
@@ -1183,14 +1196,14 @@ function Yt({
1183
1196
  m !== null && typeof m == "object" && (m = JSON.stringify(m));
1184
1197
  const L = document.createTextNode(String(m));
1185
1198
  S.replaceWith(L);
1186
- }, [t._stateKey, t._path.join("."), t._effect]), at("span", {
1199
+ }, [t._stateKey, t._path.join("."), t._effect]), ot("span", {
1187
1200
  ref: c,
1188
1201
  style: { display: "none" },
1189
1202
  "data-signal-id": h
1190
1203
  });
1191
1204
  }
1192
- function ue(t) {
1193
- const c = Pt(
1205
+ function ge(t) {
1206
+ const c = _t(
1194
1207
  (h) => {
1195
1208
  const S = o.getState().stateComponents.get(t._stateKey) || {
1196
1209
  components: /* @__PURE__ */ new Map()
@@ -1202,16 +1215,16 @@ function ue(t) {
1202
1215
  },
1203
1216
  () => o.getState().getNestedState(t._stateKey, t._path)
1204
1217
  );
1205
- return at("text", {}, String(c));
1218
+ return ot("text", {}, String(c));
1206
1219
  }
1207
- function Zt({
1220
+ function Xt({
1208
1221
  stateKey: t,
1209
1222
  itemComponentId: c,
1210
1223
  itemPath: h,
1211
1224
  children: S
1212
1225
  }) {
1213
- const [, I] = Z({}), [$, w] = Dt(), p = X(null);
1214
- return ot(() => {
1226
+ const [, I] = Z({}), [$, w] = Wt(), p = X(null);
1227
+ return rt(() => {
1215
1228
  w.height > 0 && w.height !== p.current && (p.current = w.height, o.getState().setShadowMetadata(t, h, {
1216
1229
  virtualizer: {
1217
1230
  itemHeight: w.height
@@ -1228,14 +1241,14 @@ function Zt({
1228
1241
  const n = o.getState().stateComponents.get(t);
1229
1242
  n && n.components.delete(a);
1230
1243
  };
1231
- }, [t, c, h.join(".")]), /* @__PURE__ */ ht("div", { ref: $, children: S });
1244
+ }, [t, c, h.join(".")]), /* @__PURE__ */ vt("div", { ref: $, children: S });
1232
1245
  }
1233
1246
  export {
1234
- bt as $cogsSignal,
1235
- ue as $cogsSignalStore,
1236
- ce as addStateOptions,
1237
- le as createCogsState,
1238
- de as notifyComponent,
1239
- qt as useCogsStateFn
1247
+ Nt as $cogsSignal,
1248
+ ge as $cogsSignalStore,
1249
+ le as addStateOptions,
1250
+ de as createCogsState,
1251
+ ue as notifyComponent,
1252
+ Jt as useCogsStateFn
1240
1253
  };
1241
1254
  //# sourceMappingURL=CogsState.jsx.map