cogsbox-state 0.5.316 → 0.5.318

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,34 +1,34 @@
1
1
  "use client";
2
- import { jsx as yt } from "react/jsx-runtime";
3
- import { useState as tt, useRef as Z, useEffect as it, useLayoutEffect as ut, useMemo as It, createElement as ct, useSyncExternalStore as _t, startTransition as Mt, useCallback as Tt } from "react";
4
- import { transformStateFunc as Rt, isDeepEqual as q, isFunction as Q, getNestedValue as J, getDifferences as pt, debounce as jt } from "./utility.js";
5
- import { pushFunc as vt, updateFn as st, cutFunc as dt, ValidationWrapper as Ot, FormControlComponent as Ut } from "./Functions.jsx";
2
+ import { jsx as It } from "react/jsx-runtime";
3
+ import { useState as et, useRef as X, useEffect as st, useLayoutEffect as gt, useMemo as pt, createElement as it, useSyncExternalStore as _t, startTransition as Mt, useCallback as Et } from "react";
4
+ import { transformStateFunc as Rt, isDeepEqual as Y, isFunction as K, getNestedValue as Z, getDifferences as wt, debounce as jt } from "./utility.js";
5
+ import { pushFunc as yt, updateFn as at, cutFunc as ut, ValidationWrapper as Ot, FormControlComponent as Ut } from "./Functions.jsx";
6
6
  import Ft from "superjson";
7
- import { v4 as wt } from "uuid";
7
+ import { v4 as $t } from "uuid";
8
8
  import "zod";
9
- import { getGlobalStore as o, formRefStore as Et } from "./store.js";
10
- import { useCogsConfig as Ct } from "./CogsStateClient.jsx";
9
+ import { getGlobalStore as o, formRefStore as Vt } from "./store.js";
10
+ import { useCogsConfig as xt } from "./CogsStateClient.jsx";
11
11
  import { applyPatch as Dt } from "fast-json-patch";
12
12
  import Wt from "react-use-measure";
