cogsbox-state 0.5.287 → 0.5.288

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,7 +1,7 @@
1
1
  "use client";
2
2
  import { jsx as vt } from "react/jsx-runtime";
3
- import { useState as rt, useRef as Y, useEffect as lt, useLayoutEffect as dt, useMemo as yt, createElement as st, useSyncExternalStore as xt, startTransition as Pt, useCallback as mt } from "react";
4
- import { transformStateFunc as _t, isDeepEqual as H, isFunction as Z, getNestedValue as z, getDifferences as It, debounce as Mt } from "./utility.js";
3
+ import { useState as rt, useRef as Z, useEffect as lt, useLayoutEffect as dt, useMemo as yt, createElement as st, useSyncExternalStore as xt, startTransition as Pt, useCallback as mt } from "react";
4
+ import { transformStateFunc as _t, isDeepEqual as H, isFunction as X, getNestedValue as z, getDifferences as It, debounce as Mt } from "./utility.js";
5
5
  import { pushFunc as ht, updateFn as at, cutFunc as ct, ValidationWrapper as jt, FormControlComponent as Ot } from "./Functions.jsx";
6
6
  import Rt from "superjson";
7
7
  import { v4 as pt } from "uuid";
@@ -52,7 +52,7 @@ const ce = (t, c) => {
52
52
  options: a,
53
53
  initialOptionsPart: y
54
54
  });
