cogsbox-state 0.5.294 → 0.5.295

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