13
- function At(t, c) {
13
+ function bt(t, c) {
14
14
  const m = o.getState().getInitialOptions, f = o.getState().setInitialStateOptions, y = m(t) || {};
15
15
  f(t, {
16
16
  ...y,
17
17
  ...c
18
18
  });
19
19
  }
20
- function Vt({
20
+ function At({
21
21
  stateKey: t,
22
22
  options: c,
23
23
  initialOptionsPart: m
24
24
  }) {
25
- const f = rt(t) || {}, y = m[t] || {}, b = o.getState().setInitialStateOptions, p = { ...y, ...f };
25
+ const f = rt(t) || {}, y = m[t] || {}, k = o.getState().setInitialStateOptions, p = { ...y, ...f };
26
26
  let I = !1;
27
27
  if (c)
28
28
  for (const a in c)
29
29
  p.hasOwnProperty(a) ? (a == "localStorage" && c[a] && p[a].key !== c[a]?.key && (I = !0, p[a] = c[a]), a == "initialState" && c[a] && p[a] !== c[a] && // Different references
30
- !q(p[a], c[a]) && (I = !0, p[a] = c[a])) : (I = !0, p[a] = c[a]);
31
- I && b(t, p);
30
+ !Y(p[a], c[a]) && (I = !0, p[a] = c[a])) : (I = !0, p[a] = c[a]);
31
+ I && k(t, p);
32
32
  }
33
33
  function le(t, { formElements: c, validation: m }) {
34
34
  return { initialState: t, formElements: c, validation: m };
@@ -45,14 +45,14 @@ const de = (t, c) => {
45
45
  // State-specific overrides
46
46
  }, rt(I) || o.getState().setInitialStateOptions(I, y[I]);
47
47
  }), o.getState().setInitialStates(f), o.getState().setCreatedState(f);
48
- const b = (I, a) => {
49
- const [v] = tt(a?.componentId ?? wt());
50
- Vt({
48
+ const k = (I, a) => {
49
+ const [v] = et(a?.componentId ?? $t());
50
+ At({
51
51
  stateKey: I,
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, [G, O] = Jt(
55
+ const n = o.getState().cogsStateStore[I] || f[I], S = a?.modifyState ? a.modifyState(n) : n, [z, j] = Jt(
56
56
  S,
57
57
  {
58
58
  stateKey: I,
@@ -68,22 +68,22 @@ const de = (t, c) => {
68
68
  serverState: a?.serverState
69
69
  }
70
70
  );
71
- return O;
71
+ return j;
72
72
  };
73
73
  function p(I, a) {
74
- Vt({ stateKey: I, options: a, initialOptionsPart: y }), a.localStorage && Bt(I, a), mt(I);
74
+ At({ stateKey: I, options: a, initialOptionsPart: y }), a.localStorage && Bt(I, a), ht(I);
75
75
  }
76
- return { useCogsState: b, setCogsOptions: p };
76
+ return { useCogsState: k, setCogsOptions: p };
77
77
  }, {
78
- setUpdaterState: gt,
79
- setState: et,
78
+ setUpdaterState: ft,
79
+ setState: nt,
80
80
  getInitialOptions: rt,
81
81
  getKeyState: Nt,
82
82
  getValidationErrors: Lt,
83
83
  setStateLog: Gt,
84
- updateInitialStateGlobal: $t,
84
+ updateInitialStateGlobal: Tt,
85
85
  addValidationError: Ht,
86
- removeValidationError: X,
86
+ removeValidationError: Q,
87
87
  setServerSyncActions: zt
88
88
  } = o.getState(), kt = (t, c, m, f, y) => {
89
89
  m?.log && console.log(
@@ -92,12 +92,12 @@ const de = (t, c) => {
92
92
  m.localStorage?.key,
93
93
  f
94
94
  );
95
- const b = Q(m?.localStorage?.key) ? m.localStorage?.key(t) : m?.localStorage?.key;
96
- if (b && f) {
97
- const p = `${f}-${c}-${b}`;
95
+ const k = K(m?.localStorage?.key) ? m.localStorage?.key(t) : m?.localStorage?.key;
96
+ if (k && f) {
97
+ const p = `${f}-${c}-${k}`;
98
98
  let I;
99
99
  try {
100
- I = St(p)?.lastSyncedWithServer;
100
+ I = mt(p)?.lastSyncedWithServer;
101
101
  } catch {
102
102
  }
103
103
  const a = {
@@ -110,7 +110,7 @@ const de = (t, c) => {
110
110
  JSON.stringify(v.json)
111
111
  );
112
112
  }
113
- }, St = (t) => {
113
+ }, mt = (t) => {
114
114
  if (!t) return null;
115
115
  try {
116
116
  const c = window.localStorage.getItem(t);
@@ -119,28 +119,28 @@ const de = (t, c) => {
119
119
  return console.error("Error loading from localStorage:", c), null;
120
120
  }
121
121
  }, Bt = (t, c) => {
122
- const m = o.getState().cogsStateStore[t], { sessionId: f } = Ct(), y = Q(c?.localStorage?.key) ? c.localStorage.key(m) : c?.localStorage?.key;
122
+ const m = o.getState().cogsStateStore[t], { sessionId: f } = xt(), y = K(c?.localStorage?.key) ? c.localStorage.key(m) : c?.localStorage?.key;
123
123
  if (y && f) {
124
- const b = St(
124
+ const k = mt(
125
125
  `${f}-${t}-${y}`
126
126
  );
127
- if (b && b.lastUpdated > (b.lastSyncedWithServer || 0))
128
- return et(t, b.state), mt(t), !0;
127
+ if (k && k.lastUpdated > (k.lastSyncedWithServer || 0))
128
+ return nt(t, k.state), ht(t), !0;
129
129
  }
130
130
  return !1;
131
- }, xt = (t, c, m, f, y, b) => {
131
+ }, Pt = (t, c, m, f, y, k) => {
132
132
  const p = {
133
133
  initialState: c,
134
- updaterState: ft(
134
+ updaterState: St(
135
135
  t,
136
136
  f,
137
137
  y,
138
- b
138
+ k
139
139
  ),
140
140
  state: m
141
141
  };
142
- $t(t, p.initialState), gt(t, p.updaterState), et(t, p.state);
143
- }, mt = (t) => {
142
+ Tt(t, p.initialState), ft(t, p.updaterState), nt(t, p.state);
143
+ }, ht = (t) => {
144
144
  const c = o.getState().stateComponents.get(t);
145
145
  if (!c) return;
146
146
  const m = /* @__PURE__ */ new Set();
@@ -161,18 +161,18 @@ const de = (t, c) => {
161
161
  switch (t) {
162
162
  case "update":
163
163
  return {
164
- oldValue: J(c, f),
165
- newValue: J(m, f)
164
+ oldValue: Z(c, f),
165
+ newValue: Z(m, f)
166
166
  };
167
167
  case "insert":
168
168
  return {
169
169
  oldValue: null,
170
170
  // or undefined
171
- newValue: J(m, f)
171
+ newValue: Z(m, f)
172
172
  };
173
173
  case "cut":
174
174
  return {
175
- oldValue: J(c, f),
175
+ oldValue: Z(c, f),
176
176
  newValue: null
177
177
  // or undefined
178
178
  };
@@ -185,7 +185,7 @@ function Jt(t, {
185
185
  serverSync: m,
186
186
  localStorage: f,
187
187
  formElements: y,
188
- reactiveDeps: b,
188
+ reactiveDeps: k,
189
189
  reactiveType: p,
190
190
  componentId: I,
191
191
  initialState: a,
@@ -193,14 +193,14 @@ function Jt(t, {
193
193
  dependencies: n,
194
194
  serverState: S
195
195
  } = {}) {
196
- const [G, O] = tt({}), { sessionId: U } = Ct();
197
- let H = !c;
198
- const [h] = tt(c ?? wt()), l = o.getState().stateLog[h], lt = Z(/* @__PURE__ */ new Set()), K = Z(I ?? wt()), R = Z(
196
+ const [z, j] = et({}), { sessionId: O } = xt();
197
+ let B = !c;
198
+ const [h] = et(c ?? $t()), l = o.getState().stateLog[h], ct = X(/* @__PURE__ */ new Set()), tt = X(I ?? $t()), M = X(
199
199
  null
200
200
  );
201
- R.current = rt(h) ?? null, it(() => {
201
+ M.current = rt(h) ?? null, st(() => {
202
202
  if (v && v.stateKey === h && v.path?.[0]) {
203
- et(h, (r) => ({
203
+ nt(h, (r) => ({
204
204
  ...r,
205
205
  [v.path[0]]: v.newValue
206
206
  }));
@@ -210,165 +210,165 @@ function Jt(t, {
210
210
  userId: v.userId
211
211
  });
212
212
  }
213
- }, [v]), it(() => {
213
+ }, [v]), st(() => {
214
214
  if (a) {
215
- At(h, {
215
+ bt(h, {
216
216
  initialState: a
217
217
  });
218
- const e = R.current, s = e?.serverState?.id !== void 0 && e?.serverState?.status === "success" && e?.serverState?.data, i = o.getState().initialStateGlobal[h];
219
- if (!(i && !q(i, a) || !i) && !s)
218
+ const e = M.current, s = e?.serverState?.id !== void 0 && e?.serverState?.status === "success" && e?.serverState?.data, i = o.getState().initialStateGlobal[h];
219
+ if (!(i && !Y(i, a) || !i) && !s)
220
220
  return;
221
- let u = null;
222
- const T = Q(e?.localStorage?.key) ? e?.localStorage?.key(a) : e?.localStorage?.key;
223
- T && U && (u = St(`${U}-${h}-${T}`));
224
- let w = a, V = !1;
225
- const x = s ? Date.now() : 0, C = u?.lastUpdated || 0, P = u?.lastSyncedWithServer || 0;
226
- s && x > C ? (w = e.serverState.data, V = !0) : u && C > P && (w = u.state, e?.localStorage?.onChange && e?.localStorage?.onChange(w)), o.getState().initializeShadowState(h, a), xt(
221
+ let d = null;
222
+ const T = K(e?.localStorage?.key) ? e?.localStorage?.key(a) : e?.localStorage?.key;
223
+ T && O && (d = mt(`${O}-${h}-${T}`));
224
+ let w = a, b = !1;
225
+ const N = s ? Date.now() : 0, C = d?.lastUpdated || 0, P = d?.lastSyncedWithServer || 0;
226
+ s && N > C ? (w = e.serverState.data, b = !0) : d && C > P && (w = d.state, e?.localStorage?.onChange && e?.localStorage?.onChange(w)), o.getState().initializeShadowState(h, a), Pt(
227
227
  h,
228
228
  a,
229
229
  w,
230
230
  ot,
231
- K.current,
232
- U
233
- ), V && T && U && kt(w, h, e, U, Date.now()), mt(h), (Array.isArray(p) ? p : [p || "component"]).includes("none") || O({});
231
+ tt.current,
232
+ O
233
+ ), b && T && O && kt(w, h, e, O, Date.now()), ht(h), (Array.isArray(p) ? p : [p || "component"]).includes("none") || j({});
234
234
  }
235
235
  }, [
236
236
  a,
237
237
  S?.status,
238
238
  S?.data,
239
239
  ...n || []
240
- ]), ut(() => {
241
- H && At(h, {
240
+ ]), gt(() => {
241
+ B && bt(h, {
242
242
  serverSync: m,
243
243
  formElements: y,
244
244
  initialState: a,
245
245
  localStorage: f,
246
- middleware: R.current?.middleware
246
+ middleware: M.current?.middleware
247
247
  });
248
- const e = `${h}////${K.current}`, r = o.getState().stateComponents.get(h) || {
248
+ const e = `${h}////${tt.current}`, r = o.getState().stateComponents.get(h) || {
249
249
  components: /* @__PURE__ */ new Map()
250
250
  };
251
251
  return r.components.set(e, {
252
- forceUpdate: () => O({}),
252
+ forceUpdate: () => j({}),
253
253
  paths: /* @__PURE__ */ new Set(),
254
254
  deps: [],
255
- depsFunction: b || void 0,
255
+ depsFunction: k || void 0,
256
256
  reactiveType: p ?? ["component", "deps"]
257
- }), o.getState().stateComponents.set(h, r), O({}), () => {
257
+ }), o.getState().stateComponents.set(h, r), j({}), () => {
258
258
  r && (r.components.delete(e), r.components.size === 0 && o.getState().stateComponents.delete(h));
259
259
  };
260
260
  }, []);
261
261
  const ot = (e, r, s, i) => {
262
262
  if (Array.isArray(r)) {
263
- const u = `${h}-${r.join(".")}`;
264
- lt.current.add(u);
263
+ const d = `${h}-${r.join(".")}`;
264
+ ct.current.add(d);
265
265
  }
266
266
  const g = o.getState();
267
- et(h, (u) => {
268
- const T = Q(e) ? e(u) : e, w = `${h}-${r.join(".")}`;
267
+ nt(h, (d) => {
268
+ const T = K(e) ? e(d) : e, w = `${h}-${r.join(".")}`;
269
269
  if (w) {
270
270
  let _ = !1, E = g.signalDomElements.get(w);
271
271
  if ((!E || E.size === 0) && (s.updateType === "insert" || s.updateType === "cut")) {
272
- const M = r.slice(0, -1), W = J(T, M);
272
+ const U = r.slice(0, -1), W = Z(T, U);
273
273
  if (Array.isArray(W)) {
274
274
  _ = !0;
275
- const A = `${h}-${M.join(".")}`;
276
- E = g.signalDomElements.get(A);
275
+ const V = `${h}-${U.join(".")}`;
276
+ E = g.signalDomElements.get(V);
277
277
  }
278
278
  }
279
279
  if (E) {
280
- const M = _ ? J(T, r.slice(0, -1)) : J(T, r);
281
- E.forEach(({ parentId: W, position: A, effect: F }) => {
282
- const j = document.querySelector(
280
+ const U = _ ? Z(T, r.slice(0, -1)) : Z(T, r);
281
+ E.forEach(({ parentId: W, position: V, effect: F }) => {
282
+ const R = document.querySelector(
283
283
  `[data-parent-id="${W}"]`
284
284
  );
285
- if (j) {
286
- const $ = Array.from(j.childNodes);
287
- if ($[A]) {
288
- const k = F ? new Function("state", `return (${F})(state)`)(M) : M;
289
- $[A].textContent = String(k);
285
+ if (R) {
286
+ const $ = Array.from(R.childNodes);
287
+ if ($[V]) {
288
+ const A = F ? new Function("state", `return (${F})(state)`)(U) : U;
289
+ $[V].textContent = String(A);
290
290
  }
291
291
  }
292
292
  });
293
293
  }
294
294
  }
295
- console.log("shadowState", g.shadowStateStore), s.updateType === "update" && (i || R.current?.validation?.key) && r && X(
296
- (i || R.current?.validation?.key) + "." + r.join(".")
295
+ console.log("shadowState", g.shadowStateStore), s.updateType === "update" && (i || M.current?.validation?.key) && r && Q(
296
+ (i || M.current?.validation?.key) + "." + r.join(".")
297
297
  );
298
- const V = r.slice(0, r.length - 1);
299
- s.updateType === "cut" && R.current?.validation?.key && X(
300
- R.current?.validation?.key + "." + V.join(".")
301
- ), s.updateType === "insert" && R.current?.validation?.key && Lt(
302
- R.current?.validation?.key + "." + V.join(".")
303
- ).filter(([E, M]) => {
298
+ const b = r.slice(0, r.length - 1);
299
+ s.updateType === "cut" && M.current?.validation?.key && Q(
300
+ M.current?.validation?.key + "." + b.join(".")
301
+ ), s.updateType === "insert" && M.current?.validation?.key && Lt(
302
+ M.current?.validation?.key + "." + b.join(".")
303
+ ).filter(([E, U]) => {
304
304
  let W = E?.split(".").length;
305
- if (E == V.join(".") && W == V.length - 1) {
306
- let A = E + "." + V;
307
- X(E), Ht(A, M);
305
+ if (E == b.join(".") && W == b.length - 1) {
306
+ let V = E + "." + b;
307
+ Q(E), Ht(V, U);
308
308
  }
309
309
  });
310
- const x = g.stateComponents.get(h);
311
- if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", x), x) {
312
- const _ = pt(u, T), E = new Set(_), M = s.updateType === "update" ? r.join(".") : r.slice(0, -1).join(".") || "";
310
+ const N = g.stateComponents.get(h);
311
+ if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", N), N) {
312
+ const _ = wt(d, T), E = new Set(_), U = s.updateType === "update" ? r.join(".") : r.slice(0, -1).join(".") || "";
313
313
  for (const [
314
314
  W,
315
- A
316
- ] of x.components.entries()) {
315
+ V
316
+ ] of N.components.entries()) {
317
317
  let F = !1;
318
- const j = Array.isArray(A.reactiveType) ? A.reactiveType : [A.reactiveType || "component"];
319
- if (console.log("component", A), !j.includes("none")) {
320
- if (j.includes("all")) {
321
- A.forceUpdate();
318
+ const R = Array.isArray(V.reactiveType) ? V.reactiveType : [V.reactiveType || "component"];
319
+ if (console.log("component", V), !R.includes("none")) {
320
+ if (R.includes("all")) {
321
+ V.forceUpdate();
322
322
  continue;
323
323
  }
324
- if (j.includes("component") && ((A.paths.has(M) || A.paths.has("")) && (F = !0), !F))
324
+ if (R.includes("component") && ((V.paths.has(U) || V.paths.has("")) && (F = !0), !F))
325
325
  for (const $ of E) {
326
- let k = $;
326
+ let A = $;
327
327
  for (; ; ) {
328
- if (A.paths.has(k)) {
328
+ if (V.paths.has(A)) {
329
329
  F = !0;
330
330
  break;
331
331
  }
332
- const L = k.lastIndexOf(".");
332
+ const L = A.lastIndexOf(".");
333
333
  if (L !== -1) {
334
- const z = k.substring(
334
+ const q = A.substring(
335
335
  0,
336
336
  L
337
337
  );
338
338
  if (!isNaN(
339
- Number(k.substring(L + 1))
340
- ) && A.paths.has(z)) {
339
+ Number(A.substring(L + 1))
340
+ ) && V.paths.has(q)) {
341
341
  F = !0;
342
342
  break;
343
343
  }
344
- k = z;
344
+ A = q;
345
345
  } else
346
- k = "";
347
- if (k === "")
346
+ A = "";
347
+ if (A === "")
348
348
  break;
349
349
  }
350
350
  if (F) break;
351
351
  }
352
- if (!F && j.includes("deps") && A.depsFunction) {
353
- const $ = A.depsFunction(T);
354
- let k = !1;
355
- typeof $ == "boolean" ? $ && (k = !0) : q(A.deps, $) || (A.deps = $, k = !0), k && (F = !0);
352
+ if (!F && R.includes("deps") && V.depsFunction) {
353
+ const $ = V.depsFunction(T);
354
+ let A = !1;
355
+ typeof $ == "boolean" ? $ && (A = !0) : Y(V.deps, $) || (V.deps = $, A = !0), A && (F = !0);
356
356
  }
357
- F && A.forceUpdate();
357
+ F && V.forceUpdate();
358
358
  }
359
359
  }
360
360
  }
361
361
  const C = Date.now();
362
362
  r = r.map((_, E) => {
363
- const M = r.slice(0, -1), W = J(T, M);
363
+ const U = r.slice(0, -1), W = Z(T, U);
364
364
  return E === r.length - 1 && ["insert", "cut"].includes(s.updateType) ? (W.length - 1).toString() : _;
365
365
  });
366
366
  const { oldValue: P, newValue: D } = qt(
367
367
  s.updateType,
368
- u,
368
+ d,
369
369
  T,
370
370
  r
371
- ), N = {
371
+ ), x = {
372
372
  timeStamp: C,
373
373
  stateKey: h,
374
374
  path: r,
@@ -386,26 +386,26 @@ function Jt(t, {
386
386
  g.insertShadowArrayElement(h, _, D);
387
387
  break;
388
388
  case "cut":
389
- const E = r.slice(0, -1), M = parseInt(r[r.length - 1]);
390
- g.removeShadowArrayElement(h, E, M);
389
+ const E = r.slice(0, -1), U = parseInt(r[r.length - 1]);
390
+ g.removeShadowArrayElement(h, E, U);
391
391
  break;
392
392
  }
393
393
  if (Gt(h, (_) => {
394
- const M = [..._ ?? [], N].reduce((W, A) => {
395
- const F = `${A.stateKey}:${JSON.stringify(A.path)}`, j = W.get(F);
396
- return j ? (j.timeStamp = Math.max(j.timeStamp, A.timeStamp), j.newValue = A.newValue, j.oldValue = j.oldValue ?? A.oldValue, j.updateType = A.updateType) : W.set(F, { ...A }), W;
394
+ const U = [..._ ?? [], x].reduce((W, V) => {
395
+ const F = `${V.stateKey}:${JSON.stringify(V.path)}`, R = W.get(F);
396
+ return R ? (R.timeStamp = Math.max(R.timeStamp, V.timeStamp), R.newValue = V.newValue, R.oldValue = R.oldValue ?? V.oldValue, R.updateType = V.updateType) : W.set(F, { ...V }), W;
397
397
  }, /* @__PURE__ */ new Map());
398
- return Array.from(M.values());
398
+ return Array.from(U.values());
399
399
  }), kt(
400
400
  T,
401
401
  h,
402
- R.current,
403
- U
404
- ), R.current?.middleware && R.current.middleware({
402
+ M.current,
403
+ O
404
+ ), M.current?.middleware && M.current.middleware({
405
405
  updateLog: l,
406
- update: N
407
- }), R.current?.serverSync) {
408
- const _ = g.serverState[h], E = R.current?.serverSync;
406
+ update: x
407
+ }), M.current?.serverSync) {
408
+ const _ = g.serverState[h], E = M.current?.serverSync;
409
409
  zt(h, {
410
410
  syncKey: typeof E.syncKey == "string" ? E.syncKey : E.syncKey({ state: T }),
411
411
  rollBackState: _,
@@ -416,63 +416,63 @@ function Jt(t, {
416
416
  return T;
417
417
  });
418
418
  };
419
- o.getState().updaterState[h] || (gt(
419
+ o.getState().updaterState[h] || (ft(
420
420
  h,
421
- ft(
421
+ St(
422
422
  h,
423
423
  ot,
424
- K.current,
425
- U
424
+ tt.current,
425
+ O
426
426
  )
427
- ), o.getState().cogsStateStore[h] || et(h, t), o.getState().initialStateGlobal[h] || $t(h, t));
428
- const d = It(() => ft(
427
+ ), o.getState().cogsStateStore[h] || nt(h, t), o.getState().initialStateGlobal[h] || Tt(h, t));
428
+ const u = pt(() => St(
429
429
  h,
430
430
  ot,
431
- K.current,
432
- U
433
- ), [h, U]);
434
- return [Nt(h), d];
431
+ tt.current,
432
+ O
433
+ ), [h, O]);
434
+ return [Nt(h), u];
435
435
  }
436
- function ft(t, c, m, f) {
436
+ function St(t, c, m, f) {
437
437
  const y = /* @__PURE__ */ new Map();
438
- let b = 0;
438
+ let k = 0;
439
439
  const p = (v) => {
440
440
  const n = v.join(".");
441
441
  for (const [S] of y)
442
442
  (S === n || S.startsWith(n + ".")) && y.delete(S);
443
- b++;
443
+ k++;
444
444
  }, I = {
445
445
  removeValidation: (v) => {
446
- v?.validationKey && X(v.validationKey);
446
+ v?.validationKey && Q(v.validationKey);
447
447
  },
448
448
  revertToInitialState: (v) => {
449
449
  const n = o.getState().getInitialOptions(t)?.validation;
450
- n?.key && X(n?.key), v?.validationKey && X(v.validationKey);
450
+ n?.key && Q(n?.key), v?.validationKey && Q(v.validationKey);
451
451
  const S = o.getState().initialStateGlobal[t];
452
- o.getState().clearSelectedIndexesForState(t), y.clear(), b++;
453
- const G = a(S, []), O = rt(t), U = Q(O?.localStorage?.key) ? O?.localStorage?.key(S) : O?.localStorage?.key, H = `${f}-${t}-${U}`;
454
- H && localStorage.removeItem(H), gt(t, G), et(t, S);
452
+ o.getState().clearSelectedIndexesForState(t), y.clear(), k++;
453
+ const z = a(S, []), j = rt(t), O = K(j?.localStorage?.key) ? j?.localStorage?.key(S) : j?.localStorage?.key, B = `${f}-${t}-${O}`;
454
+ B && localStorage.removeItem(B), ft(t, z), nt(t, S);
455
455
  const h = o.getState().stateComponents.get(t);
456
456
  return h && h.components.forEach((l) => {
457
457
  l.forceUpdate();
458
458
  }), S;
459
459
  },
460
460
  updateInitialState: (v) => {
461
- y.clear(), b++;
462
- const n = ft(
461
+ y.clear(), k++;
462
+ const n = St(
463
463
  t,
464
464
  c,
465
465
  m,
466
466
  f
467
- ), S = o.getState().initialStateGlobal[t], G = rt(t), O = Q(G?.localStorage?.key) ? G?.localStorage?.key(S) : G?.localStorage?.key, U = `${f}-${t}-${O}`;
468
- return localStorage.getItem(U) && localStorage.removeItem(U), Mt(() => {
469
- $t(t, v), o.getState().initializeShadowState(t, v), gt(t, n), et(t, v);
470
- const H = o.getState().stateComponents.get(t);
471
- H && H.components.forEach((h) => {
467
+ ), S = o.getState().initialStateGlobal[t], z = rt(t), j = K(z?.localStorage?.key) ? z?.localStorage?.key(S) : z?.localStorage?.key, O = `${f}-${t}-${j}`;
468
+ return localStorage.getItem(O) && localStorage.removeItem(O), Mt(() => {
469
+ Tt(t, v), o.getState().initializeShadowState(t, v), ft(t, n), nt(t, v);
470
+ const B = o.getState().stateComponents.get(t);
471
+ B && B.components.forEach((h) => {
472
472
  h.forceUpdate();
473
473
  });
474
474
  }), {
475
- fetchId: (H) => n.get()[H]
475
+ fetchId: (B) => n.get()[B]
476
476
  };
477
477
  },
478
478
  _initialState: o.getState().initialStateGlobal[t],
@@ -480,27 +480,27 @@ function ft(t, c, m, f) {
480
480
  _isLoading: o.getState().isLoadingGlobal[t],
481
481
  _isServerSynced: () => {
482
482
  const v = o.getState().serverState[t];
483
- return !!(v && q(v, Nt(t)));
483
+ return !!(v && Y(v, Nt(t)));
484
484
  }
485
485
  };
486
486
  function a(v, n = [], S) {
487
- const G = n.map(String).join(".");
488
- y.get(G);
489
- const O = function() {
487
+ const z = n.map(String).join(".");
488
+ y.get(z);
489
+ const j = function() {
490
490
  return o().getNestedState(t, n);
491
491
  };
492
492
  Object.keys(I).forEach((h) => {
493
- O[h] = I[h];
493
+ j[h] = I[h];
494
494
  });
495
- const U = {
496
- apply(h, l, lt) {
495
+ const O = {
496
+ apply(h, l, ct) {
497
497
  return console.log(
498
498
  `PROXY APPLY TRAP HIT: stateKey=${t}, path=${n.join(".")}`
499
499
  ), console.trace("Apply trap stack trace"), o().getNestedState(t, n);
500
500
  },
501
501
  get(h, l) {
502
502
  S?.validIndices && !Array.isArray(v) && (S = { ...S, validIndices: void 0 });
503
- const lt = /* @__PURE__ */ new Set([
503
+ const ct = /* @__PURE__ */ new Set([
504
504
  "insert",
505
505
  "cut",
506
506
  "cutByValue",
@@ -523,10 +523,10 @@ function ft(t, c, m, f) {
523
523
  "_stateKey",
524
524
  "getComponents"
525
525
  ]);
526
- if (l !== "then" && !l.startsWith("$") && l !== "stateMapNoRender" && !lt.has(l)) {
527
- const d = `${t}////${m}`, e = o.getState().stateComponents.get(t);
526
+ if (l !== "then" && !l.startsWith("$") && l !== "stateMapNoRender" && !ct.has(l)) {
527
+ const u = `${t}////${m}`, e = o.getState().stateComponents.get(t);
528
528
  if (e) {
529
- const r = e.components.get(d);
529
+ const r = e.components.get(u);
530
530
  if (r && !r.paths.has("")) {
531
531
  const s = n.join(".");
532
532
  let i = !0;
@@ -540,26 +540,26 @@ function ft(t, c, m, f) {
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
  );
547
547
  if (l === "sync" && n.length === 0)
548
548
  return async function() {
549
- const d = o.getState().getInitialOptions(t), e = d?.sync;
549
+ const u = o.getState().getInitialOptions(t), e = u?.sync;
550
550
  if (!e)
551
551
  return console.error(`No mutation defined for state key "${t}"`), { success: !1, error: "No mutation defined" };
552
- const r = o.getState().getNestedState(t, []), s = d?.validation?.key;
552
+ const r = o.getState().getNestedState(t, []), s = u?.validation?.key;
553
553
  try {
554
554
  const i = await e.action(r);
555
555
  if (i && !i.success && i.errors && s) {
556
- o.getState().removeValidationError(s), i.errors.forEach((u) => {
557
- const T = [s, ...u.path].join(".");
558
- o.getState().addValidationError(T, u.message);
556
+ o.getState().removeValidationError(s), i.errors.forEach((d) => {
557
+ const T = [s, ...d.path].join(".");
558
+ o.getState().addValidationError(T, d.message);
559
559
  });
560
560
  const g = o.getState().stateComponents.get(t);
561
- g && g.components.forEach((u) => {
562
- u.forceUpdate();
561
+ g && g.components.forEach((d) => {
562
+ d.forceUpdate();
563
563
  });
564
564
  }
565
565
  return i?.success && e.onSuccess ? e.onSuccess(i.data) : !i?.success && e.onError && e.onError(i.error), i;
@@ -568,31 +568,31 @@ function ft(t, c, m, f) {
568
568
  }
569
569
  };
570
570
  if (l === "_status") {
571
- const d = o.getState().getNestedState(t, n), e = o.getState().initialStateGlobal[t], r = J(e, n);
572
- return q(d, r) ? "fresh" : "stale";
571
+ const u = o.getState().getNestedState(t, n), e = o.getState().initialStateGlobal[t], r = Z(e, n);
572
+ return Y(u, r) ? "fresh" : "stale";
573
573
  }
574
574
  if (l === "getStatus")
575
575
  return function() {
576
- const d = o().getNestedState(
576
+ const u = o().getNestedState(
577
577
  t,
578
578
  n
579
- ), e = o.getState().initialStateGlobal[t], r = J(e, n);
580
- return q(d, r) ? "fresh" : "stale";
579
+ ), e = o.getState().initialStateGlobal[t], r = Z(e, n);
580
+ return Y(u, r) ? "fresh" : "stale";
581
581
  };
582
582
  if (l === "removeStorage")
583
583
  return () => {
584
- const d = o.getState().initialStateGlobal[t], e = rt(t), r = Q(e?.localStorage?.key) ? e?.localStorage?.key(d) : e?.localStorage?.key, s = `${f}-${t}-${r}`;
584
+ const u = o.getState().initialStateGlobal[t], e = rt(t), r = K(e?.localStorage?.key) ? e?.localStorage?.key(u) : e?.localStorage?.key, s = `${f}-${t}-${r}`;
585
585
  s && localStorage.removeItem(s);
586
586
  };
587
587
  if (l === "showValidationErrors")
588
588
  return () => {
589
- const d = o.getState().getInitialOptions(t)?.validation;
590
- if (!d?.key)
589
+ const u = o.getState().getInitialOptions(t)?.validation;
590
+ if (!u?.key)
591
591
  throw new Error("Validation key not found");
592
- return o.getState().getValidationErrors(d.key + "." + n.join("."));
592
+ return o.getState().getValidationErrors(u.key + "." + n.join("."));
593
593
  };
594
594
  if (Array.isArray(v)) {
595
- const d = () => S?.validIndices ? v.map((r, s) => ({
595
+ const u = () => S?.validIndices ? v.map((r, s) => ({
596
596
  item: r,
597
597
  originalIndex: S.validIndices[s]
598
598
  })) : o.getState().getNestedState(t, n).map((r, s) => ({
@@ -621,95 +621,118 @@ function ft(t, c, m, f) {
621
621
  itemHeight: r = 50,
622
622
  overscan: s = 5,
623
623
  stickToBottom: i = !1
624
- } = e, g = Z(null), [u, T] = tt({
624
+ } = e, g = X(null), [d, T] = et({
625
625
  startIndex: 0,
626
626
  endIndex: 10
627
- }), w = Z(i), [V, x] = tt(0), C = Z(!1), P = Z(0);
628
- it(() => {
627
+ }), w = X(i), [b, N] = et(0), C = X(!1), P = X(0);
628
+ st(() => {
629
629
  let $ = 0;
630
630
  return o.getState().subscribeToShadowState(t, () => {
631
631
  $++, $ <= 5 && console.log(
632
632
  `[VirtualView] Shadow update #${$}`
633
- ), x((L) => L + 1);
633
+ ), N((L) => L + 1);
634
634
  });
635
635
  }, [t]);
636
636
  const D = o().getNestedState(
637
637
  t,
638
638
  n
639
- ), N = D.length;
639
+ ), x = D.length;
640
640
  console.log(
641
- `[VirtualView] Initial setup - totalCount: ${N}, itemHeight: ${r}, stickToBottom: ${i}`
642
- ), N !== P.current && (console.log(
643
- `[VirtualView] Array size changed from ${P.current} to ${N}`
644
- ), C.current = !1, P.current = N);
645
- const { totalHeight: _, positions: E, allMeasured: M } = It(() => {
641
+ `[VirtualView] Initial setup - totalCount: ${x}, itemHeight: ${r}, stickToBottom: ${i}`
642
+ ), x !== P.current && (console.log(
643
+ `[VirtualView] Array size changed from ${P.current} to ${x}`
644
+ ), C.current = !1, P.current = x);
645
+ const { totalHeight: _, positions: E, visibleMeasured: U } = pt(() => {
646
646
  const $ = o.getState().getShadowMetadata(t, n) || [];
647
- let k = 0;
647
+ let A = 0;
648
648
  const L = [];
649
- let z = 0;
650
- for (let B = 0; B < N; B++) {
651
- L[B] = k;
652
- const at = $[B]?.virtualizer?.itemHeight;
653
- at && z++, k += at || r;
649
+ let q = 0, J = 0;
650
+ for (let H = 0; H < x; H++) {
651
+ L[H] = A;
652
+ const dt = $[H]?.virtualizer?.itemHeight;
653
+ dt && (q++, H >= d.startIndex && H < d.endIndex && J++), A += dt || r;
654
654
  }
655
- const Y = z === N && N > 0;
655
+ const G = d.endIndex - d.startIndex, lt = J === G && G > 0;
656
656
  return console.log(
657
- `[VirtualView] Heights calc - measured: ${z}/${N}, allMeasured: ${Y}, totalHeight: ${k}`
658
- ), { totalHeight: k, positions: L, allMeasured: Y };
657
+ `[VirtualView] Heights calc - measured: ${q}/${x}, visible measured: ${J}/${G}, totalHeight: ${A}`
658
+ ), {
659
+ totalHeight: A,
660
+ positions: L,
661
+ visibleMeasured: lt
662
+ };
659
663
  }, [
660
- N,
664
+ x,
661
665
  t,
662
666
  n.join("."),
663
667
  r,
664
- V
665
- ]), W = It(() => {
666
- const $ = Math.max(0, u.startIndex), k = Math.min(N, u.endIndex);
668
+ b,
669
+ d
670
+ // Add range dependency
671
+ ]), W = pt(() => {
672
+ const $ = Math.max(0, d.startIndex), A = Math.min(x, d.endIndex);
667
673
  console.log(
668
- `[VirtualView] Creating virtual slice - range: ${$}-${k} (${k - $} items)`
674
+ `[VirtualView] Creating virtual slice - range: ${$}-${A} (${A - $} items)`
669
675
  );
670
676
  const L = Array.from(
671
- { length: k - $ },
672
- (Y, B) => $ + B
673
- ), z = L.map((Y) => D[Y]);
674
- return a(z, n, {
677
+ { length: A - $ },
678
+ (J, G) => $ + G
679
+ ), q = L.map((J) => D[J]);
680
+ return a(q, n, {
675
681
  ...S,
676
682
  validIndices: L
677
683
  });
678
- }, [u.startIndex, u.endIndex, D, N]);
679
- ut(() => {
684
+ }, [d.startIndex, d.endIndex, D, x]);
685
+ gt(() => {
680
686
  const $ = g.current;
681
687
  if (!$) return;
682
- const k = () => {
688
+ const A = () => {
683
689
  if (!$) return;
684
- const { scrollTop: z } = $;
685
- let Y = 0, B = N - 1;
686
- for (; Y <= B; ) {
687
- const ht = Math.floor((Y + B) / 2);
688
- E[ht] < z ? Y = ht + 1 : B = ht - 1;
690
+ const { scrollTop: q } = $;
691
+ let J = 0, G = x - 1;
692
+ for (; J <= G; ) {
693
+ const vt = Math.floor((J + G) / 2);
694
+ E[vt] < q ? J = vt + 1 : G = vt - 1;
689
695
  }
690
- const at = Math.max(0, B - s);
691
- let nt = at;
692
- const Pt = z + $.clientHeight;
693
- for (; nt < N && E[nt] < Pt; )
694
- nt++;
695
- nt = Math.min(N, nt + s), T({ startIndex: at, endIndex: nt });
696
+ const lt = Math.max(0, G - s);
697
+ let H = lt;
698
+ const dt = q + $.clientHeight;
699
+ for (; H < x && E[H] < dt; )
700
+ H++;
701
+ H = Math.min(x, H + s), T({ startIndex: lt, endIndex: H });
696
702
  }, L = () => {
697
- w.current = $.scrollHeight - $.scrollTop - $.clientHeight < 1, k();
703
+ w.current = $.scrollHeight - $.scrollTop - $.clientHeight < 1, A();
698
704
  };
699
- return $.addEventListener("scroll", L, {
705
+ if ($.addEventListener("scroll", L, {
700
706
  passive: !0
701
- }), i && !C.current && N > 0 && (M ? (console.log(
702
- "[VirtualView] All items measured, scrolling to bottom"
703
- ), C.current = !0, $.scrollTo({
704
- top: $.scrollHeight,
705
- behavior: "auto"
706
- })) : console.log(
707
- "[VirtualView] Waiting for all measurements before scroll"
708
- )), k(), () => {
707
+ }), i && !C.current && x > 0)
708
+ if (d.endIndex >= x - 5)
709
+ console.log(
710
+ "[VirtualView] At end of list, scrolling to bottom"
711
+ ), C.current = !0, setTimeout(() => {
712
+ const G = $.scrollHeight + 1e3;
713
+ $.scrollTo({
714
+ top: G,
715
+ behavior: "auto"
716
+ }), w.current = !0;
717
+ }, 50);
718
+ else {
719
+ console.log(
720
+ "[VirtualView] Jumping near bottom to trigger measurements"
721
+ );
722
+ const G = Math.max(
723
+ 0,
724
+ (x - 20) * r
725
+ );
726
+ $.scrollTo({
727
+ top: G,
728
+ behavior: "auto"
729
+ });
730
+ }
731
+ return A(), () => {
709
732
  $.removeEventListener("scroll", L);
710
733
  };
711
- }, [N, E, i, M]);
712
- const A = Tt(
734
+ }, [x, E, i]);
735
+ const V = Et(
713
736
  ($ = "smooth") => {
714
737
  g.current && (w.current = !0, g.current.scrollTo({
715
738
  top: g.current.scrollHeight,
@@ -717,15 +740,15 @@ function ft(t, c, m, f) {
717
740
  }));
718
741
  },
719
742
  []
720
- ), F = Tt(
721
- ($, k = "smooth") => {
743
+ ), F = Et(
744
+ ($, A = "smooth") => {
722
745
  g.current && E[$] !== void 0 && (w.current = !1, g.current.scrollTo({
723
746
  top: E[$],
724
- behavior: k
747
+ behavior: A
725
748
  }));
726
749
  },
727
750
  [E]
728
- ), j = {
751
+ ), R = {
729
752
  outer: {
730
753
  ref: g,
731
754
  style: { overflowY: "auto", height: "100%" }
@@ -738,37 +761,37 @@ function ft(t, c, m, f) {
738
761
  },
739
762
  list: {
740
763
  style: {
741
- transform: `translateY(${E[u.startIndex] || 0}px)`
764
+ transform: `translateY(${E[d.startIndex] || 0}px)`
742
765
  }
743
766
  }
744
767
  };
745
768
  return {
746
769
  virtualState: W,
747
- virtualizerProps: j,
748
- scrollToBottom: A,
770
+ virtualizerProps: R,
771
+ scrollToBottom: V,
749
772
  scrollToIndex: F
750
773
  };
751
774
  };
752
775
  if (l === "stateSort")
753
776
  return (e) => {
754
- const s = [...d()].sort(
755
- (u, T) => e(u.item, T.item)
756
- ), i = s.map(({ item: u }) => u), g = {
777
+ const s = [...u()].sort(
778
+ (d, T) => e(d.item, T.item)
779
+ ), i = s.map(({ item: d }) => d), g = {
757
780
  ...S,
758
781
  validIndices: s.map(
759
- ({ originalIndex: u }) => u
782
+ ({ originalIndex: d }) => d
760
783
  )
761
784
  };
762
785
  return a(i, n, g);
763
786
  };
764
787
  if (l === "stateFilter")
765
788
  return (e) => {
766
- const s = d().filter(
767
- ({ item: u }, T) => e(u, T)
768
- ), i = s.map(({ item: u }) => u), g = {
789
+ const s = u().filter(
790
+ ({ item: d }, T) => e(d, T)
791
+ ), i = s.map(({ item: d }) => d), g = {
769
792
  ...S,
770
793
  validIndices: s.map(
771
- ({ originalIndex: u }) => u
794
+ ({ originalIndex: d }) => d
772
795
  )
773
796
  };
774
797
  return a(i, n, g);
@@ -777,20 +800,20 @@ function ft(t, c, m, f) {
777
800
  return (e) => {
778
801
  const r = o.getState().getNestedState(t, n);
779
802
  return Array.isArray(r) ? (S?.validIndices || Array.from({ length: r.length }, (i, g) => g)).map((i, g) => {
780
- const u = r[i], T = [...n, i.toString()], w = a(u, T, S);
781
- return e(u, w, {
803
+ const d = r[i], T = [...n, i.toString()], w = a(d, T, S);
804
+ return e(d, w, {
782
805
  register: () => {
783
- const [, x] = tt({}), C = `${m}-${n.join(".")}-${i}`;
784
- ut(() => {
806
+ const [, N] = et({}), C = `${m}-${n.join(".")}-${i}`;
807
+ gt(() => {
785
808
  const P = `${t}////${C}`, D = o.getState().stateComponents.get(t) || {
786
809
  components: /* @__PURE__ */ new Map()
787
810
  };
788
811
  return D.components.set(P, {
789
- forceUpdate: () => x({}),
812
+ forceUpdate: () => N({}),
790
813
  paths: /* @__PURE__ */ new Set([T.join(".")])
791
814
  }), o.getState().stateComponents.set(t, D), () => {
792
- const N = o.getState().stateComponents.get(t);
793
- N && N.components.delete(P);
815
+ const x = o.getState().stateComponents.get(t);
816
+ x && x.components.delete(P);
794
817
  };
795
818
  }, [t, C]);
796
819
  },
@@ -806,7 +829,7 @@ function ft(t, c, m, f) {
806
829
  return (e) => v.map((s, i) => {
807
830
  let g;
808
831
  S?.validIndices && S.validIndices[i] !== void 0 ? g = S.validIndices[i] : g = i;
809
- const u = [...n, g.toString()], T = a(s, u, S);
832
+ const d = [...n, g.toString()], T = a(s, d, S);
810
833
  return e(
811
834
  s,
812
835
  T,
@@ -816,7 +839,7 @@ function ft(t, c, m, f) {
816
839
  );
817
840
  });
818
841
  if (l === "$stateMap")
819
- return (e) => ct(Yt, {
842
+ return (e) => it(Yt, {
820
843
  proxy: {
821
844
  _stateKey: t,
822
845
  _path: n,
@@ -829,14 +852,14 @@ function ft(t, c, m, f) {
829
852
  return (e) => {
830
853
  const r = o.getState().getNestedState(t, n);
831
854
  return Array.isArray(r) ? (S?.validIndices || Array.from({ length: r.length }, (i, g) => g)).map((i, g) => {
832
- const u = r[i], T = [...n, i.toString()], w = a(u, T, S), V = `${m}-${n.join(".")}-${i}`;
833
- return ct(Xt, {
855
+ const d = r[i], T = [...n, i.toString()], w = a(d, T, S), b = `${m}-${n.join(".")}-${i}`;
856
+ return it(Xt, {
834
857
  key: i,
835
858
  stateKey: t,
836
- itemComponentId: V,
859
+ itemComponentId: b,
837
860
  itemPath: T,
838
861
  children: e(
839
- u,
862
+ d,
840
863
  w,
841
864
  g,
842
865
  r,
@@ -850,7 +873,7 @@ function ft(t, c, m, f) {
850
873
  if (l === "stateFlattenOn")
851
874
  return (e) => {
852
875
  const r = v;
853
- y.clear(), b++;
876
+ y.clear(), k++;
854
877
  const s = r.flatMap(
855
878
  (i) => i[e] ?? []
856
879
  );
@@ -873,36 +896,36 @@ function ft(t, c, m, f) {
873
896
  return a(s, i);
874
897
  };
875
898
  if (l === "insert")
876
- return (e) => (p(n), vt(c, e, n, t), a(
899
+ return (e) => (p(n), yt(c, e, n, t), a(
877
900
  o.getState().getNestedState(t, n),
878
901
  n
879
902
  ));
880
903
  if (l === "uniqueInsert")
881
904
  return (e, r, s) => {
882
- const i = o.getState().getNestedState(t, n), g = Q(e) ? e(i) : e;
883
- let u = null;
905
+ const i = o.getState().getNestedState(t, n), g = K(e) ? e(i) : e;
906
+ let d = null;
884
907
  if (!i.some((w) => {
885
908
  if (r) {
886
- const x = r.every(
887
- (C) => q(w[C], g[C])
909
+ const N = r.every(
910
+ (C) => Y(w[C], g[C])
888
911
  );
889
- return x && (u = w), x;
912
+ return N && (d = w), N;
890
913
  }
891
- const V = q(w, g);
892
- return V && (u = w), V;
914
+ const b = Y(w, g);
915
+ return b && (d = w), b;
893
916
  }))
894
- p(n), vt(c, g, n, t);
895
- else if (s && u) {
896
- const w = s(u), V = i.map(
897
- (x) => q(x, u) ? w : x
917
+ p(n), yt(c, g, n, t);
918
+ else if (s && d) {
919
+ const w = s(d), b = i.map(
920
+ (N) => Y(N, d) ? w : N
898
921
  );
899
- p(n), st(c, V, n);
922
+ p(n), at(c, b, n);
900
923
  }
901
924
  };
902
925
  if (l === "cut")
903
926
  return (e, r) => {
904
927
  if (!r?.waitForSync)
905
- return p(n), dt(c, n, t, e), a(
928
+ return p(n), ut(c, n, t, e), a(
906
929
  o.getState().getNestedState(t, n),
907
930
  n
908
931
  );
@@ -910,17 +933,17 @@ function ft(t, c, m, f) {
910
933
  if (l === "cutByValue")
911
934
  return (e) => {
912
935
  for (let r = 0; r < v.length; r++)
913
- v[r] === e && dt(c, n, t, r);
936
+ v[r] === e && ut(c, n, t, r);
914
937
  };
915
938
  if (l === "toggleByValue")
916
939
  return (e) => {
917
940
  const r = v.findIndex((s) => s === e);
918
- r > -1 ? dt(c, n, t, r) : vt(c, e, n, t);
941
+ r > -1 ? ut(c, n, t, r) : yt(c, e, n, t);
919
942
  };
920
943
  if (l === "stateFind")
921
944
  return (e) => {
922
- const s = d().find(
923
- ({ item: g }, u) => e(g, u)
945
+ const s = u().find(
946
+ ({ item: g }, d) => e(g, d)
924
947
  );
925
948
  if (!s) return;
926
949
  const i = [...n, s.originalIndex.toString()];
@@ -928,77 +951,77 @@ function ft(t, c, m, f) {
928
951
  };
929
952
  if (l === "findWith")
930
953
  return (e, r) => {
931
- const i = d().find(
932
- ({ item: u }) => u[e] === r
954
+ const i = u().find(
955
+ ({ item: d }) => d[e] === r
933
956
  );
934
957
  if (!i) return;
935
958
  const g = [...n, i.originalIndex.toString()];
936
959
  return a(i.item, g, S);
937
960
  };
938
961
  }
939
- const K = n[n.length - 1];
940
- if (!isNaN(Number(K))) {
941
- const d = n.slice(0, -1), e = o.getState().getNestedState(t, d);
962
+ const tt = n[n.length - 1];
963
+ if (!isNaN(Number(tt))) {
964
+ const u = n.slice(0, -1), e = o.getState().getNestedState(t, u);
942
965
  if (Array.isArray(e) && l === "cut")
943
- return () => dt(
966
+ return () => ut(
944
967
  c,
945
- d,
968
+ u,
946
969
  t,
947
- Number(K)
970
+ Number(tt)
948
971
  );
949
972
  }
950
973
  if (l === "get")
951
974
  return () => {
952
975
  if (S?.validIndices && Array.isArray(v)) {
953
- const d = o.getState().getNestedState(t, n);
954
- return S.validIndices.map((e) => d[e]);
976
+ const u = o.getState().getNestedState(t, n);
977
+ return S.validIndices.map((e) => u[e]);
955
978
  }
956
979
  return o.getState().getNestedState(t, n);
957
980
  };
958
981
  if (l === "$derive")
959
- return (d) => bt({
982
+ return (u) => Ct({
960
983
  _stateKey: t,
961
984
  _path: n,
962
- _effect: d.toString()
985
+ _effect: u.toString()
963
986
  });
964
987
  if (l === "$get")
965
- return () => bt({
988
+ return () => Ct({
966
989
  _stateKey: t,
967
990
  _path: n
968
991
  });
969
992
  if (l === "lastSynced") {
970
- const d = `${t}:${n.join(".")}`;
971
- return o.getState().getSyncInfo(d);
993
+ const u = `${t}:${n.join(".")}`;
994
+ return o.getState().getSyncInfo(u);
972
995
  }
973
996
  if (l == "getLocalStorage")
974
- return (d) => St(f + "-" + t + "-" + d);
997
+ return (u) => mt(f + "-" + t + "-" + u);
975
998
  if (l === "_selected") {
976
- const d = n.slice(0, -1), e = d.join("."), r = o.getState().getNestedState(t, d);
999
+ const u = n.slice(0, -1), e = u.join("."), r = o.getState().getNestedState(t, u);
977
1000
  return Array.isArray(r) ? Number(n[n.length - 1]) === o.getState().getSelectedIndex(t, e) : void 0;
978
1001
  }
979
1002
  if (l === "setSelected")
980
- return (d) => {
1003
+ return (u) => {
981
1004
  const e = n.slice(0, -1), r = Number(n[n.length - 1]), s = e.join(".");
982
- d ? o.getState().setSelectedIndex(t, s, r) : o.getState().setSelectedIndex(t, s, void 0);
1005
+ u ? o.getState().setSelectedIndex(t, s, r) : o.getState().setSelectedIndex(t, s, void 0);
983
1006
  const i = o.getState().getNestedState(t, [...e]);
984
- st(c, i, e), p(e);
1007
+ at(c, i, e), p(e);
985
1008
  };
986
1009
  if (l === "toggleSelected")
987
1010
  return () => {
988
- const d = n.slice(0, -1), e = Number(n[n.length - 1]), r = d.join("."), s = o.getState().getSelectedIndex(t, r);
1011
+ const u = n.slice(0, -1), e = Number(n[n.length - 1]), r = u.join("."), s = o.getState().getSelectedIndex(t, r);
989
1012
  o.getState().setSelectedIndex(
990
1013
  t,
991
1014
  r,
992
1015
  s === e ? void 0 : e
993
1016
  );
994
- const i = o.getState().getNestedState(t, [...d]);
995
- st(c, i, d), p(d);
1017
+ const i = o.getState().getNestedState(t, [...u]);
1018
+ at(c, i, u), p(u);
996
1019
  };
997
1020
  if (n.length == 0) {
998
1021
  if (l === "applyJsonPatch")
999
- return (d) => {
1000
- const e = o.getState().cogsStateStore[t], s = Dt(e, d).newDocument;
1001
- xt(
1022
+ return (u) => {
1023
+ const e = o.getState().cogsStateStore[t], s = Dt(e, u).newDocument;
1024
+ Pt(
1002
1025
  t,
1003
1026
  o.getState().initialStateGlobal[t],
1004
1027
  s,
@@ -1008,35 +1031,35 @@ function ft(t, c, m, f) {
1008
1031
  );
1009
1032
  const i = o.getState().stateComponents.get(t);
1010
1033
  if (i) {
1011
- const g = pt(e, s), u = new Set(g);
1034
+ const g = wt(e, s), d = new Set(g);
1012
1035
  for (const [
1013
1036
  T,
1014
1037
  w
1015
1038
  ] of i.components.entries()) {
1016
- let V = !1;
1017
- const x = Array.isArray(w.reactiveType) ? w.reactiveType : [w.reactiveType || "component"];
1018
- if (!x.includes("none")) {
1019
- if (x.includes("all")) {
1039
+ let b = !1;
1040
+ const N = Array.isArray(w.reactiveType) ? w.reactiveType : [w.reactiveType || "component"];
1041
+ if (!N.includes("none")) {
1042
+ if (N.includes("all")) {
1020
1043
  w.forceUpdate();
1021
1044
  continue;
1022
1045
  }
1023
- if (x.includes("component") && (w.paths.has("") && (V = !0), !V))
1024
- for (const C of u) {
1046
+ if (N.includes("component") && (w.paths.has("") && (b = !0), !b))
1047
+ for (const C of d) {
1025
1048
  if (w.paths.has(C)) {
1026
- V = !0;
1049
+ b = !0;
1027
1050
  break;
1028
1051
  }
1029
1052
  let P = C.lastIndexOf(".");
1030
1053
  for (; P !== -1; ) {
1031
1054
  const D = C.substring(0, P);
1032
1055
  if (w.paths.has(D)) {
1033
- V = !0;
1056
+ b = !0;
1034
1057
  break;
1035
1058
  }
1036
- const N = C.substring(
1059
+ const x = C.substring(
1037
1060
  P + 1
1038
1061
  );
1039
- if (!isNaN(Number(N))) {
1062
+ if (!isNaN(Number(x))) {
1040
1063
  const _ = D.lastIndexOf(".");
1041
1064
  if (_ !== -1) {
1042
1065
  const E = D.substring(
@@ -1044,44 +1067,44 @@ function ft(t, c, m, f) {
1044
1067
  _
1045
1068
  );
1046
1069
  if (w.paths.has(E)) {
1047
- V = !0;
1070
+ b = !0;
1048
1071
  break;
1049
1072
  }
1050
1073
  }
1051
1074
  }
1052
1075
  P = D.lastIndexOf(".");
1053
1076
  }
1054
- if (V) break;
1077
+ if (b) break;
1055
1078
  }
1056
- if (!V && x.includes("deps") && w.depsFunction) {
1079
+ if (!b && N.includes("deps") && w.depsFunction) {
1057
1080
  const C = w.depsFunction(s);
1058
1081
  let P = !1;
1059
- typeof C == "boolean" ? C && (P = !0) : q(w.deps, C) || (w.deps = C, P = !0), P && (V = !0);
1082
+ typeof C == "boolean" ? C && (P = !0) : Y(w.deps, C) || (w.deps = C, P = !0), P && (b = !0);
1060
1083
  }
1061
- V && w.forceUpdate();
1084
+ b && w.forceUpdate();
1062
1085
  }
1063
1086
  }
1064
1087
  }
1065
1088
  };
1066
1089
  if (l === "validateZodSchema")
1067
1090
  return () => {
1068
- const d = o.getState().getInitialOptions(t)?.validation, e = o.getState().addValidationError;
1069
- if (!d?.zodSchema)
1091
+ const u = o.getState().getInitialOptions(t)?.validation, e = o.getState().addValidationError;
1092
+ if (!u?.zodSchema)
1070
1093
  throw new Error("Zod schema not found");
1071
- if (!d?.key)
1094
+ if (!u?.key)
1072
1095
  throw new Error("Validation key not found");
1073
- X(d.key);
1096
+ Q(u.key);
1074
1097
  const r = o.getState().cogsStateStore[t];
1075
1098
  try {
1076
- const s = o.getState().getValidationErrors(d.key);
1099
+ const s = o.getState().getValidationErrors(u.key);
1077
1100
  s && s.length > 0 && s.forEach(([g]) => {
1078
- g && g.startsWith(d.key) && X(g);
1101
+ g && g.startsWith(u.key) && Q(g);
1079
1102
  });
1080
- const i = d.zodSchema.safeParse(r);
1081
- return i.success ? !0 : (i.error.errors.forEach((u) => {
1082
- const T = u.path, w = u.message, V = [d.key, ...T].join(".");
1083
- e(V, w);
1084
- }), mt(t), !1);
1103
+ const i = u.zodSchema.safeParse(r);
1104
+ return i.success ? !0 : (i.error.errors.forEach((d) => {
1105
+ const T = d.path, w = d.message, b = [u.key, ...T].join(".");
1106
+ e(b, w);
1107
+ }), ht(t), !1);
1085
1108
  } catch (s) {
1086
1109
  return console.error("Zod schema validation failed", s), !1;
1087
1110
  }
@@ -1090,7 +1113,7 @@ function ft(t, c, m, f) {
1090
1113
  if (l === "getComponents")
1091
1114
  return () => o().stateComponents.get(t);
1092
1115
  if (l === "getAllFormRefs")
1093
- return () => Et.getState().getFormRefsByStateKey(t);
1116
+ return () => Vt.getState().getFormRefsByStateKey(t);
1094
1117
  if (l === "_initialState")
1095
1118
  return o.getState().initialStateGlobal[t];
1096
1119
  if (l === "_serverState")
@@ -1103,12 +1126,12 @@ function ft(t, c, m, f) {
1103
1126
  if (l === "removeValidation") return I.removeValidation;
1104
1127
  }
1105
1128
  if (l === "getFormRef")
1106
- return () => Et.getState().getFormRef(t + "." + n.join("."));
1129
+ return () => Vt.getState().getFormRef(t + "." + n.join("."));
1107
1130
  if (l === "validationWrapper")
1108
1131
  return ({
1109
- children: d,
1132
+ children: u,
1110
1133
  hideMessage: e
1111
- }) => /* @__PURE__ */ yt(
1134
+ }) => /* @__PURE__ */ It(
1112
1135
  Ot,
1113
1136
  {
1114
1137
  formOpts: e ? { validation: { message: "" } } : void 0,
@@ -1116,53 +1139,53 @@ function ft(t, c, m, f) {
1116
1139
  validationKey: o.getState().getInitialOptions(t)?.validation?.key || "",
1117
1140
  stateKey: t,
1118
1141
  validIndices: S?.validIndices,
1119
- children: d
1142
+ children: u
1120
1143
  }
1121
1144
  );
1122
1145
  if (l === "_stateKey") return t;
1123
1146
  if (l === "_path") return n;
1124
1147
  if (l === "_isServerSynced") return I._isServerSynced;
1125
1148
  if (l === "update")
1126
- return (d, e) => {
1149
+ return (u, e) => {
1127
1150
  if (e?.debounce)
1128
1151
  jt(() => {
1129
- st(c, d, n, "");
1152
+ at(c, u, n, "");
1130
1153
  const r = o.getState().getNestedState(t, n);
1131
1154
  e?.afterUpdate && e.afterUpdate(r);
1132
1155
  }, e.debounce);
1133
1156
  else {
1134
- st(c, d, n, "");
1157
+ at(c, u, n, "");
1135
1158
  const r = o.getState().getNestedState(t, n);
1136
1159
  e?.afterUpdate && e.afterUpdate(r);
1137
1160
  }
1138
1161
  p(n);
1139
1162
  };
1140
1163
  if (l === "formElement")
1141
- return (d, e) => /* @__PURE__ */ yt(
1164
+ return (u, e) => /* @__PURE__ */ It(
1142
1165
  Ut,
1143
1166
  {
1144
1167
  setState: c,
1145
1168
  stateKey: t,
1146
1169
  path: n,
1147
- child: d,
1170
+ child: u,
1148
1171
  formOpts: e
1149
1172
  }
1150
1173
  );
1151
- const R = [...n, l], ot = o.getState().getNestedState(t, R);
1152
- return a(ot, R, S);
1174
+ const M = [...n, l], ot = o.getState().getNestedState(t, M);
1175
+ return a(ot, M, S);
1153
1176
  }
1154
- }, H = new Proxy(O, U);
1155
- return y.set(G, {
1156
- proxy: H,
1157
- stateVersion: b
1158
- }), H;
1177
+ }, B = new Proxy(j, O);
1178
+ return y.set(z, {
1179
+ proxy: B,
1180
+ stateVersion: k
1181
+ }), B;
1159
1182
  }
1160
1183
  return a(
1161
1184
  o.getState().getNestedState(t, [])
1162
1185
  );
1163
1186
  }
1164
- function bt(t) {
1165
- return ct(Zt, { proxy: t });
1187
+ function Ct(t) {
1188
+ return it(Zt, { proxy: t });
1166
1189
  }
1167
1190
  function Yt({
1168
1191
  proxy: t,
@@ -1173,14 +1196,14 @@ function Yt({
1173
1196
  m,
1174
1197
  t._path
1175
1198
  ).stateMapNoRender(
1176
- (y, b, p, I, a) => t._mapFn(y, b, p, I, a)
1199
+ (y, k, p, I, a) => t._mapFn(y, k, p, I, a)
1177
1200
  ) : null;
1178
1201
  }
1179
1202
  function Zt({
1180
1203
  proxy: t
1181
1204
  }) {
1182
- const c = Z(null), m = `${t._stateKey}-${t._path.join(".")}`;
1183
- return it(() => {
1205
+ const c = X(null), m = `${t._stateKey}-${t._path.join(".")}`;
1206
+ return st(() => {
1184
1207
  const f = c.current;
1185
1208
  if (!f || !f.parentElement) return;
1186
1209
  const y = f.parentElement, p = Array.from(y.childNodes).indexOf(f);
@@ -1201,15 +1224,15 @@ function Zt({
1201
1224
  "state",
1202
1225
  `return (${t._effect})(state)`
1203
1226
  )(n);
1204
- } catch (O) {
1205
- console.error("Error evaluating effect function during mount:", O), S = n;
1227
+ } catch (j) {
1228
+ console.error("Error evaluating effect function during mount:", j), S = n;
1206
1229
  }
1207
1230
  else
1208
1231
  S = n;
1209
1232
  S !== null && typeof S == "object" && (S = JSON.stringify(S));
1210
- const G = document.createTextNode(String(S));
1211
- f.replaceWith(G);
1212
- }, [t._stateKey, t._path.join("."), t._effect]), ct("span", {
1233
+ const z = document.createTextNode(String(S));
1234
+ f.replaceWith(z);
1235
+ }, [t._stateKey, t._path.join("."), t._effect]), it("span", {
1213
1236
  ref: c,
1214
1237
  style: { display: "none" },
1215
1238
  "data-signal-id": m
@@ -1228,7 +1251,7 @@ function ge(t) {
1228
1251
  },
1229
1252
  () => o.getState().getNestedState(t._stateKey, t._path)
1230
1253
  );
1231
- return ct("text", {}, String(c));
1254
+ return it("text", {}, String(c));
1232
1255
  }
1233
1256
  function Xt({
1234
1257
  stateKey: t,
@@ -1236,14 +1259,14 @@ function Xt({
1236
1259
  itemPath: m,
1237
1260
  children: f
1238
1261
  }) {
1239
- const [, y] = tt({}), [b, p] = Wt(), I = Z(null);
1240
- return it(() => {
1262
+ const [, y] = et({}), [k, p] = Wt(), I = X(null);
1263
+ return st(() => {
1241
1264
  p.height > 0 && p.height !== I.current && (I.current = p.height, o.getState().setShadowMetadata(t, m, {
1242
1265
  virtualizer: {
1243
1266
  itemHeight: p.height
1244
1267
  }
1245
1268
  }));
1246
- }, [p.height, t, m]), ut(() => {
1269
+ }, [p.height, t, m]), gt(() => {
1247
1270
  const a = `${t}////${c}`, v = o.getState().stateComponents.get(t) || {
1248
1271
  components: /* @__PURE__ */ new Map()
1249
1272
  };
@@ -1254,10 +1277,10 @@ function Xt({
1254
1277
  const n = o.getState().stateComponents.get(t);
1255
1278
  n && n.components.delete(a);
1256
1279
  };
1257
- }, [t, c, m.join(".")]), /* @__PURE__ */ yt("div", { ref: b, children: f });
1280
+ }, [t, c, m.join(".")]), /* @__PURE__ */ It("div", { ref: k, children: f });
1258
1281
  }
1259
1282
  export {
1260
- bt as $cogsSignal,
1283
+ Ct as $cogsSignal,
1261
1284
  ge as $cogsSignalStore,
1262
1285
  le as addStateOptions,
1263
1286
  de as createCogsState,