cogsbox-state 0.5.283 → 0.5.285

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