cogsbox-state 0.5.427 → 0.5.429

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