cogsbox-state 0.5.376 → 0.5.377

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