cogsbox-state 0.5.427 → 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,179 +1,179 @@
1
1
  "use client";
2
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";
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";
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";
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";
13
12
  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,
18
- ...i
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
19
19
  });
20
20
  }
21
- function $t({
21
+ function Ct({
22
22
  stateKey: t,
23
- options: i,
23
+ options: s,
24
24
  initialOptionsPart: S
25
25
  }) {
26
- const f = at(t) || {}, h = S[t] || {}, b = r.getState().setInitialStateOptions, p = { ...h, ...f };
26
+ const g = it(t) || {}, I = S[t] || {}, k = o.getState().setInitialStateOptions, w = { ...I, ...g };
27
27
  let y = !1;
28
- if (i)
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);
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);
33
33
  }
34
- function me(t, { formElements: i, validation: S }) {
35
- return { initialState: t, formElements: i, validation: S };
34
+ function me(t, { formElements: s, validation: S }) {
35
+ return { initialState: t, formElements: s, validation: S };
36
36
  }
37
- const he = (t, i) => {
37
+ const Ie = (t, s) => {
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 = {
42
- ...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,
43
43
  // Global defaults first
44
- ...i?.validation,
45
- ...h[y].formElements || {}
44
+ ...s?.validation,
45
+ ...I[y].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({
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({
52
52
  stateKey: y,
53
- options: s,
54
- initialOptionsPart: h
53
+ options: i,
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 v = o.getState().cogsStateStore[y] || g[y], r = i?.modifyState ? i.modifyState(v) : v, [h, M] = Kt(
57
+ r,
58
58
  {
59
59
  stateKey: y,
60
- syncUpdate: s?.syncUpdate,
61
- componentId: m,
62
- localStorage: s?.localStorage,
63
- middleware: s?.middleware,
64
- enabledSync: s?.enabledSync,
65
- reactiveType: s?.reactiveType,
66
- reactiveDeps: s?.reactiveDeps,
67
- initialState: s?.initialState,
68
- dependencies: s?.dependencies,
69
- 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
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(y, i) {
75
+ Ct({ stateKey: y, options: i, initialOptionsPart: I }), i.localStorage && Qt(y, i), et(y);
76
76
  }
77
- return { useCogsState: b, setCogsOptions: p };
77
+ return { useCogsState: k, setCogsOptions: w };
78
78
  }, {
79
- setUpdaterState: ht,
80
- setState: ot,
81
- getInitialOptions: at,
79
+ setUpdaterState: mt,
80
+ setState: rt,
81
+ getInitialOptions: it,
82
82
  getKeyState: Rt,
83
83
  getValidationErrors: Jt,
84
- setStateLog: Yt,
85
- updateInitialStateGlobal: xt,
86
- addValidationError: Mt,
84
+ setStateLog: Zt,
85
+ updateInitialStateGlobal: Tt,
86
+ addValidationError: xt,
87
87
  removeValidationError: Q,
88
- setServerSyncActions: Zt
89
- } = r.getState(), Nt = (t, i, S, f, h) => {
88
+ setServerSyncActions: Yt
89
+ } = o.getState(), pt = (t, s, S, g, I) => {
90
90
  S?.log && console.log(
91
91
  "saving to localstorage",
92
- i,
92
+ s,
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}`;
96
+ const k = X(S?.localStorage?.key) ? S.localStorage?.key(t) : S?.localStorage?.key;
97
+ if (k && g) {
98
+ const w = `${g}-${s}-${k}`;
99
99
  let y;
100
100
  try {
101
- y = It(p)?.lastSyncedWithServer;
101
+ y = vt(w)?.lastSyncedWithServer;
102
102
  } catch {
103
103
  }
104
- const s = {
104
+ const i = {
105
105
  state: t,
106
106
  lastUpdated: Date.now(),
107
- lastSyncedWithServer: h ?? y
108
- }, m = zt.serialize(s);
107
+ lastSyncedWithServer: I ?? y
108
+ }, $ = Bt.serialize(i);
109
109
  window.localStorage.setItem(
110
- p,
111
- JSON.stringify(m.json)
110
+ w,
111
+ JSON.stringify($.json)
112
112
  );
113
113
  }
114
- }, It = (t) => {
114
+ }, vt = (t) => {
115
115
  if (!t) return null;
116
116
  try {
117
- const i = window.localStorage.getItem(t);
118
- return i ? JSON.parse(i) : null;
119
- } catch (i) {
120
- 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;
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
+ }, 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}`
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 rt(t, k.state), et(t), !0;
130
130
  }
131
131
  return !1;
132
- }, Ot = (t, i, S, f, h, b) => {
133
- const p = {
134
- initialState: i,
135
- updaterState: yt(
132
+ }, jt = (t, s, S, g, I, k) => {
133
+ const w = {
134
+ initialState: s,
135
+ updaterState: It(
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);
146
- 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;
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
+ s.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
+ }, ve = (t, s) => {
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}////${s}`, 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
+ }, Xt = (t, s, 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(s, 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(s, g),
177
177
  newValue: null
178
178
  // or undefined
179
179
  };
@@ -182,326 +182,332 @@ const he = (t, i) => {
182
182
  }
183
183
  };
184
184
  function Kt(t, {
185
- stateKey: i,
185
+ stateKey: s,
186
186
  serverSync: S,
187
- localStorage: f,
188
- formElements: h,
189
- reactiveDeps: b,
190
- reactiveType: p,
187
+ localStorage: g,
188
+ formElements: I,
189
+ reactiveDeps: k,
190
+ reactiveType: w,
191
191
  componentId: y,
192
- initialState: s,
193
- syncUpdate: m,
194
- dependencies: n,
195
- serverState: u
192
+ initialState: i,
193
+ syncUpdate: $,
194
+ dependencies: v,
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] = 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(
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 = it(f) ?? null, nt(() => {
203
+ if ($ && $.stateKey === f && $.path?.[0]) {
204
+ rt(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(() => {
215
- if (s) {
216
- Vt(g, {
217
- initialState: s
214
+ }, [$]), nt(() => {
215
+ if (i) {
216
+ Nt(f, {
217
+ initialState: i
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, d = o.getState().initialStateGlobal[f];
220
+ if (!(d && !Z(d, i) || !d) && !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,
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({});
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({});
235
235
  }
236
236
  }, [
237
- s,
238
- u?.status,
239
- u?.data,
240
- ...n || []
241
- ]), Ct(() => {
242
- z && Vt(g, {
237
+ i,
238
+ r?.status,
239
+ r?.data,
240
+ ...v || []
241
+ ]), Pt(() => {
242
+ Y && Nt(f, {
243
243
  serverSync: S,
244
- formElements: h,
245
- initialState: s,
246
- localStorage: f,
247
- middleware: j.current?.middleware
244
+ formElements: I,
245
+ initialState: i,
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}////${K.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 ot = (n, e, a, d) => {
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 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);
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 (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}"]`
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 (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);
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" && (d || P.current?.validation?.key) && e && Q(
297
+ (d || 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 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);
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 = 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(".") || "";
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
+ p,
317
+ A
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")) {
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 (q.includes("component") && ((A.paths.has(E) || A.paths.has("")) && (C = !0), !C))
327
+ for (const W of T) {
328
+ let O = W;
328
329
  for (; ; ) {
329
- if (T.paths.has(W)) {
330
- U = !0;
330
+ if (A.paths.has(O)) {
331
+ C = !0;
331
332
  break;
332
333
  }
333
- const x = W.lastIndexOf(".");
334
- if (x !== -1) {
335
- const _ = W.substring(
334
+ const lt = O.lastIndexOf(".");
335
+ if (lt !== -1) {
336
+ const V = O.substring(
336
337
  0,
337
- x
338
+ lt
338
339
  );
339
340
  if (!isNaN(
340
- Number(W.substring(x + 1))
341
- ) && T.paths.has(_)) {
342
- U = !0;
341
+ Number(O.substring(lt + 1))
342
+ ) && A.paths.has(V)) {
343
+ C = !0;
343
344
  break;
344
345
  }
345
- W = _;
346
+ O = V;
346
347
  } else
347
- W = "";
348
- if (W === "")
348
+ O = "";
349
+ if (O === "")
349
350
  break;
350
351
  }
351
- if (U) break;
352
+ if (C) 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 (!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);
357
358
  }
358
- U && T.forceUpdate();
359
+ C && 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 B = Date.now();
364
+ let { oldValue: D, newValue: H } = Xt(
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 at = {
371
+ timeStamp: B,
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 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);
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, u.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 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);
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 (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),
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] || (mt(
426
+ f,
427
+ It(
428
+ f,
429
+ ot,
430
+ K.current,
431
+ _
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] || 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];
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++;
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++;
445
450
  }, y = {
446
- removeValidation: (m) => {
447
- m?.validationKey && Q(m.validationKey);
451
+ removeValidation: (v) => {
452
+ v?.validationKey && Q(v.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: (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) => {
458
463
  c.forceUpdate();
459
- }), u;
464
+ }), h;
460
465
  },
461
- updateInitialState: (m) => {
462
- h.clear(), b++;
463
- const n = yt(
466
+ updateInitialState: (v) => {
467
+ I.clear(), k++;
468
+ const r = It(
464
469
  t,
465
- i,
470
+ s,
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 = 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();
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 v = o.getState().serverState[t];
489
+ return !!(v && Z(v, Rt(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 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);
492
501
  };
493
- Object.keys(y).forEach((g) => {
494
- R[g] = y[g];
502
+ Object.keys(y).forEach((G) => {
503
+ _[G] = y[G];
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 Y = {
506
+ apply(G, c, K) {
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(G, c) {
510
+ const K = /* @__PURE__ */ new Set([
505
511
  "insert",
506
512
  "cut",
507
513
  "cutByValue",
@@ -524,740 +530,669 @@ 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" && !K.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(".");
533
- let d = !0;
534
- for (const v of o.paths)
535
- if (a.startsWith(v) && (a === v || a[v.length] === ".")) {
536
- 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;
537
543
  break;
538
544
  }
539
- d && o.paths.add(a);
545
+ u && a.paths.add(d);
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 () => Mt(
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, []), d = 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;
567
- } catch (d) {
568
- 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 };
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 Z(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 Z(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 = it(t), a = X(e?.localStorage?.key) ? e.localStorage.key(n) : e?.localStorage?.key, d = `${g}-${t}-${a}`;
585
+ d && localStorage.removeItem(d);
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(v)) {
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], d = `id:${a.id}`;
602
+ return $(a, [...r, d], 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 = (i(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
623
  stickToBottom: d = !1,
625
- dependencies: v = []
626
- } = e, I = J(null), [E, A] = st({
624
+ dependencies: u = []
625
+ } = n, l = z(null), [m, x] = ct({
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
+ }), [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);
632
631
  }), [t]);
633
- const C = r().getNestedState(
632
+ const T = 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 = T.length, { totalHeight: p, positions: A } = Et(() => {
636
+ let V = 0;
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;
644
646
  }
645
- return { totalHeight: N, positions: q };
647
+ return { totalHeight: V, positions: j };
646
648
  }, [
647
- w,
649
+ E,
648
650
  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
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
660
660
  });
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({
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({
669
675
  behavior: "auto",
670
- block: "end",
671
- inline: "nearest"
676
+ block: "end"
672
677
  }), !0;
673
678
  }
674
679
  }
675
680
  return !1;
676
- }, [t, n, w]);
677
- 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);
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);
707
695
  break;
708
696
  }
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
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
716
704
  );
717
- (vt !== K.current.startIndex || wt !== K.current.endIndex) && (K.current = {
718
- startIndex: vt,
705
+ (ht !== at.current.startIndex || wt !== at.current.endIndex) && (at.current = {
706
+ startIndex: ht,
719
707
  endIndex: wt
720
- }, A({
721
- startIndex: vt,
708
+ }, x({
709
+ startIndex: ht,
722
710
  endIndex: wt
723
711
  }));
724
712
  };
725
- if (x.addEventListener("scroll", _, {
713
+ return V.addEventListener("scroll", j, {
726
714
  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;
747
- }
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
- });
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 });
756
725
  },
757
- [V, t, n, w]
758
- // Add totalCount to the dependencies
759
- ), W = {
726
+ [A]
727
+ ), lt = {
760
728
  outer: {
761
- ref: I,
729
+ ref: l,
762
730
  style: { overflowY: "auto", height: "100%" }
763
731
  },
764
732
  inner: {
765
733
  style: {
766
- height: `${D}px`,
734
+ height: `${p}px`,
767
735
  position: "relative"
768
736
  }
769
737
  },
770
738
  list: {
771
739
  style: {
772
- transform: `translateY(${V[E.startIndex] || 0}px)`
740
+ transform: `translateY(${A[m.startIndex] || 0}px)`
773
741
  }
774
742
  }
775
743
  };
776
744
  return {
777
- virtualState: T,
778
- virtualizerProps: W,
779
- scrollToBottom: F,
780
- scrollToIndex: B
745
+ virtualState: C,
746
+ virtualizerProps: lt,
747
+ scrollToBottom: W,
748
+ scrollToIndex: O
781
749
  };
782
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
+ };
783
765
  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
- });
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
+ };
796
779
  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
780
+ return (n) => ut(te, {
781
+ proxy: { _stateKey: t, _path: r, _mapFn: n },
782
+ rebuildStateShape: $
805
783
  });
806
784
  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}`;
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}`;
811
795
  return ut(ne, {
812
- key: v,
796
+ key: x,
813
797
  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)
798
+ itemComponentId: D,
799
+ itemPath: R,
800
+ children: n(
801
+ l,
802
+ B,
803
+ { localIndex: m, originalIndex: b },
804
+ e,
805
+ u
823
806
  )
824
807
  });
825
- }) : (console.warn(
826
- `stateList called on a non-array value at path: ${n.join(".")}.`
827
- ), null);
808
+ });
828
809
  };
829
810
  if (c === "stateFlattenOn")
830
- return (e) => {
831
- const o = m;
832
- h.clear(), b++;
833
- const a = o.flatMap(
834
- (d) => d[e] ?? []
811
+ return (n) => {
812
+ const e = v;
813
+ I.clear(), k++;
814
+ const a = e.flatMap(
815
+ (d) => d[n] ?? []
835
816
  );
836
- return s(
817
+ return $(
837
818
  a,
838
- [...n, "[*]", e],
839
- u
819
+ [...r, "[*]", n],
820
+ h
840
821
  );
841
822
  };
842
823
  if (c === "index")
843
- return (e) => {
844
- const o = m[e];
845
- 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);
846
835
  };
847
836
  if (c === "last")
848
837
  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);
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);
853
842
  };
854
843
  if (c === "insert")
855
- return (e) => (p(n), pt(i, e, n, t), s(
856
- r.getState().getNestedState(t, n),
857
- n
844
+ return (n) => (w(r), $t(s, n, r, t), $(
845
+ o.getState().getNestedState(t, r),
846
+ r
858
847
  ));
859
848
  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;
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;
872
857
  }))
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
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
877
862
  );
878
- p(n), dt(i, k, n);
863
+ w(r), dt(s, b, r);
879
864
  }
880
865
  };
881
866
  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
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
887
872
  );
888
873
  };
889
874
  if (c === "cutByValue")
890
- return (e) => {
891
- for (let o = 0; o < m.length; o++)
892
- m[o] === e && St(i, n, t, o);
875
+ return (n) => {
876
+ const e = v.findIndex((a) => a === n);
877
+ e > -1 && ft(s, r, t, e);
893
878
  };
894
879
  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);
880
+ return (n) => {
881
+ const e = v.findIndex((a) => a === n);
882
+ e > -1 ? ft(s, r, t, e) : $t(s, n, r, t);
898
883
  };
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);
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
+ });
907
907
  };
908
908
  if (c === "findWith")
909
- return (e, o) => {
910
- const d = l().find(
911
- ({ item: I }) => I[e] === o
909
+ return (n, e) => {
910
+ const a = v.find(
911
+ (l) => l[n] === e
912
912
  );
913
- if (!d) return;
914
- const v = [...n, d.originalIndex.toString()];
915
- return s(d.item, v, u);
913
+ if (!a) return;
914
+ const d = `id:${a.id}`, u = [...r, d];
915
+ return $(a, u, h);
916
916
  };
917
917
  }
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);
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);
921
921
  if (Array.isArray(e) && c === "cut")
922
- return () => St(
923
- i,
924
- l,
922
+ return () => ft(
923
+ s,
924
+ n,
925
925
  t,
926
- Number(nt)
926
+ Number(P)
927
927
  );
928
928
  }
929
929
  if (c === "get")
930
930
  return () => {
931
- if (u?.validIndices && Array.isArray(m)) {
932
- const l = r.getState().getNestedState(t, n);
933
- 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);
934
938
  }
935
- return r.getState().getNestedState(t, n);
939
+ return o.getState().getNestedState(t, r);
936
940
  };
937
941
  if (c === "$derive")
938
- return (l) => Pt({
942
+ return (n) => _t({
939
943
  _stateKey: t,
940
- _path: n,
941
- _effect: l.toString()
944
+ _path: r,
945
+ _effect: n.toString()
942
946
  });
943
947
  if (c === "$get")
944
- return () => Pt({
945
- _stateKey: t,
946
- _path: n
947
- });
948
+ return () => _t({ _stateKey: t, _path: r });
948
949
  if (c === "lastSynced") {
949
- const l = `${t}:${n.join(".")}`;
950
- return r.getState().getSyncInfo(l);
950
+ const n = `${t}:${r.join(".")}`;
951
+ return o.getState().getSyncInfo(n);
951
952
  }
952
953
  if (c == "getLocalStorage")
953
- return (l) => It(f + "-" + t + "-" + l);
954
+ return (n) => vt(g + "-" + t + "-" + n);
954
955
  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;
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;
957
964
  }
958
965
  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);
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);
964
977
  };
965
978
  if (c === "toggleSelected")
966
979
  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(
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(
969
984
  t,
970
- o,
971
- a === e ? void 0 : e
985
+ u,
986
+ l === d ? void 0 : d
972
987
  );
973
- const d = r.getState().getNestedState(t, [...l]);
974
- dt(i, d, l), p(l);
988
+ const m = o.getState().getNestedState(t, [...n]);
989
+ dt(s, m, n), w(n);
975
990
  };
976
- if (n.length == 0) {
991
+ if (r.length == 0) {
977
992
  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);
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);
986
1000
  };
987
1001
  if (c === "applyJsonPatch")
988
- return (l) => {
989
- const e = r.getState().cogsStateStore[t], a = Bt(e, l).newDocument;
990
- Ot(
1002
+ return (n) => {
1003
+ const e = o.getState().cogsStateStore[t], a = Ht(e, n).newDocument;
1004
+ jt(
991
1005
  t,
992
- r.getState().initialStateGlobal[t],
1006
+ o.getState().initialStateGlobal[t],
993
1007
  a,
994
- i,
1008
+ s,
995
1009
  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
- }
1010
+ g
1011
+ ), et(t);
1054
1012
  };
1055
1013
  if (c === "validateZodSchema")
1056
1014
  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
- }
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);
1077
1024
  };
1078
1025
  if (c === "_componentId") return S;
1079
1026
  if (c === "getComponents")
1080
- return () => r().stateComponents.get(t);
1027
+ return () => o().stateComponents.get(t);
1081
1028
  if (c === "getAllFormRefs")
1082
- return () => bt.getState().getFormRefsByStateKey(t);
1029
+ return () => Vt.getState().getFormRefsByStateKey(t);
1083
1030
  if (c === "_initialState")
1084
- return r.getState().initialStateGlobal[t];
1031
+ return o.getState().initialStateGlobal[t];
1085
1032
  if (c === "_serverState")
1086
- return r.getState().serverState[t];
1033
+ return o.getState().serverState[t];
1087
1034
  if (c === "_isLoading")
1088
- return r.getState().isLoadingGlobal[t];
1035
+ return o.getState().isLoadingGlobal[t];
1089
1036
  if (c === "revertToInitialState")
1090
1037
  return y.revertToInitialState;
1091
1038
  if (c === "updateInitialState") return y.updateInitialState;
1092
1039
  if (c === "removeValidation") return y.removeValidation;
1093
1040
  }
1094
1041
  if (c === "getFormRef")
1095
- return () => bt.getState().getFormRef(t + "." + n.join("."));
1042
+ return () => Vt.getState().getFormRef(t + "." + r.join("."));
1096
1043
  if (c === "validationWrapper")
1097
1044
  return ({
1098
- children: l,
1045
+ children: n,
1099
1046
  hideMessage: e
1100
1047
  }) => /* @__PURE__ */ At(
1101
1048
  Wt,
1102
1049
  {
1103
1050
  formOpts: e ? { validation: { message: "" } } : void 0,
1104
- path: n,
1051
+ path: r,
1105
1052
  stateKey: t,
1106
- validIndices: u?.validIndices,
1107
- children: l
1053
+ children: n
1108
1054
  }
1109
1055
  );
1110
1056
  if (c === "_stateKey") return t;
1111
- if (c === "_path") return n;
1057
+ if (c === "_path") return r;
1112
1058
  if (c === "_isServerSynced") return y._isServerSynced;
1113
1059
  if (c === "update")
1114
- return (l, e) => {
1060
+ return (n, e) => {
1115
1061
  if (e?.debounce)
1116
1062
  Gt(() => {
1117
- dt(i, l, n, "");
1118
- const o = r.getState().getNestedState(t, n);
1119
- e?.afterUpdate && e.afterUpdate(o);
1063
+ dt(s, n, r, "");
1064
+ const a = o.getState().getNestedState(t, r);
1065
+ e?.afterUpdate && e.afterUpdate(a);
1120
1066
  }, e.debounce);
1121
1067
  else {
1122
- dt(i, l, n, "");
1123
- const o = r.getState().getNestedState(t, n);
1124
- e?.afterUpdate && e.afterUpdate(o);
1068
+ dt(s, n, r, "");
1069
+ const a = o.getState().getNestedState(t, r);
1070
+ e?.afterUpdate && e.afterUpdate(a);
1125
1071
  }
1126
- p(n);
1072
+ w(r);
1127
1073
  };
1128
1074
  if (c === "formElement")
1129
- return (l, e) => /* @__PURE__ */ At(
1130
- Ht,
1075
+ return (n, e) => /* @__PURE__ */ At(
1076
+ zt,
1131
1077
  {
1132
- setState: i,
1078
+ setState: s,
1133
1079
  stateKey: t,
1134
- path: n,
1135
- child: l,
1080
+ path: r,
1081
+ child: n,
1136
1082
  formOpts: e
1137
1083
  }
1138
1084
  );
1139
- const j = [...n, c], it = r.getState().getNestedState(t, j);
1140
- return s(it, j, u);
1085
+ const ot = [...r, c], yt = o.getState().getNestedState(t, ot);
1086
+ return $(yt, ot, h);
1141
1087
  }
1142
- }, z = new Proxy(R, L);
1143
- return h.set(G, {
1144
- proxy: z,
1145
- stateVersion: b
1146
- }), z;
1088
+ }, f = new Proxy(_, Y);
1089
+ return I.set(M, {
1090
+ proxy: f,
1091
+ stateVersion: k
1092
+ }), f;
1147
1093
  }
1148
- return s(
1149
- r.getState().getNestedState(t, [])
1094
+ return $(
1095
+ o.getState().getNestedState(t, [])
1150
1096
  );
1151
1097
  }
1152
- function Pt(t) {
1098
+ function _t(t) {
1153
1099
  return ut(ee, { proxy: t });
1154
1100
  }
1155
1101
  function te({
1156
1102
  proxy: t,
1157
- rebuildStateShape: i
1103
+ rebuildStateShape: s
1158
1104
  }) {
1159
- const S = r().getNestedState(t._stateKey, t._path);
1160
- return Array.isArray(S) ? i(
1105
+ const S = o().getNestedState(t._stateKey, t._path);
1106
+ return Array.isArray(S) ? s(
1161
1107
  S,
1162
1108
  t._path
1163
1109
  ).stateMapNoRender(
1164
- (h, b, p, y, s) => t._mapFn(h, b, p, y, s)
1110
+ (I, k, w, y, i) => t._mapFn(I, k, w, y, i)
1165
1111
  ) : null;
1166
1112
  }
1167
1113
  function ee({
1168
1114
  proxy: t
1169
1115
  }) {
1170
- const i = J(null), S = `${t._stateKey}-${t._path.join(".")}`;
1171
- 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 = {
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 $ = {
1178
1124
  instanceId: `instance-${crypto.randomUUID()}`,
1179
1125
  parentId: y,
1180
- position: p,
1126
+ position: w,
1181
1127
  effect: t._effect
1182
1128
  };
1183
- r.getState().addSignalElement(S, m);
1184
- const n = r.getState().getNestedState(t._stateKey, t._path);
1185
- let u;
1129
+ o.getState().addSignalElement(S, $);
1130
+ const v = o.getState().getNestedState(t._stateKey, t._path);
1131
+ let r = v;
1186
1132
  if (t._effect)
1187
1133
  try {
1188
- u = new Function(
1134
+ r = new Function(
1189
1135
  "state",
1190
1136
  `return (${t._effect})(state)`
1191
- )(n);
1192
- } catch (R) {
1193
- console.error("Error evaluating effect function during mount:", R), u = n;
1137
+ )(v);
1138
+ } catch (M) {
1139
+ console.error("Error evaluating effect function:", M);
1194
1140
  }
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);
1141
+ r !== null && typeof r == "object" && (r = JSON.stringify(r));
1142
+ const h = document.createTextNode(String(r));
1143
+ g.replaceWith(h);
1200
1144
  }, [t._stateKey, t._path.join("."), t._effect]), ut("span", {
1201
- ref: i,
1145
+ ref: s,
1202
1146
  style: { display: "none" },
1203
1147
  "data-signal-id": S
1204
1148
  });
1205
1149
  }
1206
- function Ie(t) {
1207
- const i = Ut(
1150
+ function ye(t) {
1151
+ const s = Ut(
1208
1152
  (S) => {
1209
- const f = r.getState().stateComponents.get(t._stateKey) || {
1210
- components: /* @__PURE__ */ new Map()
1211
- };
1212
- 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, {
1213
1155
  forceUpdate: S,
1214
1156
  paths: /* @__PURE__ */ new Set([t._path.join(".")])
1215
- }), () => f.components.delete(t._stateKey);
1157
+ }), o.getState().stateComponents.set(t._stateKey, g), () => g.components.delete(t._stateKey);
1216
1158
  },
1217
- () => r.getState().getNestedState(t._stateKey, t._path)
1159
+ () => o.getState().getNestedState(t._stateKey, t._path)
1218
1160
  );
1219
- return ut("text", {}, String(i));
1161
+ return ut("text", {}, String(s));
1220
1162
  }
1221
1163
  function ne({
1222
1164
  stateKey: t,
1223
- itemComponentId: i,
1165
+ itemComponentId: s,
1224
1166
  itemPath: S,
1225
- formOpts: f,
1226
- children: h
1167
+ children: g
1227
1168
  }) {
1228
- const [, b] = st({}), [p, y] = qt(), s = J(null), m = J(null), n = mt(
1229
- (u) => {
1230
- 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;
1231
1172
  },
1232
- [p]
1173
+ [k]
1233
1174
  );
1234
- 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
- }
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 }
1241
1178
  }));
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({}),
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({}),
1248
1183
  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);
1184
+ }), o.getState().stateComponents.set(t, r), () => {
1185
+ const h = o.getState().stateComponents.get(t);
1186
+ h && h.components.delete(v);
1252
1187
  };
1253
- }, [t, i, S.join(".")]), /* @__PURE__ */ At("div", { ref: n, children: h });
1188
+ }, [t, s, S.join(".")]), /* @__PURE__ */ At("div", { ref: $, children: g });
1254
1189
  }
1255
1190
  export {
1256
- Pt as $cogsSignal,
1257
- Ie as $cogsSignalStore,
1191
+ _t as $cogsSignal,
1192
+ ye as $cogsSignalStore,
1258
1193
  me as addStateOptions,
1259
- he as createCogsState,
1260
- ye as notifyComponent,
1194
+ Ie as createCogsState,
1195
+ ve as notifyComponent,
1261
1196
  Kt as useCogsStateFn
1262
1197
  };
1263
1198
  //# sourceMappingURL=CogsState.jsx.map