cogsbox-state 0.5.339 → 0.5.342

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