cogsbox-state 0.5.283 → 0.5.284

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,143 @@ 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().getNestedState(
633
+ t,
634
+ n
635
+ );
636
+ let b = 0;
637
+ const G = [];
638
+ for (let z = 0; z < $.length; z++)
639
+ G[z] = b, b += w(z);
640
+ return { totalHeight: b, positions: G };
641
+ }, [w]), V = Z(i), C = Z(0), x = Z(!0), U = o().getNestedState(
630
642
  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, {
643
+ n
644
+ ), D = U.length, P = xt(() => {
645
+ const $ = Math.max(0, u.startIndex), b = Math.min(D, u.endIndex), G = Array.from(
646
+ { length: b - $ },
647
+ (K, st) => $ + st
648
+ ), z = G.map((K) => U[K]);
649
+ return a(z, n, {
638
650
  ...S,
639
- validIndices: A
651
+ validIndices: G
640
652
  });
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);
653
+ }, [u.startIndex, u.endIndex, U, D]);
654
+ ft(() => {
655
+ const $ = g.current;
656
+ if (!$) return;
657
+ const b = V.current, G = D > C.current;
658
+ C.current = D;
659
+ const { totalHeight: z, positions: K } = A(), st = () => {
660
+ const { scrollTop: vt, clientHeight: $t, scrollHeight: jt } = $;
661
+ V.current = jt - vt - $t < 10;
662
+ let J = 0, It = K.length - 1;
663
+ for (; J < It; ) {
664
+ const et = Math.floor((J + It) / 2);
665
+ K[et] < vt ? J = et + 1 : It = et;
666
+ }
667
+ J = Math.max(0, J - s);
668
+ const Ot = vt + $t;
669
+ let tt = J;
670
+ for (; tt < K.length && K[tt] < Ot; )
671
+ tt++;
672
+ tt = Math.min(D, tt + s), E((et) => et.startIndex !== J || et.endIndex !== tt ? { startIndex: J, endIndex: tt } : et);
658
673
  };
659
- return T.addEventListener("scroll", P, {
674
+ return $.addEventListener("scroll", st, {
660
675
  passive: !0
661
- }), i && (x.current ? T.scrollTo({
662
- top: T.scrollHeight,
676
+ }), i && (x.current ? $.scrollTo({
677
+ top: $.scrollHeight,
663
678
  behavior: "auto"
664
- }) : _ && A && requestAnimationFrame(() => {
665
- T.scrollTo({
666
- top: T.scrollHeight,
679
+ }) : b && G && requestAnimationFrame(() => {
680
+ $.scrollTo({
681
+ top: $.scrollHeight,
667
682
  behavior: "smooth"
668
683
  });
669
- })), x.current = !1, P(), () => T.removeEventListener("scroll", P);
670
- }, [V, n, s, i]);
671
- const B = ft(
672
- (T = "smooth") => {
684
+ })), x.current = !1, st(), () => $.removeEventListener("scroll", st);
685
+ }, [D, A, s, i]);
686
+ const N = dt(
687
+ ($ = "smooth") => {
673
688
  g.current && g.current.scrollTo({
674
689
  top: g.current.scrollHeight,
675
- behavior: T
690
+ behavior: $
676
691
  });
677
692
  },
678
693
  []
679
- ), M = ft(
680
- (T, _ = "smooth") => {
681
- g.current && g.current.scrollTo({
682
- top: T * n,
683
- behavior: _
684
- });
694
+ ), M = dt(
695
+ ($, b = "smooth") => {
696
+ if (g.current) {
697
+ const { positions: G } = A();
698
+ g.current.scrollTo({
699
+ top: G[$] || 0,
700
+ behavior: b
701
+ });
702
+ }
685
703
  },
686
- [n]
687
- ), N = {
704
+ [A]
705
+ ), {
706
+ totalHeight: W,
707
+ positions: T
708
+ } = A(), F = T[u.startIndex] || 0, j = {
688
709
  outer: {
689
710
  ref: g,
690
711
  style: { overflowY: "auto", height: "100%" }
691
712
  },
692
713
  inner: {
693
714
  style: {
694
- height: `${V * n}px`,
715
+ height: `${W}px`,
695
716
  position: "relative"
696
717
  }
697
718
  },
698
719
  list: {
699
720
  style: {
700
- transform: `translateY(${u.startIndex * n}px)`
721
+ transform: `translateY(${F}px)`
701
722
  }
702
723
  }
703
724
  };
704
725
  return {
705
- virtualState: D,
706
- virtualizerProps: N,
707
- scrollToBottom: B,
726
+ virtualState: P,
727
+ virtualizerProps: j,
728
+ scrollToBottom: N,
708
729
  scrollToIndex: M
709
730
  };
710
731
  };
@@ -718,7 +739,7 @@ function lt(t, c, h, f) {
718
739
  ({ originalIndex: u }) => u
719
740
  )
720
741
  };
721
- return a(i, r, g);
742
+ return a(i, n, g);
722
743
  };
