cogsbox-state 0.5.294 → 0.5.296

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
- 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";
7
- import { v4 as pt } from "uuid";
2
+ import { jsx as pt } from "react/jsx-runtime";
3
+ import { useState as rt, useRef as Y, useEffect as dt, useLayoutEffect as ut, useMemo as wt, createElement as st, useSyncExternalStore as jt, startTransition as Ot, useCallback as vt } from "react";
4
+ import { transformStateFunc as Rt, isDeepEqual as H, isFunction as Z, getNestedValue as z, getDifferences as Et, debounce as Ft } from "./utility.js";
5
+ import { pushFunc as It, updateFn as at, cutFunc as lt, ValidationWrapper as Ut, FormControlComponent as Dt } from "./Functions.jsx";
6
+ import Wt from "superjson";
7
+ import { v4 as At } from "uuid";
8
8
  import "zod";
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";
12
- import Ut from "react-use-measure";
13
- function Tt(t, i) {
9
+ import { getGlobalStore as o, formRefStore as $t } from "./store.js";
10
+ import { useCogsConfig as Ct } from "./CogsStateClient.jsx";
11
+ import { applyPatch as Gt } from "fast-json-patch";
12
+ import Lt from "react-use-measure";
13
+ function kt(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 Vt({
21
21
  stateKey: t,
22
- options: i,
22
+ options: c,
23
23
  initialOptionsPart: h
24
24
  }) {
25
25
  const f = nt(t) || {}, v = h[t] || {}, k = o.getState().setInitialStateOptions, w = { ...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]);
27
+ if (c)
28
+ for (const a in c)
29
+ w.hasOwnProperty(a) ? (a == "localStorage" && c[a] && w[a].key !== c[a]?.key && (I = !0, w[a] = c[a]), a == "initialState" && c[a] && w[a] !== c[a] && // Different references
30
+ !H(w[a], c[a]) && (I = !0, w[a] = c[a])) : (I = !0, w[a] = c[a]);
31
31
  I && k(t, w);
32
32
  }
33
- function se(t, { formElements: i, validation: h }) {
34
- return { initialState: t, formElements: i, validation: h };
33
+ function fe(t, { formElements: c, validation: h }) {
34
+ return { initialState: t, formElements: c, validation: h };
35
35
  }
36
- const ie = (t, i) => {
36
+ const Se = (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] = Rt(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
- const [y] = rt(a?.componentId ?? pt());
50
- At({
49
+ const [y] = rt(a?.componentId ?? At());
50
+ Vt({
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] = Zt(
56
56
  S,
57
57
  {
58
58
  stateKey: I,
@@ -68,70 +68,70 @@ const ie = (t, i) => {
68
68
  serverState: a?.serverState
69
69
  }
70
70
  );
71
- return R;
71
+ return F;
72
72
  };
73
73
  function w(I, a) {
74
- At({ stateKey: I, options: a, initialOptionsPart: v }), a.localStorage && Lt(I, a), St(I);
74
+ Vt({ stateKey: I, options: a, initialOptionsPart: v }), a.localStorage && Jt(I, a), mt(I);
75
75
  }
76
76
  return { useCogsState: k, setCogsOptions: w };
77
77
  }, {
78
- setUpdaterState: ut,
78
+ setUpdaterState: gt,
79
79
  setState: K,
80
80
  getInitialOptions: nt,
81
- getKeyState: Ct,
82
- getValidationErrors: Ft,
83
- setStateLog: Dt,
84
- updateInitialStateGlobal: wt,
85
- addValidationError: Wt,
81
+ getKeyState: bt,
82
+ getValidationErrors: Ht,
83
+ setStateLog: zt,
84
+ updateInitialStateGlobal: Tt,
85
+ addValidationError: Bt,
86
86
  removeValidationError: J,
87
- setServerSyncActions: Gt
88
- } = o.getState(), $t = (t, i, h, f, v) => {
87
+ setServerSyncActions: qt
88
+ } = o.getState(), xt = (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
95
  const k = Z(h?.localStorage?.key) ? h.localStorage?.key(t) : h?.localStorage?.key;
96
96
  if (k && f) {
97
- const w = `${f}-${i}-${k}`;
97
+ const w = `${f}-${c}-${k}`;
98
98
  let I;
99
99
  try {
100
- I = ft(w)?.lastSyncedWithServer;
100
+ I = St(w)?.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 = Wt.serialize(a);
108
108
  window.localStorage.setItem(
109
109
  w,
110
110
  JSON.stringify(y.json)
111
111
  );
112
112
  }
113
- }, ft = (t) => {
113
+ }, St = (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
+ }, Jt = (t, c) => {
122
+ const h = o.getState().cogsStateStore[t], { sessionId: f } = Ct(), v = Z(c?.localStorage?.key) ? c.localStorage.key(h) : c?.localStorage?.key;
123
123
  if (v && f) {
124
- const k = ft(
124
+ const k = St(
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 K(t, k.state), mt(t), !0;
129
129
  }
130
130
  return !1;
131
- }, Vt = (t, i, h, f, v, k) => {
131
+ }, Pt = (t, c, h, f, v, k) => {
132
132
  const w = {
133
- initialState: i,
134
- updaterState: gt(
133
+ initialState: c,
134
+ updaterState: ft(
135
135
  t,
136
136
  f,
137
137
  v,
@@ -139,29 +139,29 @@ 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);
143
- }, St = (t) => {
144
- const i = o.getState().stateComponents.get(t);
145
- if (!i) return;
142
+ Tt(t, w.initialState), gt(t, w.updaterState), K(t, w.state);
143
+ }, mt = (t) => {
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
+ }, me = (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
+ }, Yt = (t, c, h, f) => {
161
161
  switch (t) {
162
162
  case "update":
163
163
  return {
164
- oldValue: z(i, f),
164
+ oldValue: z(c, f),
165
165
  newValue: z(h, f)
166
166
  };
167
167
  case "insert":
@@ -172,7 +172,7 @@ const ie = (t, i) => {
172
172
  };
173
173
  case "cut":
174
174
  return {
175
- oldValue: z(i, f),
175
+ oldValue: z(c, f),
176
176
  newValue: null
177
177
  // or undefined
178
178
  };
@@ -180,8 +180,8 @@ const ie = (t, i) => {
180
180
  return { oldValue: null, newValue: null };
181
181
  }
182
182
  };
183
- function zt(t, {
184
- stateKey: i,
183
+ function Zt(t, {
184
+ stateKey: c,
185
185
  serverSync: h,
186
186
  localStorage: f,
187
187
  formElements: v,
@@ -193,12 +193,12 @@ function zt(t, {
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 } = Ct();
197
+ let G = !c;
198
+ const [m] = rt(c ?? At()), l = o.getState().stateLog[m], it = Y(/* @__PURE__ */ new Set()), X = Y(I ?? At()), O = Y(
199
199
  null
200
200
  );
201
- O.current = nt(m) ?? null, st(() => {
201
+ O.current = nt(m) ?? null, dt(() => {
202
202
  if (y && y.stateKey === m && y.path?.[0]) {
203
203
  K(m, (r) => ({
204
204
  ...r,
@@ -210,35 +210,35 @@ function zt(t, {
210
210
  userId: y.userId
211
211
  });
212
212
  }
213
- }, [y]), st(() => {
213
+ }, [y]), dt(() => {
214
214
  if (a) {
215
- Tt(m, {
215
+ kt(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 && !H(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 = Z(e?.localStorage?.key) ? e?.localStorage?.key(a) : e?.localStorage?.key;
223
+ A && U && (u = St(`${U}-${m}-${A}`));
224
+ let p = a, T = !1;
225
+ const b = s ? Date.now() : 0, N = u?.lastUpdated || 0, M = u?.lastSyncedWithServer || 0;
226
+ s && b > N ? (p = e.serverState.data, T = !0) : u && N > M && (p = u.state, e?.localStorage?.onChange && e?.localStorage?.onChange(p)), o.getState().initializeShadowState(m, a), Pt(
227
227
  m,
228
228
  a,
229
- E,
229
+ p,
230
230
  ot,
231
231
  X.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 && xt(p, m, e, U, Date.now()), mt(m), (Array.isArray(w) ? w : [w || "component"]).includes("none") || F({});
234
234
  }
235
235
  }, [
236
236
  a,
237
237
  S?.status,
238
238
  S?.data,
239
239
  ...n || []
240
- ]), dt(() => {
241
- G && Tt(m, {
240
+ ]), ut(() => {
241
+ G && kt(m, {
242
242
  serverSync: h,
243
243
  formElements: v,
244
244
  initialState: a,
@@ -249,191 +249,191 @@ function zt(t, {
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
256
  reactiveType: w ?? ["component", "deps"]
257
- }), o.getState().stateComponents.set(m, r), R({}), () => {
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
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);
268
+ const A = Z(e) ? e(u) : e, p = `${m}-${r.join(".")}`;
269
+ if (p) {
270
+ let C = !1, V = g.signalDomElements.get(p);
271
+ if ((!V || V.size === 0) && (s.updateType === "insert" || s.updateType === "cut")) {
272
+ const R = r.slice(0, -1), D = z(A, R);
273
273
  if (Array.isArray(D)) {
274
- b = !0;
275
- const A = `${m}-${j.join(".")}`;
276
- C = g.signalDomElements.get(A);
274
+ C = !0;
275
+ const $ = `${m}-${R.join(".")}`;
276
+ V = 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 (V) {
280
+ const R = C ? z(A, r.slice(0, -1)) : z(A, r);
281
+ V.forEach(({ parentId: D, position: $, effect: E }) => {
282
+ const x = 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 (x) {
286
+ const _ = Array.from(x.childNodes);
287
+ if (_[$]) {
288
+ const j = E ? new Function("state", `return (${E})(state)`)(R) : R;
289
+ _[$].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 && J(
296
+ (i || O.current?.validation?.key) + "." + r.join(".")
297
297
  );
298
- const $ = r.slice(0, r.length - 1);
298
+ const T = r.slice(0, r.length - 1);
299
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);
300
+ O.current?.validation?.key + "." + T.join(".")
301
+ ), s.updateType === "insert" && O.current?.validation?.key && Ht(
302
+ O.current?.validation?.key + "." + T.join(".")
303
+ ).filter(([V, R]) => {
304
+ let D = V?.split(".").length;
305
+ if (V == T.join(".") && D == T.length - 1) {
306
+ let $ = V + "." + T;
307
+ J(V), Bt($, 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 b = g.stateComponents.get(m);
311
+ if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", b), b) {
312
+ const C = Et(u, A), V = new Set(C), 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 b.components.entries()) {
317
+ let E = !1;
318
+ const x = Array.isArray($.reactiveType) ? $.reactiveType : [$.reactiveType || "component"];
319
+ if (console.log("component", $), !x.includes("none")) {
320
+ if (x.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 (x.includes("component") && (($.paths.has(R) || $.paths.has("")) && (E = !0), !E))
325
+ for (const _ of V) {
326
+ let j = _;
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 L = j.lastIndexOf(".");
333
+ if (L !== -1) {
334
+ const Q = j.substring(
335
335
  0,
336
- B
336
+ L
337
337
  );
338
338
  if (!isNaN(
339
- Number(M.substring(B + 1))
340
- ) && A.paths.has(tt)) {
341
- p = !0;
339
+ Number(j.substring(L + 1))
340
+ ) && $.paths.has(Q)) {
341
+ E = !0;
342
342
  break;
343
343
  }
344
- M = tt;
344
+ j = Q;
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 && x.includes("deps") && $.depsFunction) {
353
+ const _ = $.depsFunction(A);
354
+ let j = !1;
355
+ typeof _ == "boolean" ? _ && (j = !0) : H($.deps, _) || ($.deps = _, 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 N = Date.now();
362
+ r = r.map((C, V) => {
363
+ const R = r.slice(0, -1), D = z(A, R);
364
+ return V === r.length - 1 && ["insert", "cut"].includes(s.updateType) ? (D.length - 1).toString() : C;
365
365
  });
366
- const { oldValue: N, newValue: F } = Ht(
366
+ const { oldValue: M, newValue: P } = Yt(
367
367
  s.updateType,
368
368
  u,
369
- T,
369
+ A,
370
370
  r
371
- ), L = {
372
- timeStamp: x,
371
+ ), B = {
372
+ timeStamp: N,
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: M,
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
- const b = r.slice(0, -1);
386
- g.insertShadowArrayElement(m, b, F);
385
+ const C = r.slice(0, -1);
386
+ g.insertShadowArrayElement(m, C, 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 V = r.slice(0, -1), R = parseInt(r[r.length - 1]);
390
+ g.removeShadowArrayElement(m, V, 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 (zt(m, (C) => {
394
+ const R = [...C ?? [], B].reduce((D, $) => {
395
+ const E = `${$.stateKey}:${JSON.stringify($.path)}`, x = D.get(E);
396
+ return x ? (x.timeStamp = Math.max(x.timeStamp, $.timeStamp), x.newValue = $.newValue, x.oldValue = x.oldValue ?? $.oldValue, x.updateType = $.updateType) : D.set(E, { ...$ }), D;
397
397
  }, /* @__PURE__ */ new Map());
398
- return Array.from(j.values());
399
- }), $t(
400
- T,
398
+ return Array.from(R.values());
399
+ }), xt(
400
+ A,
401
401
  m,
402
402
  O.current,
403
403
  U
404
404
  ), O.current?.middleware && O.current.middleware({
405
405
  updateLog: l,
406
- update: L
406
+ update: B
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 }),
411
- rollBackState: b,
412
- actionTimeStamp: Date.now() + (C.debounce ?? 3e3),
408
+ const C = g.serverState[m], V = O.current?.serverSync;
409
+ qt(m, {
410
+ syncKey: typeof V.syncKey == "string" ? V.syncKey : V.syncKey({ state: A }),
411
+ rollBackState: C,
412
+ actionTimeStamp: Date.now() + (V.debounce ?? 3e3),
413
413
  status: "waiting"
414
414
  });
415
415
  }
416
- return T;
416
+ return A;
417
417
  });
418
418
  };
419
- o.getState().updaterState[m] || (ut(
419
+ o.getState().updaterState[m] || (gt(
420
420
  m,
421
- gt(
421
+ ft(
422
422
  m,
423
423
  ot,
424
424
  X.current,
425
425
  U
426
426
  )
427
- ), o.getState().cogsStateStore[m] || K(m, t), o.getState().initialStateGlobal[m] || wt(m, t));
428
- const d = vt(() => gt(
427
+ ), o.getState().cogsStateStore[m] || K(m, t), o.getState().initialStateGlobal[m] || Tt(m, t));
428
+ const d = wt(() => ft(
429
429
  m,
430
430
  ot,
431
431
  X.current,
432
432
  U
433
433
  ), [m, U]);
434
- return [Ct(m), d];
434
+ return [bt(m), d];
435
435
  }
436
- function gt(t, i, h, f) {
436
+ function ft(t, c, h, f) {
437
437
  const v = /* @__PURE__ */ new Map();
438
438
  let k = 0;
439
439
  const w = (y) => {
@@ -450,8 +450,8 @@ function gt(t, i, h, f) {
450
450
  n?.key && J(n?.key), y?.validationKey && J(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 = Z(F?.localStorage?.key) ? F?.localStorage?.key(S) : F?.localStorage?.key, G = `${f}-${t}-${U}`;
454
+ G && localStorage.removeItem(G), gt(t, W), K(t, S);
455
455
  const m = o.getState().stateComponents.get(t);
456
456
  return m && m.components.forEach((l) => {
457
457
  l.forceUpdate();
@@ -459,14 +459,14 @@ function gt(t, i, h, f) {
459
459
  },
460
460
  updateInitialState: (y) => {
461
461
  v.clear(), k++;
462
- const n = gt(
462
+ const n = ft(
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 = Z(W?.localStorage?.key) ? W?.localStorage?.key(S) : W?.localStorage?.key, U = `${f}-${t}-${F}`;
468
+ return localStorage.getItem(U) && localStorage.removeItem(U), Ot(() => {
469
+ Tt(t, y), o.getState().initializeShadowState(t, y), gt(t, n), K(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 && H(y, bt(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,24 +523,24 @@ 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
  }
542
542
  if (l === "getDifferences")
543
- return () => It(
543
+ return () => Et(
544
544
  o.getState().cogsStateStore[t],
545
545
  o.getState().initialStateGlobal[t]
546
546
  );
@@ -551,20 +551,20 @@ 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") {
@@ -621,142 +621,148 @@ 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 = Y(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
+ }), p = vt(
629
+ (E) => o.getState().getShadowMetadata(t, [...n, E.toString()])?.virtualizer?.itemHeight || r,
630
+ [r, t, n]
631
+ ), T = Y(i), b = Y(0), N = Y(!0), M = 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 = M.length, { totalHeight: B, positions: C } = wt(() => {
635
+ let E = 0;
636
+ const x = [];
637
+ for (let _ = 0; _ < P; _++)
638
+ x[_] = E, E += p(_), console.log("height", p(_), E);
639
+ return { totalHeight: E, positions: x };
640
+ }, [P, p]), V = wt(() => {
641
+ const E = Math.max(0, u.startIndex), x = Math.min(P, u.endIndex), _ = Array.from(
642
+ { length: x - E },
643
+ (L, Q) => E + Q
644
+ ), j = _.map((L) => M[L]);
645
+ return a(j, n, {
646
646
  ...S,
647
647
  validIndices: _
648
648
  });
649
- }, [u.startIndex, u.endIndex, x, N]);
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;
665
- break;
649
+ }, [u.startIndex, u.endIndex, M, P]);
650
+ ut(() => {
651
+ const E = g.current;
652
+ if (!E) return;
653
+ const x = T.current, _ = P > b.current;
654
+ b.current = P;
655
+ const j = () => {
656
+ const { scrollTop: L, clientHeight: Q, scrollHeight: _t } = E;
657
+ T.current = _t - L - Q < 10;
658
+ let tt = ((et, Mt) => {
659
+ let ct = 0, ht = et.length - 1;
660
+ for (; ct <= ht; ) {
661
+ const yt = Math.floor((ct + ht) / 2);
662
+ et[yt] < Mt ? ct = yt + 1 : ht = yt - 1;
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
+ return ct;
665
+ })(C, L), q = tt;
666
+ for (; q < P && C[q] < L + Q; )
667
+ q++;
668
+ tt = Math.max(0, tt - s), q = Math.min(P, q + s), console.log("startIndex", tt, "endIndex", q), A((et) => et.startIndex !== tt || et.endIndex !== q ? { startIndex: tt, endIndex: q } : et);
671
669
  };
672
- return N > V && (P.current = !0), V = N, p.addEventListener("scroll", _, {
670
+ return E.addEventListener("scroll", j, {
673
671
  passive: !0
674
- }), _(), () => p.removeEventListener("scroll", _);
675
- }, [N, s, b]);
676
- const j = mt(
677
- (p = "smooth") => {
672
+ }), i && (N.current ? E.scrollTo({
673
+ top: E.scrollHeight,
674
+ behavior: "auto"
675
+ }) : x && _ && requestAnimationFrame(() => {
676
+ E.scrollTo({
677
+ top: E.scrollHeight,
678
+ behavior: "smooth"
679
+ });
680
+ })), N.current = !1, j(), () => E.removeEventListener("scroll", j);
681
+ }, [P, s, i, C]);
682
+ const R = vt(
683
+ (E = "smooth") => {
678
684
  g.current && g.current.scrollTo({
679
685
  top: g.current.scrollHeight,
680
- behavior: p
686
+ behavior: E
681
687
  });
682
688
  },
683
689
  []
684
- ), D = mt(
685
- (p, V = "smooth") => {
686
- g.current && b[p] !== void 0 && g.current.scrollTo({
687
- top: b[p],
688
- behavior: V
690
+ ), D = vt(
691
+ (E, x = "smooth") => {
692
+ g.current && g.current.scrollTo({
693
+ top: C[E] || 0,
694
+ behavior: x
689
695
  });
690
696
  },
691
- [b]
692
- ), A = {
697
+ [C]
698
+ ), $ = {
693
699
  outer: {
694
700
  ref: g,
695
701
  style: { overflowY: "auto", height: "100%" }
696
702
  },
697
703
  inner: {
698
- style: { height: `${L}px`, position: "relative" }
704
+ style: { height: `${B}px`, position: "relative" }
699
705
  },
700
706
  list: {
701
707
  style: {
702
- transform: `translateY(${b[u.startIndex] || 0}px)`
708
+ transform: `translateY(${C[u.startIndex] || 0}px)`
703
709
  }
704
710
  }
705
711
  };
706
712
  return {
707
- virtualState: C,
708
- virtualizerProps: A,
709
- scrollToBottom: j,
713
+ virtualState: V,
714
+ virtualizerProps: $,
715
+ scrollToBottom: R,
710
716
  scrollToIndex: D
711
717
  };
712
718
  };
713
719
  if (l === "stateSort")
714
720
  return (e) => {
715
721
  const s = [...d()].sort(
716
- (u, T) => e(u.item, T.item)
717
- ), c = s.map(({ item: u }) => u), g = {
722
+ (u, A) => e(u.item, A.item)
723
+ ), i = s.map(({ item: u }) => u), g = {
718
724
  ...S,
719
725
  validIndices: s.map(
720
726
  ({ originalIndex: u }) => u
721
727
  )
722
728
  };
723
- return a(c, n, g);
729
+ return a(i, n, g);
724
730
  };
725
731
  if (l === "stateFilter")
726
732
  return (e) => {
727
733
  const s = d().filter(
728
- ({ item: u }, T) => e(u, T)
729
- ), c = s.map(({ item: u }) => u), g = {
734
+ ({ item: u }, A) => e(u, A)
735
+ ), i = s.map(({ item: u }) => u), g = {
730
736
  ...S,
731
737
  validIndices: s.map(
732
738
  ({ originalIndex: u }) => u
733
739
  )
734
740
  };
735
- return a(c, n, g);
741
+ return a(i, n, g);
736
742
  };
737
743
  if (l === "stateMap")
738
744
  return (e) => {
739
745
  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, {
746
+ return Array.isArray(r) ? (S?.validIndices || Array.from({ length: r.length }, (i, g) => g)).map((i, g) => {
747
+ const u = r[i], A = [...n, i.toString()], p = a(u, A, S);
748
+ return e(u, p, {
743
749
  register: () => {
744
- const [, P] = rt({}), x = `${h}-${n.join(".")}-${c}`;
745
- dt(() => {
746
- const N = `${t}////${x}`, F = o.getState().stateComponents.get(t) || {
750
+ const [, b] = rt({}), N = `${h}-${n.join(".")}-${i}`;
751
+ ut(() => {
752
+ const M = `${t}////${N}`, P = o.getState().stateComponents.get(t) || {
747
753
  components: /* @__PURE__ */ new Map()
748
754
  };
749
- return F.components.set(N, {
750
- forceUpdate: () => P({}),
751
- paths: /* @__PURE__ */ new Set([T.join(".")])
752
- }), o.getState().stateComponents.set(t, F), () => {
753
- const L = o.getState().stateComponents.get(t);
754
- L && L.components.delete(N);
755
+ return P.components.set(M, {
756
+ forceUpdate: () => b({}),
757
+ paths: /* @__PURE__ */ new Set([A.join(".")])
758
+ }), o.getState().stateComponents.set(t, P), () => {
759
+ const B = o.getState().stateComponents.get(t);
760
+ B && B.components.delete(M);
755
761
  };
756
- }, [t, x]);
762
+ }, [t, N]);
757
763
  },
758
764
  index: g,
759
- originalIndex: c
765
+ originalIndex: i
760
766
  });
761
767
  }) : (console.warn(
762
768
  `stateMap called on a non-array value at path: ${n.join(".")}. The current value is:`,
@@ -764,20 +770,20 @@ function gt(t, i, h, f) {
764
770
  ), null);
765
771
  };
766
772
  if (l === "stateMapNoRender")
767
- return (e) => y.map((s, c) => {
773
+ return (e) => y.map((s, i) => {
768
774
  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);
775
+ S?.validIndices && S.validIndices[i] !== void 0 ? g = S.validIndices[i] : g = i;
776
+ const u = [...n, g.toString()], A = a(s, u, S);
771
777
  return e(
772
778
  s,
773
- T,
774
- c,
779
+ A,
780
+ i,
775
781
  y,
776
782
  a(y, n, S)
777
783
  );
778
784
  });
779
785
  if (l === "$stateMap")
780
- return (e) => it(Bt, {
786
+ return (e) => st(Xt, {
781
787
  proxy: {
782
788
  _stateKey: t,
783
789
  _path: n,
@@ -789,16 +795,16 @@ function gt(t, i, h, f) {
789
795
  if (l === "stateList")
790
796
  return (e) => {
791
797
  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,
798
+ return Array.isArray(r) ? (S?.validIndices || Array.from({ length: r.length }, (i, g) => g)).map((i, g) => {
799
+ const u = r[i], A = [...n, i.toString()], p = a(u, A, S), T = `${h}-${n.join(".")}-${i}`;
800
+ return st(Kt, {
801
+ key: i,
796
802
  stateKey: t,
797
- itemComponentId: $,
798
- itemPath: T,
803
+ itemComponentId: T,
804
+ itemPath: A,
799
805
  children: e(
800
806
  u,
801
- E,
807
+ p,
802
808
  g,
803
809
  r,
804
810
  a(r, n, S)
@@ -813,7 +819,7 @@ function gt(t, i, h, f) {
813
819
  const r = y;
814
820
  v.clear(), k++;
815
821
  const s = r.flatMap(
816
- (c) => c[e] ?? []
822
+ (i) => i[e] ?? []
817
823
  );
818
824
  return a(
819
825
  s,
@@ -830,40 +836,40 @@ function gt(t, i, h, f) {
830
836
  return () => {
831
837
  const e = o.getState().getNestedState(t, n);
832
838
  if (e.length === 0) return;
833
- const r = e.length - 1, s = e[r], c = [...n, r.toString()];
834
- return a(s, c);
839
+ const r = e.length - 1, s = e[r], i = [...n, r.toString()];
840
+ return a(s, i);
835
841
  };
836
842
  if (l === "insert")
837
- return (e) => (w(n), ht(i, e, n, t), a(
843
+ return (e) => (w(n), It(c, e, n, t), a(
838
844
  o.getState().getNestedState(t, n),
839
845
  n
840
846
  ));
841
847
  if (l === "uniqueInsert")
842
848
  return (e, r, s) => {
843
- const c = o.getState().getNestedState(t, n), g = Z(e) ? e(c) : e;
849
+ const i = o.getState().getNestedState(t, n), g = Z(e) ? e(i) : e;
844
850
  let u = null;
845
- if (!c.some((E) => {
851
+ if (!i.some((p) => {
846
852
  if (r) {
847
- const P = r.every(
848
- (x) => H(E[x], g[x])
853
+ const b = r.every(
854
+ (N) => H(p[N], g[N])
849
855
  );
850
- return P && (u = E), P;
856
+ return b && (u = p), b;
851
857
  }
852
- const $ = H(E, g);
853
- return $ && (u = E), $;
858
+ const T = H(p, g);
859
+ return T && (u = p), T;
854
860
  }))
855
- w(n), ht(i, g, n, t);
861
+ w(n), It(c, g, n, t);
856
862
  else if (s && u) {
857
- const E = s(u), $ = c.map(
858
- (P) => H(P, u) ? E : P
863
+ const p = s(u), T = i.map(
864
+ (b) => H(b, u) ? p : b
859
865
  );
860
- w(n), at(i, $, n);
866
+ w(n), at(c, T, n);
861
867
  }
862
868
  };
863
869
  if (l === "cut")
864
870
  return (e, r) => {
865
871
  if (!r?.waitForSync)
866
- return w(n), lt(i, n, t, e), a(
872
+ return w(n), lt(c, n, t, e), a(
867
873
  o.getState().getNestedState(t, n),
868
874
  n
869
875
  );
@@ -871,12 +877,12 @@ function gt(t, i, h, f) {
871
877
  if (l === "cutByValue")
872
878
  return (e) => {
873
879
  for (let r = 0; r < y.length; r++)
874
- y[r] === e && lt(i, n, t, r);
880
+ y[r] === e && lt(c, n, t, r);
875
881
  };
876
882
  if (l === "toggleByValue")
877
883
  return (e) => {
878
884
  const r = y.findIndex((s) => s === e);
879
- r > -1 ? lt(i, n, t, r) : ht(i, e, n, t);
885
+ r > -1 ? lt(c, n, t, r) : It(c, e, n, t);
880
886
  };
881
887
  if (l === "stateFind")
882
888
  return (e) => {
@@ -884,17 +890,17 @@ function gt(t, i, h, f) {
884
890
  ({ item: g }, u) => e(g, u)
885
891
  );
886
892
  if (!s) return;
887
- const c = [...n, s.originalIndex.toString()];
888
- return a(s.item, c, S);
893
+ const i = [...n, s.originalIndex.toString()];
894
+ return a(s.item, i, S);
889
895
  };
890
896
  if (l === "findWith")
891
897
  return (e, r) => {
892
- const c = d().find(
898
+ const i = d().find(
893
899
  ({ item: u }) => u[e] === r
894
900
  );
895
- if (!c) return;
896
- const g = [...n, c.originalIndex.toString()];
897
- return a(c.item, g, S);
901
+ if (!i) return;
902
+ const g = [...n, i.originalIndex.toString()];
903
+ return a(i.item, g, S);
898
904
  };
899
905
  }
900
906
  const X = n[n.length - 1];
@@ -902,7 +908,7 @@ function gt(t, i, h, f) {
902
908
  const d = n.slice(0, -1), e = o.getState().getNestedState(t, d);
903
909
  if (Array.isArray(e) && l === "cut")
904
910
  return () => lt(
905
- i,
911
+ c,
906
912
  d,
907
913
  t,
908
914
  Number(X)
@@ -917,13 +923,13 @@ function gt(t, i, h, f) {
917
923
  return o.getState().getNestedState(t, n);
918
924
  };
919
925
  if (l === "$derive")
920
- return (d) => kt({
926
+ return (d) => Nt({
921
927
  _stateKey: t,
922
928
  _path: n,
923
929
  _effect: d.toString()
924
930
  });
925
931
  if (l === "$get")
926
- return () => kt({
932
+ return () => Nt({
927
933
  _stateKey: t,
928
934
  _path: n
929
935
  });
@@ -932,7 +938,7 @@ function gt(t, i, h, f) {
932
938
  return o.getState().getSyncInfo(d);
933
939
  }
934
940
  if (l == "getLocalStorage")
935
- return (d) => ft(f + "-" + t + "-" + d);
941
+ return (d) => St(f + "-" + t + "-" + d);
936
942
  if (l === "_selected") {
937
943
  const d = n.slice(0, -1), e = d.join("."), r = o.getState().getNestedState(t, d);
938
944
  return Array.isArray(r) ? Number(n[n.length - 1]) === o.getState().getSelectedIndex(t, e) : void 0;
@@ -941,8 +947,8 @@ function gt(t, i, h, f) {
941
947
  return (d) => {
942
948
  const e = n.slice(0, -1), r = Number(n[n.length - 1]), s = e.join(".");
943
949
  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);
950
+ const i = o.getState().getNestedState(t, [...e]);
951
+ at(c, i, e), w(e);
946
952
  };
947
953
  if (l === "toggleSelected")
948
954
  return () => {
@@ -952,74 +958,74 @@ function gt(t, i, h, f) {
952
958
  r,
953
959
  s === e ? void 0 : e
954
960
  );
955
- const c = o.getState().getNestedState(t, [...d]);
956
- at(i, c, d), w(d);
961
+ const i = o.getState().getNestedState(t, [...d]);
962
+ at(c, i, d), w(d);
957
963
  };
958
964
  if (n.length == 0) {
959
965
  if (l === "applyJsonPatch")
960
966
  return (d) => {
961
- const e = o.getState().cogsStateStore[t], s = Rt(e, d).newDocument;
962
- Vt(
967
+ const e = o.getState().cogsStateStore[t], s = Gt(e, d).newDocument;
968
+ Pt(
963
969
  t,
964
970
  o.getState().initialStateGlobal[t],
965
971
  s,
966
- i,
972
+ c,
967
973
  h,
968
974
  f
969
975
  );
970
- const c = o.getState().stateComponents.get(t);
971
- if (c) {
972
- const g = It(e, s), u = new Set(g);
976
+ const i = o.getState().stateComponents.get(t);
977
+ if (i) {
978
+ const g = Et(e, s), u = new Set(g);
973
979
  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();
980
+ A,
981
+ p
982
+ ] of i.components.entries()) {
983
+ let T = !1;
984
+ const b = Array.isArray(p.reactiveType) ? p.reactiveType : [p.reactiveType || "component"];
985
+ if (!b.includes("none")) {
986
+ if (b.includes("all")) {
987
+ p.forceUpdate();
982
988
  continue;
983
989
  }
984
- if (P.includes("component") && (E.paths.has("") && ($ = !0), !$))
985
- for (const x of u) {
986
- if (E.paths.has(x)) {
987
- $ = !0;
990
+ if (b.includes("component") && (p.paths.has("") && (T = !0), !T))
991
+ for (const N of u) {
992
+ if (p.paths.has(N)) {
993
+ T = !0;
988
994
  break;
989
995
  }
990
- let N = x.lastIndexOf(".");
991
- for (; N !== -1; ) {
992
- const F = x.substring(0, N);
993
- if (E.paths.has(F)) {
994
- $ = !0;
996
+ let M = N.lastIndexOf(".");
997
+ for (; M !== -1; ) {
998
+ const P = N.substring(0, M);
999
+ if (p.paths.has(P)) {
1000
+ T = !0;
995
1001
  break;
996
1002
  }
997
- const L = x.substring(
998
- N + 1
1003
+ const B = N.substring(
1004
+ M + 1
999
1005
  );
1000
- if (!isNaN(Number(L))) {
1001
- const b = F.lastIndexOf(".");
1002
- if (b !== -1) {
1003
- const C = F.substring(
1006
+ if (!isNaN(Number(B))) {
1007
+ const C = P.lastIndexOf(".");
1008
+ if (C !== -1) {
1009
+ const V = P.substring(
1004
1010
  0,
1005
- b
1011
+ C
1006
1012
  );
1007
- if (E.paths.has(C)) {
1008
- $ = !0;
1013
+ if (p.paths.has(V)) {
1014
+ T = !0;
1009
1015
  break;
1010
1016
  }
1011
1017
  }
1012
1018
  }
1013
- N = F.lastIndexOf(".");
1019
+ M = P.lastIndexOf(".");
1014
1020
  }
1015
- if ($) break;
1021
+ if (T) break;
1016
1022
  }
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);
1023
+ if (!T && b.includes("deps") && p.depsFunction) {
1024
+ const N = p.depsFunction(s);
1025
+ let M = !1;
1026
+ typeof N == "boolean" ? N && (M = !0) : H(p.deps, N) || (p.deps = N, M = !0), M && (T = !0);
1021
1027
  }
1022
- $ && E.forceUpdate();
1028
+ T && p.forceUpdate();
1023
1029
  }
1024
1030
  }
1025
1031
  }
@@ -1038,11 +1044,11 @@ function gt(t, i, h, f) {
1038
1044
  s && s.length > 0 && s.forEach(([g]) => {
1039
1045
  g && g.startsWith(d.key) && J(g);
1040
1046
  });
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);
1045
- }), St(t), !1);
1047
+ const i = d.zodSchema.safeParse(r);
1048
+ return i.success ? !0 : (i.error.errors.forEach((u) => {
1049
+ const A = u.path, p = u.message, T = [d.key, ...A].join(".");
1050
+ e(T, p);
1051
+ }), mt(t), !1);
1046
1052
  } catch (s) {
1047
1053
  return console.error("Zod schema validation failed", s), !1;
1048
1054
  }
@@ -1051,7 +1057,7 @@ function gt(t, i, h, f) {
1051
1057
  if (l === "getComponents")
1052
1058
  return () => o().stateComponents.get(t);
1053
1059
  if (l === "getAllFormRefs")
1054
- return () => Et.getState().getFormRefsByStateKey(t);
1060
+ return () => $t.getState().getFormRefsByStateKey(t);
1055
1061
  if (l === "_initialState")
1056
1062
  return o.getState().initialStateGlobal[t];
1057
1063
  if (l === "_serverState")
@@ -1064,13 +1070,13 @@ function gt(t, i, h, f) {
1064
1070
  if (l === "removeValidation") return I.removeValidation;
1065
1071
  }
1066
1072
  if (l === "getFormRef")
1067
- return () => Et.getState().getFormRef(t + "." + n.join("."));
1073
+ return () => $t.getState().getFormRef(t + "." + n.join("."));
1068
1074
  if (l === "validationWrapper")
1069
1075
  return ({
1070
1076
  children: d,
1071
1077
  hideMessage: e
1072
- }) => /* @__PURE__ */ yt(
1073
- Mt,
1078
+ }) => /* @__PURE__ */ pt(
1079
+ Ut,
1074
1080
  {
1075
1081
  formOpts: e ? { validation: { message: "" } } : void 0,
1076
1082
  path: n,
@@ -1086,23 +1092,23 @@ function gt(t, i, h, f) {
1086
1092
  if (l === "update")
1087
1093
  return (d, e) => {
1088
1094
  if (e?.debounce)
1089
- _t(() => {
1090
- at(i, d, n, "");
1095
+ Ft(() => {
1096
+ at(c, d, n, "");
1091
1097
  const r = o.getState().getNestedState(t, n);
1092
1098
  e?.afterUpdate && e.afterUpdate(r);
1093
1099
  }, e.debounce);
1094
1100
  else {
1095
- at(i, d, n, "");
1101
+ at(c, d, n, "");
1096
1102
  const r = o.getState().getNestedState(t, n);
1097
1103
  e?.afterUpdate && e.afterUpdate(r);
1098
1104
  }
1099
1105
  w(n);
1100
1106
  };
1101
1107
  if (l === "formElement")
1102
- return (d, e) => /* @__PURE__ */ yt(
1103
- Ot,
1108
+ return (d, e) => /* @__PURE__ */ pt(
1109
+ Dt,
1104
1110
  {
1105
- setState: i,
1111
+ setState: c,
1106
1112
  stateKey: t,
1107
1113
  path: n,
1108
1114
  child: d,
@@ -1112,7 +1118,7 @@ function gt(t, i, h, f) {
1112
1118
  const O = [...n, l], ot = o.getState().getNestedState(t, O);
1113
1119
  return a(ot, O, S);
1114
1120
  }
1115
- }, G = new Proxy(R, U);
1121
+ }, G = new Proxy(F, U);
1116
1122
  return v.set(W, {
1117
1123
  proxy: G,
1118
1124
  stateVersion: k
@@ -1122,27 +1128,27 @@ function gt(t, i, h, f) {
1122
1128
  o.getState().getNestedState(t, [])
1123
1129
  );
1124
1130
  }
1125
- function kt(t) {
1126
- return it(qt, { proxy: t });
1131
+ function Nt(t) {
1132
+ return st(Qt, { proxy: t });
1127
1133
  }
1128
- function Bt({
1134
+ function Xt({
1129
1135
  proxy: t,
1130
- rebuildStateShape: i
1136
+ rebuildStateShape: c
1131
1137
  }) {
1132
1138
  const h = o().getNestedState(t._stateKey, t._path);
1133
- return Array.isArray(h) ? i(
1139
+ return Array.isArray(h) ? c(
1134
1140
  h,
1135
1141
  t._path
1136
1142
  ).stateMapNoRender(
1137
1143
  (v, k, w, I, a) => t._mapFn(v, k, w, I, a)
1138
1144
  ) : null;
1139
1145
  }
1140
- function qt({
1146
+ function Qt({
1141
1147
  proxy: t
1142
1148
  }) {
1143
- const i = Y(null), h = `${t._stateKey}-${t._path.join(".")}`;
1144
- return st(() => {
1145
- const f = i.current;
1149
+ const c = Y(null), h = `${t._stateKey}-${t._path.join(".")}`;
1150
+ return dt(() => {
1151
+ const f = c.current;
1146
1152
  if (!f || !f.parentElement) return;
1147
1153
  const v = f.parentElement, w = Array.from(v.childNodes).indexOf(f);
1148
1154
  let I = v.getAttribute("data-parent-id");
@@ -1162,22 +1168,22 @@ function qt({
1162
1168
  "state",
1163
1169
  `return (${t._effect})(state)`
1164
1170
  )(n);
1165
- } catch (R) {
1166
- console.error("Error evaluating effect function during mount:", R), S = n;
1171
+ } catch (F) {
1172
+ console.error("Error evaluating effect function during mount:", F), S = n;
1167
1173
  }
1168
1174
  else
1169
1175
  S = n;
1170
1176
  S !== null && typeof S == "object" && (S = JSON.stringify(S));
1171
1177
  const W = document.createTextNode(String(S));
1172
1178
  f.replaceWith(W);
1173
- }, [t._stateKey, t._path.join("."), t._effect]), it("span", {
1174
- ref: i,
1179
+ }, [t._stateKey, t._path.join("."), t._effect]), st("span", {
1180
+ ref: c,
1175
1181
  style: { display: "none" },
1176
1182
  "data-signal-id": h
1177
1183
  });
1178
1184
  }
1179
- function le(t) {
1180
- const i = bt(
1185
+ function he(t) {
1186
+ const c = jt(
1181
1187
  (h) => {
1182
1188
  const f = o.getState().stateComponents.get(t._stateKey) || {
1183
1189
  components: /* @__PURE__ */ new Map()
@@ -1189,23 +1195,23 @@ function le(t) {
1189
1195
  },
1190
1196
  () => o.getState().getNestedState(t._stateKey, t._path)
1191
1197
  );
1192
- return it("text", {}, String(i));
1198
+ return st("text", {}, String(c));
1193
1199
  }
1194
- function Jt({
1200
+ function Kt({
1195
1201
  stateKey: t,
1196
- itemComponentId: i,
1202
+ itemComponentId: c,
1197
1203
  itemPath: h,
1198
1204
  children: f
1199
1205
  }) {
1200
- const [, v] = rt({}), [k, w] = Ut();
1201
- return st(() => {
1206
+ const [, v] = rt({}), [k, w] = Lt();
1207
+ return dt(() => {
1202
1208
  w.height > 0 && o.getState().setShadowMetadata(t, h, {
1203
1209
  virtualizer: {
1204
1210
  itemHeight: w.height
1205
1211
  }
1206
1212
  });
1207
- }, [w.height]), dt(() => {
1208
- const I = `${t}////${i}`, a = o.getState().stateComponents.get(t) || {
1213
+ }, [w.height]), ut(() => {
1214
+ const I = `${t}////${c}`, a = o.getState().stateComponents.get(t) || {
1209
1215
  components: /* @__PURE__ */ new Map()
1210
1216
  };
1211
1217
  return a.components.set(I, {
@@ -1215,14 +1221,14 @@ function Jt({
1215
1221
  const y = o.getState().stateComponents.get(t);
1216
1222
  y && y.components.delete(I);
1217
1223
  };
1218
- }, [t, i, h.join(".")]), /* @__PURE__ */ yt("div", { ref: k, children: f });
1224
+ }, [t, c, h.join(".")]), /* @__PURE__ */ pt("div", { ref: k, children: f });
1219
1225
  }
1220
1226
  export {
1221
- kt as $cogsSignal,
1222
- le as $cogsSignalStore,
1223
- se as addStateOptions,
1224
- ie as createCogsState,
1225
- ce as notifyComponent,
1226
- zt as useCogsStateFn
1227
+ Nt as $cogsSignal,
1228
+ he as $cogsSignalStore,
1229
+ fe as addStateOptions,
1230
+ Se as createCogsState,
1231
+ me as notifyComponent,
1232
+ Zt as useCogsStateFn
1227
1233
  };
1228
1234
  //# sourceMappingURL=CogsState.jsx.map