cogsbox-state 0.5.408 → 0.5.410

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,23 +1,23 @@
1
1
  "use client";
2
2
  import { jsx as It } from "react/jsx-runtime";
3
- import { useState as et, useRef as Q, useEffect as nt, useLayoutEffect as Et, useMemo as pt, createElement as ct, useSyncExternalStore as Ot, startTransition as Ut, useCallback as $t } from "react";
4
- import { transformStateFunc as Ft, isDeepEqual as J, isFunction as K, getNestedValue as Y, getDifferences as wt, debounce as Dt } from "./utility.js";
5
- import { pushFunc as yt, updateFn as it, cutFunc as gt, ValidationWrapper as Wt, FormControlComponent as Lt } from "./Functions.jsx";
6
- import Gt from "superjson";
3
+ import { useState as et, useRef as Q, useEffect as nt, useLayoutEffect as Et, useMemo as pt, createElement as ct, useSyncExternalStore as jt, startTransition as Rt, useCallback as kt } from "react";
4
+ import { transformStateFunc as Ot, isDeepEqual as q, isFunction as K, getNestedValue as J, getDifferences as wt, debounce as Ut } from "./utility.js";
5
+ import { pushFunc as yt, updateFn as it, cutFunc as gt, ValidationWrapper as Ft, FormControlComponent as Dt } from "./Functions.jsx";
6
+ import Wt from "superjson";
7
7
  import { v4 as Tt } from "uuid";
8
8
  import "zod";
9
- import { getGlobalStore as o, formRefStore as bt } from "./store.js";
10
- import { useCogsConfig as Pt } from "./CogsStateClient.jsx";
11
- import { applyPatch as Ht } from "fast-json-patch";
9
+ import { getGlobalStore as o, formRefStore as $t } from "./store.js";
10
+ import { useCogsConfig as Ct } from "./CogsStateClient.jsx";
11
+ import { applyPatch as Lt } from "fast-json-patch";
12
12
  import Bt from "react-use-measure";
