cogsbox-state 0.5.367 → 0.5.368

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