cogsbox-state 0.5.325 → 0.5.327

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