cogsbox-state 0.5.418 → 0.5.420

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
- import { jsx as It } from "react/jsx-runtime";
3
- import { useState as ot, useRef as J, useEffect as et, useLayoutEffect as Nt, useMemo as wt, createElement as ct, useSyncExternalStore as Ot, startTransition as jt, useCallback as ft } from "react";
4
- import { transformStateFunc as Ut, isDeepEqual as q, isFunction as K, getNestedValue as Y, getDifferences as pt, debounce as Ft } from "./utility.js";
5
- import { pushFunc as yt, updateFn as st, cutFunc as gt, ValidationWrapper as Dt, FormControlComponent as Lt } from "./Functions.jsx";
6
- import Gt from "superjson";
7
- import { v4 as Et } from "uuid";
2
+ import { jsx as yt } from "react/jsx-runtime";
3
+ import { useState as ot, useRef as J, useEffect as et, useLayoutEffect as xt, useMemo as It, createElement as ct, useSyncExternalStore as Mt, startTransition as Ot, useCallback as ft } from "react";
4
+ import { transformStateFunc as jt, isDeepEqual as q, isFunction as K, getNestedValue as Y, getDifferences as wt, debounce as Ut } from "./utility.js";
5
+ import { pushFunc as vt, updateFn as st, cutFunc as gt, ValidationWrapper as Ft, FormControlComponent as Dt } from "./Functions.jsx";
6
+ import Lt from "superjson";
7
+ import { v4 as pt } from "uuid";
8
8
  import "zod";
9
9
  import { getGlobalStore as o, formRefStore as kt } from "./store.js";