13
- function xt(t, c) {
13
+ function bt(t, c) {
14
14
  const S = o.getState().getInitialOptions, g = o.getState().setInitialStateOptions, y = S(t) || {};
15
15
  g(t, {
16
16
  ...y,
17
17
  ...c
18
18
  });
19
19
  }
20
- function Vt({
20
+ function xt({
21
21
  stateKey: t,
22
22
  options: c,
23
23
  initialOptionsPart: S
@@ -27,15 +27,15 @@ function Vt({
27
27
  if (c)
28
28
  for (const s in c)
29
29
  w.hasOwnProperty(s) ? (s == "localStorage" && c[s] && w[s].key !== c[s]?.key && (I = !0, w[s] = c[s]), s == "initialState" && c[s] && w[s] !== c[s] && // Different references
30
- !J(w[s], c[s]) && (I = !0, w[s] = c[s])) : (I = !0, w[s] = c[s]);
30
+ !q(w[s], c[s]) && (I = !0, w[s] = c[s])) : (I = !0, w[s] = c[s]);
31
31
  I && x(t, w);
32
32
  }
33
- function ge(t, { formElements: c, validation: S }) {
33
+ function ue(t, { formElements: c, validation: S }) {
34
34
  return { initialState: t, formElements: c, validation: S };
35
35
  }
36
- const fe = (t, c) => {
36
+ const ge = (t, c) => {
37
37
  let S = t;
38
- const [g, y] = Ft(S);
38
+ const [g, y] = Ot(S);
39
39
  (Object.keys(y).length > 0 || c && Object.keys(c).length > 0) && Object.keys(y).forEach((I) => {
40
40
  y[I] = y[I] || {}, y[I].formElements = {
41
41
  ...c?.formElements,
@@ -47,12 +47,12 @@ const fe = (t, c) => {
47
47
  }), o.getState().setInitialStates(g), o.getState().setCreatedState(g);
48
48
  const x = (I, s) => {
49
49
  const [h] = et(s?.componentId ?? Tt());
50
- Vt({
50
+ xt({
51
51
  stateKey: I,
52
52
  options: s,
53
53
  initialOptionsPart: y
54
54
  });
55
- const r = o.getState().cogsStateStore[I] || g[I], f = s?.modifyState ? s.modifyState(r) : r, [H, U] = Xt(
55
+ const r = o.getState().cogsStateStore[I] || g[I], f = s?.modifyState ? s.modifyState(r) : r, [B, O] = Yt(
56
56
  f,
57
57
  {
58
58
  stateKey: I,
@@ -68,24 +68,24 @@ const fe = (t, c) => {
68
68
  serverState: s?.serverState
69
69
  }
70
70
  );
71
- return U;
71
+ return O;
72
72
  };
73
73
  function w(I, s) {
74
- Vt({ stateKey: I, options: s, initialOptionsPart: y }), s.localStorage && Yt(I, s), lt(I);
74
+ xt({ stateKey: I, options: s, initialOptionsPart: y }), s.localStorage && qt(I, s), lt(I);
75
75
  }
76
76
  return { useCogsState: x, setCogsOptions: w };
77
77
  }, {
78
78
  setUpdaterState: ft,
79
79
  setState: rt,
80
80
  getInitialOptions: at,
81
- getKeyState: _t,
82
- getValidationErrors: zt,
83
- setStateLog: qt,
81
+ getKeyState: Pt,
82
+ getValidationErrors: Gt,
83
+ setStateLog: Ht,
84
84
  updateInitialStateGlobal: At,
85
- addValidationError: Mt,
85
+ addValidationError: _t,
86
86
  removeValidationError: X,
87
- setServerSyncActions: Jt
88
- } = o.getState(), Nt = (t, c, S, g, y) => {
87
+ setServerSyncActions: zt
88
+ } = o.getState(), Vt = (t, c, S, g, y) => {
89
89
  S?.log && console.log(
90
90
  "saving to localstorage",
91
91
  c,
@@ -104,7 +104,7 @@ const fe = (t, c) => {
104
104
  state: t,
105
105
  lastUpdated: Date.now(),
106
106
  lastSyncedWithServer: y ?? I
107
- }, h = Gt.serialize(s);
107
+ }, h = Wt.serialize(s);
108
108
  window.localStorage.setItem(
109
109
  w,
110
110
  JSON.stringify(h.json)
@@ -118,8 +118,8 @@ const fe = (t, c) => {
118
118
  } catch (c) {
119
119
  return console.error("Error loading from localStorage:", c), null;
120
120
  }
121
- }, Yt = (t, c) => {
122
- const S = o.getState().cogsStateStore[t], { sessionId: g } = Pt(), y = K(c?.localStorage?.key) ? c.localStorage.key(S) : c?.localStorage?.key;
121
+ }, qt = (t, c) => {
122
+ const S = o.getState().cogsStateStore[t], { sessionId: g } = Ct(), y = K(c?.localStorage?.key) ? c.localStorage.key(S) : c?.localStorage?.key;
123
123
  if (y && g) {
124
124
  const x = mt(
125
125
  `${g}-${t}-${y}`
@@ -128,7 +128,7 @@ const fe = (t, c) => {
128
128
  return rt(t, x.state), lt(t), !0;
129
129
  }
130
130
  return !1;
131
- }, jt = (t, c, S, g, y, x) => {
131
+ }, Mt = (t, c, S, g, y, x) => {
132
132
  const w = {
133
133
  initialState: c,
134
134
  updaterState: St(
@@ -149,7 +149,7 @@ const fe = (t, c) => {
149
149
  }), queueMicrotask(() => {
150
150
  S.forEach((g) => g());
151
151
  });
152
- }, Se = (t, c) => {
152
+ }, fe = (t, c) => {
153
153
  const S = o.getState().stateComponents.get(t);
154
154
  if (S) {
155
155
  const g = `${t}////${c}`, y = S.components.get(g);
@@ -157,22 +157,22 @@ const fe = (t, c) => {
157
157
  return;
158
158
  y && y.forceUpdate();
159
159
  }
160
- }, Zt = (t, c, S, g) => {
160
+ }, Jt = (t, c, S, g) => {
161
161
  switch (t) {
162
162
  case "update":
163
163
  return {
164
- oldValue: Y(c, g),
165
- newValue: Y(S, g)
164
+ oldValue: J(c, g),
165
+ newValue: J(S, g)
166
166
  };
167
167
  case "insert":
168
168
  return {
169
169
  oldValue: null,
170
170
  // or undefined
171
- newValue: Y(S, g)
171
+ newValue: J(S, g)
172
172
  };
173
173
  case "cut":
174
174
  return {
175
- oldValue: Y(c, g),
175
+ oldValue: J(c, g),
176
176
  newValue: null
177
177
  // or undefined
178
178
  };
@@ -180,7 +180,7 @@ const fe = (t, c) => {
180
180
  return { oldValue: null, newValue: null };
181
181
  }
182
182
  };
183
- function Xt(t, {
183
+ function Yt(t, {
184
184
  stateKey: c,
185
185
  serverSync: S,
186
186
  localStorage: g,
@@ -193,8 +193,8 @@ function Xt(t, {
193
193
  dependencies: r,
194
194
  serverState: f
195
195
  } = {}) {
196
- const [H, U] = et({}), { sessionId: F } = Pt();
197
- let B = !c;
196
+ const [B, O] = et({}), { sessionId: U } = Ct();
197
+ let G = !c;
198
198
  const [m] = et(c ?? Tt()), l = o.getState().stateLog[m], dt = Q(/* @__PURE__ */ new Set()), tt = Q(I ?? Tt()), j = Q(
199
199
  null
200
200
  );
@@ -212,25 +212,25 @@ function Xt(t, {
212
212
  }
213
213
  }, [h]), nt(() => {
214
214
  if (s) {
215
- xt(m, {
215
+ bt(m, {
216
216
  initialState: s
217
217
  });
218
218
  const e = j.current, a = e?.serverState?.id !== void 0 && e?.serverState?.status === "success" && e?.serverState?.data, i = o.getState().initialStateGlobal[m];
219
- if (!(i && !J(i, s) || !i) && !a)
219
+ if (!(i && !q(i, s) || !i) && !a)
220
220
  return;
221
221
  let u = null;
222
222
  const p = K(e?.localStorage?.key) ? e?.localStorage?.key(s) : e?.localStorage?.key;
223
- p && F && (u = mt(`${F}-${m}-${p}`));
223
+ p && U && (u = mt(`${U}-${m}-${p}`));
224
224
  let T = s, A = !1;
225
- const _ = a ? Date.now() : 0, $ = u?.lastUpdated || 0, M = u?.lastSyncedWithServer || 0;
226
- a && _ > $ ? (T = e.serverState.data, A = !0) : u && $ > M && (T = u.state, e?.localStorage?.onChange && e?.localStorage?.onChange(T)), o.getState().initializeShadowState(m, s), jt(
225
+ const _ = a ? Date.now() : 0, k = u?.lastUpdated || 0, M = u?.lastSyncedWithServer || 0;
226
+ a && _ > k ? (T = e.serverState.data, A = !0) : u && k > M && (T = u.state, e?.localStorage?.onChange && e?.localStorage?.onChange(T)), o.getState().initializeShadowState(m, s), Mt(
227
227
  m,
228
228
  s,
229
229
  T,
230
230
  st,
231
231
  tt.current,
232
- F
233
- ), A && p && F && Nt(T, m, e, F, Date.now()), lt(m), (Array.isArray(w) ? w : [w || "component"]).includes("none") || U({});
232
+ U
233
+ ), A && p && U && Vt(T, m, e, U, Date.now()), lt(m), (Array.isArray(w) ? w : [w || "component"]).includes("none") || O({});
234
234
  }
235
235
  }, [
236
236
  s,
@@ -238,7 +238,7 @@ function Xt(t, {
238
238
  f?.data,
239
239
  ...r || []
240
240
  ]), Et(() => {
241
- B && xt(m, {
241
+ G && bt(m, {
242
242
  serverSync: S,
243
243
  formElements: y,
244
244
  initialState: s,
@@ -249,12 +249,12 @@ function Xt(t, {
249
249
  components: /* @__PURE__ */ new Map()
250
250
  };
251
251
  return n.components.set(e, {
252
- forceUpdate: () => U({}),
252
+ forceUpdate: () => O({}),
253
253
  paths: /* @__PURE__ */ new Set(),
254
254
  deps: [],
255
255
  depsFunction: x || void 0,
256
256
  reactiveType: w ?? ["component", "deps"]
257
- }), o.getState().stateComponents.set(m, n), U({}), () => {
257
+ }), o.getState().stateComponents.set(m, n), O({}), () => {
258
258
  n && (n.components.delete(e), n.components.size === 0 && o.getState().stateComponents.delete(m));
259
259
  };
260
260
  }, []);
@@ -269,24 +269,24 @@ function Xt(t, {
269
269
  if (T) {
270
270
  let b = !1, N = v.signalDomElements.get(T);
271
271
  if ((!N || N.size === 0) && (a.updateType === "insert" || a.updateType === "cut")) {
272
- const C = n.slice(0, -1), L = Y(p, C);
273
- if (Array.isArray(L)) {
272
+ const C = n.slice(0, -1), W = J(p, C);
273
+ if (Array.isArray(W)) {
274
274
  b = !0;
275
275
  const E = `${m}-${C.join(".")}`;
276
276
  N = v.signalDomElements.get(E);
277
277
  }
278
278
  }
279
279
  if (N) {
280
- const C = b ? Y(p, n.slice(0, -1)) : Y(p, n);
281
- N.forEach(({ parentId: L, position: E, effect: D }) => {
280
+ const C = b ? J(p, n.slice(0, -1)) : J(p, n);
281
+ N.forEach(({ parentId: W, position: E, effect: F }) => {
282
282
  const R = document.querySelector(
283
- `[data-parent-id="${L}"]`
283
+ `[data-parent-id="${W}"]`
284
284
  );
285
285
  if (R) {
286
286
  const V = Array.from(R.childNodes);
287
287
  if (V[E]) {
288
- const k = D ? new Function("state", `return (${D})(state)`)(C) : C;
289
- V[E].textContent = String(k);
288
+ const $ = F ? new Function("state", `return (${F})(state)`)(C) : C;
289
+ V[E].textContent = String($);
290
290
  }
291
291
  }
292
292
  });
@@ -298,78 +298,78 @@ function Xt(t, {
298
298
  const A = n.slice(0, n.length - 1);
299
299
  a.updateType === "cut" && j.current?.validation?.key && X(
300
300
  j.current?.validation?.key + "." + A.join(".")
301
- ), a.updateType === "insert" && j.current?.validation?.key && zt(
301
+ ), a.updateType === "insert" && j.current?.validation?.key && Gt(
302
302
  j.current?.validation?.key + "." + A.join(".")
303
303
  ).filter(([N, C]) => {
304
- let L = N?.split(".").length;
305
- if (N == A.join(".") && L == A.length - 1) {
304
+ let W = N?.split(".").length;
305
+ if (N == A.join(".") && W == A.length - 1) {
306
306
  let E = N + "." + A;
307
- X(N), Mt(E, C);
307
+ X(N), _t(E, C);
308
308
  }
309
309
  });
310
310
  const _ = v.stateComponents.get(m);
311
311
  if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", _), _) {
312
312
  const b = wt(u, p), N = new Set(b), C = a.updateType === "update" ? n.join(".") : n.slice(0, -1).join(".") || "";
313
313
  for (const [
314
- L,
314
+ W,
315
315
  E
316
316
  ] of _.components.entries()) {
317
- let D = !1;
317
+ let F = !1;
318
318
  const R = Array.isArray(E.reactiveType) ? E.reactiveType : [E.reactiveType || "component"];
319
319
  if (console.log("component", E), !R.includes("none")) {
320
320
  if (R.includes("all")) {
321
321
  E.forceUpdate();
322
322
  continue;
323
323
  }
324
- if (R.includes("component") && ((E.paths.has(C) || E.paths.has("")) && (D = !0), !D))
324
+ if (R.includes("component") && ((E.paths.has(C) || E.paths.has("")) && (F = !0), !F))
325
325
  for (const V of N) {
326
- let k = V;
326
+ let $ = V;
327
327
  for (; ; ) {
328
- if (E.paths.has(k)) {
329
- D = !0;
328
+ if (E.paths.has($)) {
329
+ F = !0;
330
330
  break;
331
331
  }
332
- const W = k.lastIndexOf(".");
333
- if (W !== -1) {
334
- const G = k.substring(
332
+ const D = $.lastIndexOf(".");
333
+ if (D !== -1) {
334
+ const L = $.substring(
335
335
  0,
336
- W
336
+ D
337
337
  );
338
338
  if (!isNaN(
339
- Number(k.substring(W + 1))
340
- ) && E.paths.has(G)) {
341
- D = !0;
339
+ Number($.substring(D + 1))
340
+ ) && E.paths.has(L)) {
341
+ F = !0;
342
342
  break;
343
343
  }
344
- k = G;
344
+ $ = L;
345
345
  } else
346
- k = "";
347
- if (k === "")
346
+ $ = "";
347
+ if ($ === "")
348
348
  break;
349
349
  }
350
- if (D) break;
350
+ if (F) break;
351
351
  }
352
- if (!D && R.includes("deps") && E.depsFunction) {
352
+ if (!F && R.includes("deps") && E.depsFunction) {
353
353
  const V = E.depsFunction(p);
354
- let k = !1;
355
- typeof V == "boolean" ? V && (k = !0) : J(E.deps, V) || (E.deps = V, k = !0), k && (D = !0);
354
+ let $ = !1;
355
+ typeof V == "boolean" ? V && ($ = !0) : q(E.deps, V) || (E.deps = V, $ = !0), $ && (F = !0);
356
356
  }
357
- D && E.forceUpdate();
357
+ F && E.forceUpdate();
358
358
  }
359
359
  }
360
360
  }
361
- const $ = Date.now();
361
+ const k = Date.now();
362
362
  n = n.map((b, N) => {
363
- const C = n.slice(0, -1), L = Y(p, C);
364
- return N === n.length - 1 && ["insert", "cut"].includes(a.updateType) ? (L.length - 1).toString() : b;
363
+ const C = n.slice(0, -1), W = J(p, C);
364
+ return N === n.length - 1 && ["insert", "cut"].includes(a.updateType) ? (W.length - 1).toString() : b;
365
365
  });
366
- const { oldValue: M, newValue: P } = Zt(
366
+ const { oldValue: M, newValue: P } = Jt(
367
367
  a.updateType,
368
368
  u,
369
369
  p,
370
370
  n
371
- ), z = {
372
- timeStamp: $,
371
+ ), H = {
372
+ timeStamp: k,
373
373
  stateKey: m,
374
374
  path: n,
375
375
  updateType: a.updateType,
@@ -390,23 +390,23 @@ function Xt(t, {
390
390
  v.removeShadowArrayElement(m, N, C);
391
391
  break;
392
392
  }
393
- if (qt(m, (b) => {
394
- const C = [...b ?? [], z].reduce((L, E) => {
395
- const D = `${E.stateKey}:${JSON.stringify(E.path)}`, R = L.get(D);
396
- return R ? (R.timeStamp = Math.max(R.timeStamp, E.timeStamp), R.newValue = E.newValue, R.oldValue = R.oldValue ?? E.oldValue, R.updateType = E.updateType) : L.set(D, { ...E }), L;
393
+ if (Ht(m, (b) => {
394
+ const C = [...b ?? [], H].reduce((W, E) => {
395
+ const F = `${E.stateKey}:${JSON.stringify(E.path)}`, R = W.get(F);
396
+ return R ? (R.timeStamp = Math.max(R.timeStamp, E.timeStamp), R.newValue = E.newValue, R.oldValue = R.oldValue ?? E.oldValue, R.updateType = E.updateType) : W.set(F, { ...E }), W;
397
397
  }, /* @__PURE__ */ new Map());
398
398
  return Array.from(C.values());
399
- }), Nt(
399
+ }), Vt(
400
400
  p,
401
401
  m,
402
402
  j.current,
403
- F
403
+ U
404
404
  ), j.current?.middleware && j.current.middleware({
405
405
  updateLog: l,
406
- update: z
406
+ update: H
407
407
  }), j.current?.serverSync) {
408
408
  const b = v.serverState[m], N = j.current?.serverSync;
409
- Jt(m, {
409
+ zt(m, {
410
410
  syncKey: typeof N.syncKey == "string" ? N.syncKey : N.syncKey({ state: p }),
411
411
  rollBackState: b,
412
412
  actionTimeStamp: Date.now() + (N.debounce ?? 3e3),
@@ -422,16 +422,16 @@ function Xt(t, {
422
422
  m,
423
423
  st,
424
424
  tt.current,
425
- F
425
+ U
426
426
  )
427
427
  ), o.getState().cogsStateStore[m] || rt(m, t), o.getState().initialStateGlobal[m] || At(m, t));
428
428
  const d = pt(() => St(
429
429
  m,
430
430
  st,
431
431
  tt.current,
432
- F
433
- ), [m, F]);
434
- return [_t(m), d];
432
+ U
433
+ ), [m, U]);
434
+ return [Pt(m), d];
435
435
  }
436
436
  function St(t, c, S, g) {
437
437
  const y = /* @__PURE__ */ new Map();
@@ -450,8 +450,8 @@ function St(t, c, S, g) {
450
450
  r?.key && X(r?.key), h?.validationKey && X(h.validationKey);
451
451
  const f = o.getState().initialStateGlobal[t];
452
452
  o.getState().clearSelectedIndexesForState(t), y.clear(), x++;
453
- const H = s(f, []), U = at(t), F = K(U?.localStorage?.key) ? U?.localStorage?.key(f) : U?.localStorage?.key, B = `${g}-${t}-${F}`;
454
- B && localStorage.removeItem(B), ft(t, H), rt(t, f);
453
+ const B = s(f, []), O = at(t), U = K(O?.localStorage?.key) ? O?.localStorage?.key(f) : O?.localStorage?.key, G = `${g}-${t}-${U}`;
454
+ G && localStorage.removeItem(G), ft(t, B), rt(t, f);
455
455
  const m = o.getState().stateComponents.get(t);
456
456
  return m && m.components.forEach((l) => {
457
457
  l.forceUpdate();
@@ -464,15 +464,15 @@ function St(t, c, S, g) {
464
464
  c,
465
465
  S,
466
466
  g
467
- ), f = o.getState().initialStateGlobal[t], H = at(t), U = K(H?.localStorage?.key) ? H?.localStorage?.key(f) : H?.localStorage?.key, F = `${g}-${t}-${U}`;
468
- return localStorage.getItem(F) && localStorage.removeItem(F), Ut(() => {
467
+ ), f = o.getState().initialStateGlobal[t], B = at(t), O = K(B?.localStorage?.key) ? B?.localStorage?.key(f) : B?.localStorage?.key, U = `${g}-${t}-${O}`;
468
+ return localStorage.getItem(U) && localStorage.removeItem(U), Rt(() => {
469
469
  At(t, h), o.getState().initializeShadowState(t, h), ft(t, r), rt(t, h);
470
- const B = o.getState().stateComponents.get(t);
471
- B && B.components.forEach((m) => {
470
+ const G = o.getState().stateComponents.get(t);
471
+ G && G.components.forEach((m) => {
472
472
  m.forceUpdate();
473
473
  });
474
474
  }), {
475
- fetchId: (B) => r.get()[B]
475
+ fetchId: (G) => r.get()[G]
476
476
  };
477
477
  },
478
478
  _initialState: o.getState().initialStateGlobal[t],
@@ -480,19 +480,19 @@ function St(t, c, S, g) {
480
480
  _isLoading: o.getState().isLoadingGlobal[t],
481
481
  _isServerSynced: () => {
482
482
  const h = o.getState().serverState[t];
483
- return !!(h && J(h, _t(t)));
483
+ return !!(h && q(h, Pt(t)));
484
484
  }
485
485
  };
486
486
  function s(h, r = [], f) {
487
- const H = r.map(String).join(".");
488
- y.get(H);
489
- const U = function() {
487
+ const B = r.map(String).join(".");
488
+ y.get(B);
489
+ const O = function() {
490
490
  return o().getNestedState(t, r);
491
491
  };
492
492
  Object.keys(I).forEach((m) => {
493
- U[m] = I[m];
493
+ O[m] = I[m];
494
494
  });
495
- const F = {
495
+ const U = {
496
496
  apply(m, l, dt) {
497
497
  return console.log(
498
498
  `PROXY APPLY TRAP HIT: stateKey=${t}, path=${r.join(".")}`
@@ -568,16 +568,16 @@ function St(t, c, S, g) {
568
568
  }
569
569
  };
570
570
  if (l === "_status") {
571
- const d = o.getState().getNestedState(t, r), e = o.getState().initialStateGlobal[t], n = Y(e, r);
572
- return J(d, n) ? "fresh" : "stale";
571
+ const d = o.getState().getNestedState(t, r), e = o.getState().initialStateGlobal[t], n = J(e, r);
572
+ return q(d, n) ? "fresh" : "stale";
573
573
  }
574
574
  if (l === "getStatus")
575
575
  return function() {
576
576
  const d = o().getNestedState(
577
577
  t,
578
578
  r
579
- ), e = o.getState().initialStateGlobal[t], n = Y(e, r);
580
- return J(d, n) ? "fresh" : "stale";
579
+ ), e = o.getState().initialStateGlobal[t], n = J(e, r);
580
+ return q(d, n) ? "fresh" : "stale";
581
581
  };
582
582
  if (l === "removeStorage")
583
583
  return () => {
@@ -625,114 +625,111 @@ function St(t, c, S, g) {
625
625
  } = e, u = Q(null), [p, T] = et({
626
626
  startIndex: 0,
627
627
  endIndex: 10
628
- }), [A, _] = et(0), $ = Q(!1), M = Q(!0), P = Q(
628
+ }), [A, _] = et(0), k = Q(!1), M = Q(!0), P = Q(
629
629
  null
630
630
  );
631
631
  nt(() => o.getState().subscribeToShadowState(t, () => {
632
- _((k) => k + 1);
632
+ _(($) => $ + 1);
633
633
  }), [t]);
634
- const z = o().getNestedState(
634
+ const H = o().getNestedState(
635
635
  t,
636
636
  r
637
- ), b = z.length, { totalHeight: N, positions: C } = pt(() => {
637
+ ), b = H.length, { totalHeight: N, positions: C } = pt(() => {
638
638
  const V = o.getState().getShadowMetadata(t, r) || [];
639
- let k = 0;
640
- const W = [];
641
- for (let G = 0; G < b; G++) {
642
- W[G] = k;
643
- const Z = V[G]?.virtualizer?.itemHeight;
644
- k += Z || n;
639
+ let $ = 0;
640
+ const D = [];
641
+ for (let L = 0; L < b; L++) {
642
+ D[L] = $;
643
+ const Y = V[L]?.virtualizer?.itemHeight;
644
+ $ += Y || n;
645
645
  }
646
- return { totalHeight: k, positions: W };
646
+ return { totalHeight: $, positions: D };
647
647
  }, [
648
648
  b,
649
649
  t,
650
650
  r.join("."),
651
651
  n,
652
652
  A
653
- ]), L = pt(() => {
654
- const V = Math.max(0, p.startIndex), k = Math.min(b, p.endIndex), W = Array.from(
655
- { length: k - V },
656
- (Z, O) => V + O
657
- ), G = W.map((Z) => z[Z]);
658
- return s(G, r, {
653
+ ]), W = pt(() => {
654
+ const V = Math.max(0, p.startIndex), $ = Math.min(b, p.endIndex), D = Array.from(
655
+ { length: $ - V },
656
+ (Y, Z) => V + Z
657
+ ), L = D.map((Y) => H[Y]);
658
+ return s(L, r, {
659
659
  ...f,
660
- validIndices: W
660
+ validIndices: D
661
661
  });
662
- }, [p.startIndex, p.endIndex, z, b]);
662
+ }, [p.startIndex, p.endIndex, H, b]);
663
663
  nt(() => {
664
664
  if (!i || !u.current || b === 0 || !M.current) return;
665
665
  P.current && clearInterval(P.current);
666
- const V = 50, k = p.endIndex < V, W = b > p.endIndex + V;
667
- if (k || W) {
668
- $.current = !0;
669
- const O = {
666
+ const V = 50, $ = p.endIndex < V, D = b > p.endIndex + V;
667
+ if ($ || D) {
668
+ k.current = !0;
669
+ const Z = {
670
670
  startIndex: Math.max(0, b - 20),
671
671
  endIndex: b
672
672
  };
673
- T(O), setTimeout(() => {
674
- $.current = !1;
673
+ T(Z), setTimeout(() => {
674
+ k.current = !1;
675
675
  }, 100);
676
676
  }
677
- let G = 0;
678
- const Z = 50;
677
+ let L = 0;
678
+ const Y = 50;
679
679
  return P.current = setInterval(() => {
680
- const O = u.current;
681
- if (!O) return;
682
- G++;
683
- const { scrollTop: ht, scrollHeight: ot, clientHeight: ut } = O, vt = ht + ut, kt = ot - vt < 50;
684
- if (kt || G >= Z)
685
- clearInterval(P.current), P.current = null, kt && O.scrollTop < O.scrollHeight - O.clientHeight && (O.scrollTop = O.scrollHeight);
686
- else {
687
- const Rt = O.scrollHeight - O.clientHeight;
688
- Math.abs(O.scrollTop - Rt) > 1 && (O.scrollTop = O.scrollHeight);
689
- }
680
+ const Z = u.current;
681
+ if (!Z) return;
682
+ L++;
683
+ const { scrollTop: ht, scrollHeight: ot, clientHeight: ut } = Z, vt = ht + ut;
684
+ ot - vt < 10 || L >= Y ? (clearInterval(P.current), P.current = null) : (k.current = !0, Z.scrollTop = Z.scrollHeight, setTimeout(() => {
685
+ k.current = !1;
686
+ }, 50));
690
687
  }, 100), () => {
691
688
  P.current && (clearInterval(P.current), P.current = null);
692
689
  };
693
690
  }, [b, i, p.startIndex, p.endIndex]), nt(() => {
694
691
  const V = u.current;
695
692
  if (!V) return;
696
- const k = () => {
697
- if ($.current)
693
+ const $ = () => {
694
+ if (k.current)
698
695
  return;
699
- const { scrollTop: W, scrollHeight: G, clientHeight: Z } = V, ht = G - W - Z < 50;
696
+ const { scrollTop: D, scrollHeight: L, clientHeight: Y } = V, ht = L - D - Y < 50;
700
697
  P.current && (clearInterval(P.current), P.current = null), M.current = ht;
701
698
  let ot = 0;
702
- for (let q = 0; q < C.length; q++)
703
- if (C[q] > W - n * a) {
704
- ot = Math.max(0, q - 1);
699
+ for (let z = 0; z < C.length; z++)
700
+ if (C[z] > D - n * a) {
701
+ ot = Math.max(0, z - 1);
705
702
  break;
706
703
  }
707
704
  let ut = ot;
708
- const vt = W + Z;
709
- for (let q = ot; q < C.length && !(C[q] > vt + n * a); q++)
710
- ut = q;
705
+ const vt = D + Y;
706
+ for (let z = ot; z < C.length && !(C[z] > vt + n * a); z++)
707
+ ut = z;
711
708
  T({
712
709
  startIndex: Math.max(0, ot),
713
710
  endIndex: Math.min(b, ut + 1 + a)
714
711
  });
715
712
  };
716
- return V.addEventListener("scroll", k, {
713
+ return V.addEventListener("scroll", $, {
717
714
  passive: !0
718
- }), k(), () => {
719
- V.removeEventListener("scroll", k);
715
+ }), $(), () => {
716
+ V.removeEventListener("scroll", $);
720
717
  };
721
718
  }, [C, b, n, a]);
722
- const E = $t(
719
+ const E = kt(
723
720
  (V = "auto") => {
724
- M.current = !0, $.current = !0, u.current && (u.current.scrollTop = u.current.scrollHeight), setTimeout(() => {
725
- $.current = !1;
721
+ M.current = !0, k.current = !0, u.current && (u.current.scrollTop = u.current.scrollHeight), setTimeout(() => {
722
+ k.current = !1;
726
723
  }, 100);
727
724
  },
728
725
  []
729
- ), D = $t(
730
- (V, k = "smooth") => {
731
- $.current = !0, u.current && C[V] !== void 0 && u.current.scrollTo({
726
+ ), F = kt(
727
+ (V, $ = "smooth") => {
728
+ k.current = !0, u.current && C[V] !== void 0 && u.current.scrollTo({
732
729
  top: C[V],
733
- behavior: k
730
+ behavior: $
734
731
  }), setTimeout(() => {
735
- $.current = !1;
732
+ k.current = !1;
736
733
  }, 100);
737
734
  },
738
735
  [C]
@@ -754,10 +751,10 @@ function St(t, c, S, g) {
754
751
  }
755
752
  };
756
753
  return {
757
- virtualState: L,
754
+ virtualState: W,
758
755
  virtualizerProps: R,
759
756
  scrollToBottom: E,
760
- scrollToIndex: D
757
+ scrollToIndex: F
761
758
  };
762
759
  };
763
760
  if (l === "stateSort")
@@ -791,19 +788,19 @@ function St(t, c, S, g) {
791
788
  const u = n[i], p = [...r, i.toString()], T = s(u, p, f);
792
789
  return e(u, T, {
793
790
  register: () => {
794
- const [, _] = et({}), $ = `${S}-${r.join(".")}-${i}`;
791
+ const [, _] = et({}), k = `${S}-${r.join(".")}-${i}`;
795
792
  Et(() => {
796
- const M = `${t}////${$}`, P = o.getState().stateComponents.get(t) || {
793
+ const M = `${t}////${k}`, P = o.getState().stateComponents.get(t) || {
797
794
  components: /* @__PURE__ */ new Map()
798
795
  };
799
796
  return P.components.set(M, {
800
797
  forceUpdate: () => _({}),
801
798
  paths: /* @__PURE__ */ new Set([p.join(".")])
802
799
  }), o.getState().stateComponents.set(t, P), () => {
803
- const z = o.getState().stateComponents.get(t);
804
- z && z.components.delete(M);
800
+ const H = o.getState().stateComponents.get(t);
801
+ H && H.components.delete(M);
805
802
  };
806
- }, [t, $]);
803
+ }, [t, k]);
807
804
  },
808
805
  index: v,
809
806
  originalIndex: i
@@ -827,7 +824,7 @@ function St(t, c, S, g) {
827
824
  );
828
825
  });
829
826
  if (l === "$stateMap")
830
- return (e) => ct(Qt, {
827
+ return (e) => ct(Zt, {
831
828
  proxy: {
832
829
  _stateKey: t,
833
830
  _path: r,
@@ -841,7 +838,7 @@ function St(t, c, S, g) {
841
838
  const n = o.getState().getNestedState(t, r);
842
839
  return Array.isArray(n) ? (f?.validIndices || Array.from({ length: n.length }, (i, v) => v)).map((i, v) => {
843
840
  const u = n[i], p = [...r, i.toString()], T = s(u, p, f), A = `${S}-${r.join(".")}-${i}`;
844
- return ct(te, {
841
+ return ct(Qt, {
845
842
  key: i,
846
843
  stateKey: t,
847
844
  itemComponentId: A,
@@ -895,17 +892,17 @@ function St(t, c, S, g) {
895
892
  if (!i.some((T) => {
896
893
  if (n) {
897
894
  const _ = n.every(
898
- ($) => J(T[$], v[$])
895
+ (k) => q(T[k], v[k])
899
896
  );
900
897
  return _ && (u = T), _;
901
898
  }
902
- const A = J(T, v);
899
+ const A = q(T, v);
903
900
  return A && (u = T), A;
904
901
  }))
905
902
  w(r), yt(c, v, r, t);
906
903
  else if (a && u) {
907
904
  const T = a(u), A = i.map(
908
- (_) => J(_, u) ? T : _
905
+ (_) => q(_, u) ? T : _
909
906
  );
910
907
  w(r), it(c, A, r);
911
908
  }
@@ -967,13 +964,13 @@ function St(t, c, S, g) {
967
964
  return o.getState().getNestedState(t, r);
968
965
  };
969
966
  if (l === "$derive")
970
- return (d) => Ct({
967
+ return (d) => Nt({
971
968
  _stateKey: t,
972
969
  _path: r,
973
970
  _effect: d.toString()
974
971
  });
975
972
  if (l === "$get")
976
- return () => Ct({
973
+ return () => Nt({
977
974
  _stateKey: t,
978
975
  _path: r
979
976
  });
@@ -1013,13 +1010,13 @@ function St(t, c, S, g) {
1013
1010
  throw new Error("Validation key not found");
1014
1011
  X(e.key), console.log("addValidationError", d), d.forEach((n) => {
1015
1012
  const a = [e.key, ...n.path].join(".");
1016
- console.log("fullErrorPath", a), Mt(a, n.message);
1013
+ console.log("fullErrorPath", a), _t(a, n.message);
1017
1014
  }), lt(t);
1018
1015
  };
1019
1016
  if (l === "applyJsonPatch")
1020
1017
  return (d) => {
1021
- const e = o.getState().cogsStateStore[t], a = Ht(e, d).newDocument;
1022
- jt(
1018
+ const e = o.getState().cogsStateStore[t], a = Lt(e, d).newDocument;
1019
+ Mt(
1023
1020
  t,
1024
1021
  o.getState().initialStateGlobal[t],
1025
1022
  a,
@@ -1042,22 +1039,22 @@ function St(t, c, S, g) {
1042
1039
  continue;
1043
1040
  }
1044
1041
  if (_.includes("component") && (T.paths.has("") && (A = !0), !A))
1045
- for (const $ of u) {
1046
- if (T.paths.has($)) {
1042
+ for (const k of u) {
1043
+ if (T.paths.has(k)) {
1047
1044
  A = !0;
1048
1045
  break;
1049
1046
  }
1050
- let M = $.lastIndexOf(".");
1047
+ let M = k.lastIndexOf(".");
1051
1048
  for (; M !== -1; ) {
1052
- const P = $.substring(0, M);
1049
+ const P = k.substring(0, M);
1053
1050
  if (T.paths.has(P)) {
1054
1051
  A = !0;
1055
1052
  break;
1056
1053
  }
1057
- const z = $.substring(
1054
+ const H = k.substring(
1058
1055
  M + 1
1059
1056
  );
1060
- if (!isNaN(Number(z))) {
1057
+ if (!isNaN(Number(H))) {
1061
1058
  const b = P.lastIndexOf(".");
1062
1059
  if (b !== -1) {
1063
1060
  const N = P.substring(
@@ -1075,9 +1072,9 @@ function St(t, c, S, g) {
1075
1072
  if (A) break;
1076
1073
  }
1077
1074
  if (!A && _.includes("deps") && T.depsFunction) {
1078
- const $ = T.depsFunction(a);
1075
+ const k = T.depsFunction(a);
1079
1076
  let M = !1;
1080
- typeof $ == "boolean" ? $ && (M = !0) : J(T.deps, $) || (T.deps = $, M = !0), M && (A = !0);
1077
+ typeof k == "boolean" ? k && (M = !0) : q(T.deps, k) || (T.deps = k, M = !0), M && (A = !0);
1081
1078
  }
1082
1079
  A && T.forceUpdate();
1083
1080
  }
@@ -1111,7 +1108,7 @@ function St(t, c, S, g) {
1111
1108
  if (l === "getComponents")
1112
1109
  return () => o().stateComponents.get(t);
1113
1110
  if (l === "getAllFormRefs")
1114
- return () => bt.getState().getFormRefsByStateKey(t);
1111
+ return () => $t.getState().getFormRefsByStateKey(t);
1115
1112
  if (l === "_initialState")
1116
1113
  return o.getState().initialStateGlobal[t];
1117
1114
  if (l === "_serverState")
@@ -1124,13 +1121,13 @@ function St(t, c, S, g) {
1124
1121
  if (l === "removeValidation") return I.removeValidation;
1125
1122
  }
1126
1123
  if (l === "getFormRef")
1127
- return () => bt.getState().getFormRef(t + "." + r.join("."));
1124
+ return () => $t.getState().getFormRef(t + "." + r.join("."));
1128
1125
  if (l === "validationWrapper")
1129
1126
  return ({
1130
1127
  children: d,
1131
1128
  hideMessage: e
1132
1129
  }) => /* @__PURE__ */ It(
1133
- Wt,
1130
+ Ft,
1134
1131
  {
1135
1132
  formOpts: e ? { validation: { message: "" } } : void 0,
1136
1133
  path: r,
@@ -1146,7 +1143,7 @@ function St(t, c, S, g) {
1146
1143
  if (l === "update")
1147
1144
  return (d, e) => {
1148
1145
  if (e?.debounce)
1149
- Dt(() => {
1146
+ Ut(() => {
1150
1147
  it(c, d, r, "");
1151
1148
  const n = o.getState().getNestedState(t, r);
1152
1149
  e?.afterUpdate && e.afterUpdate(n);
@@ -1160,7 +1157,7 @@ function St(t, c, S, g) {
1160
1157
  };
1161
1158
  if (l === "formElement")
1162
1159
  return (d, e) => /* @__PURE__ */ It(
1163
- Lt,
1160
+ Dt,
1164
1161
  {
1165
1162
  setState: c,
1166
1163
  stateKey: t,
@@ -1172,20 +1169,20 @@ function St(t, c, S, g) {
1172
1169
  const j = [...r, l], st = o.getState().getNestedState(t, j);
1173
1170
  return s(st, j, f);
1174
1171
  }
1175
- }, B = new Proxy(U, F);
1176
- return y.set(H, {
1177
- proxy: B,
1172
+ }, G = new Proxy(O, U);
1173
+ return y.set(B, {
1174
+ proxy: G,
1178
1175
  stateVersion: x
1179
- }), B;
1176
+ }), G;
1180
1177
  }
1181
1178
  return s(
1182
1179
  o.getState().getNestedState(t, [])
1183
1180
  );
1184
1181
  }
1185
- function Ct(t) {
1186
- return ct(Kt, { proxy: t });
1182
+ function Nt(t) {
1183
+ return ct(Xt, { proxy: t });
1187
1184
  }
1188
- function Qt({
1185
+ function Zt({
1189
1186
  proxy: t,
1190
1187
  rebuildStateShape: c
1191
1188
  }) {
@@ -1197,7 +1194,7 @@ function Qt({
1197
1194
  (y, x, w, I, s) => t._mapFn(y, x, w, I, s)
1198
1195
  ) : null;
1199
1196
  }
1200
- function Kt({
1197
+ function Xt({
1201
1198
  proxy: t
1202
1199
  }) {
1203
1200
  const c = Q(null), S = `${t._stateKey}-${t._path.join(".")}`;
@@ -1222,22 +1219,22 @@ function Kt({
1222
1219
  "state",
1223
1220
  `return (${t._effect})(state)`
1224
1221
  )(r);
1225
- } catch (U) {
1226
- console.error("Error evaluating effect function during mount:", U), f = r;
1222
+ } catch (O) {
1223
+ console.error("Error evaluating effect function during mount:", O), f = r;
1227
1224
  }
1228
1225
  else
1229
1226
  f = r;
1230
1227
  f !== null && typeof f == "object" && (f = JSON.stringify(f));
1231
- const H = document.createTextNode(String(f));
1232
- g.replaceWith(H);
1228
+ const B = document.createTextNode(String(f));
1229
+ g.replaceWith(B);
1233
1230
  }, [t._stateKey, t._path.join("."), t._effect]), ct("span", {
1234
1231
  ref: c,
1235
1232
  style: { display: "none" },
1236
1233
  "data-signal-id": S
1237
1234
  });
1238
1235
  }
1239
- function me(t) {
1240
- const c = Ot(
1236
+ function Se(t) {
1237
+ const c = jt(
1241
1238
  (S) => {
1242
1239
  const g = o.getState().stateComponents.get(t._stateKey) || {
1243
1240
  components: /* @__PURE__ */ new Map()
@@ -1251,7 +1248,7 @@ function me(t) {
1251
1248
  );
1252
1249
  return ct("text", {}, String(c));
1253
1250
  }
1254
- function te({
1251
+ function Qt({
1255
1252
  stateKey: t,
1256
1253
  itemComponentId: c,
1257
1254
  itemPath: S,
@@ -1278,11 +1275,11 @@ function te({
1278
1275
  }, [t, c, S.join(".")]), /* @__PURE__ */ It("div", { ref: x, children: g });
1279
1276
  }
1280
1277
  export {
1281
- Ct as $cogsSignal,
1282
- me as $cogsSignalStore,
1283
- ge as addStateOptions,
1284
- fe as createCogsState,
1285
- Se as notifyComponent,
1286
- Xt as useCogsStateFn
1278
+ Nt as $cogsSignal,
1279
+ Se as $cogsSignalStore,
1280
+ ue as addStateOptions,
1281
+ ge as createCogsState,
1282
+ fe as notifyComponent,
1283
+ Yt as useCogsStateFn
1287
1284
  };
1288
1285
  //# sourceMappingURL=CogsState.jsx.map