cogsbox-state 0.5.296 → 0.5.298

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