10
- import { useCogsConfig as Pt } from "./CogsStateClient.jsx";
11
- import { applyPatch as Wt } from "fast-json-patch";
12
- import zt from "react-use-measure";
13
- function bt(t, s) {
10
+ import { useCogsConfig as Nt } from "./CogsStateClient.jsx";
11
+ import { applyPatch as Gt } from "fast-json-patch";
12
+ import Wt from "react-use-measure";
13
+ function At(t, s) {
14
14
  const S = o.getState().getInitialOptions, u = o.getState().setInitialStateOptions, v = S(t) || {};
15
15
  u(t, {
16
16
  ...v,
17
17
  ...s
18
18
  });
19
19
  }
20
- function Vt({
20
+ function bt({
21
21
  stateKey: t,
22
22
  options: s,
23
23
  initialOptionsPart: S
@@ -30,12 +30,12 @@ function Vt({
30
30
  !q(I[i], s[i]) && (y = !0, I[i] = s[i])) : (y = !0, I[i] = s[i]);
31
31
  y && V(t, I);
32
32
  }
33
- function ue(t, { formElements: s, validation: S }) {
33
+ function de(t, { formElements: s, validation: S }) {
34
34
  return { initialState: t, formElements: s, validation: S };
35
35
  }
36
- const ge = (t, s) => {
36
+ const ue = (t, s) => {
37
37
  let S = t;
38
- const [u, v] = Ut(S);
38
+ const [u, v] = jt(S);
39
39
  (Object.keys(v).length > 0 || s && Object.keys(s).length > 0) && Object.keys(v).forEach((y) => {
40
40
  v[y] = v[y] || {}, v[y].formElements = {
41
41
  ...s?.formElements,
@@ -46,13 +46,13 @@ const ge = (t, s) => {
46
46
  }, rt(y) || o.getState().setInitialStateOptions(y, v[y]);
47
47
  }), o.getState().setInitialStates(u), o.getState().setCreatedState(u);
48
48
  const V = (y, i) => {
49
- const [h] = ot(i?.componentId ?? Et());
50
- Vt({
49
+ const [h] = ot(i?.componentId ?? pt());
50
+ bt({
51
51
  stateKey: y,
52
52
  options: i,
53
53
  initialOptionsPart: v
54
54
  });
55
- const n = o.getState().cogsStateStore[y] || u[y], g = i?.modifyState ? i.modifyState(n) : n, [L, U] = Zt(
55
+ const n = o.getState().cogsStateStore[y] || u[y], g = i?.modifyState ? i.modifyState(n) : n, [L, U] = Yt(
56
56
  g,
57
57
  {
58
58
  stateKey: y,
@@ -71,21 +71,21 @@ const ge = (t, s) => {
71
71
  return U;
72
72
  };
73
73
  function I(y, i) {
74
- Vt({ stateKey: y, options: i, initialOptionsPart: v }), i.localStorage && Jt(y, i), lt(y);
74
+ bt({ stateKey: y, options: i, initialOptionsPart: v }), i.localStorage && qt(y, i), lt(y);
75
75
  }
76
76
  return { useCogsState: V, setCogsOptions: I };
77
77
  }, {
78
78
  setUpdaterState: St,
79
79
  setState: nt,
80
80
  getInitialOptions: rt,
81
- getKeyState: Ct,
82
- getValidationErrors: Ht,
83
- setStateLog: Bt,
84
- updateInitialStateGlobal: At,
85
- addValidationError: _t,
81
+ getKeyState: Pt,
82
+ getValidationErrors: zt,
83
+ setStateLog: Ht,
84
+ updateInitialStateGlobal: Et,
85
+ addValidationError: Ct,
86
86
  removeValidationError: X,
87
- setServerSyncActions: qt
88
- } = o.getState(), $t = (t, s, S, u, v) => {
87
+ setServerSyncActions: Bt
88
+ } = o.getState(), Vt = (t, s, S, u, v) => {
89
89
  S?.log && console.log(
90
90
  "saving to localstorage",
91
91
  s,
@@ -104,7 +104,7 @@ const ge = (t, s) => {
104
104
  state: t,
105
105
  lastUpdated: Date.now(),
106
106
  lastSyncedWithServer: v ?? y
107
- }, h = Gt.serialize(i);
107
+ }, h = Lt.serialize(i);
108
108
  window.localStorage.setItem(
109
109
  I,
110
110
  JSON.stringify(h.json)
@@ -118,8 +118,8 @@ const ge = (t, s) => {
118
118
  } catch (s) {
119
119
  return console.error("Error loading from localStorage:", s), null;
120
120
  }
121
- }, Jt = (t, s) => {
122
- const S = o.getState().cogsStateStore[t], { sessionId: u } = Pt(), v = K(s?.localStorage?.key) ? s.localStorage.key(S) : s?.localStorage?.key;
121
+ }, qt = (t, s) => {
122
+ const S = o.getState().cogsStateStore[t], { sessionId: u } = Nt(), v = K(s?.localStorage?.key) ? s.localStorage.key(S) : s?.localStorage?.key;
123
123
  if (v && u) {
124
124
  const V = ht(
125
125
  `${u}-${t}-${v}`
@@ -128,7 +128,7 @@ const ge = (t, s) => {
128
128
  return nt(t, V.state), lt(t), !0;
129
129
  }
130
130
  return !1;
131
- }, Rt = (t, s, S, u, v, V) => {
131
+ }, _t = (t, s, S, u, v, V) => {
132
132
  const I = {
133
133
  initialState: s,
134
134
  updaterState: mt(
@@ -139,7 +139,7 @@ const ge = (t, s) => {
139
139
  ),
140
140
  state: S
141
141
  };
142
- At(t, I.initialState), St(t, I.updaterState), nt(t, I.state);
142
+ Et(t, I.initialState), St(t, I.updaterState), nt(t, I.state);
143
143
  }, lt = (t) => {
144
144
  const s = o.getState().stateComponents.get(t);
145
145
  if (!s) return;
@@ -149,7 +149,7 @@ const ge = (t, s) => {
149
149
  }), queueMicrotask(() => {
150
150
  S.forEach((u) => u());
151
151
  });
152
- }, fe = (t, s) => {
152
+ }, ge = (t, s) => {
153
153
  const S = o.getState().stateComponents.get(t);
154
154
  if (S) {
155
155
  const u = `${t}////${s}`, v = S.components.get(u);
@@ -157,7 +157,7 @@ const ge = (t, s) => {
157
157
  return;
158
158
  v && v.forceUpdate();
159
159
  }
160
- }, Yt = (t, s, S, u) => {
160
+ }, Jt = (t, s, S, u) => {
161
161
  switch (t) {
162
162
  case "update":
163
163
  return {
@@ -180,7 +180,7 @@ const ge = (t, s) => {
180
180
  return { oldValue: null, newValue: null };
181
181
  }
182
182
  };
183
- function Zt(t, {
183
+ function Yt(t, {
184
184
  stateKey: s,
185
185
  serverSync: S,
186
186
  localStorage: u,
@@ -193,12 +193,12 @@ function Zt(t, {
193
193
  dependencies: n,
194
194
  serverState: g
195
195
  } = {}) {
196
- const [L, U] = ot({}), { sessionId: F } = Pt();
196
+ const [L, U] = ot({}), { sessionId: F } = Nt();
197
197
  let z = !s;
198
- const [f] = ot(s ?? Et()), l = o.getState().stateLog[f], dt = J(/* @__PURE__ */ new Set()), tt = J(y ?? Et()), R = J(
198
+ const [f] = ot(s ?? pt()), l = o.getState().stateLog[f], dt = J(/* @__PURE__ */ new Set()), tt = J(y ?? pt()), M = J(
199
199
  null
200
200
  );
201
- R.current = rt(f) ?? null, et(() => {
201
+ M.current = rt(f) ?? null, et(() => {
202
202
  if (h && h.stateKey === f && h.path?.[0]) {
203
203
  nt(f, (r) => ({
204
204
  ...r,
@@ -212,38 +212,38 @@ function Zt(t, {
212
212
  }
213
213
  }, [h]), et(() => {
214
214
  if (i) {
215
- bt(f, {
215
+ At(f, {
216
216
  initialState: i
217
217
  });
218
- const e = R.current, a = e?.serverState?.id !== void 0 && e?.serverState?.status === "success" && e?.serverState?.data, c = o.getState().initialStateGlobal[f];
218
+ const e = M.current, a = e?.serverState?.id !== void 0 && e?.serverState?.status === "success" && e?.serverState?.data, c = o.getState().initialStateGlobal[f];
219
219
  if (!(c && !q(c, i) || !c) && !a)
220
220
  return;
221
221
  let m = null;
222
222
  const k = K(e?.localStorage?.key) ? e?.localStorage?.key(i) : e?.localStorage?.key;
223
223
  k && F && (m = ht(`${F}-${f}-${k}`));
224
224
  let p = i, b = !1;
225
- const C = a ? Date.now() : 0, P = m?.lastUpdated || 0, M = m?.lastSyncedWithServer || 0;
226
- a && C > P ? (p = e.serverState.data, b = !0) : m && P > M && (p = m.state, e?.localStorage?.onChange && e?.localStorage?.onChange(p)), o.getState().initializeShadowState(f, i), Rt(
225
+ const _ = a ? Date.now() : 0, P = m?.lastUpdated || 0, R = m?.lastSyncedWithServer || 0;
226
+ a && _ > P ? (p = e.serverState.data, b = !0) : m && P > R && (p = m.state, e?.localStorage?.onChange && e?.localStorage?.onChange(p)), o.getState().initializeShadowState(f, i), _t(
227
227
  f,
228
228
  i,
229
229
  p,
230
230
  at,
231
231
  tt.current,
232
232
  F
233
- ), b && k && F && $t(p, f, e, F, Date.now()), lt(f), (Array.isArray(I) ? I : [I || "component"]).includes("none") || U({});
233
+ ), b && k && F && Vt(p, f, e, F, Date.now()), lt(f), (Array.isArray(I) ? I : [I || "component"]).includes("none") || U({});
234
234
  }
235
235
  }, [
236
236
  i,
237
237
  g?.status,
238
238
  g?.data,
239
239
  ...n || []
240
- ]), Nt(() => {
241
- z && bt(f, {
240
+ ]), xt(() => {
241
+ z && At(f, {
242
242
  serverSync: S,
243
243
  formElements: v,
244
244
  initialState: i,
245
245
  localStorage: u,
246
- middleware: R.current?.middleware
246
+ middleware: M.current?.middleware
247
247
  });
248
248
  const e = `${f}////${tt.current}`, r = o.getState().stateComponents.get(f) || {
249
249
  components: /* @__PURE__ */ new Map()
@@ -267,81 +267,81 @@ function Zt(t, {
267
267
  nt(f, (m) => {
268
268
  const k = K(e) ? e(m) : e, p = `${f}-${r.join(".")}`;
269
269
  if (p) {
270
- let A = !1, $ = w.signalDomElements.get(p);
270
+ let T = !1, $ = w.signalDomElements.get(p);
271
271
  if ((!$ || $.size === 0) && (a.updateType === "insert" || a.updateType === "cut")) {
272
272
  const x = r.slice(0, -1), G = Y(k, x);
273
273
  if (Array.isArray(G)) {
274
- A = !0;
275
- const T = `${f}-${x.join(".")}`;
276
- $ = w.signalDomElements.get(T);
274
+ T = !0;
275
+ const A = `${f}-${x.join(".")}`;
276
+ $ = w.signalDomElements.get(A);
277
277
  }
278
278
  }
279
279
  if ($) {
280
- const x = A ? Y(k, r.slice(0, -1)) : Y(k, r);
281
- $.forEach(({ parentId: G, position: T, effect: D }) => {
280
+ const x = T ? Y(k, r.slice(0, -1)) : Y(k, r);
281
+ $.forEach(({ parentId: G, position: A, effect: D }) => {
282
282
  const O = document.querySelector(
283
283
  `[data-parent-id="${G}"]`
284
284
  );
285
285
  if (O) {
286
286
  const B = Array.from(O.childNodes);
287
- if (B[T]) {
287
+ if (B[A]) {
288
288
  const E = D ? new Function("state", `return (${D})(state)`)(x) : x;
289
- B[T].textContent = String(E);
289
+ B[A].textContent = String(E);
290
290
  }
291
291
  }
292
292
  });
293
293
  }
294
294
  }
295
- console.log("shadowState", w.shadowStateStore), a.updateType === "update" && (c || R.current?.validation?.key) && r && X(
296
- (c || R.current?.validation?.key) + "." + r.join(".")
295
+ console.log("shadowState", w.shadowStateStore), a.updateType === "update" && (c || M.current?.validation?.key) && r && X(
296
+ (c || M.current?.validation?.key) + "." + r.join(".")
297
297
  );
298
298
  const b = r.slice(0, r.length - 1);
299
- a.updateType === "cut" && R.current?.validation?.key && X(
300
- R.current?.validation?.key + "." + b.join(".")
301
- ), a.updateType === "insert" && R.current?.validation?.key && Ht(
302
- R.current?.validation?.key + "." + b.join(".")
299
+ a.updateType === "cut" && M.current?.validation?.key && X(
300
+ M.current?.validation?.key + "." + b.join(".")
301
+ ), a.updateType === "insert" && M.current?.validation?.key && zt(
302
+ M.current?.validation?.key + "." + b.join(".")
303
303
  ).filter(([$, x]) => {
304
304
  let G = $?.split(".").length;
305
305
  if ($ == b.join(".") && G == b.length - 1) {
306
- let T = $ + "." + b;
307
- X($), _t(T, x);
306
+ let A = $ + "." + b;
307
+ X($), Ct(A, x);
308
308
  }
309
309
  });
310
- const C = w.stateComponents.get(f);
311
- if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", C), C) {
312
- const A = pt(m, k), $ = new Set(A), x = a.updateType === "update" ? r.join(".") : r.slice(0, -1).join(".") || "";
310
+ const _ = w.stateComponents.get(f);
311
+ if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", _), _) {
312
+ const T = wt(m, k), $ = new Set(T), x = a.updateType === "update" ? r.join(".") : r.slice(0, -1).join(".") || "";
313
313
  for (const [
314
314
  G,
315
- T
316
- ] of C.components.entries()) {
315
+ A
316
+ ] of _.components.entries()) {
317
317
  let D = !1;
318
- const O = Array.isArray(T.reactiveType) ? T.reactiveType : [T.reactiveType || "component"];
319
- if (console.log("component", T), !O.includes("none")) {
318
+ const O = Array.isArray(A.reactiveType) ? A.reactiveType : [A.reactiveType || "component"];
319
+ if (console.log("component", A), !O.includes("none")) {
320
320
  if (O.includes("all")) {
321
- T.forceUpdate();
321
+ A.forceUpdate();
322
322
  continue;
323
323
  }
324
- if (O.includes("component") && ((T.paths.has(x) || T.paths.has("")) && (D = !0), !D))
324
+ if (O.includes("component") && ((A.paths.has(x) || A.paths.has("")) && (D = !0), !D))
325
325
  for (const B of $) {
326
326
  let E = B;
327
327
  for (; ; ) {
328
- if (T.paths.has(E)) {
328
+ if (A.paths.has(E)) {
329
329
  D = !0;
330
330
  break;
331
331
  }
332
332
  const N = E.lastIndexOf(".");
333
333
  if (N !== -1) {
334
- const _ = E.substring(
334
+ const C = E.substring(
335
335
  0,
336
336
  N
337
337
  );
338
338
  if (!isNaN(
339
339
  Number(E.substring(N + 1))
340
- ) && T.paths.has(_)) {
340
+ ) && A.paths.has(C)) {
341
341
  D = !0;
342
342
  break;
343
343
  }
344
- E = _;
344
+ E = C;
345
345
  } else
346
346
  E = "";
347
347
  if (E === "")
@@ -349,21 +349,21 @@ function Zt(t, {
349
349
  }
350
350
  if (D) break;
351
351
  }
352
- if (!D && O.includes("deps") && T.depsFunction) {
353
- const B = T.depsFunction(k);
352
+ if (!D && O.includes("deps") && A.depsFunction) {
353
+ const B = A.depsFunction(k);
354
354
  let E = !1;
355
- typeof B == "boolean" ? B && (E = !0) : q(T.deps, B) || (T.deps = B, E = !0), E && (D = !0);
355
+ typeof B == "boolean" ? B && (E = !0) : q(A.deps, B) || (A.deps = B, E = !0), E && (D = !0);
356
356
  }
357
- D && T.forceUpdate();
357
+ D && A.forceUpdate();
358
358
  }
359
359
  }
360
360
  }
361
361
  const P = Date.now();
362
- r = r.map((A, $) => {
362
+ r = r.map((T, $) => {
363
363
  const x = r.slice(0, -1), G = Y(k, x);
364
- return $ === r.length - 1 && ["insert", "cut"].includes(a.updateType) ? (G.length - 1).toString() : A;
364
+ return $ === r.length - 1 && ["insert", "cut"].includes(a.updateType) ? (G.length - 1).toString() : T;
365
365
  });
366
- const { oldValue: M, newValue: H } = Yt(
366
+ const { oldValue: R, newValue: H } = Jt(
367
367
  a.updateType,
368
368
  m,
369
369
  k,
@@ -374,7 +374,7 @@ function Zt(t, {
374
374
  path: r,
375
375
  updateType: a.updateType,
376
376
  status: "new",
377
- oldValue: M,
377
+ oldValue: R,
378
378
  newValue: H
379
379
  };
380
380
  switch (a.updateType) {
@@ -382,33 +382,33 @@ function Zt(t, {
382
382
  w.updateShadowAtPath(f, r, k);
383
383
  break;
384
384
  case "insert":
385
- const A = r.slice(0, -1);
386
- w.insertShadowArrayElement(f, A, H);
385
+ const T = r.slice(0, -1);
386
+ w.insertShadowArrayElement(f, T, H);
387
387
  break;
388
388
  case "cut":
389
389
  const $ = r.slice(0, -1), x = parseInt(r[r.length - 1]);
390
390
  w.removeShadowArrayElement(f, $, x);
391
391
  break;
392
392
  }
393
- if (Bt(f, (A) => {
394
- const x = [...A ?? [], Q].reduce((G, T) => {
395
- const D = `${T.stateKey}:${JSON.stringify(T.path)}`, O = G.get(D);
396
- return O ? (O.timeStamp = Math.max(O.timeStamp, T.timeStamp), O.newValue = T.newValue, O.oldValue = O.oldValue ?? T.oldValue, O.updateType = T.updateType) : G.set(D, { ...T }), G;
393
+ if (Ht(f, (T) => {
394
+ const x = [...T ?? [], Q].reduce((G, A) => {
395
+ const D = `${A.stateKey}:${JSON.stringify(A.path)}`, O = G.get(D);
396
+ return O ? (O.timeStamp = Math.max(O.timeStamp, A.timeStamp), O.newValue = A.newValue, O.oldValue = O.oldValue ?? A.oldValue, O.updateType = A.updateType) : G.set(D, { ...A }), G;
397
397
  }, /* @__PURE__ */ new Map());
398
398
  return Array.from(x.values());
399
- }), $t(
399
+ }), Vt(
400
400
  k,
401
401
  f,
402
- R.current,
402
+ M.current,
403
403
  F
404
- ), R.current?.middleware && R.current.middleware({
404
+ ), M.current?.middleware && M.current.middleware({
405
405
  updateLog: l,
406
406
  update: Q
407
- }), R.current?.serverSync) {
408
- const A = w.serverState[f], $ = R.current?.serverSync;
409
- qt(f, {
407
+ }), M.current?.serverSync) {
408
+ const T = w.serverState[f], $ = M.current?.serverSync;
409
+ Bt(f, {
410
410
  syncKey: typeof $.syncKey == "string" ? $.syncKey : $.syncKey({ state: k }),
411
- rollBackState: A,
411
+ rollBackState: T,
412
412
  actionTimeStamp: Date.now() + ($.debounce ?? 3e3),
413
413
  status: "waiting"
414
414
  });
@@ -424,14 +424,14 @@ function Zt(t, {
424
424
  tt.current,
425
425
  F
426
426
  )
427
- ), o.getState().cogsStateStore[f] || nt(f, t), o.getState().initialStateGlobal[f] || At(f, t));
428
- const d = wt(() => mt(
427
+ ), o.getState().cogsStateStore[f] || nt(f, t), o.getState().initialStateGlobal[f] || Et(f, t));
428
+ const d = It(() => mt(
429
429
  f,
430
430
  at,
431
431
  tt.current,
432
432
  F
433
433
  ), [f, F]);
434
- return [Ct(f), d];
434
+ return [Pt(f), d];
435
435
  }
436
436
  function mt(t, s, S, u) {
437
437
  const v = /* @__PURE__ */ new Map();
@@ -465,8 +465,8 @@ function mt(t, s, S, u) {
465
465
  S,
466
466
  u
467
467
  ), g = o.getState().initialStateGlobal[t], L = rt(t), U = K(L?.localStorage?.key) ? L?.localStorage?.key(g) : L?.localStorage?.key, F = `${u}-${t}-${U}`;
468
- return localStorage.getItem(F) && localStorage.removeItem(F), jt(() => {
469
- At(t, h), o.getState().initializeShadowState(t, h), St(t, n), nt(t, h);
468
+ return localStorage.getItem(F) && localStorage.removeItem(F), Ot(() => {
469
+ Et(t, h), o.getState().initializeShadowState(t, h), St(t, n), nt(t, h);
470
470
  const z = o.getState().stateComponents.get(t);
471
471
  z && z.components.forEach((f) => {
472
472
  f.forceUpdate();
@@ -480,7 +480,7 @@ function mt(t, s, S, u) {
480
480
  _isLoading: o.getState().isLoadingGlobal[t],
481
481
  _isServerSynced: () => {
482
482
  const h = o.getState().serverState[t];
483
- return !!(h && q(h, Ct(t)));
483
+ return !!(h && q(h, Pt(t)));
484
484
  }
485
485
  };
486
486
  function i(h, n = [], g) {
@@ -540,7 +540,7 @@ function mt(t, s, S, u) {
540
540
  }
541
541
  }
542
542
  if (l === "getDifferences")
543
- return () => pt(
543
+ return () => wt(
544
544
  o.getState().cogsStateStore[t],
545
545
  o.getState().initialStateGlobal[t]
546
546
  );
@@ -625,44 +625,44 @@ function mt(t, s, S, u) {
625
625
  } = e, m = J(null), [k, p] = ot({
626
626
  startIndex: 0,
627
627
  endIndex: 10
628
- }), [b, C] = ot(0), P = J(!0), M = J(0), H = J(!1);
628
+ }), [b, _] = ot(0), P = J(!0), R = J(!1), H = J(0);
629
629
  et(() => o.getState().subscribeToShadowState(t, () => {
630
- C((N) => N + 1);
630
+ _((N) => N + 1);
631
631
  }), [t]);
632
632
  const Q = o().getNestedState(
633
633
  t,
634
634
  n
635
- ), A = Q.length, { totalHeight: $, positions: x } = wt(() => {
635
+ ), T = Q.length, { totalHeight: $, positions: x } = It(() => {
636
636
  const E = o.getState().getShadowMetadata(t, n) || [];
637
637
  let N = 0;
638
- const _ = [];
639
- for (let j = 0; j < A; j++) {
640
- _[j] = N;
638
+ const C = [];
639
+ for (let j = 0; j < T; j++) {
640
+ C[j] = N;
641
641
  const W = E[j]?.virtualizer?.itemHeight;
642
642
  N += W || r;
643
643
  }
644
- return { totalHeight: N, positions: _ };
644
+ return { totalHeight: N, positions: C };
645
645
  }, [
646
- A,
646
+ T,
647
647
  t,
648
648
  n.join("."),
649
649
  r,
650
650
  b
651
- ]), G = wt(() => {
652
- const E = Math.max(0, k.startIndex), N = Math.min(A, k.endIndex), _ = Array.from(
651
+ ]), G = It(() => {
652
+ const E = Math.max(0, k.startIndex), N = Math.min(T, k.endIndex), C = Array.from(
653
653
  { length: N - E },
654
654
  (W, it) => E + it
655
- ), j = _.map((W) => Q[W]);
655
+ ), j = C.map((W) => Q[W]);
656
656
  return i(j, n, {
657
657
  ...g,
658
- validIndices: _
658
+ validIndices: C
659
659
  });
660
- }, [k.startIndex, k.endIndex, Q, A]), T = ft(() => {
661
- const E = o.getState().getShadowMetadata(t, n) || [], N = A - 1;
660
+ }, [k.startIndex, k.endIndex, Q, T]), A = ft(() => {
661
+ const E = o.getState().getShadowMetadata(t, n) || [], N = T - 1;
662
662
  if (N >= 0) {
663
- const _ = E[N];
664
- if (_?.virtualizer?.domRef) {
665
- const j = _.virtualizer.domRef;
663
+ const C = E[N];
664
+ if (C?.virtualizer?.domRef) {
665
+ const j = C.virtualizer.domRef;
666
666
  if (j && j.scrollIntoView)
667
667
  return j.scrollIntoView({
668
668
  behavior: "auto",
@@ -672,57 +672,61 @@ function mt(t, s, S, u) {
672
672
  }
673
673
  }
674
674
  return !1;
675
- }, [t, n, A]);
675
+ }, [t, n, T]);
676
676
  et(() => {
677
- if (!c || A === 0) return;
678
- const E = A > M.current, N = M.current === 0 && A > 0;
679
- if ((E || N) && P.current && !H.current) {
680
- const _ = Math.ceil(
677
+ if (!c || T === 0) return;
678
+ const E = T > H.current, N = H.current === 0 && T > 0;
679
+ if ((E || N) && P.current && !R.current) {
680
+ const C = Math.ceil(
681
681
  (m.current?.clientHeight || 0) / r
682
682
  ), j = {
683
683
  startIndex: Math.max(
684
684
  0,
685
- A - _ - a
685
+ T - C - a
686
686
  ),
687
- endIndex: A
687
+ endIndex: T
688
688
  };
689
689
  p(j);
690
690
  const W = setTimeout(() => {
691
- !T() && m.current && (m.current.scrollTop = m.current.scrollHeight);
691
+ m.current && (m.current.scrollTop = m.current.scrollHeight);
692
692
  }, 50);
693
- return M.current = A, () => clearTimeout(W);
693
+ return () => clearTimeout(W);
694
694
  }
695
- M.current = A;
696
- }, [A]), et(() => {
695
+ H.current = T;
696
+ }, [T, r, a]), et(() => {
697
697
  const E = m.current;
698
698
  if (!E) return;
699
699
  const N = () => {
700
- const { scrollTop: _, scrollHeight: j, clientHeight: W } = E, it = j - _ - W, vt = it < 5;
701
- P.current = vt, !vt && it > 50 && (H.current = !0), vt && (H.current = !1);
700
+ const { scrollTop: C, scrollHeight: j, clientHeight: W } = E, it = j - C - W;
701
+ P.current = it < 5, it > 100 && (R.current = !0), it < 5 && (R.current = !1);
702
702
  let ut = 0;
703
703
  for (let Z = 0; Z < x.length; Z++)
704
- if (x[Z] > _ - r * a) {
704
+ if (x[Z] > C - r * a) {
705
705
  ut = Math.max(0, Z - 1);
706
706
  break;
707
707
  }
708
708
  let Tt = ut;
709
- const Mt = _ + W;
710
- for (let Z = ut; Z < x.length && !(x[Z] > Mt + r * a); Z++)
709
+ const Rt = C + W;
710
+ for (let Z = ut; Z < x.length && !(x[Z] > Rt + r * a); Z++)
711
711
  Tt = Z;
712
712
  p({
713
713
  startIndex: Math.max(0, ut),
714
- endIndex: Math.min(A, Tt + 1 + a)
714
+ endIndex: Math.min(T, Tt + 1 + a)
715
715
  });
716
716
  };
717
- return E.addEventListener("scroll", N, {
717
+ if (E.addEventListener("scroll", N, {
718
718
  passive: !0
719
- }), c && A > 0 && (E.scrollTop = E.scrollHeight), N(), () => {
719
+ }), c && T > 0 && !R.current) {
720
+ const { scrollTop: C } = E;
721
+ C === 0 && (E.scrollTop = E.scrollHeight, P.current = !0);
722
+ }
723
+ return N(), () => {
720
724
  E.removeEventListener("scroll", N);
721
725
  };
722
- }, [x, A, r, a, c]);
726
+ }, [x, T, r, a, c]);
723
727
  const D = ft(() => {
724
- P.current = !0, !T() && m.current && (m.current.scrollTop = m.current.scrollHeight);
725
- }, [T]), O = ft(
728
+ P.current = !0, R.current = !1, !A() && m.current && (m.current.scrollTop = m.current.scrollHeight);
729
+ }, [A]), O = ft(
726
730
  (E, N = "smooth") => {
727
731
  const j = (o.getState().getShadowMetadata(t, n) || [])[E];
728
732
  if (j?.virtualizer?.domRef) {
@@ -776,7 +780,7 @@ function mt(t, s, S, u) {
776
780
  );
777
781
  });
778
782
  if (l === "$stateMap")
779
- return (e) => ct(Xt, {
783
+ return (e) => ct(Zt, {
780
784
  proxy: {
781
785
  _stateKey: t,
782
786
  _path: n,
@@ -790,7 +794,7 @@ function mt(t, s, S, u) {
790
794
  const r = o.getState().getNestedState(t, n);
791
795
  return Array.isArray(r) ? (g?.validIndices || Array.from({ length: r.length }, (c, w) => w)).map((c, w) => {
792
796
  const m = r[c], k = [...n, c.toString()], p = i(m, k, g), b = `${S}-${n.join(".")}-${c}`;
793
- return ct(Kt, {
797
+ return ct(Qt, {
794
798
  key: c,
795
799
  stateKey: t,
796
800
  itemComponentId: b,
@@ -833,7 +837,7 @@ function mt(t, s, S, u) {
833
837
  return i(a, c);
834
838
  };
835
839
  if (l === "insert")
836
- return (e) => (I(n), yt(s, e, n, t), i(
840
+ return (e) => (I(n), vt(s, e, n, t), i(
837
841
  o.getState().getNestedState(t, n),
838
842
  n
839
843
  ));
@@ -843,18 +847,18 @@ function mt(t, s, S, u) {
843
847
  let m = null;
844
848
  if (!c.some((p) => {
845
849
  if (r) {
846
- const C = r.every(
850
+ const _ = r.every(
847
851
  (P) => q(p[P], w[P])
848
852
  );
849
- return C && (m = p), C;
853
+ return _ && (m = p), _;
850
854
  }
851
855
  const b = q(p, w);
852
856
  return b && (m = p), b;
853
857
  }))
854
- I(n), yt(s, w, n, t);
858
+ I(n), vt(s, w, n, t);
855
859
  else if (a && m) {
856
860
  const p = a(m), b = c.map(
857
- (C) => q(C, m) ? p : C
861
+ (_) => q(_, m) ? p : _
858
862
  );
859
863
  I(n), st(s, b, n);
860
864
  }
@@ -875,7 +879,7 @@ function mt(t, s, S, u) {
875
879
  if (l === "toggleByValue")
876
880
  return (e) => {
877
881
  const r = h.findIndex((a) => a === e);
878
- r > -1 ? gt(s, n, t, r) : yt(s, e, n, t);
882
+ r > -1 ? gt(s, n, t, r) : vt(s, e, n, t);
879
883
  };
880
884
  if (l === "stateFind")
881
885
  return (e) => {
@@ -916,13 +920,13 @@ function mt(t, s, S, u) {
916
920
  return o.getState().getNestedState(t, n);
917
921
  };
918
922
  if (l === "$derive")
919
- return (d) => xt({
923
+ return (d) => $t({
920
924
  _stateKey: t,
921
925
  _path: n,
922
926
  _effect: d.toString()
923
927
  });
924
928
  if (l === "$get")
925
- return () => xt({
929
+ return () => $t({
926
930
  _stateKey: t,
927
931
  _path: n
928
932
  });
@@ -962,13 +966,13 @@ function mt(t, s, S, u) {
962
966
  throw new Error("Validation key not found");
963
967
  X(e.key), console.log("addValidationError", d), d.forEach((r) => {
964
968
  const a = [e.key, ...r.path].join(".");
965
- console.log("fullErrorPath", a), _t(a, r.message);
969
+ console.log("fullErrorPath", a), Ct(a, r.message);
966
970
  }), lt(t);
967
971
  };
968
972
  if (l === "applyJsonPatch")
969
973
  return (d) => {
970
- const e = o.getState().cogsStateStore[t], a = Wt(e, d).newDocument;
971
- Rt(
974
+ const e = o.getState().cogsStateStore[t], a = Gt(e, d).newDocument;
975
+ _t(
972
976
  t,
973
977
  o.getState().initialStateGlobal[t],
974
978
  a,
@@ -978,40 +982,40 @@ function mt(t, s, S, u) {
978
982
  );
979
983
  const c = o.getState().stateComponents.get(t);
980
984
  if (c) {
981
- const w = pt(e, a), m = new Set(w);
985
+ const w = wt(e, a), m = new Set(w);
982
986
  for (const [
983
987
  k,
984
988
  p
985
989
  ] of c.components.entries()) {
986
990
  let b = !1;
987
- const C = Array.isArray(p.reactiveType) ? p.reactiveType : [p.reactiveType || "component"];
988
- if (!C.includes("none")) {
989
- if (C.includes("all")) {
991
+ const _ = Array.isArray(p.reactiveType) ? p.reactiveType : [p.reactiveType || "component"];
992
+ if (!_.includes("none")) {
993
+ if (_.includes("all")) {
990
994
  p.forceUpdate();
991
995
  continue;
992
996
  }
993
- if (C.includes("component") && (p.paths.has("") && (b = !0), !b))
997
+ if (_.includes("component") && (p.paths.has("") && (b = !0), !b))
994
998
  for (const P of m) {
995
999
  if (p.paths.has(P)) {
996
1000
  b = !0;
997
1001
  break;
998
1002
  }
999
- let M = P.lastIndexOf(".");
1000
- for (; M !== -1; ) {
1001
- const H = P.substring(0, M);
1003
+ let R = P.lastIndexOf(".");
1004
+ for (; R !== -1; ) {
1005
+ const H = P.substring(0, R);
1002
1006
  if (p.paths.has(H)) {
1003
1007
  b = !0;
1004
1008
  break;
1005
1009
  }
1006
1010
  const Q = P.substring(
1007
- M + 1
1011
+ R + 1
1008
1012
  );
1009
1013
  if (!isNaN(Number(Q))) {
1010
- const A = H.lastIndexOf(".");
1011
- if (A !== -1) {
1014
+ const T = H.lastIndexOf(".");
1015
+ if (T !== -1) {
1012
1016
  const $ = H.substring(
1013
1017
  0,
1014
- A
1018
+ T
1015
1019
  );
1016
1020
  if (p.paths.has($)) {
1017
1021
  b = !0;
@@ -1019,14 +1023,14 @@ function mt(t, s, S, u) {
1019
1023
  }
1020
1024
  }
1021
1025
  }
1022
- M = H.lastIndexOf(".");
1026
+ R = H.lastIndexOf(".");
1023
1027
  }
1024
1028
  if (b) break;
1025
1029
  }
1026
- if (!b && C.includes("deps") && p.depsFunction) {
1030
+ if (!b && _.includes("deps") && p.depsFunction) {
1027
1031
  const P = p.depsFunction(a);
1028
- let M = !1;
1029
- typeof P == "boolean" ? P && (M = !0) : q(p.deps, P) || (p.deps = P, M = !0), M && (b = !0);
1032
+ let R = !1;
1033
+ typeof P == "boolean" ? P && (R = !0) : q(p.deps, P) || (p.deps = P, R = !0), R && (b = !0);
1030
1034
  }
1031
1035
  b && p.forceUpdate();
1032
1036
  }
@@ -1078,8 +1082,8 @@ function mt(t, s, S, u) {
1078
1082
  return ({
1079
1083
  children: d,
1080
1084
  hideMessage: e
1081
- }) => /* @__PURE__ */ It(
1082
- Dt,
1085
+ }) => /* @__PURE__ */ yt(
1086
+ Ft,
1083
1087
  {
1084
1088
  formOpts: e ? { validation: { message: "" } } : void 0,
1085
1089
  path: n,
@@ -1095,7 +1099,7 @@ function mt(t, s, S, u) {
1095
1099
  if (l === "update")
1096
1100
  return (d, e) => {
1097
1101
  if (e?.debounce)
1098
- Ft(() => {
1102
+ Ut(() => {
1099
1103
  st(s, d, n, "");
1100
1104
  const r = o.getState().getNestedState(t, n);
1101
1105
  e?.afterUpdate && e.afterUpdate(r);
@@ -1108,8 +1112,8 @@ function mt(t, s, S, u) {
1108
1112
  I(n);
1109
1113
  };
1110
1114
  if (l === "formElement")
1111
- return (d, e) => /* @__PURE__ */ It(
1112
- Lt,
1115
+ return (d, e) => /* @__PURE__ */ yt(
1116
+ Dt,
1113
1117
  {
1114
1118
  setState: s,
1115
1119
  stateKey: t,
@@ -1118,8 +1122,8 @@ function mt(t, s, S, u) {
1118
1122
  formOpts: e
1119
1123
  }
1120
1124
  );
1121
- const R = [...n, l], at = o.getState().getNestedState(t, R);
1122
- return i(at, R, g);
1125
+ const M = [...n, l], at = o.getState().getNestedState(t, M);
1126
+ return i(at, M, g);
1123
1127
  }
1124
1128
  }, z = new Proxy(U, F);
1125
1129
  return v.set(L, {
@@ -1131,10 +1135,10 @@ function mt(t, s, S, u) {
1131
1135
  o.getState().getNestedState(t, [])
1132
1136
  );
1133
1137
  }
1134
- function xt(t) {
1135
- return ct(Qt, { proxy: t });
1138
+ function $t(t) {
1139
+ return ct(Xt, { proxy: t });
1136
1140
  }
1137
- function Xt({
1141
+ function Zt({
1138
1142
  proxy: t,
1139
1143
  rebuildStateShape: s
1140
1144
  }) {
@@ -1146,7 +1150,7 @@ function Xt({
1146
1150
  (v, V, I, y, i) => t._mapFn(v, V, I, y, i)
1147
1151
  ) : null;
1148
1152
  }
1149
- function Qt({
1153
+ function Xt({
1150
1154
  proxy: t
1151
1155
  }) {
1152
1156
  const s = J(null), S = `${t._stateKey}-${t._path.join(".")}`;
@@ -1185,8 +1189,8 @@ function Qt({
1185
1189
  "data-signal-id": S
1186
1190
  });
1187
1191
  }
1188
- function Se(t) {
1189
- const s = Ot(
1192
+ function fe(t) {
1193
+ const s = Mt(
1190
1194
  (S) => {
1191
1195
  const u = o.getState().stateComponents.get(t._stateKey) || {
1192
1196
  components: /* @__PURE__ */ new Map()
@@ -1200,13 +1204,13 @@ function Se(t) {
1200
1204
  );
1201
1205
  return ct("text", {}, String(s));
1202
1206
  }
1203
- function Kt({
1207
+ function Qt({
1204
1208
  stateKey: t,
1205
1209
  itemComponentId: s,
1206
1210
  itemPath: S,
1207
1211
  children: u
1208
1212
  }) {
1209
- const [, v] = ot({}), [V, I] = zt(), y = J(null), i = J(null), h = ft(
1213
+ const [, v] = ot({}), [V, I] = Wt(), y = J(null), i = J(null), h = ft(
1210
1214
  (n) => {
1211
1215
  V(n), y.current = n;
1212
1216
  },
@@ -1220,7 +1224,7 @@ function Kt({
1220
1224
  // Store the actual DOM element reference
1221
1225
  }
1222
1226
  }));
1223
- }, [I.height, t, S]), Nt(() => {
1227
+ }, [I.height, t, S]), xt(() => {
1224
1228
  const n = `${t}////${s}`, g = o.getState().stateComponents.get(t) || {
1225
1229
  components: /* @__PURE__ */ new Map()
1226
1230
  };
@@ -1231,14 +1235,14 @@ function Kt({
1231
1235
  const L = o.getState().stateComponents.get(t);
1232
1236
  L && L.components.delete(n);
1233
1237
  };
1234
- }, [t, s, S.join(".")]), /* @__PURE__ */ It("div", { ref: h, children: u });
1238
+ }, [t, s, S.join(".")]), /* @__PURE__ */ yt("div", { ref: h, children: u });
1235
1239
  }
1236
1240
  export {
1237
- xt as $cogsSignal,
1238
- Se as $cogsSignalStore,
1239
- ue as addStateOptions,
1240
- ge as createCogsState,
1241
- fe as notifyComponent,
1242
- Zt as useCogsStateFn
1241
+ $t as $cogsSignal,
1242
+ fe as $cogsSignalStore,
1243
+ de as addStateOptions,
1244
+ ue as createCogsState,
1245
+ ge as notifyComponent,
1246
+ Yt as useCogsStateFn
1243
1247
  };
1244
1248
  //# sourceMappingURL=CogsState.jsx.map