cogsbox-state 0.5.426 → 0.5.428

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