55
- const n = o.getState().cogsStateStore[I] || f[I], S = a?.modifyState ? a.modifyState(n) : n, [W, R] = Bt(
55
+ const n = o.getState().cogsStateStore[I] || f[I], S = a?.modifyState ? a.modifyState(n) : n, [W, F] = Bt(
56
56
  S,
57
57
  {
58
58
  stateKey: I,
@@ -68,7 +68,7 @@ const ce = (t, c) => {
68
68
  serverState: a?.serverState
69
69
  }
70
70
  );
71
- return R;
71
+ return F;
72
72
  };
73
73
  function p(I, a) {
74
74
  Tt({ stateKey: I, options: a, initialOptionsPart: y }), a.localStorage && Ht(I, a), St(I);
@@ -83,7 +83,7 @@ const ce = (t, c) => {
83
83
  setStateLog: Wt,
84
84
  updateInitialStateGlobal: wt,
85
85
  addValidationError: Gt,
86
- removeValidationError: J,
86
+ removeValidationError: Y,
87
87
  setServerSyncActions: Lt
88
88
  } = o.getState(), $t = (t, c, v, f, y) => {
89
89
  v?.log && console.log(
@@ -92,7 +92,7 @@ const ce = (t, c) => {
92
92
  v.localStorage?.key,
93
93
  f
94
94
  );
95
- const k = Z(v?.localStorage?.key) ? v.localStorage?.key(t) : v?.localStorage?.key;
95
+ const k = X(v?.localStorage?.key) ? v.localStorage?.key(t) : v?.localStorage?.key;
96
96
  if (k && f) {
97
97
  const p = `${f}-${c}-${k}`;
98
98
  let I;
@@ -119,7 +119,7 @@ const ce = (t, c) => {
119
119
  return console.error("Error loading from localStorage:", c), null;
120
120
  }
121
121
  }, Ht = (t, c) => {
122
- const v = o.getState().cogsStateStore[t], { sessionId: f } = Nt(), y = Z(c?.localStorage?.key) ? c.localStorage.key(v) : c?.localStorage?.key;
122
+ const v = o.getState().cogsStateStore[t], { sessionId: f } = Nt(), y = X(c?.localStorage?.key) ? c.localStorage.key(v) : c?.localStorage?.key;
123
123
  if (y && f) {
124
124
  const k = ft(
125
125
  `${f}-${t}-${y}`
@@ -193,12 +193,12 @@ function Bt(t, {
193
193
  dependencies: n,
194
194
  serverState: S
195
195
  } = {}) {
196
- const [W, R] = rt({}), { sessionId: F } = Nt();
196
+ const [W, F] = rt({}), { sessionId: U } = Nt();
197
197
  let G = !c;
198
- const [h] = rt(c ?? pt()), l = o.getState().stateLog[h], it = Y(/* @__PURE__ */ new Set()), X = Y(I ?? pt()), j = Y(
198
+ const [h] = rt(c ?? pt()), l = o.getState().stateLog[h], it = Z(/* @__PURE__ */ new Set()), Q = Z(I ?? pt()), O = Z(
199
199
  null
200
200
  );
201
- j.current = nt(h) ?? null, lt(() => {
201
+ O.current = nt(h) ?? null, lt(() => {
202
202
  if (m && m.stateKey === h && m.path?.[0]) {
203
203
  tt(h, (r) => ({
204
204
  ...r,
@@ -215,22 +215,22 @@ function Bt(t, {
215
215
  At(h, {
216
216
  initialState: a
217
217
  });
218
- const e = j.current, s = e?.serverState?.id !== void 0 && e?.serverState?.status === "success" && e?.serverState?.data, i = o.getState().initialStateGlobal[h];
218
+ const e = O.current, s = e?.serverState?.id !== void 0 && e?.serverState?.status === "success" && e?.serverState?.data, i = o.getState().initialStateGlobal[h];
219
219
  if (!(i && !H(i, a) || !i) && !s)
220
220
  return;
221
221
  let u = null;
222
- const A = Z(e?.localStorage?.key) ? e?.localStorage?.key(a) : e?.localStorage?.key;
223
- A && F && (u = ft(`${F}-${h}-${A}`));
222
+ const A = X(e?.localStorage?.key) ? e?.localStorage?.key(a) : e?.localStorage?.key;
223
+ A && U && (u = ft(`${U}-${h}-${A}`));
224
224
  let w = a, T = !1;
225
- const P = s ? Date.now() : 0, x = u?.lastUpdated || 0, b = u?.lastSyncedWithServer || 0;
226
- s && P > x ? (w = e.serverState.data, T = !0) : u && x > b && (w = u.state, e?.localStorage?.onChange && e?.localStorage?.onChange(w)), o.getState().initializeShadowState(h, a), Ct(
225
+ const x = s ? Date.now() : 0, V = u?.lastUpdated || 0, _ = u?.lastSyncedWithServer || 0;
226
+ s && x > V ? (w = e.serverState.data, T = !0) : u && V > _ && (w = u.state, e?.localStorage?.onChange && e?.localStorage?.onChange(w)), o.getState().initializeShadowState(h, a), Ct(
227
227
  h,
228
228
  a,
229
229
  w,
230
230
  ot,
231
- X.current,
232
- F
233
- ), T && A && F && $t(w, h, e, F, Date.now()), St(h), (Array.isArray(p) ? p : [p || "component"]).includes("none") || R({});
231
+ Q.current,
232
+ U
233
+ ), T && A && U && $t(w, h, e, U, Date.now()), St(h), (Array.isArray(p) ? p : [p || "component"]).includes("none") || F({});
234
234
  }
235
235
  }, [
236
236
  a,
@@ -243,18 +243,18 @@ function Bt(t, {
243
243
  formElements: y,
244
244
  initialState: a,
245
245
  localStorage: f,
246
- middleware: j.current?.middleware
246
+ middleware: O.current?.middleware
247
247
  });
248
- const e = `${h}////${X.current}`, r = o.getState().stateComponents.get(h) || {
248
+ const e = `${h}////${Q.current}`, r = o.getState().stateComponents.get(h) || {
249
249
  components: /* @__PURE__ */ new Map()
250
250
  };
251
251
  return r.components.set(e, {
252
- forceUpdate: () => R({}),
252
+ forceUpdate: () => F({}),
253
253
  paths: /* @__PURE__ */ new Set(),
254
254
  deps: [],
255
255
  depsFunction: k || void 0,
256
256
  reactiveType: p ?? ["component", "deps"]
257
- }), o.getState().stateComponents.set(h, r), R({}), () => {
257
+ }), o.getState().stateComponents.set(h, r), F({}), () => {
258
258
  r && (r.components.delete(e), r.components.size === 0 && o.getState().stateComponents.delete(h));
259
259
  };
260
260
  }, []);
@@ -265,151 +265,151 @@ function Bt(t, {
265
265
  }
266
266
  const g = o.getState();
267
267
  tt(h, (u) => {
268
- const A = Z(e) ? e(u) : e, w = `${h}-${r.join(".")}`;
268
+ const A = X(e) ? e(u) : e, w = `${h}-${r.join(".")}`;
269
269
  if (w) {
270
- let C = !1, N = g.signalDomElements.get(w);
271
- if ((!N || N.size === 0) && (s.updateType === "insert" || s.updateType === "cut")) {
272
- const O = r.slice(0, -1), D = z(A, O);
270
+ let N = !1, b = g.signalDomElements.get(w);
271
+ if ((!b || b.size === 0) && (s.updateType === "insert" || s.updateType === "cut")) {
272
+ const R = r.slice(0, -1), D = z(A, R);
273
273
  if (Array.isArray(D)) {
274
- C = !0;
275
- const $ = `${h}-${O.join(".")}`;
276
- N = g.signalDomElements.get($);
274
+ N = !0;
275
+ const $ = `${h}-${R.join(".")}`;
276
+ b = g.signalDomElements.get($);
277
277
  }
278
278
  }
279
- if (N) {
280
- const O = C ? z(A, r.slice(0, -1)) : z(A, r);
281
- N.forEach(({ parentId: D, position: $, effect: E }) => {
282
- const V = document.querySelector(
279
+ if (b) {
280
+ const R = N ? z(A, r.slice(0, -1)) : z(A, r);
281
+ b.forEach(({ parentId: D, position: $, effect: E }) => {
282
+ const C = document.querySelector(
283
283
  `[data-parent-id="${D}"]`
284
284
  );
285
- if (V) {
286
- const _ = Array.from(V.childNodes);
287
- if (_[$]) {
288
- const M = E ? new Function("state", `return (${E})(state)`)(O) : O;
289
- _[$].textContent = String(M);
285
+ if (C) {
286
+ const M = Array.from(C.childNodes);
287
+ if (M[$]) {
288
+ const j = E ? new Function("state", `return (${E})(state)`)(R) : R;
289
+ M[$].textContent = String(j);
290
290
  }
291
291
  }
292
292
  });
293
293
  }
294
294
  }
295
- console.log("shadowState", g.shadowStateStore), s.updateType === "update" && (i || j.current?.validation?.key) && r && J(
296
- (i || j.current?.validation?.key) + "." + r.join(".")
295
+ console.log("shadowState", g.shadowStateStore), s.updateType === "update" && (i || O.current?.validation?.key) && r && Y(
296
+ (i || O.current?.validation?.key) + "." + r.join(".")
297
297
  );
298
298
  const T = r.slice(0, r.length - 1);
299
- s.updateType === "cut" && j.current?.validation?.key && J(
300
- j.current?.validation?.key + "." + T.join(".")
301
- ), s.updateType === "insert" && j.current?.validation?.key && Dt(
302
- j.current?.validation?.key + "." + T.join(".")
303
- ).filter(([N, O]) => {
304
- let D = N?.split(".").length;
305
- if (N == T.join(".") && D == T.length - 1) {
306
- let $ = N + "." + T;
307
- J(N), Gt($, O);
299
+ s.updateType === "cut" && O.current?.validation?.key && Y(
300
+ O.current?.validation?.key + "." + T.join(".")
301
+ ), s.updateType === "insert" && O.current?.validation?.key && Dt(
302
+ O.current?.validation?.key + "." + T.join(".")
303
+ ).filter(([b, R]) => {
304
+ let D = b?.split(".").length;
305
+ if (b == T.join(".") && D == T.length - 1) {
306
+ let $ = b + "." + T;
307
+ Y(b), Gt($, R);
308
308
  }
309
309
  });
310
- const P = g.stateComponents.get(h);
311
- if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", P), P) {
312
- const C = It(u, A), N = new Set(C), O = s.updateType === "update" ? r.join(".") : r.slice(0, -1).join(".") || "";
310
+ const x = g.stateComponents.get(h);
311
+ if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", x), x) {
312
+ const N = It(u, A), b = new Set(N), R = s.updateType === "update" ? r.join(".") : r.slice(0, -1).join(".") || "";
313
313
  for (const [
314
314
  D,
315
315
  $
316
- ] of P.components.entries()) {
316
+ ] of x.components.entries()) {
317
317
  let E = !1;
318
- const V = Array.isArray($.reactiveType) ? $.reactiveType : [$.reactiveType || "component"];
319
- if (console.log("component", $), !V.includes("none")) {
320
- if (V.includes("all")) {
318
+ const C = Array.isArray($.reactiveType) ? $.reactiveType : [$.reactiveType || "component"];
319
+ if (console.log("component", $), !C.includes("none")) {
320
+ if (C.includes("all")) {
321
321
  $.forceUpdate();
322
322
  continue;
323
323
  }
324
- if (V.includes("component") && (($.paths.has(O) || $.paths.has("")) && (E = !0), !E))
325
- for (const _ of N) {
326
- let M = _;
324
+ if (C.includes("component") && (($.paths.has(R) || $.paths.has("")) && (E = !0), !E))
325
+ for (const M of b) {
326
+ let j = M;
327
327
  for (; ; ) {
328
- if ($.paths.has(M)) {
328
+ if ($.paths.has(j)) {
329
329
  E = !0;
330
330
  break;
331
331
  }
332
- const L = M.lastIndexOf(".");
332
+ const L = j.lastIndexOf(".");
333
333
  if (L !== -1) {
334
- const Q = M.substring(
334
+ const K = j.substring(
335
335
  0,
336
336
  L
337
337
  );
338
338
  if (!isNaN(
339
- Number(M.substring(L + 1))
340
- ) && $.paths.has(Q)) {
339
+ Number(j.substring(L + 1))
340
+ ) && $.paths.has(K)) {
341
341
  E = !0;
342
342
  break;
343
343
  }
344
- M = Q;
344
+ j = K;
345
345
  } else
346
- M = "";
347
- if (M === "")
346
+ j = "";
347
+ if (j === "")
348
348
  break;
349
349
  }
350
350
  if (E) break;
351
351
  }
352
- if (!E && V.includes("deps") && $.depsFunction) {
353
- const _ = $.depsFunction(A);
354
- let M = !1;
355
- typeof _ == "boolean" ? _ && (M = !0) : H($.deps, _) || ($.deps = _, M = !0), M && (E = !0);
352
+ if (!E && C.includes("deps") && $.depsFunction) {
353
+ const M = $.depsFunction(A);
354
+ let j = !1;
355
+ typeof M == "boolean" ? M && (j = !0) : H($.deps, M) || ($.deps = M, j = !0), j && (E = !0);
356
356
  }
357
357
  E && $.forceUpdate();
358
358
  }
359
359
  }
360
360
  }
361
- const x = Date.now();
362
- r = r.map((C, N) => {
363
- const O = r.slice(0, -1), D = z(A, O);
364
- return N === r.length - 1 && ["insert", "cut"].includes(s.updateType) ? (D.length - 1).toString() : C;
361
+ const V = Date.now();
362
+ r = r.map((N, b) => {
363
+ const R = r.slice(0, -1), D = z(A, R);
364
+ return b === r.length - 1 && ["insert", "cut"].includes(s.updateType) ? (D.length - 1).toString() : N;
365
365
  });
366
- const { oldValue: b, newValue: U } = zt(
366
+ const { oldValue: _, newValue: P } = zt(
367
367
  s.updateType,
368
368
  u,
369
369
  A,
370
370
  r
371
371
  ), B = {
372
- timeStamp: x,
372
+ timeStamp: V,
373
373
  stateKey: h,
374
374
  path: r,
375
375
  updateType: s.updateType,
376
376
  status: "new",
377
- oldValue: b,
378
- newValue: U
377
+ oldValue: _,
378
+ newValue: P
379
379
  };
380
380
  switch (s.updateType) {
381
381
  case "update":
382
382
  g.updateShadowAtPath(h, r, A);
383
383
  break;
384
384
  case "insert":
385
- const C = r.slice(0, -1);
386
- g.insertShadowArrayElement(h, C, U);
385
+ const N = r.slice(0, -1);
386
+ g.insertShadowArrayElement(h, N, P);
387
387
  break;
388
388
  case "cut":
389
- const N = r.slice(0, -1), O = parseInt(r[r.length - 1]);
390
- g.removeShadowArrayElement(h, N, O);
389
+ const b = r.slice(0, -1), R = parseInt(r[r.length - 1]);
390
+ g.removeShadowArrayElement(h, b, R);
391
391
  break;
392
392
  }
393
- if (Wt(h, (C) => {
394
- const O = [...C ?? [], B].reduce((D, $) => {
395
- const E = `${$.stateKey}:${JSON.stringify($.path)}`, V = D.get(E);
396
- return V ? (V.timeStamp = Math.max(V.timeStamp, $.timeStamp), V.newValue = $.newValue, V.oldValue = V.oldValue ?? $.oldValue, V.updateType = $.updateType) : D.set(E, { ...$ }), D;
393
+ if (Wt(h, (N) => {
394
+ const R = [...N ?? [], B].reduce((D, $) => {
395
+ const E = `${$.stateKey}:${JSON.stringify($.path)}`, C = D.get(E);
396
+ return C ? (C.timeStamp = Math.max(C.timeStamp, $.timeStamp), C.newValue = $.newValue, C.oldValue = C.oldValue ?? $.oldValue, C.updateType = $.updateType) : D.set(E, { ...$ }), D;
397
397
  }, /* @__PURE__ */ new Map());
398
- return Array.from(O.values());
398
+ return Array.from(R.values());
399
399
  }), $t(
400
400
  A,
401
401
  h,
402
- j.current,
403
- F
404
- ), j.current?.middleware && j.current.middleware({
402
+ O.current,
403
+ U
404
+ ), O.current?.middleware && O.current.middleware({
405
405
  updateLog: l,
406
406
  update: B
407
- }), j.current?.serverSync) {
408
- const C = g.serverState[h], N = j.current?.serverSync;
407
+ }), O.current?.serverSync) {
408
+ const N = g.serverState[h], b = O.current?.serverSync;
409
409
  Lt(h, {
410
- syncKey: typeof N.syncKey == "string" ? N.syncKey : N.syncKey({ state: A }),
411
- rollBackState: C,
412
- actionTimeStamp: Date.now() + (N.debounce ?? 3e3),
410
+ syncKey: typeof b.syncKey == "string" ? b.syncKey : b.syncKey({ state: A }),
411
+ rollBackState: N,
412
+ actionTimeStamp: Date.now() + (b.debounce ?? 3e3),
413
413
  status: "waiting"
414
414
  });
415
415
  }
@@ -421,16 +421,16 @@ function Bt(t, {
421
421
  gt(
422
422
  h,
423
423
  ot,
424
- X.current,
425
- F
424
+ Q.current,
425
+ U
426
426
  )
427
427
  ), o.getState().cogsStateStore[h] || tt(h, t), o.getState().initialStateGlobal[h] || wt(h, t));
428
428
  const d = yt(() => gt(
429
429
  h,
430
430
  ot,
431
- X.current,
432
- F
433
- ), [h, F]);
431
+ Q.current,
432
+ U
433
+ ), [h, U]);
434
434
  return [bt(h), d];
435
435
  }
436
436
  function gt(t, c, v, f) {
@@ -443,14 +443,14 @@ function gt(t, c, v, f) {
443
443
  k++;
444
444
  }, I = {
445
445
  removeValidation: (m) => {
446
- m?.validationKey && J(m.validationKey);
446
+ m?.validationKey && Y(m.validationKey);
447
447
  },
448
448
  revertToInitialState: (m) => {
449
449
  const n = o.getState().getInitialOptions(t)?.validation;
450
- n?.key && J(n?.key), m?.validationKey && J(m.validationKey);
450
+ n?.key && Y(n?.key), m?.validationKey && Y(m.validationKey);
451
451
  const S = o.getState().initialStateGlobal[t];
452
452
  o.getState().clearSelectedIndexesForState(t), y.clear(), k++;
453
- const W = a(S, []), R = nt(t), F = Z(R?.localStorage?.key) ? R?.localStorage?.key(S) : R?.localStorage?.key, G = `${f}-${t}-${F}`;
453
+ const W = a(S, []), F = nt(t), U = X(F?.localStorage?.key) ? F?.localStorage?.key(S) : F?.localStorage?.key, G = `${f}-${t}-${U}`;
454
454
  G && localStorage.removeItem(G), ut(t, W), tt(t, S);
455
455
  const h = o.getState().stateComponents.get(t);
456
456
  return h && h.components.forEach((l) => {
@@ -464,8 +464,8 @@ function gt(t, c, v, f) {
464
464
  c,
465
465
  v,
466
466
  f
467
- ), S = o.getState().initialStateGlobal[t], W = nt(t), R = Z(W?.localStorage?.key) ? W?.localStorage?.key(S) : W?.localStorage?.key, F = `${f}-${t}-${R}`;
468
- return localStorage.getItem(F) && localStorage.removeItem(F), console.log("udpating intial State", t, m), Pt(() => {
467
+ ), S = o.getState().initialStateGlobal[t], W = nt(t), F = X(W?.localStorage?.key) ? W?.localStorage?.key(S) : W?.localStorage?.key, U = `${f}-${t}-${F}`;
468
+ return localStorage.getItem(U) && localStorage.removeItem(U), console.log("udpating intial State", t, m), Pt(() => {
469
469
  wt(t, m), o.getState().initializeShadowState(t, m), ut(t, n), tt(t, m);
470
470
  const G = o.getState().stateComponents.get(t);
471
471
  G && G.components.forEach((h) => {
@@ -486,13 +486,13 @@ function gt(t, c, v, f) {
486
486
  function a(m, n = [], S) {
487
487
  const W = n.map(String).join(".");
488
488
  y.get(W);
489
- const R = function() {
489
+ const F = function() {
490
490
  return o().getNestedState(t, n);
491
491
  };
492
492
  Object.keys(I).forEach((h) => {
493
- R[h] = I[h];
493
+ F[h] = I[h];
494
494
  });
495
- const F = {
495
+ const U = {
496
496
  apply(h, l, it) {
497
497
  return console.log(
498
498
  `PROXY APPLY TRAP HIT: stateKey=${t}, path=${n.join(".")}`
@@ -581,7 +581,7 @@ function gt(t, c, v, f) {
581
581
  };
582
582
  if (l === "removeStorage")
583
583
  return () => {
584
- const d = o.getState().initialStateGlobal[t], e = nt(t), r = Z(e?.localStorage?.key) ? e?.localStorage?.key(d) : e?.localStorage?.key, s = `${f}-${t}-${r}`;
584
+ const d = o.getState().initialStateGlobal[t], e = nt(t), r = X(e?.localStorage?.key) ? e?.localStorage?.key(d) : e?.localStorage?.key, s = `${f}-${t}-${r}`;
585
585
  s && localStorage.removeItem(s);
586
586
  };
587
587
  if (l === "showValidationErrors")
@@ -619,66 +619,66 @@ function gt(t, c, v, f) {
619
619
  return (e) => {
620
620
  const {
621
621
  itemHeight: r = 50,
622
- // Default height for unmeasured items
622
+ // Serves as a fallback for unmeasured items
623
623
  overscan: s = 5,
624
624
  stickToBottom: i = !1
625
- } = e, g = Y(null), [u, A] = rt({
625
+ } = e, g = Z(null), [u, A] = rt({
626
626
  startIndex: 0,
627
627
  endIndex: 10
628
- }), w = Y(i), T = Y(0), P = Y(!0), x = o().getNestedState(
629
- t,
630
- n
631
- ), b = x.length, U = mt(
628
+ }), w = mt(
632
629
  (E) => o.getState().getShadowMetadata(t, [...n, E.toString()])?.virtualizer?.itemHeight || r,
633
630
  [r, t, n]
634
- ), { totalHeight: B, positions: C } = yt(() => {
631
+ ), T = Z(i), x = Z(0), V = Z(!0), _ = o().getNestedState(
632
+ t,
633
+ n
634
+ ), P = _.length, { totalHeight: B, positions: N } = yt(() => {
635
635
  let E = 0;
636
- const V = [];
637
- for (let _ = 0; _ < b; _++)
638
- V[_] = E, E += U(_);
639
- return { totalHeight: E, positions: V };
640
- }, [b, U]), N = yt(() => {
641
- const E = Math.max(0, u.startIndex), V = Math.min(b, u.endIndex), _ = Array.from(
642
- { length: V - E },
643
- (L, Q) => E + Q
644
- ), M = _.map((L) => x[L]);
645
- return a(M, n, {
636
+ const C = [];
637
+ for (let M = 0; M < P; M++)
638
+ C[M] = E, E += w(M);
639
+ return { totalHeight: E, positions: C };
640
+ }, [P, w]), b = yt(() => {
641
+ const E = Math.max(0, u.startIndex), C = Math.min(P, u.endIndex), M = Array.from(
642
+ { length: C - E },
643
+ (L, K) => E + K
644
+ ), j = M.map((L) => _[L]);
645
+ return a(j, n, {
646
646
  ...S,
647
- validIndices: _
647
+ validIndices: M
648
648
  });
649
- }, [u.startIndex, u.endIndex, x, b]);
649
+ }, [u.startIndex, u.endIndex, _, P]);
650
650
  dt(() => {
651
651
  const E = g.current;
652
652
  if (!E) return;
653
- const V = w.current, _ = b > T.current;
654
- T.current = b;
655
- const M = () => {
656
- const { scrollTop: L, clientHeight: Q, scrollHeight: Vt } = E;
657
- w.current = Vt - L - Q < 10;
653
+ const C = T.current, M = P > x.current;
654
+ x.current = P;
655
+ const j = () => {
656
+ const { scrollTop: L, clientHeight: K, scrollHeight: Vt } = E;
657
+ T.current = Vt - L - K < 10;
658
658
  let et = 0;
659
- for (let q = 0; q < C.length; q++)
660
- if (C[q] >= L) {
661
- et = q;
659
+ for (let J = 0; J < N.length; J++)
660
+ if (N[J] >= L) {
661
+ et = J;
662
662
  break;
663
663
  }
664
- let K = et;
665
- for (; K < b && C[K] < L + Q; )
666
- K++;
667
- et = Math.max(0, et - s), K = Math.min(b, K + s), A((q) => q.startIndex !== et || q.endIndex !== K ? { startIndex: et, endIndex: K } : q);
664
+ let q = et;
665
+ for (; q < P && N[q] && N[q] < L + K; )
666
+ q++;
667
+ et = Math.max(0, et - s), q = Math.min(P, q + s), A((J) => J.startIndex !== et || J.endIndex !== q ? { startIndex: et, endIndex: q } : J);
668
668
  };
669
- return E.addEventListener("scroll", M, {
669
+ return E.addEventListener("scroll", j, {
670
670
  passive: !0
671
- }), i && (P.current ? E.scrollTo({
671
+ }), i && (V.current ? E.scrollTo({
672
672
  top: E.scrollHeight,
673
673
  behavior: "auto"
674
- }) : V && _ && requestAnimationFrame(() => {
674
+ }) : C && M && requestAnimationFrame(() => {
675
675
  E.scrollTo({
676
676
  top: E.scrollHeight,
677
677
  behavior: "smooth"
678
678
  });
679
- })), P.current = !1, M(), () => E.removeEventListener("scroll", M);
680
- }, [b, s, i, C]);
681
- const O = mt(
679
+ })), V.current = !1, j(), () => E.removeEventListener("scroll", j);
680
+ }, [P, s, i, N]);
681
+ const R = mt(
682
682
  (E = "smooth") => {
683
683
  g.current && g.current.scrollTo({
684
684
  top: g.current.scrollHeight,
@@ -687,14 +687,15 @@ function gt(t, c, v, f) {
687
687
  },
688
688
  []
689
689
  ), D = mt(
690
- (E, V = "smooth") => {
691
- g.current && C[E] !== void 0 && g.current.scrollTo({
692
- top: C[E],
693
- behavior: V
690
+ (E, C = "smooth") => {
691
+ g.current && N[E] !== void 0 && g.current.scrollTo({
692
+ top: N[E],
693
+ // Instead of `index * itemHeight`
694
+ behavior: C
694
695
  });
695
696
  },
696
- [C]
697
- // Depends on the calculated positions
697
+ [N]
698
+ // Depends on `positions` now instead of `itemHeight`
698
699
  ), $ = {
699
700
  outer: {
700
701
  ref: g,
@@ -703,19 +704,21 @@ function gt(t, c, v, f) {
703
704
  inner: {
704
705
  style: {
705
706
  height: `${B}px`,
707
+ // Use calculated total height
706
708
  position: "relative"
707
709
  }
708
710
  },
709
711
  list: {
710
712
  style: {
711
- transform: `translateY(${C[u.startIndex] || 0}px)`
713
+ // Use the pre-calculated position of the first visible item
714
+ transform: `translateY(${N[u.startIndex] || 0}px)`
712
715
  }
713
716
  }
714
717
  };
715
718
  return {
716
- virtualState: N,
719
+ virtualState: b,
717
720
  virtualizerProps: $,
718
- scrollToBottom: O,
721
+ scrollToBottom: R,
719
722
  scrollToIndex: D
720
723
  };
721
724
  };
@@ -750,19 +753,19 @@ function gt(t, c, v, f) {
750
753
  const u = r[i], A = [...n, i.toString()], w = a(u, A, S);
751
754
  return e(u, w, {
752
755
  register: () => {
753
- const [, P] = rt({}), x = `${v}-${n.join(".")}-${i}`;
756
+ const [, x] = rt({}), V = `${v}-${n.join(".")}-${i}`;
754
757
  dt(() => {
755
- const b = `${t}////${x}`, U = o.getState().stateComponents.get(t) || {
758
+ const _ = `${t}////${V}`, P = o.getState().stateComponents.get(t) || {
756
759
  components: /* @__PURE__ */ new Map()
757
760
  };
758
- return U.components.set(b, {
759
- forceUpdate: () => P({}),
761
+ return P.components.set(_, {
762
+ forceUpdate: () => x({}),
760
763
  paths: /* @__PURE__ */ new Set([A.join(".")])
761
- }), o.getState().stateComponents.set(t, U), () => {
764
+ }), o.getState().stateComponents.set(t, P), () => {
762
765
  const B = o.getState().stateComponents.get(t);
763
- B && B.components.delete(b);
766
+ B && B.components.delete(_);
764
767
  };
765
- }, [t, x]);
768
+ }, [t, V]);
766
769
  },
767
770
  index: g,
768
771
  originalIndex: i
@@ -849,14 +852,14 @@ function gt(t, c, v, f) {
849
852
  ));
850
853
  if (l === "uniqueInsert")
851
854
  return (e, r, s) => {
852
- const i = o.getState().getNestedState(t, n), g = Z(e) ? e(i) : e;
855
+ const i = o.getState().getNestedState(t, n), g = X(e) ? e(i) : e;
853
856
  let u = null;
854
857
  if (!i.some((w) => {
855
858
  if (r) {
856
- const P = r.every(
857
- (x) => H(w[x], g[x])
859
+ const x = r.every(
860
+ (V) => H(w[V], g[V])
858
861
  );
859
- return P && (u = w), P;
862
+ return x && (u = w), x;
860
863
  }
861
864
  const T = H(w, g);
862
865
  return T && (u = w), T;
@@ -864,7 +867,7 @@ function gt(t, c, v, f) {
864
867
  p(n), ht(c, g, n, t);
865
868
  else if (s && u) {
866
869
  const w = s(u), T = i.map(
867
- (P) => H(P, u) ? w : P
870
+ (x) => H(x, u) ? w : x
868
871
  );
869
872
  p(n), at(c, T, n);
870
873
  }
@@ -906,15 +909,15 @@ function gt(t, c, v, f) {
906
909
  return a(i.item, g, S);
907
910
  };
908
911
  }
909
- const X = n[n.length - 1];
910
- if (!isNaN(Number(X))) {
912
+ const Q = n[n.length - 1];
913
+ if (!isNaN(Number(Q))) {
911
914
  const d = n.slice(0, -1), e = o.getState().getNestedState(t, d);
912
915
  if (Array.isArray(e) && l === "cut")
913
916
  return () => ct(
914
917
  c,
915
918
  d,
916
919
  t,
917
- Number(X)
920
+ Number(Q)
918
921
  );
919
922
  }
920
923
  if (l === "get")
@@ -984,49 +987,49 @@ function gt(t, c, v, f) {
984
987
  w
985
988
  ] of i.components.entries()) {
986
989
  let T = !1;
987
- const P = Array.isArray(w.reactiveType) ? w.reactiveType : [w.reactiveType || "component"];
988
- if (!P.includes("none")) {
989
- if (P.includes("all")) {
990
+ const x = Array.isArray(w.reactiveType) ? w.reactiveType : [w.reactiveType || "component"];
991
+ if (!x.includes("none")) {
992
+ if (x.includes("all")) {
990
993
  w.forceUpdate();
991
994
  continue;
992
995
  }
993
- if (P.includes("component") && (w.paths.has("") && (T = !0), !T))
994
- for (const x of u) {
995
- if (w.paths.has(x)) {
996
+ if (x.includes("component") && (w.paths.has("") && (T = !0), !T))
997
+ for (const V of u) {
998
+ if (w.paths.has(V)) {
996
999
  T = !0;
997
1000
  break;
998
1001
  }
999
- let b = x.lastIndexOf(".");
1000
- for (; b !== -1; ) {
1001
- const U = x.substring(0, b);
1002
- if (w.paths.has(U)) {
1002
+ let _ = V.lastIndexOf(".");
1003
+ for (; _ !== -1; ) {
1004
+ const P = V.substring(0, _);
1005
+ if (w.paths.has(P)) {
1003
1006
  T = !0;
1004
1007
  break;
1005
1008
  }
1006
- const B = x.substring(
1007
- b + 1
1009
+ const B = V.substring(
1010
+ _ + 1
1008
1011
  );
1009
1012
  if (!isNaN(Number(B))) {
1010
- const C = U.lastIndexOf(".");
1011
- if (C !== -1) {
1012
- const N = U.substring(
1013
+ const N = P.lastIndexOf(".");
1014
+ if (N !== -1) {
1015
+ const b = P.substring(
1013
1016
  0,
1014
- C
1017
+ N
1015
1018
  );
1016
- if (w.paths.has(N)) {
1019
+ if (w.paths.has(b)) {
1017
1020
  T = !0;
1018
1021
  break;
1019
1022
  }
1020
1023
  }
1021
1024
  }
1022
- b = U.lastIndexOf(".");
1025
+ _ = P.lastIndexOf(".");
1023
1026
  }
1024
1027
  if (T) break;
1025
1028
  }
1026
- if (!T && P.includes("deps") && w.depsFunction) {
1027
- const x = w.depsFunction(s);
1028
- let b = !1;
1029
- typeof x == "boolean" ? x && (b = !0) : H(w.deps, x) || (w.deps = x, b = !0), b && (T = !0);
1029
+ if (!T && x.includes("deps") && w.depsFunction) {
1030
+ const V = w.depsFunction(s);
1031
+ let _ = !1;
1032
+ typeof V == "boolean" ? V && (_ = !0) : H(w.deps, V) || (w.deps = V, _ = !0), _ && (T = !0);
1030
1033
  }
1031
1034
  T && w.forceUpdate();
1032
1035
  }
@@ -1040,12 +1043,12 @@ function gt(t, c, v, f) {
1040
1043
  throw new Error("Zod schema not found");
1041
1044
  if (!d?.key)
1042
1045
  throw new Error("Validation key not found");
1043
- J(d.key);
1046
+ Y(d.key);
1044
1047
  const r = o.getState().cogsStateStore[t];
1045
1048
  try {
1046
1049
  const s = o.getState().getValidationErrors(d.key);
1047
1050
  s && s.length > 0 && s.forEach(([g]) => {
1048
- g && g.startsWith(d.key) && J(g);
1051
+ g && g.startsWith(d.key) && Y(g);
1049
1052
  });
1050
1053
  const i = d.zodSchema.safeParse(r);
1051
1054
  return i.success ? !0 : (i.error.errors.forEach((u) => {
@@ -1118,10 +1121,10 @@ function gt(t, c, v, f) {
1118
1121
  formOpts: e
1119
1122
  }
1120
1123
  );
1121
- const j = [...n, l], ot = o.getState().getNestedState(t, j);
1122
- return a(ot, j, S);
1124
+ const O = [...n, l], ot = o.getState().getNestedState(t, O);
1125
+ return a(ot, O, S);
1123
1126
  }
1124
- }, G = new Proxy(R, F);
1127
+ }, G = new Proxy(F, U);
1125
1128
  return y.set(W, {
1126
1129
  proxy: G,
1127
1130
  stateVersion: k
@@ -1149,7 +1152,7 @@ function qt({
1149
1152
  function Jt({
1150
1153
  proxy: t
1151
1154
  }) {
1152
- const c = Y(null), v = `${t._stateKey}-${t._path.join(".")}`;
1155
+ const c = Z(null), v = `${t._stateKey}-${t._path.join(".")}`;
1153
1156
  return lt(() => {
1154
1157
  const f = c.current;
1155
1158
  if (!f || !f.parentElement) return;
@@ -1171,8 +1174,8 @@ function Jt({
1171
1174
  "state",
1172
1175
  `return (${t._effect})(state)`
1173
1176
  )(n);
1174
- } catch (R) {
1175
- console.error("Error evaluating effect function during mount:", R), S = n;
1177
+ } catch (F) {
1178
+ console.error("Error evaluating effect function during mount:", F), S = n;
1176
1179
  }
1177
1180
  else
1178
1181
  S = n;