723
744
  if (l === "stateFilter")
724
745
  return (e) => {
@@ -730,26 +751,26 @@ function lt(t, c, h, f) {
730
751
  ({ originalIndex: u }) => u
731
752
  )
732
753
  };
733
- return a(i, r, g);
754
+ return a(i, n, g);
734
755
  };
735
756
  if (l === "stateMap")
736
757
  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);
758
+ const r = o.getState().getNestedState(t, n);
759
+ return Array.isArray(r) ? (S?.validIndices || Array.from({ length: r.length }, (i, g) => g)).map((i, g) => {
760
+ const u = r[i], E = [...n, i.toString()], w = a(u, E, S);
740
761
  return e(u, w, {
741
762
  register: () => {
742
- const [, x] = K({}), C = `${h}-${r.join(".")}-${i}`;
743
- it(() => {
744
- const V = `${t}////${C}`, D = o.getState().stateComponents.get(t) || {
763
+ const [, V] = ot({}), C = `${h}-${n.join(".")}-${i}`;
764
+ ft(() => {
765
+ const x = `${t}////${C}`, U = o.getState().stateComponents.get(t) || {
745
766
  components: /* @__PURE__ */ new Map()
746
767
  };
747
- return D.components.set(V, {
748
- forceUpdate: () => x({}),
768
+ return U.components.set(x, {
769
+ forceUpdate: () => V({}),
749
770
  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);
771
+ }), o.getState().stateComponents.set(t, U), () => {
772
+ const D = o.getState().stateComponents.get(t);
773
+ D && D.components.delete(x);
753
774
  };
754
775
  }, [t, C]);
755
776
  },
@@ -757,28 +778,28 @@ function lt(t, c, h, f) {
757
778
  originalIndex: i
758
779
  });
759
780
  }) : (console.warn(
760
- `stateMap called on a non-array value at path: ${r.join(".")}. The current value is:`,
761
- n
781
+ `stateMap called on a non-array value at path: ${n.join(".")}. The current value is:`,
782
+ r
762
783
  ), null);
763
784
  };
764
785
  if (l === "stateMapNoRender")
765
786
  return (e) => y.map((s, i) => {
766
787
  let g;
767
788
  S?.validIndices && S.validIndices[i] !== void 0 ? g = S.validIndices[i] : g = i;
768
- const u = [...r, g.toString()], E = a(s, u, S);
789
+ const u = [...n, g.toString()], E = a(s, u, S);
769
790
  return e(
770
791
  s,
771
792
  E,
772
793
  i,
773
794
  y,
774
- a(y, r, S)
795
+ a(y, n, S)
775
796
  );
776
797
  });
777
798
  if (l === "$stateMap")
778
- return (e) => rt(zt, {
799
+ return (e) => ct(Kt, {
779
800
  proxy: {
780
801
  _stateKey: t,
781
- _path: r,
802
+ _path: n,
782
803
  _mapFn: e
783
804
  // Pass the actual function, not string
784
805
  },
@@ -786,95 +807,95 @@ function lt(t, c, h, f) {
786
807
  });
787
808
  if (l === "stateList")
788
809
  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, {
810
+ const r = o.getState().getNestedState(t, n);
811
+ return Array.isArray(r) ? (S?.validIndices || Array.from({ length: r.length }, (i, g) => g)).map((i, g) => {
812
+ const u = r[i], E = [...n, i.toString()], w = a(u, E, S), A = `${h}-${n.join(".")}-${i}`;
813
+ return ct(ee, {
793
814
  key: i,
794
815
  stateKey: t,
795
- itemComponentId: $,
816
+ itemComponentId: A,
796
817
  itemPath: E,
797
818
  children: e(
798
819
  u,
799
820
  w,
800
821
  g,
801
- n,
802
- a(n, r, S)
822
+ r,
823
+ a(r, n, S)
803
824
  )
804
825
  });
805
826
  }) : (console.warn(
806
- `stateList called on a non-array value at path: ${r.join(".")}.`
827
+ `stateList called on a non-array value at path: ${n.join(".")}.`
807
828
  ), null);
808
829
  };
809
830
  if (l === "stateFlattenOn")
810
831
  return (e) => {
811
- const n = y;
832
+ const r = y;
812
833
  v.clear(), k++;
813
- const s = n.flatMap(
834
+ const s = r.flatMap(
814
835
  (i) => i[e] ?? []
815
836
  );
816
837
  return a(
817
838
  s,
818
- [...r, "[*]", e],
839
+ [...n, "[*]", e],
819
840
  S
820
841
  );
821
842
  };
822
843
  if (l === "index")
823
844
  return (e) => {
824
- const n = y[e];
825
- return a(n, [...r, e.toString()]);
845
+ const r = y[e];
846
+ return a(r, [...n, e.toString()]);
826
847
  };
827
848
  if (l === "last")
828
849
  return () => {
829
- const e = o.getState().getNestedState(t, r);
850
+ const e = o.getState().getNestedState(t, n);
830
851
  if (e.length === 0) return;
831
- const n = e.length - 1, s = e[n], i = [...r, n.toString()];
852
+ const r = e.length - 1, s = e[r], i = [...n, r.toString()];
832
853
  return a(s, i);
833
854
  };
834
855
  if (l === "insert")
835
- return (e) => (p(r), St(c, e, r, t), a(
836
- o.getState().getNestedState(t, r),
837
- r
856
+ return (e) => (p(n), pt(c, e, n, t), a(
857
+ o.getState().getNestedState(t, n),
858
+ n
838
859
  ));
839
860
  if (l === "uniqueInsert")
840
- return (e, n, s) => {
841
- const i = o.getState().getNestedState(t, r), g = J(e) ? e(i) : e;
861
+ return (e, r, s) => {
862
+ const i = o.getState().getNestedState(t, n), g = X(e) ? e(i) : e;
842
863
  let u = null;
843
864
  if (!i.some((w) => {
844
- if (n) {
845
- const x = n.every(
846
- (C) => L(w[C], g[C])
865
+ if (r) {
866
+ const V = r.every(
867
+ (C) => B(w[C], g[C])
847
868
  );
848
- return x && (u = w), x;
869
+ return V && (u = w), V;
849
870
  }
850
- const $ = L(w, g);
851
- return $ && (u = w), $;
871
+ const A = B(w, g);
872
+ return A && (u = w), A;
852
873
  }))
853
- p(r), St(c, g, r, t);
874
+ p(n), pt(c, g, n, t);
854
875
  else if (s && u) {
855
- const w = s(u), $ = i.map(
856
- (x) => L(x, u) ? w : x
876
+ const w = s(u), A = i.map(
877
+ (V) => B(V, u) ? w : V
857
878
  );
858
- p(r), nt(c, $, r);
879
+ p(n), it(c, A, n);
859
880
  }
860
881
  };
861
882
  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
883
+ return (e, r) => {
884
+ if (!r?.waitForSync)
885
+ return p(n), ut(c, n, t, e), a(
886
+ o.getState().getNestedState(t, n),
887
+ n
867
888
  );
868
889
  };
869
890
  if (l === "cutByValue")
870
891
  return (e) => {
871
- for (let n = 0; n < y.length; n++)
872
- y[n] === e && at(c, r, t, n);
892
+ for (let r = 0; r < y.length; r++)
893
+ y[r] === e && ut(c, n, t, r);
873
894
  };
874
895
  if (l === "toggleByValue")
875
896
  return (e) => {
876
- const n = y.findIndex((s) => s === e);
877
- n > -1 ? at(c, r, t, n) : St(c, e, r, t);
897
+ const r = y.findIndex((s) => s === e);
898
+ r > -1 ? ut(c, n, t, r) : pt(c, e, n, t);
878
899
  };
879
900
  if (l === "stateFind")
880
901
  return (e) => {
@@ -882,82 +903,82 @@ function lt(t, c, h, f) {
882
903
  ({ item: g }, u) => e(g, u)
883
904
  );
884
905
  if (!s) return;
885
- const i = [...r, s.originalIndex.toString()];
906
+ const i = [...n, s.originalIndex.toString()];
886
907
  return a(s.item, i, S);
887
908
  };
888
909
  if (l === "findWith")
889
- return (e, n) => {
910
+ return (e, r) => {
890
911
  const i = d().find(
891
- ({ item: u }) => u[e] === n
912
+ ({ item: u }) => u[e] === r
892
913
  );
893
914
  if (!i) return;
894
- const g = [...r, i.originalIndex.toString()];
915
+ const g = [...n, i.originalIndex.toString()];
895
916
  return a(i.item, g, S);
896
917
  };
897
918
  }
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);
919
+ const Q = n[n.length - 1];
920
+ if (!isNaN(Number(Q))) {
921
+ const d = n.slice(0, -1), e = o.getState().getNestedState(t, d);
901
922
  if (Array.isArray(e) && l === "cut")
902
- return () => at(
923
+ return () => ut(
903
924
  c,
904
925
  d,
905
926
  t,
906
- Number(Y)
927
+ Number(Q)
907
928
  );
908
929
  }
909
930
  if (l === "get")
910
931
  return () => {
911
932
  if (S?.validIndices && Array.isArray(y)) {
912
- const d = o.getState().getNestedState(t, r);
933
+ const d = o.getState().getNestedState(t, n);
913
934
  return S.validIndices.map((e) => d[e]);
914
935
  }
915
- return o.getState().getNestedState(t, r);
936
+ return o.getState().getNestedState(t, n);
916
937
  };
917
938
  if (l === "$derive")
918
- return (d) => At({
939
+ return (d) => Vt({
919
940
  _stateKey: t,
920
- _path: r,
941
+ _path: n,
921
942
  _effect: d.toString()
922
943
  });
923
944
  if (l === "$get")
924
- return () => At({
945
+ return () => Vt({
925
946
  _stateKey: t,
926
- _path: r
947
+ _path: n
927
948
  });
928
949
  if (l === "lastSynced") {
929
- const d = `${t}:${r.join(".")}`;
950
+ const d = `${t}:${n.join(".")}`;
930
951
  return o.getState().getSyncInfo(d);
931
952
  }
932
953
  if (l == "getLocalStorage")
933
- return (d) => dt(f + "-" + t + "-" + d);
954
+ return (d) => ht(f + "-" + t + "-" + d);
934
955
  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;
956
+ const d = n.slice(0, -1), e = d.join("."), r = o.getState().getNestedState(t, d);
957
+ return Array.isArray(r) ? Number(n[n.length - 1]) === o.getState().getSelectedIndex(t, e) : void 0;
937
958
  }
938
959
  if (l === "setSelected")
939
960
  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);
961
+ const e = n.slice(0, -1), r = Number(n[n.length - 1]), s = e.join(".");
962
+ d ? o.getState().setSelectedIndex(t, s, r) : o.getState().setSelectedIndex(t, s, void 0);
942
963
  const i = o.getState().getNestedState(t, [...e]);
943
- nt(c, i, e), p(e);
964
+ it(c, i, e), p(e);
944
965
  };
945
966
  if (l === "toggleSelected")
946
967
  return () => {
947
- const d = r.slice(0, -1), e = Number(r[r.length - 1]), n = d.join("."), s = o.getState().getSelectedIndex(t, n);
968
+ const d = n.slice(0, -1), e = Number(n[n.length - 1]), r = d.join("."), s = o.getState().getSelectedIndex(t, r);
948
969
  o.getState().setSelectedIndex(
949
970
  t,
950
- n,
971
+ r,
951
972
  s === e ? void 0 : e
952
973
  );
953
974
  const i = o.getState().getNestedState(t, [...d]);
954
- nt(c, i, d), p(d);
975
+ it(c, i, d), p(d);
955
976
  };
956
- if (r.length == 0) {
977
+ if (n.length == 0) {
957
978
  if (l === "applyJsonPatch")
958
979
  return (d) => {
959
- const e = o.getState().cogsStateStore[t], s = jt(e, d).newDocument;
960
- Nt(
980
+ const e = o.getState().cogsStateStore[t], s = Ht(e, d).newDocument;
981
+ Mt(
961
982
  t,
962
983
  o.getState().initialStateGlobal[t],
963
984
  s,
@@ -967,57 +988,57 @@ function lt(t, c, h, f) {
967
988
  );
968
989
  const i = o.getState().stateComponents.get(t);
969
990
  if (i) {
970
- const g = ht(e, s), u = new Set(g);
991
+ const g = Et(e, s), u = new Set(g);
971
992
  for (const [
972
993
  E,
973
994
  w
974
995
  ] 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")) {
996
+ let A = !1;
997
+ const V = Array.isArray(w.reactiveType) ? w.reactiveType : [w.reactiveType || "component"];
998
+ if (!V.includes("none")) {
999
+ if (V.includes("all")) {
979
1000
  w.forceUpdate();
980
1001
  continue;
981
1002
  }
982
- if (x.includes("component") && (w.paths.has("") && ($ = !0), !$))
1003
+ if (V.includes("component") && (w.paths.has("") && (A = !0), !A))
983
1004
  for (const C of u) {
984
1005
  if (w.paths.has(C)) {
985
- $ = !0;
1006
+ A = !0;
986
1007
  break;
987
1008
  }
988
- let V = C.lastIndexOf(".");
989
- for (; V !== -1; ) {
990
- const D = C.substring(0, V);
991
- if (w.paths.has(D)) {
992
- $ = !0;
1009
+ let x = C.lastIndexOf(".");
1010
+ for (; x !== -1; ) {
1011
+ const U = C.substring(0, x);
1012
+ if (w.paths.has(U)) {
1013
+ A = !0;
993
1014
  break;
994
1015
  }
995
- const B = C.substring(
996
- V + 1
1016
+ const D = C.substring(
1017
+ x + 1
997
1018
  );
998
- if (!isNaN(Number(B))) {
999
- const M = D.lastIndexOf(".");
1000
- if (M !== -1) {
1001
- const N = D.substring(
1019
+ if (!isNaN(Number(D))) {
1020
+ const P = U.lastIndexOf(".");
1021
+ if (P !== -1) {
1022
+ const N = U.substring(
1002
1023
  0,
1003
- M
1024
+ P
1004
1025
  );
1005
1026
  if (w.paths.has(N)) {
1006
- $ = !0;
1027
+ A = !0;
1007
1028
  break;
1008
1029
  }
1009
1030
  }
1010
1031
  }
1011
- V = D.lastIndexOf(".");
1032
+ x = U.lastIndexOf(".");
1012
1033
  }
1013
- if ($) break;
1034
+ if (A) break;
1014
1035
  }
1015
- if (!$ && x.includes("deps") && w.depsFunction) {
1036
+ if (!A && V.includes("deps") && w.depsFunction) {
1016
1037
  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);
1038
+ let x = !1;
1039
+ typeof C == "boolean" ? C && (x = !0) : B(w.deps, C) || (w.deps = C, x = !0), x && (A = !0);
1019
1040
  }
1020
- $ && w.forceUpdate();
1041
+ A && w.forceUpdate();
1021
1042
  }
1022
1043
  }
1023
1044
  }
@@ -1029,18 +1050,18 @@ function lt(t, c, h, f) {
1029
1050
  throw new Error("Zod schema not found");
1030
1051
  if (!d?.key)
1031
1052
  throw new Error("Validation key not found");
1032
- H(d.key);
1033
- const n = o.getState().cogsStateStore[t];
1053
+ Y(d.key);
1054
+ const r = o.getState().cogsStateStore[t];
1034
1055
  try {
1035
1056
  const s = o.getState().getValidationErrors(d.key);
1036
1057
  s && s.length > 0 && s.forEach(([g]) => {
1037
- g && g.startsWith(d.key) && H(g);
1058
+ g && g.startsWith(d.key) && Y(g);
1038
1059
  });
1039
- const i = d.zodSchema.safeParse(n);
1060
+ const i = d.zodSchema.safeParse(r);
1040
1061
  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);
1062
+ const E = u.path, w = u.message, A = [d.key, ...E].join(".");
1063
+ e(A, w);
1064
+ }), yt(t), !1);
1044
1065
  } catch (s) {
1045
1066
  return console.error("Zod schema validation failed", s), !1;
1046
1067
  }
@@ -1049,7 +1070,7 @@ function lt(t, c, h, f) {
1049
1070
  if (l === "getComponents")
1050
1071
  return () => o().stateComponents.get(t);
1051
1072
  if (l === "getAllFormRefs")
1052
- return () => It.getState().getFormRefsByStateKey(t);
1073
+ return () => kt.getState().getFormRefsByStateKey(t);
1053
1074
  if (l === "_initialState")
1054
1075
  return o.getState().initialStateGlobal[t];
1055
1076
  if (l === "_serverState")
@@ -1062,16 +1083,16 @@ function lt(t, c, h, f) {
1062
1083
  if (l === "removeValidation") return I.removeValidation;
1063
1084
  }
1064
1085
  if (l === "getFormRef")
1065
- return () => It.getState().getFormRef(t + "." + r.join("."));
1086
+ return () => kt.getState().getFormRef(t + "." + n.join("."));
1066
1087
  if (l === "validationWrapper")
1067
1088
  return ({
1068
1089
  children: d,
1069
1090
  hideMessage: e
1070
- }) => /* @__PURE__ */ mt(
1071
- Pt,
1091
+ }) => /* @__PURE__ */ wt(
1092
+ Wt,
1072
1093
  {
1073
1094
  formOpts: e ? { validation: { message: "" } } : void 0,
1074
- path: r,
1095
+ path: n,
1075
1096
  validationKey: o.getState().getInitialOptions(t)?.validation?.key || "",
1076
1097
  stateKey: t,
1077
1098
  validIndices: S?.validIndices,
@@ -1079,51 +1100,51 @@ function lt(t, c, h, f) {
1079
1100
  }
1080
1101
  );
1081
1102
  if (l === "_stateKey") return t;
1082
- if (l === "_path") return r;
1103
+ if (l === "_path") return n;
1083
1104
  if (l === "_isServerSynced") return I._isServerSynced;
1084
1105
  if (l === "update")
1085
1106
  return (d, e) => {
1086
1107
  if (e?.debounce)
1087
- xt(() => {
1088
- nt(c, d, r, "");
1089
- const n = o.getState().getNestedState(t, r);
1090
- e?.afterUpdate && e.afterUpdate(n);
1108
+ Dt(() => {
1109
+ it(c, d, n, "");
1110
+ const r = o.getState().getNestedState(t, n);
1111
+ e?.afterUpdate && e.afterUpdate(r);
1091
1112
  }, e.debounce);
1092
1113
  else {
1093
- nt(c, d, r, "");
1094
- const n = o.getState().getNestedState(t, r);
1095
- e?.afterUpdate && e.afterUpdate(n);
1114
+ it(c, d, n, "");
1115
+ const r = o.getState().getNestedState(t, n);
1116
+ e?.afterUpdate && e.afterUpdate(r);
1096
1117
  }
1097
- p(r);
1118
+ p(n);
1098
1119
  };
1099
1120
  if (l === "formElement")
1100
- return (d, e) => /* @__PURE__ */ mt(
1101
- _t,
1121
+ return (d, e) => /* @__PURE__ */ wt(
1122
+ Gt,
1102
1123
  {
1103
1124
  setState: c,
1104
1125
  stateKey: t,
1105
- path: r,
1126
+ path: n,
1106
1127
  child: d,
1107
1128
  formOpts: e
1108
1129
  }
1109
1130
  );
1110
- const j = [...r, l], tt = o.getState().getNestedState(t, j);
1111
- return a(tt, j, S);
1131
+ const _ = [...n, l], at = o.getState().getNestedState(t, _);
1132
+ return a(at, _, S);
1112
1133
  }
1113
- }, G = new Proxy(O, R);
1114
- return v.set(W, {
1115
- proxy: G,
1134
+ }, H = new Proxy(O, R);
1135
+ return v.set(L, {
1136
+ proxy: H,
1116
1137
  stateVersion: k
1117
- }), G;
1138
+ }), H;
1118
1139
  }
1119
1140
  return a(
1120
1141
  o.getState().getNestedState(t, [])
1121
1142
  );
1122
1143
  }
1123
- function At(t) {
1124
- return rt(Bt, { proxy: t });
1144
+ function Vt(t) {
1145
+ return ct(te, { proxy: t });
1125
1146
  }
1126
- function zt({
1147
+ function Kt({
1127
1148
  proxy: t,
1128
1149
  rebuildStateShape: c
1129
1150
  }) {
@@ -1135,11 +1156,11 @@ function zt({
1135
1156
  (v, k, p, I, a) => t._mapFn(v, k, p, I, a)
1136
1157
  ) : null;
1137
1158
  }
1138
- function Bt({
1159
+ function te({
1139
1160
  proxy: t
1140
1161
  }) {
1141
- const c = q(null), h = `${t._stateKey}-${t._path.join(".")}`;
1142
- return st(() => {
1162
+ const c = Z(null), h = `${t._stateKey}-${t._path.join(".")}`;
1163
+ return gt(() => {
1143
1164
  const f = c.current;
1144
1165
  if (!f || !f.parentElement) return;
1145
1166
  const v = f.parentElement, p = Array.from(v.childNodes).indexOf(f);
@@ -1152,30 +1173,30 @@ function Bt({
1152
1173
  effect: t._effect
1153
1174
  };
1154
1175
  o.getState().addSignalElement(h, y);
1155
- const r = o.getState().getNestedState(t._stateKey, t._path);
1176
+ const n = o.getState().getNestedState(t._stateKey, t._path);
1156
1177
  let S;
1157
1178
  if (t._effect)
1158
1179
  try {
1159
1180
  S = new Function(
1160
1181
  "state",
1161
1182
  `return (${t._effect})(state)`
1162
- )(r);
1183
+ )(n);
1163
1184
  } catch (O) {
1164
- console.error("Error evaluating effect function during mount:", O), S = r;
1185
+ console.error("Error evaluating effect function during mount:", O), S = n;
1165
1186
  }
1166
1187
  else
1167
- S = r;
1188
+ S = n;
1168
1189
  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", {
1190
+ const L = document.createTextNode(String(S));
1191
+ f.replaceWith(L);
1192
+ }, [t._stateKey, t._path.join("."), t._effect]), ct("span", {
1172
1193
  ref: c,
1173
1194
  style: { display: "none" },
1174
1195
  "data-signal-id": h
1175
1196
  });
1176
1197
  }
1177
- function ie(t) {
1178
- const c = Ct(
1198
+ function he(t) {
1199
+ const c = Rt(
1179
1200
  (h) => {
1180
1201
  const f = o.getState().stateComponents.get(t._stateKey) || {
1181
1202
  components: /* @__PURE__ */ new Map()
@@ -1187,22 +1208,22 @@ function ie(t) {
1187
1208
  },
1188
1209
  () => o.getState().getNestedState(t._stateKey, t._path)
1189
1210
  );
1190
- return rt("text", {}, String(c));
1211
+ return ct("text", {}, String(c));
1191
1212
  }
1192
- function Ht({
1213
+ function ee({
1193
1214
  stateKey: t,
1194
1215
  itemComponentId: c,
1195
1216
  itemPath: h,
1196
1217
  children: f
1197
1218
  }) {
1198
- const [, v] = K({}), [k, p] = Ot();
1199
- return st(() => {
1219
+ const [, v] = ot({}), [k, p] = zt();
1220
+ return gt(() => {
1200
1221
  p.height > 0 && o.getState().setShadowMetadata(t, h, {
1201
1222
  virtualizer: {
1202
1223
  itemHeight: p.height
1203
1224
  }
1204
1225
  });
1205
- }, [p.height]), it(() => {
1226
+ }, [p.height]), ft(() => {
1206
1227
  const I = `${t}////${c}`, a = o.getState().stateComponents.get(t) || {
1207
1228
  components: /* @__PURE__ */ new Map()
1208
1229
  };
@@ -1213,14 +1234,14 @@ function Ht({
1213
1234
  const y = o.getState().stateComponents.get(t);
1214
1235
  y && y.components.delete(I);
1215
1236
  };
1216
- }, [t, c, h.join(".")]), /* @__PURE__ */ mt("div", { ref: k, children: f });
1237
+ }, [t, c, h.join(".")]), /* @__PURE__ */ wt("div", { ref: k, children: f });
1217
1238
  }
1218
1239
  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
1240
+ Vt as $cogsSignal,
1241
+ he as $cogsSignalStore,
1242
+ fe as addStateOptions,
1243
+ Se as createCogsState,
1244
+ me as notifyComponent,
1245
+ Qt as useCogsStateFn
1225
1246
  };
1226
1247
  //# sourceMappingURL=CogsState.jsx.map