cogsbox-state 0.5.305 → 0.5.307

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