cogsbox-state 0.5.435 → 0.5.436

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,260 +1,260 @@
1
1
  "use client";
2
- import { jsx as re, Fragment as ke } from "react/jsx-runtime";
3
- import { memo as Re, useState as X, useRef as G, useCallback as ce, useEffect as Q, useLayoutEffect as de, useMemo as ge, createElement as le, startTransition as je } from "react";
4
- import { createRoot as Ce } from "react-dom/client";
5
- import { transformStateFunc as Fe, isFunction as ee, isArray as Te, getDifferences as Pe, isDeepEqual as se } from "./utility.js";
6
- import { ValidationWrapper as be } from "./Functions.jsx";
7
- import Oe from "superjson";
8
- import { v4 as te } from "uuid";
9
- import { getGlobalStore as t, formRefStore as pe } from "./store.js";
10
- import { useCogsConfig as _e } from "./CogsStateClient.jsx";
11
- import { useInView as Le } from "react-intersection-observer";
12
- function he(e, i) {
13
- const f = t.getState().getInitialOptions, g = t.getState().setInitialStateOptions, h = f(e) || {};
14
- g(e, {
15
- ...h,
16
- ...i
2
+ import { jsx as ot, Fragment as Ut } from "react/jsx-runtime";
3
+ import { memo as Ot, useState as K, useRef as q, useCallback as ct, useEffect as Z, useLayoutEffect as dt, useMemo as ft, createElement as lt, startTransition as Rt } from "react";
4
+ import { createRoot as At } from "react-dom/client";
5
+ import { transformStateFunc as jt, isFunction as at, isArray as Tt, getDifferences as Ct, isDeepEqual as st } from "./utility.js";
6
+ import { ValidationWrapper as kt } from "./Functions.jsx";
7
+ import Ft from "superjson";
8
+ import { v4 as rt } from "uuid";
9
+ import { getGlobalStore as e, formRefStore as vt } from "./store.js";
10
+ import { useCogsConfig as Pt } from "./CogsStateClient.jsx";
11
+ import { useInView as Lt } from "react-intersection-observer";
12
+ function mt(t, o) {
13
+ const h = e.getState().getInitialOptions, S = e.getState().setInitialStateOptions, y = h(t) || {};
14
+ S(t, {
15
+ ...y,
16
+ ...o
17
17
  });
18
18
  }
19
- function Ee({
20
- stateKey: e,
21
- options: i,
22
- initialOptionsPart: f
19
+ function Vt({
20
+ stateKey: t,
21
+ options: o,
22
+ initialOptionsPart: h
23
23
  }) {
24
- const g = ae(e) || {}, h = f[e] || {}, p = t.getState().setInitialStateOptions, M = { ...h, ...g };
25
- let l = !1;
26
- if (i)
27
- for (const u in i)
28
- M.hasOwnProperty(u) ? (u == "localStorage" && i[u] && M[u].key !== i[u]?.key && (l = !0, M[u] = i[u]), u == "defaultState" && i[u] && M[u] !== i[u] && !se(M[u], i[u]) && (l = !0, M[u] = i[u])) : (l = !0, M[u] = i[u]);
29
- l && p(e, M);
24
+ const S = nt(t) || {}, y = h[t] || {}, I = e.getState().setInitialStateOptions, V = { ...y, ...S };
25
+ let u = !1;
26
+ if (o)
27
+ for (const f in o)
28
+ V.hasOwnProperty(f) ? (f == "localStorage" && o[f] && V[f].key !== o[f]?.key && (u = !0, V[f] = o[f]), f == "defaultState" && o[f] && V[f] !== o[f] && !st(V[f], o[f]) && (u = !0, V[f] = o[f])) : (u = !0, V[f] = o[f]);
29
+ u && I(t, V);
30
30
  }
31
- function it(e, { formElements: i, validation: f }) {
32
- return { initialState: e, formElements: i, validation: f };
31
+ function se(t, { formElements: o, validation: h }) {
32
+ return { initialState: t, formElements: o, validation: h };
33
33
  }
34
- const ct = (e, i) => {
35
- let f = e;
36
- const [g, h] = Fe(f);
37
- Object.keys(g).forEach((l) => {
38
- let u = h[l] || {};
34
+ const ie = (t, o) => {
35
+ let h = t;
36
+ const [S, y] = jt(h);
37
+ Object.keys(S).forEach((u) => {
38
+ let f = y[u] || {};
39
39
  const A = {
40
- ...u
40
+ ...f
41
41
  };
42
- if (i?.formElements && (A.formElements = {
43
- ...i.formElements,
44
- ...u.formElements || {}
45
- }), i?.validation && (A.validation = {
46
- ...i.validation,
47
- ...u.validation || {}
48
- }, i.validation.key && !u.validation?.key && (A.validation.key = `${i.validation.key}.${l}`)), Object.keys(A).length > 0) {
49
- const b = ae(l);
50
- b ? t.getState().setInitialStateOptions(l, {
51
- ...b,
42
+ if (o?.formElements && (A.formElements = {
43
+ ...o.formElements,
44
+ ...f.formElements || {}
45
+ }), o?.validation && (A.validation = {
46
+ ...o.validation,
47
+ ...f.validation || {}
48
+ }, o.validation.key && !f.validation?.key && (A.validation.key = `${o.validation.key}.${u}`)), Object.keys(A).length > 0) {
49
+ const w = nt(u);
50
+ w ? e.getState().setInitialStateOptions(u, {
51
+ ...w,
52
52
  ...A
53
- }) : t.getState().setInitialStateOptions(l, A);
53
+ }) : e.getState().setInitialStateOptions(u, A);
54
54
  }
55
- }), Object.keys(g).forEach((l) => {
56
- t.getState().initializeShadowState(l, g[l]);
55
+ }), Object.keys(S).forEach((u) => {
56
+ e.getState().initializeShadowState(u, S[u]);
57
57
  });
58
- const p = (l, u) => {
59
- const [A] = X(u?.componentId ?? te());
60
- Ee({
61
- stateKey: l,
62
- options: u,
63
- initialOptionsPart: h
58
+ const I = (u, f) => {
59
+ const [A] = K(f?.componentId ?? rt());
60
+ Vt({
61
+ stateKey: u,
62
+ options: f,
63
+ initialOptionsPart: y
64
64
  });
65
- const b = t.getState().getShadowValue(l) || g[l], o = u?.modifyState ? u.modifyState(b) : b;
66
- return Be(o, {
67
- stateKey: l,
68
- syncUpdate: u?.syncUpdate,
65
+ const w = e.getState().getShadowValue(u) || S[u], i = f?.modifyState ? f.modifyState(w) : w;
66
+ return xt(i, {
67
+ stateKey: u,
68
+ syncUpdate: f?.syncUpdate,
69
69
  componentId: A,
70
- localStorage: u?.localStorage,
71
- middleware: u?.middleware,
72
- reactiveType: u?.reactiveType,
73
- reactiveDeps: u?.reactiveDeps,
74
- defaultState: u?.defaultState,
75
- dependencies: u?.dependencies,
76
- serverState: u?.serverState
70
+ localStorage: f?.localStorage,
71
+ middleware: f?.middleware,
72
+ reactiveType: f?.reactiveType,
73
+ reactiveDeps: f?.reactiveDeps,
74
+ defaultState: f?.defaultState,
75
+ dependencies: f?.dependencies,
76
+ serverState: f?.serverState
77
77
  });
78
78
  };
79
- function M(l, u) {
80
- Ee({ stateKey: l, options: u, initialOptionsPart: h }), u.localStorage && xe(l, u), ie(l);
79
+ function V(u, f) {
80
+ Vt({ stateKey: u, options: f, initialOptionsPart: y }), f.localStorage && Ht(u, f), it(u);
81
81
  }
82
- return { useCogsState: p, setCogsOptions: M };
82
+ return { useCogsState: I, setCogsOptions: V };
83
83
  }, {
84
- getInitialOptions: ae,
85
- getValidationErrors: Ne,
86
- setStateLog: We,
87
- updateInitialStateGlobal: $e,
88
- addValidationError: ve,
89
- removeValidationError: ne
90
- } = t.getState(), He = (e, i, f, g, h) => {
91
- f?.log && console.log(
84
+ getInitialOptions: nt,
85
+ getValidationErrors: ce,
86
+ setStateLog: Nt,
87
+ updateInitialStateGlobal: Dt,
88
+ addValidationError: yt,
89
+ removeValidationError: ut
90
+ } = e.getState(), Wt = (t, o, h, S, y) => {
91
+ h?.log && console.log(
92
92
  "saving to localstorage",
93
- i,
94
- f.localStorage?.key,
95
- g
93
+ o,
94
+ h.localStorage?.key,
95
+ S
96
96
  );
97
- const p = ee(f?.localStorage?.key) ? f.localStorage?.key(e) : f?.localStorage?.key;
98
- if (p && g) {
99
- const M = `${g}-${i}-${p}`;
100
- let l;
97
+ const I = at(h?.localStorage?.key) ? h.localStorage?.key(t) : h?.localStorage?.key;
98
+ if (I && S) {
99
+ const V = `${S}-${o}-${I}`;
100
+ let u;
101
101
  try {
102
- l = Se(M)?.lastSyncedWithServer;
102
+ u = gt(V)?.lastSyncedWithServer;
103
103
  } catch {
104
104
  }
105
- const u = t.getState().getShadowMetadata(i, []), A = {
106
- state: e,
105
+ const f = e.getState().getShadowMetadata(o, []), A = {
106
+ state: t,
107
107
  lastUpdated: Date.now(),
108
- lastSyncedWithServer: l,
109
- stateSource: u?.stateSource,
110
- baseServerState: u?.baseServerState
111
- }, b = Oe.serialize(A);
108
+ lastSyncedWithServer: u,
109
+ stateSource: f?.stateSource,
110
+ baseServerState: f?.baseServerState
111
+ }, w = Ft.serialize(A);
112
112
  window.localStorage.setItem(
113
- M,
114
- JSON.stringify(b.json)
113
+ V,
114
+ JSON.stringify(w.json)
115
115
  );
116
116
  }
117
- }, Se = (e) => {
118
- if (!e) return null;
117
+ }, gt = (t) => {
118
+ if (!t) return null;
119
119
  try {
120
- const i = window.localStorage.getItem(e);
121
- return i ? JSON.parse(i) : null;
122
- } catch (i) {
123
- return console.error("Error loading from localStorage:", i), null;
120
+ const o = window.localStorage.getItem(t);
121
+ return o ? JSON.parse(o) : null;
122
+ } catch (o) {
123
+ return console.error("Error loading from localStorage:", o), null;
124
124
  }
125
- }, xe = (e, i) => {
126
- const f = t.getState().getShadowValue(e), { sessionId: g } = _e(), h = ee(i?.localStorage?.key) ? i.localStorage.key(f) : i?.localStorage?.key;
127
- if (h && g) {
128
- const p = Se(
129
- `${g}-${e}-${h}`
125
+ }, Ht = (t, o) => {
126
+ const h = e.getState().getShadowValue(t), { sessionId: S } = Pt(), y = at(o?.localStorage?.key) ? o.localStorage.key(h) : o?.localStorage?.key;
127
+ if (y && S) {
128
+ const I = gt(
129
+ `${S}-${t}-${y}`
130
130
  );
131
- if (p && p.lastUpdated > (p.lastSyncedWithServer || 0))
132
- return ie(e), !0;
131
+ if (I && I.lastUpdated > (I.lastSyncedWithServer || 0))
132
+ return it(t), !0;
133
133
  }
134
134
  return !1;
135
- }, ie = (e) => {
136
- const i = t.getState().getShadowMetadata(e, []);
137
- if (!i) return;
138
- const f = /* @__PURE__ */ new Set();
139
- i?.components?.forEach((g) => {
140
- (g ? Array.isArray(g.reactiveType) ? g.reactiveType : [g.reactiveType || "component"] : null)?.includes("none") || f.add(() => g.forceUpdate());
135
+ }, it = (t) => {
136
+ const o = e.getState().getShadowMetadata(t, []);
137
+ if (!o) return;
138
+ const h = /* @__PURE__ */ new Set();
139
+ o?.components?.forEach((S) => {
140
+ (S ? Array.isArray(S.reactiveType) ? S.reactiveType : [S.reactiveType || "component"] : null)?.includes("none") || h.add(() => S.forceUpdate());
141
141
  }), queueMicrotask(() => {
142
- f.forEach((g) => g());
142
+ h.forEach((S) => S());
143
143
  });
144
- }, lt = (e, i) => {
145
- const f = t.getState().getShadowMetadata(e, []);
146
- if (f) {
147
- const g = `${e}////${i}`, h = f?.components?.get(g);
148
- if ((h ? Array.isArray(h.reactiveType) ? h.reactiveType : [h.reactiveType || "component"] : null)?.includes("none"))
144
+ }, le = (t, o) => {
145
+ const h = e.getState().getShadowMetadata(t, []);
146
+ if (h) {
147
+ const S = `${t}////${o}`, y = h?.components?.get(S);
148
+ if ((y ? Array.isArray(y.reactiveType) ? y.reactiveType : [y.reactiveType || "component"] : null)?.includes("none"))
149
149
  return;
150
- h && h.forceUpdate();
150
+ y && y.forceUpdate();
151
151
  }
152
152
  };
153
- function we(e, i, f, g) {
154
- const h = t.getState(), p = h.getShadowMetadata(e, i);
155
- if (h.setShadowMetadata(e, i, {
156
- ...p,
153
+ function wt(t, o, h, S) {
154
+ const y = e.getState(), I = y.getShadowMetadata(t, o);
155
+ if (y.setShadowMetadata(t, o, {
156
+ ...I,
157
157
  isDirty: !1,
158
158
  stateSource: "server",
159
- lastServerSync: g || Date.now()
160
- }), Array.isArray(f)) {
161
- const M = h.getShadowMetadata(e, i);
162
- M?.arrayKeys && M.arrayKeys.forEach((l, u) => {
163
- const A = l.split(".").slice(1), b = f[u];
164
- b !== void 0 && we(
165
- e,
159
+ lastServerSync: S || Date.now()
160
+ }), Array.isArray(h)) {
161
+ const V = y.getShadowMetadata(t, o);
162
+ V?.arrayKeys && V.arrayKeys.forEach((u, f) => {
163
+ const A = u.split(".").slice(1), w = h[f];
164
+ w !== void 0 && wt(
165
+ t,
166
166
  A,
167
- b,
168
- g
167
+ w,
168
+ S
169
169
  );
170
170
  });
171
- } else f && typeof f == "object" && f.constructor === Object && Object.keys(f).forEach((M) => {
172
- const l = [...i, M], u = f[M];
173
- we(e, l, u, g);
171
+ } else h && typeof h == "object" && h.constructor === Object && Object.keys(h).forEach((V) => {
172
+ const u = [...o, V], f = h[V];
173
+ wt(t, u, f, S);
174
174
  });
175
175
  }
176
- function Be(e, {
177
- stateKey: i,
178
- localStorage: f,
179
- formElements: g,
180
- reactiveDeps: h,
181
- reactiveType: p,
182
- componentId: M,
183
- defaultState: l,
184
- syncUpdate: u,
176
+ function xt(t, {
177
+ stateKey: o,
178
+ localStorage: h,
179
+ formElements: S,
180
+ reactiveDeps: y,
181
+ reactiveType: I,
182
+ componentId: V,
183
+ defaultState: u,
184
+ syncUpdate: f,
185
185
  dependencies: A,
186
- serverState: b
186
+ serverState: w
187
187
  } = {}) {
188
- const [o, S] = X({}), { sessionId: T } = _e();
189
- let W = !i;
190
- const [c] = X(i ?? te()), x = t.getState().stateLog[c], Y = G(/* @__PURE__ */ new Set()), J = G(M ?? te()), U = G(
188
+ const [i, g] = K({}), { sessionId: M } = Pt();
189
+ let z = !o;
190
+ const [c] = K(o ?? rt()), B = e.getState().stateLog[c], J = q(/* @__PURE__ */ new Set()), H = q(V ?? rt()), W = q(
191
191
  null
192
192
  );
193
- U.current = ae(c) ?? null, Q(() => {
194
- if (u && u.stateKey === c && u.path?.[0]) {
195
- const n = `${u.stateKey}:${u.path.join(".")}`;
196
- t.getState().setSyncInfo(n, {
197
- timeStamp: u.timeStamp,
198
- userId: u.userId
193
+ W.current = nt(c) ?? null, Z(() => {
194
+ if (f && f.stateKey === c && f.path?.[0]) {
195
+ const n = `${f.stateKey}:${f.path.join(".")}`;
196
+ e.getState().setSyncInfo(n, {
197
+ timeStamp: f.timeStamp,
198
+ userId: f.userId
199
199
  });
200
200
  }
201
- }, [u]);
202
- const v = ce(
201
+ }, [f]);
202
+ const m = ct(
203
203
  (n) => {
204
- const a = n ? { ...ae(c), ...n } : ae(c), d = a?.defaultState || l || e;
204
+ const a = n ? { ...nt(c), ...n } : nt(c), d = a?.defaultState || u || t;
205
205
  if (a?.serverState?.status === "success" && a?.serverState?.data !== void 0)
206
206
  return {
207
207
  value: a.serverState.data,
208
208
  source: "server",
209
209
  timestamp: a.serverState.timestamp || Date.now()
210
210
  };
211
- if (a?.localStorage?.key && T) {
212
- const m = ee(a.localStorage.key) ? a.localStorage.key(d) : a.localStorage.key, P = Se(
213
- `${T}-${c}-${m}`
211
+ if (a?.localStorage?.key && M) {
212
+ const p = at(a.localStorage.key) ? a.localStorage.key(d) : a.localStorage.key, E = gt(
213
+ `${M}-${c}-${p}`
214
214
  );
215
- if (P && P.lastUpdated > (a?.serverState?.timestamp || 0))
215
+ if (E && E.lastUpdated > (a?.serverState?.timestamp || 0))
216
216
  return {
217
- value: P.state,
217
+ value: E.state,
218
218
  source: "localStorage",
219
- timestamp: P.lastUpdated
219
+ timestamp: E.lastUpdated
220
220
  };
221
221
  }
222
222
  return {
223
- value: d || e,
223
+ value: d || t,
224
224
  source: "default",
225
225
  timestamp: Date.now()
226
226
  };
227
227
  },
228
- [c, l, e, T]
228
+ [c, u, t, M]
229
229
  );
230
- Q(() => {
231
- t.getState().setServerStateUpdate(c, b);
232
- }, [b, c]), Q(() => t.getState().subscribeToPath(c, (r) => {
230
+ Z(() => {
231
+ e.getState().setServerStateUpdate(c, w);
232
+ }, [w, c]), Z(() => e.getState().subscribeToPath(c, (r) => {
233
233
  if (r?.type === "SERVER_STATE_UPDATE") {
234
234
  const a = r.serverState;
235
235
  if (a?.status === "success" && a.data !== void 0) {
236
- he(c, { serverState: a });
237
- const w = typeof a.merge == "object" ? a.merge : a.merge === !0 ? {} : null, m = t.getState().getShadowValue(c), P = a.data;
238
- if (w && Array.isArray(m) && Array.isArray(P)) {
239
- const D = w.key || "id", j = new Set(
240
- m.map((O) => O[D])
241
- ), F = P.filter((O) => !j.has(O[D]));
242
- F.length > 0 && F.forEach((O) => {
243
- t.getState().insertShadowArrayElement(c, [], O);
244
- const L = t.getState().getShadowMetadata(c, []);
245
- if (L?.arrayKeys) {
246
- const N = L.arrayKeys[L.arrayKeys.length - 1];
247
- if (N) {
248
- const B = N.split(".").slice(1);
249
- t.getState().setShadowMetadata(c, B, {
236
+ mt(c, { serverState: a });
237
+ const l = typeof a.merge == "object" ? a.merge : a.merge === !0 ? {} : null, p = e.getState().getShadowValue(c), E = a.data;
238
+ if (l && Array.isArray(p) && Array.isArray(E)) {
239
+ const U = l.key || "id", C = new Set(
240
+ p.map((x) => x[U])
241
+ ), F = E.filter((x) => !C.has(x[U]));
242
+ F.length > 0 && F.forEach((x) => {
243
+ e.getState().insertShadowArrayElement(c, [], x);
244
+ const j = e.getState().getShadowMetadata(c, []);
245
+ if (j?.arrayKeys) {
246
+ const L = j.arrayKeys[j.arrayKeys.length - 1];
247
+ if (L) {
248
+ const b = L.split(".").slice(1);
249
+ e.getState().setShadowMetadata(c, b, {
250
250
  isDirty: !1,
251
251
  stateSource: "server",
252
252
  lastServerSync: a.timestamp || Date.now()
253
253
  });
254
- const V = t.getState().getShadowValue(N);
255
- V && typeof V == "object" && !Array.isArray(V) && Object.keys(V).forEach((E) => {
256
- const $ = [...B, E];
257
- t.getState().setShadowMetadata(c, $, {
254
+ const k = e.getState().getShadowValue(L);
255
+ k && typeof k == "object" && !Array.isArray(k) && Object.keys(k).forEach((P) => {
256
+ const O = [...b, P];
257
+ e.getState().setShadowMetadata(c, O, {
258
258
  isDirty: !1,
259
259
  stateSource: "server",
260
260
  lastServerSync: a.timestamp || Date.now()
@@ -264,401 +264,388 @@ function Be(e, {
264
264
  }
265
265
  });
266
266
  } else
267
- t.getState().initializeShadowState(c, P), we(
267
+ e.getState().initializeShadowState(c, E), wt(
268
268
  c,
269
269
  [],
270
- P,
270
+ E,
271
271
  a.timestamp
272
272
  );
273
- const R = t.getState().getShadowMetadata(c, []);
274
- t.getState().setShadowMetadata(c, [], {
275
- ...R,
273
+ const D = e.getState().getShadowMetadata(c, []);
274
+ e.getState().setShadowMetadata(c, [], {
275
+ ...D,
276
276
  stateSource: "server",
277
277
  lastServerSync: a.timestamp || Date.now(),
278
278
  isDirty: !1
279
279
  });
280
280
  }
281
281
  }
282
- }), [c, v]), Q(() => {
283
- const n = t.getState().getShadowMetadata(c, []);
282
+ }), [c, m]), Z(() => {
283
+ const n = e.getState().getShadowMetadata(c, []);
284
284
  if (n && n.stateSource)
285
285
  return;
286
- const r = ae(c);
287
- if (r?.defaultState !== void 0 || l !== void 0) {
288
- const a = r?.defaultState || l;
289
- r?.defaultState || he(c, {
286
+ const r = nt(c);
287
+ if (r?.defaultState !== void 0 || u !== void 0) {
288
+ const a = r?.defaultState || u;
289
+ r?.defaultState || mt(c, {
290
290
  defaultState: a
291
291
  });
292
- const { value: d, source: w, timestamp: m } = v();
293
- t.getState().initializeShadowState(c, d), t.getState().setShadowMetadata(c, [], {
294
- stateSource: w,
295
- lastServerSync: w === "server" ? m : void 0,
292
+ const { value: d, source: l, timestamp: p } = m();
293
+ e.getState().initializeShadowState(c, d), e.getState().setShadowMetadata(c, [], {
294
+ stateSource: l,
295
+ lastServerSync: l === "server" ? p : void 0,
296
296
  isDirty: !1,
297
- baseServerState: w === "server" ? d : void 0
298
- }), ie(c);
297
+ baseServerState: l === "server" ? d : void 0
298
+ }), it(c);
299
299
  }
300
- }, [c, ...A || []]), de(() => {
301
- W && he(c, {
302
- formElements: g,
303
- defaultState: l,
304
- localStorage: f,
305
- middleware: U.current?.middleware
300
+ }, [c, ...A || []]), dt(() => {
301
+ z && mt(c, {
302
+ formElements: S,
303
+ defaultState: u,
304
+ localStorage: h,
305
+ middleware: W.current?.middleware
306
306
  });
307
- const n = `${c}////${J.current}`, r = t.getState().getShadowMetadata(c, []), a = r?.components || /* @__PURE__ */ new Map();
307
+ const n = `${c}////${H.current}`, r = e.getState().getShadowMetadata(c, []), a = r?.components || /* @__PURE__ */ new Map();
308
308
  return a.set(n, {
309
- forceUpdate: () => S({}),
310
- reactiveType: p ?? ["component", "deps"],
309
+ forceUpdate: () => g({}),
310
+ reactiveType: I ?? ["component", "deps"],
311
311
  paths: /* @__PURE__ */ new Set(),
312
- depsFunction: h || void 0,
313
- deps: h ? h(t.getState().getShadowValue(c)) : [],
314
- prevDeps: h ? h(t.getState().getShadowValue(c)) : []
315
- }), t.getState().setShadowMetadata(c, [], {
312
+ depsFunction: y || void 0,
313
+ deps: y ? y(e.getState().getShadowValue(c)) : [],
314
+ prevDeps: y ? y(e.getState().getShadowValue(c)) : []
315
+ }), e.getState().setShadowMetadata(c, [], {
316
316
  ...r,
317
317
  components: a
318
- }), S({}), () => {
319
- const d = t.getState().getShadowMetadata(c, []), w = d?.components?.get(n);
320
- w?.paths && w.paths.forEach((m) => {
321
- const R = m.split(".").slice(1), D = t.getState().getShadowMetadata(c, R);
322
- D?.pathComponents && D.pathComponents.size === 0 && (delete D.pathComponents, t.getState().setShadowMetadata(c, R, D));
323
- }), d?.components && t.getState().setShadowMetadata(c, [], d);
318
+ }), g({}), () => {
319
+ const d = e.getState().getShadowMetadata(c, []), l = d?.components?.get(n);
320
+ l?.paths && l.paths.forEach((p) => {
321
+ const D = p.split(".").slice(1), U = e.getState().getShadowMetadata(c, D);
322
+ U?.pathComponents && U.pathComponents.size === 0 && (delete U.pathComponents, e.getState().setShadowMetadata(c, D, U));
323
+ }), d?.components && e.getState().setShadowMetadata(c, [], d);
324
324
  };
325
325
  }, []);
326
- const K = G(null), oe = (n, r, a, d) => {
327
- const w = [c, ...r].join(".");
326
+ const Y = q(null), Q = (n, r, a) => {
327
+ const d = [c, ...r].join(".");
328
328
  if (Array.isArray(r)) {
329
- const V = `${c}-${r.join(".")}`;
330
- Y.current.add(V);
329
+ const b = `${c}-${r.join(".")}`;
330
+ J.current.add(b);
331
331
  }
332
- const m = t.getState(), P = m.getShadowMetadata(c, r), R = m.getShadowValue(w), D = a.updateType === "insert" && ee(n) ? n({ state: R, uuid: te() }) : ee(n) ? n(R) : n, F = {
332
+ const l = e.getState(), p = l.getShadowMetadata(c, r), E = l.getShadowValue(d), D = a.updateType === "insert" && at(n) ? n({ state: E, uuid: rt() }) : at(n) ? n(E) : n, C = {
333
333
  timeStamp: Date.now(),
334
334
  stateKey: c,
335
335
  path: r,
336
336
  updateType: a.updateType,
337
337
  status: "new",
338
- oldValue: R,
338
+ oldValue: E,
339
339
  newValue: D
340
340
  };
341
341
  switch (a.updateType) {
342
342
  case "insert": {
343
- m.insertShadowArrayElement(c, r, F.newValue), m.markAsDirty(c, r, { bubble: !0 });
344
- const V = m.getShadowMetadata(c, r);
345
- if (V?.arrayKeys) {
346
- const E = V.arrayKeys[V.arrayKeys.length - 1];
347
- if (E) {
348
- const $ = E.split(".").slice(1);
349
- m.markAsDirty(c, $, { bubble: !1 });
343
+ l.insertShadowArrayElement(c, r, C.newValue), l.markAsDirty(c, r, { bubble: !0 });
344
+ const b = l.getShadowMetadata(c, r);
345
+ if (b?.arrayKeys) {
346
+ const k = b.arrayKeys[b.arrayKeys.length - 1];
347
+ if (k) {
348
+ const P = k.split(".").slice(1);
349
+ l.markAsDirty(c, P, { bubble: !1 });
350
350
  }
351
351
  }
352
352
  break;
353
353
  }
354
354
  case "cut": {
355
- const V = r.slice(0, -1);
356
- m.removeShadowArrayElement(c, r), m.markAsDirty(c, V, { bubble: !0 });
355
+ const b = r.slice(0, -1);
356
+ l.removeShadowArrayElement(c, r), l.markAsDirty(c, b, { bubble: !0 });
357
357
  break;
358
358
  }
359
359
  case "update": {
360
- m.updateShadowAtPath(c, r, F.newValue), m.markAsDirty(c, r, { bubble: !0 });
360
+ l.updateShadowAtPath(c, r, C.newValue), l.markAsDirty(c, r, { bubble: !0 });
361
361
  break;
362
362
  }
363
363
  }
364
- if (console.log("sdadasdasd", K.current, F), K.current && K.current.connected && K.current.updateState({ operation: F }), P?.signals && P.signals.length > 0) {
365
- const V = a.updateType === "cut" ? null : D;
366
- P.signals.forEach(({ parentId: E, position: $, effect: I }) => {
367
- const y = document.querySelector(`[data-parent-id="${E}"]`);
368
- if (y) {
369
- const k = Array.from(y.childNodes);
370
- if (k[$]) {
371
- let C = V;
372
- if (I && V !== null)
364
+ if (a.sync !== !1 && Y.current && Y.current.connected && Y.current.updateState({ operation: C }), p?.signals && p.signals.length > 0) {
365
+ const b = a.updateType === "cut" ? null : D;
366
+ p.signals.forEach(({ parentId: k, position: P, effect: O }) => {
367
+ const v = document.querySelector(`[data-parent-id="${k}"]`);
368
+ if (v) {
369
+ const T = Array.from(v.childNodes);
370
+ if (T[P]) {
371
+ let $ = b;
372
+ if (O && b !== null)
373
373
  try {
374
- C = new Function(
374
+ $ = new Function(
375
375
  "state",
376
- `return (${I})(state)`
377
- )(V);
376
+ `return (${O})(state)`
377
+ )(b);
378
378
  } catch (_) {
379
379
  console.error("Error evaluating effect function:", _);
380
380
  }
381
- C != null && typeof C == "object" && (C = JSON.stringify(C)), k[$].textContent = String(C ?? "");
381
+ $ != null && typeof $ == "object" && ($ = JSON.stringify($)), T[P].textContent = String($ ?? "");
382
382
  }
383
383
  }
384
384
  });
385
385
  }
386
- if (a.updateType === "insert" && P?.mapWrappers && P.mapWrappers.length > 0) {
387
- const V = m.getShadowMetadata(c, r)?.arrayKeys || [], E = V[V.length - 1], $ = m.getShadowValue(E), I = m.getShadowValue(
386
+ if (a.updateType === "insert" && p?.mapWrappers && p.mapWrappers.length > 0) {
387
+ const b = l.getShadowMetadata(c, r)?.arrayKeys || [], k = b[b.length - 1], P = l.getShadowValue(k), O = l.getShadowValue(
388
388
  [c, ...r].join(".")
389
389
  );
390
- if (!E || $ === void 0) return;
391
- P.mapWrappers.forEach((y) => {
392
- let k = !0, C = -1;
393
- if (y.meta?.transforms && y.meta.transforms.length > 0) {
394
- for (const _ of y.meta.transforms)
395
- if (_.type === "filter" && !_.fn($, -1)) {
396
- k = !1;
390
+ if (!k || P === void 0) return;
391
+ p.mapWrappers.forEach((v) => {
392
+ let T = !0, $ = -1;
393
+ if (v.meta?.transforms && v.meta.transforms.length > 0) {
394
+ for (const _ of v.meta.transforms)
395
+ if (_.type === "filter" && !_.fn(P, -1)) {
396
+ T = !1;
397
397
  break;
398
398
  }
399
- if (k) {
400
- const _ = Ie(
399
+ if (T) {
400
+ const _ = It(
401
401
  c,
402
402
  r,
403
- y.meta.transforms
404
- ), q = y.meta.transforms.find(
405
- (H) => H.type === "sort"
403
+ v.meta.transforms
404
+ ), R = v.meta.transforms.find(
405
+ (N) => N.type === "sort"
406
406
  );
407
- if (q) {
408
- const H = _.map((z) => ({
409
- key: z,
410
- value: m.getShadowValue(z)
407
+ if (R) {
408
+ const N = _.map((G) => ({
409
+ key: G,
410
+ value: l.getShadowValue(G)
411
411
  }));
412
- H.push({ key: E, value: $ }), H.sort((z, Z) => q.fn(z.value, Z.value)), C = H.findIndex(
413
- (z) => z.key === E
412
+ N.push({ key: k, value: P }), N.sort((G, tt) => R.fn(G.value, tt.value)), $ = N.findIndex(
413
+ (G) => G.key === k
414
414
  );
415
415
  } else
416
- C = _.length;
416
+ $ = _.length;
417
417
  }
418
418
  } else
419
- k = !0, C = V.length - 1;
420
- if (k && y.containerRef && y.containerRef.isConnected) {
419
+ T = !0, $ = b.length - 1;
420
+ if (T && v.containerRef && v.containerRef.isConnected) {
421
421
  const _ = document.createElement("div");
422
- _.setAttribute("data-item-path", E);
423
- const q = Array.from(y.containerRef.children);
424
- C >= 0 && C < q.length ? y.containerRef.insertBefore(
422
+ _.setAttribute("data-item-path", k);
423
+ const R = Array.from(v.containerRef.children);
424
+ $ >= 0 && $ < R.length ? v.containerRef.insertBefore(
425
425
  _,
426
- q[C]
427
- ) : y.containerRef.appendChild(_);
428
- const H = Ce(_), z = te(), Z = E.split(".").slice(1), ue = y.rebuildStateShape({
429
- path: y.path,
430
- currentState: I,
431
- componentId: y.componentId,
432
- meta: y.meta
426
+ R[$]
427
+ ) : v.containerRef.appendChild(_);
428
+ const N = At(_), G = rt(), tt = k.split(".").slice(1), et = v.rebuildStateShape({
429
+ path: v.path,
430
+ currentState: O,
431
+ componentId: v.componentId,
432
+ meta: v.meta
433
433
  });
434
- H.render(
435
- le(Me, {
434
+ N.render(
435
+ lt(Mt, {
436
436
  stateKey: c,
437
- itemComponentId: z,
438
- itemPath: Z,
439
- localIndex: C,
440
- arraySetter: ue,
441
- rebuildStateShape: y.rebuildStateShape,
442
- renderFn: y.mapFn
437
+ itemComponentId: G,
438
+ itemPath: tt,
439
+ localIndex: $,
440
+ arraySetter: et,
441
+ rebuildStateShape: v.rebuildStateShape,
442
+ renderFn: v.mapFn
443
443
  })
444
444
  );
445
445
  }
446
446
  });
447
447
  }
448
448
  if (a.updateType === "cut") {
449
- const V = r.slice(0, -1), E = m.getShadowMetadata(c, V);
450
- E?.mapWrappers && E.mapWrappers.length > 0 && E.mapWrappers.forEach(($) => {
451
- if ($.containerRef && $.containerRef.isConnected) {
452
- const I = $.containerRef.querySelector(
453
- `[data-item-path="${w}"]`
449
+ const b = r.slice(0, -1), k = l.getShadowMetadata(c, b);
450
+ k?.mapWrappers && k.mapWrappers.length > 0 && k.mapWrappers.forEach((P) => {
451
+ if (P.containerRef && P.containerRef.isConnected) {
452
+ const O = P.containerRef.querySelector(
453
+ `[data-item-path="${d}"]`
454
454
  );
455
- I && I.remove();
455
+ O && O.remove();
456
456
  }
457
457
  });
458
458
  }
459
- a.updateType === "update" && (d || U.current?.validation?.key) && r && ne(
460
- (d || U.current?.validation?.key) + "." + r.join(".")
461
- );
462
- const O = r.slice(0, r.length - 1);
463
- a.updateType === "cut" && U.current?.validation?.key && ne(
464
- U.current?.validation?.key + "." + O.join(".")
465
- ), a.updateType === "insert" && U.current?.validation?.key && Ne(
466
- U.current?.validation?.key + "." + O.join(".")
467
- ).filter((E) => {
468
- let $ = E?.split(".").length;
469
- const I = "";
470
- if (E == O.join(".") && $ == O.length - 1) {
471
- let y = E + "." + O;
472
- ne(E), ve(y, I);
459
+ const x = e.getState().getShadowValue(c), j = e.getState().getShadowMetadata(c, []), L = /* @__PURE__ */ new Set();
460
+ if (console.log(
461
+ "rootMeta",
462
+ c,
463
+ e.getState().shadowStateStore
464
+ ), !j?.components)
465
+ return x;
466
+ if (a.updateType === "update") {
467
+ let b = [...r];
468
+ for (; ; ) {
469
+ const k = l.getShadowMetadata(c, b);
470
+ if (k?.pathComponents && k.pathComponents.forEach((P) => {
471
+ if (L.has(P))
472
+ return;
473
+ const O = j.components?.get(P);
474
+ O && ((Array.isArray(O.reactiveType) ? O.reactiveType : [O.reactiveType || "component"]).includes("none") || (O.forceUpdate(), L.add(P)));
475
+ }), b.length === 0)
476
+ break;
477
+ b.pop();
473
478
  }
474
- });
475
- const L = m.getShadowValue(c), N = m.getShadowMetadata(c, []), B = /* @__PURE__ */ new Set();
476
- if (!N?.components)
477
- return L;
478
- if (a.updateType === "update")
479
- P?.pathComponents && P.pathComponents.forEach((V) => {
480
- if (B.has(V))
481
- return;
482
- const E = N.components?.get(V);
483
- E && ((Array.isArray(E.reactiveType) ? E.reactiveType : [E.reactiveType || "component"]).includes("none") || (E.forceUpdate(), B.add(V)));
484
- }), D && typeof D == "object" && !Te(D) && R && typeof R == "object" && !Te(R) && Pe(D, R).forEach((E) => {
485
- const $ = E.split("."), I = [...r, ...$], y = m.getShadowMetadata(c, I);
486
- y?.pathComponents && y.pathComponents.forEach((k) => {
487
- if (B.has(k))
479
+ D && typeof D == "object" && !Tt(D) && E && typeof E == "object" && !Tt(E) && Ct(D, E).forEach((P) => {
480
+ const O = P.split("."), v = [...r, ...O], T = l.getShadowMetadata(c, v);
481
+ T?.pathComponents && T.pathComponents.forEach(($) => {
482
+ if (L.has($))
488
483
  return;
489
- const C = N.components?.get(k);
490
- C && ((Array.isArray(C.reactiveType) ? C.reactiveType : [C.reactiveType || "component"]).includes("none") || (C.forceUpdate(), B.add(k)));
484
+ const _ = j.components?.get($);
485
+ _ && ((Array.isArray(_.reactiveType) ? _.reactiveType : [_.reactiveType || "component"]).includes("none") || (_.forceUpdate(), L.add($)));
491
486
  });
492
487
  });
493
- else if (a.updateType === "insert" || a.updateType === "cut") {
494
- const V = a.updateType === "insert" ? r : r.slice(0, -1), E = m.getShadowMetadata(c, V);
495
- if (E?.signals && E.signals.length > 0) {
496
- const $ = [c, ...V].join("."), I = m.getShadowValue($);
497
- E.signals.forEach(({ parentId: y, position: k, effect: C }) => {
488
+ } else if (a.updateType === "insert" || a.updateType === "cut") {
489
+ const b = a.updateType === "insert" ? r : r.slice(0, -1), k = l.getShadowMetadata(c, b);
490
+ if (k?.signals && k.signals.length > 0) {
491
+ const P = [c, ...b].join("."), O = l.getShadowValue(P);
492
+ k.signals.forEach(({ parentId: v, position: T, effect: $ }) => {
498
493
  const _ = document.querySelector(
499
- `[data-parent-id="${y}"]`
494
+ `[data-parent-id="${v}"]`
500
495
  );
501
496
  if (_) {
502
- const q = Array.from(_.childNodes);
503
- if (q[k]) {
504
- let H = I;
505
- if (C)
497
+ const R = Array.from(_.childNodes);
498
+ if (R[T]) {
499
+ let N = O;
500
+ if ($)
506
501
  try {
507
- H = new Function(
502
+ N = new Function(
508
503
  "state",
509
- `return (${C})(state)`
510
- )(I);
511
- } catch (z) {
512
- console.error("Error evaluating effect function:", z), H = I;
504
+ `return (${$})(state)`
505
+ )(O);
506
+ } catch (G) {
507
+ console.error("Error evaluating effect function:", G), N = O;
513
508
  }
514
- H != null && typeof H == "object" && (H = JSON.stringify(H)), q[k].textContent = String(H ?? "");
509
+ N != null && typeof N == "object" && (N = JSON.stringify(N)), R[T].textContent = String(N ?? "");
515
510
  }
516
511
  }
517
512
  });
518
513
  }
519
- E?.pathComponents && E.pathComponents.forEach(($) => {
520
- if (!B.has($)) {
521
- const I = N.components?.get($);
522
- I && (I.forceUpdate(), B.add($));
514
+ k?.pathComponents && k.pathComponents.forEach((P) => {
515
+ if (!L.has(P)) {
516
+ const O = j.components?.get(P);
517
+ O && (O.forceUpdate(), L.add(P));
523
518
  }
524
519
  });
525
520
  }
526
- return N.components.forEach((V, E) => {
527
- if (B.has(E))
521
+ return j.components.forEach((b, k) => {
522
+ if (L.has(k))
528
523
  return;
529
- const $ = Array.isArray(V.reactiveType) ? V.reactiveType : [V.reactiveType || "component"];
530
- if ($.includes("all")) {
531
- V.forceUpdate(), B.add(E);
524
+ const P = Array.isArray(b.reactiveType) ? b.reactiveType : [b.reactiveType || "component"];
525
+ if (P.includes("all")) {
526
+ b.forceUpdate(), L.add(k);
532
527
  return;
533
528
  }
534
- if ($.includes("deps") && V.depsFunction) {
535
- const I = m.getShadowValue(c), y = V.depsFunction(I);
536
- let k = !1;
537
- y === !0 ? k = !0 : Array.isArray(y) && (se(V.prevDeps, y) || (V.prevDeps = y, k = !0)), k && (V.forceUpdate(), B.add(E));
529
+ if (P.includes("deps") && b.depsFunction) {
530
+ const O = l.getShadowValue(c), v = b.depsFunction(O);
531
+ let T = !1;
532
+ v === !0 ? T = !0 : Array.isArray(v) && (st(b.prevDeps, v) || (b.prevDeps = v, T = !0)), T && (b.forceUpdate(), L.add(k));
538
533
  }
539
- }), B.clear(), We(c, (V) => {
540
- const E = [...V ?? [], F], $ = /* @__PURE__ */ new Map();
541
- return E.forEach((I) => {
542
- const y = `${I.stateKey}:${JSON.stringify(I.path)}`, k = $.get(y);
543
- k ? (k.timeStamp = Math.max(k.timeStamp, I.timeStamp), k.newValue = I.newValue, k.oldValue = k.oldValue ?? I.oldValue, k.updateType = I.updateType) : $.set(y, { ...I });
544
- }), Array.from($.values());
545
- }), He(
534
+ }), L.clear(), Nt(c, (b) => {
535
+ const k = [...b ?? [], C], P = /* @__PURE__ */ new Map();
536
+ return k.forEach((O) => {
537
+ const v = `${O.stateKey}:${JSON.stringify(O.path)}`, T = P.get(v);
538
+ T ? (T.timeStamp = Math.max(T.timeStamp, O.timeStamp), T.newValue = O.newValue, T.oldValue = T.oldValue ?? O.oldValue, T.updateType = O.updateType) : P.set(v, { ...O });
539
+ }), Array.from(P.values());
540
+ }), Wt(
546
541
  D,
547
542
  c,
548
- U.current,
549
- T
550
- ), U.current?.middleware && U.current.middleware({
551
- updateLog: x,
552
- update: F
553
- }), L;
543
+ W.current,
544
+ M
545
+ ), W.current?.middleware && W.current.middleware({
546
+ updateLog: B,
547
+ update: C
548
+ }), x;
554
549
  };
555
- t.getState().initialStateGlobal[c] || $e(c, e);
556
- const fe = ge(() => De(
550
+ e.getState().initialStateGlobal[c] || Dt(c, t);
551
+ const X = ft(() => _t(
557
552
  c,
558
- oe,
559
- J.current,
560
- T
561
- ), [c, T]), s = U.current?.cogsSync;
562
- return s && (K.current = s(fe)), fe;
553
+ Q,
554
+ H.current,
555
+ M
556
+ ), [c, M]), s = W.current?.cogsSync;
557
+ return s && (Y.current = s(X)), X;
563
558
  }
564
- function qe(e) {
565
- return !e || e.length === 0 ? "" : e.map(
566
- (i) => (
559
+ function zt(t) {
560
+ return !t || t.length === 0 ? "" : t.map(
561
+ (o) => (
567
562
  // Safely stringify dependencies. An empty array becomes '[]'.
568
- `${i.type}${JSON.stringify(i.dependencies || [])}`
563
+ `${o.type}${JSON.stringify(o.dependencies || [])}`
569
564
  )
570
565
  ).join("");
571
566
  }
572
- const Ie = (e, i, f) => {
573
- let g = t.getState().getShadowMetadata(e, i)?.arrayKeys || [];
574
- if (!f || f.length === 0)
575
- return g;
576
- let h = g.map((p) => ({
577
- key: p,
578
- value: t.getState().getShadowValue(p)
567
+ const It = (t, o, h) => {
568
+ let S = e.getState().getShadowMetadata(t, o)?.arrayKeys || [];
569
+ if (!h || h.length === 0)
570
+ return S;
571
+ let y = S.map((I) => ({
572
+ key: I,
573
+ value: e.getState().getShadowValue(I)
579
574
  }));
580
- for (const p of f)
581
- p.type === "filter" ? h = h.filter(
582
- ({ value: M }, l) => p.fn(M, l)
583
- ) : p.type === "sort" && h.sort((M, l) => p.fn(M.value, l.value));
584
- return h.map(({ key: p }) => p);
585
- }, Ve = (e, i, f) => {
586
- const g = `${e}////${i}`, p = t.getState().getShadowMetadata(e, [])?.components?.get(g);
587
- if (!p || p.reactiveType == "none" || !(Array.isArray(p.reactiveType) ? p.reactiveType : [p.reactiveType]).includes("component"))
588
- return;
589
- const M = [e, ...f].join(".");
590
- p.paths.add(M);
591
- const l = t.getState().getShadowMetadata(e, f) || {}, u = l.pathComponents || /* @__PURE__ */ new Set();
592
- u.add(g), t.getState().setShadowMetadata(e, f, {
593
- ...l,
594
- pathComponents: u
595
- });
596
- }, ye = (e, i, f) => {
597
- const g = t.getState(), h = g.getShadowMetadata(e, []), p = /* @__PURE__ */ new Set();
598
- h?.components && h.components.forEach((l, u) => {
599
- (Array.isArray(l.reactiveType) ? l.reactiveType : [l.reactiveType || "component"]).includes("all") && (l.forceUpdate(), p.add(u));
600
- }), g.getShadowMetadata(e, [...i, "getSelected"])?.pathComponents?.forEach((l) => {
601
- h?.components?.get(l)?.forceUpdate();
575
+ for (const I of h)
576
+ I.type === "filter" ? y = y.filter(
577
+ ({ value: V }, u) => I.fn(V, u)
578
+ ) : I.type === "sort" && y.sort((V, u) => I.fn(V.value, u.value));
579
+ return y.map(({ key: I }) => I);
580
+ }, Et = (t, o, h) => {
581
+ const S = `${t}////${o}`, { addPathComponent: y, getShadowMetadata: I } = e.getState(), u = I(t, [])?.components?.get(S);
582
+ !u || u.reactiveType === "none" || !(Array.isArray(u.reactiveType) ? u.reactiveType : [u.reactiveType]).includes("component") || y(t, h, S);
583
+ }, pt = (t, o, h) => {
584
+ const S = e.getState(), y = S.getShadowMetadata(t, []), I = /* @__PURE__ */ new Set();
585
+ y?.components && y.components.forEach((u, f) => {
586
+ (Array.isArray(u.reactiveType) ? u.reactiveType : [u.reactiveType || "component"]).includes("all") && (u.forceUpdate(), I.add(f));
587
+ }), S.getShadowMetadata(t, [...o, "getSelected"])?.pathComponents?.forEach((u) => {
588
+ y?.components?.get(u)?.forceUpdate();
602
589
  });
603
- const M = g.getShadowMetadata(e, i);
604
- for (let l of M?.arrayKeys || []) {
605
- const u = l + ".selected", A = g.getShadowMetadata(
606
- e,
607
- u.split(".").slice(1)
590
+ const V = S.getShadowMetadata(t, o);
591
+ for (let u of V?.arrayKeys || []) {
592
+ const f = u + ".selected", A = S.getShadowMetadata(
593
+ t,
594
+ f.split(".").slice(1)
608
595
  );
609
- l == f && A?.pathComponents?.forEach((b) => {
610
- h?.components?.get(b)?.forceUpdate();
596
+ u == h && A?.pathComponents?.forEach((w) => {
597
+ y?.components?.get(w)?.forceUpdate();
611
598
  });
612
599
  }
613
600
  };
614
- function De(e, i, f, g) {
615
- const h = /* @__PURE__ */ new Map();
616
- let p = 0;
617
- const M = (b) => {
618
- const o = b.join(".");
619
- for (const [S] of h)
620
- (S === o || S.startsWith(o + ".")) && h.delete(S);
621
- p++;
601
+ function _t(t, o, h, S) {
602
+ const y = /* @__PURE__ */ new Map();
603
+ let I = 0;
604
+ const V = (w) => {
605
+ const i = w.join(".");
606
+ for (const [g] of y)
607
+ (g === i || g.startsWith(i + ".")) && y.delete(g);
608
+ I++;
622
609
  };
623
- function l({
624
- currentState: b,
625
- path: o = [],
626
- meta: S,
627
- componentId: T
610
+ function u({
611
+ currentState: w,
612
+ path: i = [],
613
+ meta: g,
614
+ componentId: M
628
615
  }) {
629
- const W = o.map(String).join("."), c = [e, ...o].join(".");
630
- b = t.getState().getShadowValue(c, S?.validIds);
631
- const x = function() {
632
- return t().getShadowValue(e, o);
633
- }, Y = {
634
- apply(U, v, K) {
616
+ const z = i.map(String).join("."), c = [t, ...i].join(".");
617
+ w = e.getState().getShadowValue(c, g?.validIds);
618
+ const B = function() {
619
+ return e().getShadowValue(t, i);
620
+ }, J = {
621
+ apply(W, m, Y) {
635
622
  },
636
- get(U, v) {
637
- if (v === "_rebuildStateShape")
638
- return l;
639
- if (Object.getOwnPropertyNames(u).includes(v) && o.length === 0)
640
- return u[v];
641
- if (v === "getDifferences")
623
+ get(W, m) {
624
+ if (m === "_rebuildStateShape")
625
+ return u;
626
+ if (Object.getOwnPropertyNames(f).includes(m) && i.length === 0)
627
+ return f[m];
628
+ if (m === "getDifferences")
642
629
  return () => {
643
- const s = t.getState().getShadowMetadata(e, []), n = t.getState().getShadowValue(e);
630
+ const s = e.getState().getShadowMetadata(t, []), n = e.getState().getShadowValue(t);
644
631
  let r;
645
- return s?.stateSource === "server" && s.baseServerState ? r = s.baseServerState : r = t.getState().initialStateGlobal[e], Pe(n, r);
632
+ return s?.stateSource === "server" && s.baseServerState ? r = s.baseServerState : r = e.getState().initialStateGlobal[t], Ct(n, r);
646
633
  };
647
- if (v === "sync" && o.length === 0)
634
+ if (m === "sync" && i.length === 0)
648
635
  return async function() {
649
- const s = t.getState().getInitialOptions(e), n = s?.sync;
636
+ const s = e.getState().getInitialOptions(t), n = s?.sync;
650
637
  if (!n)
651
- return console.error(`No mutation defined for state key "${e}"`), { success: !1, error: "No mutation defined" };
652
- const r = t.getState().getShadowValue(e, []), a = s?.validation?.key;
638
+ return console.error(`No mutation defined for state key "${t}"`), { success: !1, error: "No mutation defined" };
639
+ const r = e.getState().getShadowValue(t, []), a = s?.validation?.key;
653
640
  try {
654
641
  const d = await n.action(r);
655
- if (d && !d.success && d.errors && a && (t.getState().removeValidationError(a), d.errors.forEach((w) => {
656
- const m = [a, ...w.path].join(".");
657
- t.getState().addValidationError(m, w.message);
658
- }), ie(e)), d?.success) {
659
- const w = t.getState().getShadowMetadata(e, []);
660
- t.getState().setShadowMetadata(e, [], {
661
- ...w,
642
+ if (d && !d.success && d.errors && a && (e.getState().removeValidationError(a), d.errors.forEach((l) => {
643
+ const p = [a, ...l.path].join(".");
644
+ e.getState().addValidationError(p, l.message);
645
+ }), it(t)), d?.success) {
646
+ const l = e.getState().getShadowMetadata(t, []);
647
+ e.getState().setShadowMetadata(t, [], {
648
+ ...l,
662
649
  isDirty: !1,
663
650
  lastServerSync: Date.now(),
664
651
  stateSource: "server",
@@ -671,227 +658,226 @@ function De(e, i, f, g) {
671
658
  return n.onError && n.onError(d), { success: !1, error: d };
672
659
  }
673
660
  };
674
- if (v === "_status" || v === "getStatus") {
661
+ if (m === "_status" || m === "getStatus") {
675
662
  const s = () => {
676
- const n = t.getState().getShadowMetadata(e, o), r = t.getState().getShadowValue(c);
677
- return n?.isDirty === !0 ? "dirty" : n?.isDirty === !1 || n?.stateSource === "server" ? "synced" : n?.stateSource === "localStorage" ? "restored" : n?.stateSource === "default" ? "fresh" : t.getState().getShadowMetadata(e, [])?.stateSource === "server" && !n?.isDirty ? "synced" : r !== void 0 && !n ? "fresh" : "unknown";
663
+ const n = e.getState().getShadowMetadata(t, i), r = e.getState().getShadowValue(c);
664
+ return n?.isDirty === !0 ? "dirty" : n?.isDirty === !1 || n?.stateSource === "server" ? "synced" : n?.stateSource === "localStorage" ? "restored" : n?.stateSource === "default" ? "fresh" : e.getState().getShadowMetadata(t, [])?.stateSource === "server" && !n?.isDirty ? "synced" : r !== void 0 && !n ? "fresh" : "unknown";
678
665
  };
679
- return v === "_status" ? s() : s;
666
+ return m === "_status" ? s() : s;
680
667
  }
681
- if (v === "removeStorage")
668
+ if (m === "removeStorage")
682
669
  return () => {
683
- const s = t.getState().initialStateGlobal[e], n = ae(e), r = ee(n?.localStorage?.key) ? n.localStorage.key(s) : n?.localStorage?.key, a = `${g}-${e}-${r}`;
670
+ const s = e.getState().initialStateGlobal[t], n = nt(t), r = at(n?.localStorage?.key) ? n.localStorage.key(s) : n?.localStorage?.key, a = `${S}-${t}-${r}`;
684
671
  a && localStorage.removeItem(a);
685
672
  };
686
- if (v === "showValidationErrors")
673
+ if (m === "showValidationErrors")
687
674
  return () => {
688
- const s = t.getState().getInitialOptions(e)?.validation;
689
- if (!s?.key) throw new Error("Validation key not found");
690
- return t.getState().getValidationErrors(s.key + "." + o.join("."));
675
+ const s = e.getState().getShadowMetadata(t, i);
676
+ return s?.validation?.status === "VALIDATION_FAILED" && s.validation.message ? [s.validation.message] : [];
691
677
  };
692
- if (Array.isArray(b)) {
693
- if (v === "getSelected")
678
+ if (Array.isArray(w)) {
679
+ if (m === "getSelected")
694
680
  return () => {
695
- const s = e + "." + o.join(".");
696
- Ve(e, T, [
697
- ...o,
681
+ const s = t + "." + i.join(".");
682
+ Et(t, M, [
683
+ ...i,
698
684
  "getSelected"
699
685
  ]);
700
- const n = t.getState().selectedIndicesMap;
686
+ const n = e.getState().selectedIndicesMap;
701
687
  if (!n || !n.has(s))
702
688
  return;
703
689
  const r = n.get(s);
704
- if (S?.validIds && !S.validIds.includes(r))
690
+ if (g?.validIds && !g.validIds.includes(r))
705
691
  return;
706
- const a = t.getState().getShadowValue(r);
692
+ const a = e.getState().getShadowValue(r);
707
693
  if (a)
708
- return l({
694
+ return u({
709
695
  currentState: a,
710
696
  path: r.split(".").slice(1),
711
- componentId: T
697
+ componentId: M
712
698
  });
713
699
  };
714
- if (v === "getSelectedIndex")
715
- return () => t.getState().getSelectedIndex(
716
- e + "." + o.join("."),
717
- S?.validIds
700
+ if (m === "getSelectedIndex")
701
+ return () => e.getState().getSelectedIndex(
702
+ t + "." + i.join("."),
703
+ g?.validIds
718
704
  );
719
- if (v === "clearSelected")
720
- return ye(e, o), () => {
721
- t.getState().clearSelectedIndex({
722
- arrayKey: e + "." + o.join(".")
705
+ if (m === "clearSelected")
706
+ return pt(t, i), () => {
707
+ e.getState().clearSelectedIndex({
708
+ arrayKey: t + "." + i.join(".")
723
709
  });
724
710
  };
725
- if (v === "useVirtualView")
711
+ if (m === "useVirtualView")
726
712
  return (s) => {
727
713
  const {
728
714
  itemHeight: n = 50,
729
715
  overscan: r = 6,
730
716
  stickToBottom: a = !1,
731
717
  scrollStickTolerance: d = 75
732
- } = s, w = G(null), [m, P] = X({
718
+ } = s, l = q(null), [p, E] = K({
733
719
  startIndex: 0,
734
720
  endIndex: 10
735
- }), [R, D] = X({}), j = G(!0), F = G({
721
+ }), [D, U] = K({}), C = q(!0), F = q({
736
722
  isUserScrolling: !1,
737
723
  lastScrollTop: 0,
738
724
  scrollUpCount: 0,
739
725
  isNearBottom: !0
740
- }), O = G(
726
+ }), x = q(
741
727
  /* @__PURE__ */ new Map()
742
728
  );
743
- de(() => {
744
- if (!a || !w.current || F.current.isUserScrolling)
729
+ dt(() => {
730
+ if (!a || !l.current || F.current.isUserScrolling)
745
731
  return;
746
- const I = w.current;
747
- I.scrollTo({
748
- top: I.scrollHeight,
749
- behavior: j.current ? "instant" : "smooth"
732
+ const v = l.current;
733
+ v.scrollTo({
734
+ top: v.scrollHeight,
735
+ behavior: C.current ? "instant" : "smooth"
750
736
  });
751
- }, [R, a]);
752
- const L = t.getState().getShadowMetadata(e, o)?.arrayKeys || [], { totalHeight: N, itemOffsets: B } = ge(() => {
753
- let I = 0;
754
- const y = /* @__PURE__ */ new Map();
755
- return (t.getState().getShadowMetadata(e, o)?.arrayKeys || []).forEach((C) => {
756
- const _ = C.split(".").slice(1), q = t.getState().getShadowMetadata(e, _)?.virtualizer?.itemHeight || n;
757
- y.set(C, {
758
- height: q,
759
- offset: I
760
- }), I += q;
761
- }), O.current = y, { totalHeight: I, itemOffsets: y };
762
- }, [L.length, n]);
763
- de(() => {
764
- if (a && L.length > 0 && w.current && !F.current.isUserScrolling && j.current) {
765
- const I = w.current, y = () => {
766
- if (I.clientHeight > 0) {
767
- const k = Math.ceil(
768
- I.clientHeight / n
769
- ), C = L.length - 1, _ = Math.max(
737
+ }, [D, a]);
738
+ const j = e.getState().getShadowMetadata(t, i)?.arrayKeys || [], { totalHeight: L, itemOffsets: b } = ft(() => {
739
+ let v = 0;
740
+ const T = /* @__PURE__ */ new Map();
741
+ return (e.getState().getShadowMetadata(t, i)?.arrayKeys || []).forEach((_) => {
742
+ const R = _.split(".").slice(1), N = e.getState().getShadowMetadata(t, R)?.virtualizer?.itemHeight || n;
743
+ T.set(_, {
744
+ height: N,
745
+ offset: v
746
+ }), v += N;
747
+ }), x.current = T, { totalHeight: v, itemOffsets: T };
748
+ }, [j.length, n]);
749
+ dt(() => {
750
+ if (a && j.length > 0 && l.current && !F.current.isUserScrolling && C.current) {
751
+ const v = l.current, T = () => {
752
+ if (v.clientHeight > 0) {
753
+ const $ = Math.ceil(
754
+ v.clientHeight / n
755
+ ), _ = j.length - 1, R = Math.max(
770
756
  0,
771
- C - k - r
757
+ _ - $ - r
772
758
  );
773
- P({ startIndex: _, endIndex: C }), requestAnimationFrame(() => {
774
- E("instant"), j.current = !1;
759
+ E({ startIndex: R, endIndex: _ }), requestAnimationFrame(() => {
760
+ P("instant"), C.current = !1;
775
761
  });
776
762
  } else
777
- requestAnimationFrame(y);
763
+ requestAnimationFrame(T);
778
764
  };
779
- y();
765
+ T();
780
766
  }
781
- }, [L.length, a, n, r]);
782
- const V = ce(() => {
783
- const I = w.current;
784
- if (!I) return;
785
- const y = I.scrollTop, { scrollHeight: k, clientHeight: C } = I, _ = F.current, q = k - (y + C), H = _.isNearBottom;
786
- _.isNearBottom = q <= d, y < _.lastScrollTop ? (_.scrollUpCount++, _.scrollUpCount > 3 && H && (_.isUserScrolling = !0, console.log("User scrolled away from bottom"))) : _.isNearBottom && (_.isUserScrolling = !1, _.scrollUpCount = 0), _.lastScrollTop = y;
787
- let z = 0;
788
- for (let Z = 0; Z < L.length; Z++) {
789
- const ue = L[Z], me = O.current.get(ue);
790
- if (me && me.offset + me.height > y) {
791
- z = Z;
767
+ }, [j.length, a, n, r]);
768
+ const k = ct(() => {
769
+ const v = l.current;
770
+ if (!v) return;
771
+ const T = v.scrollTop, { scrollHeight: $, clientHeight: _ } = v, R = F.current, N = $ - (T + _), G = R.isNearBottom;
772
+ R.isNearBottom = N <= d, T < R.lastScrollTop ? (R.scrollUpCount++, R.scrollUpCount > 3 && G && (R.isUserScrolling = !0, console.log("User scrolled away from bottom"))) : R.isNearBottom && (R.isUserScrolling = !1, R.scrollUpCount = 0), R.lastScrollTop = T;
773
+ let tt = 0;
774
+ for (let et = 0; et < j.length; et++) {
775
+ const St = j[et], ht = x.current.get(St);
776
+ if (ht && ht.offset + ht.height > T) {
777
+ tt = et;
792
778
  break;
793
779
  }
794
780
  }
795
- if (z !== m.startIndex) {
796
- const Z = Math.ceil(C / n);
797
- P({
798
- startIndex: Math.max(0, z - r),
781
+ if (tt !== p.startIndex) {
782
+ const et = Math.ceil(_ / n);
783
+ E({
784
+ startIndex: Math.max(0, tt - r),
799
785
  endIndex: Math.min(
800
- L.length - 1,
801
- z + Z + r
786
+ j.length - 1,
787
+ tt + et + r
802
788
  )
803
789
  });
804
790
  }
805
791
  }, [
806
- L.length,
807
- m.startIndex,
792
+ j.length,
793
+ p.startIndex,
808
794
  n,
809
795
  r,
810
796
  d
811
797
  ]);
812
- Q(() => {
813
- const I = w.current;
814
- if (!(!I || !a))
815
- return I.addEventListener("scroll", V, {
798
+ Z(() => {
799
+ const v = l.current;
800
+ if (!(!v || !a))
801
+ return v.addEventListener("scroll", k, {
816
802
  passive: !0
817
803
  }), () => {
818
- I.removeEventListener("scroll", V);
804
+ v.removeEventListener("scroll", k);
819
805
  };
820
- }, [V, a]);
821
- const E = ce(
822
- (I = "smooth") => {
823
- const y = w.current;
824
- if (!y) return;
806
+ }, [k, a]);
807
+ const P = ct(
808
+ (v = "smooth") => {
809
+ const T = l.current;
810
+ if (!T) return;
825
811
  F.current.isUserScrolling = !1, F.current.isNearBottom = !0, F.current.scrollUpCount = 0;
826
- const k = () => {
827
- const C = (_ = 0) => {
828
- if (_ > 5) return;
829
- const q = y.scrollHeight, H = y.scrollTop, z = y.clientHeight;
830
- H + z >= q - 1 || (y.scrollTo({
831
- top: q,
832
- behavior: I
812
+ const $ = () => {
813
+ const _ = (R = 0) => {
814
+ if (R > 5) return;
815
+ const N = T.scrollHeight, G = T.scrollTop, tt = T.clientHeight;
816
+ G + tt >= N - 1 || (T.scrollTo({
817
+ top: N,
818
+ behavior: v
833
819
  }), setTimeout(() => {
834
- const Z = y.scrollHeight, ue = y.scrollTop;
835
- (Z !== q || ue + z < Z - 1) && C(_ + 1);
820
+ const et = T.scrollHeight, St = T.scrollTop;
821
+ (et !== N || St + tt < et - 1) && _(R + 1);
836
822
  }, 50));
837
823
  };
838
- C();
824
+ _();
839
825
  };
840
- "requestIdleCallback" in window ? requestIdleCallback(k, { timeout: 100 }) : requestAnimationFrame(() => {
841
- requestAnimationFrame(k);
826
+ "requestIdleCallback" in window ? requestIdleCallback($, { timeout: 100 }) : requestAnimationFrame(() => {
827
+ requestAnimationFrame($);
842
828
  });
843
829
  },
844
830
  []
845
831
  );
846
- return Q(() => {
847
- if (!a || !w.current) return;
848
- const I = w.current, y = F.current;
849
- let k;
850
- const C = () => {
851
- clearTimeout(k), k = setTimeout(() => {
852
- !y.isUserScrolling && y.isNearBottom && E(
853
- j.current ? "instant" : "smooth"
832
+ return Z(() => {
833
+ if (!a || !l.current) return;
834
+ const v = l.current, T = F.current;
835
+ let $;
836
+ const _ = () => {
837
+ clearTimeout($), $ = setTimeout(() => {
838
+ !T.isUserScrolling && T.isNearBottom && P(
839
+ C.current ? "instant" : "smooth"
854
840
  );
855
841
  }, 100);
856
- }, _ = new MutationObserver(() => {
857
- y.isUserScrolling || C();
842
+ }, R = new MutationObserver(() => {
843
+ T.isUserScrolling || _();
858
844
  });
859
- _.observe(I, {
845
+ R.observe(v, {
860
846
  childList: !0,
861
847
  subtree: !0,
862
848
  attributes: !0,
863
849
  attributeFilter: ["style", "class"]
864
850
  // More specific than just 'height'
865
851
  });
866
- const q = (H) => {
867
- H.target instanceof HTMLImageElement && !y.isUserScrolling && C();
852
+ const N = (G) => {
853
+ G.target instanceof HTMLImageElement && !T.isUserScrolling && _();
868
854
  };
869
- return I.addEventListener("load", q, !0), j.current ? setTimeout(() => {
870
- E("instant");
871
- }, 0) : C(), () => {
872
- clearTimeout(k), _.disconnect(), I.removeEventListener("load", q, !0);
855
+ return v.addEventListener("load", N, !0), C.current ? setTimeout(() => {
856
+ P("instant");
857
+ }, 0) : _(), () => {
858
+ clearTimeout($), R.disconnect(), v.removeEventListener("load", N, !0);
873
859
  };
874
- }, [a, L.length, E]), {
875
- virtualState: ge(() => {
876
- const I = t.getState(), y = I.getShadowValue(
877
- [e, ...o].join(".")
878
- ), k = I.getShadowMetadata(e, o)?.arrayKeys || [], C = y.slice(
879
- m.startIndex,
880
- m.endIndex + 1
881
- ), _ = k.slice(
882
- m.startIndex,
883
- m.endIndex + 1
860
+ }, [a, j.length, P]), {
861
+ virtualState: ft(() => {
862
+ const v = e.getState(), T = v.getShadowValue(
863
+ [t, ...i].join(".")
864
+ ), $ = v.getShadowMetadata(t, i)?.arrayKeys || [], _ = T.slice(
865
+ p.startIndex,
866
+ p.endIndex + 1
867
+ ), R = $.slice(
868
+ p.startIndex,
869
+ p.endIndex + 1
884
870
  );
885
- return l({
886
- currentState: C,
887
- path: o,
888
- componentId: T,
889
- meta: { ...S, validIds: _ }
871
+ return u({
872
+ currentState: _,
873
+ path: i,
874
+ componentId: M,
875
+ meta: { ...g, validIds: R }
890
876
  });
891
- }, [m.startIndex, m.endIndex, L.length]),
877
+ }, [p.startIndex, p.endIndex, j.length]),
892
878
  virtualizerProps: {
893
879
  outer: {
894
- ref: w,
880
+ ref: l,
895
881
  style: {
896
882
  overflowY: "auto",
897
883
  height: "100%",
@@ -900,100 +886,100 @@ function De(e, i, f, g) {
900
886
  },
901
887
  inner: {
902
888
  style: {
903
- height: `${N}px`,
889
+ height: `${L}px`,
904
890
  position: "relative"
905
891
  }
906
892
  },
907
893
  list: {
908
894
  style: {
909
- transform: `translateY(${O.current.get(
910
- L[m.startIndex]
895
+ transform: `translateY(${x.current.get(
896
+ j[p.startIndex]
911
897
  )?.offset || 0}px)`
912
898
  }
913
899
  }
914
900
  },
915
- scrollToBottom: E,
916
- scrollToIndex: (I, y = "smooth") => {
917
- if (w.current && L[I]) {
918
- const k = O.current.get(L[I])?.offset || 0;
919
- w.current.scrollTo({ top: k, behavior: y });
901
+ scrollToBottom: P,
902
+ scrollToIndex: (v, T = "smooth") => {
903
+ if (l.current && j[v]) {
904
+ const $ = x.current.get(j[v])?.offset || 0;
905
+ l.current.scrollTo({ top: $, behavior: T });
920
906
  }
921
907
  }
922
908
  };
923
909
  };
924
- if (v === "stateMap")
910
+ if (m === "stateMap")
925
911
  return (s) => {
926
- const [n, r] = X(
927
- S?.validIds ?? t.getState().getShadowMetadata(e, o)?.arrayKeys
928
- ), a = t.getState().getShadowValue(c, S?.validIds);
912
+ const [n, r] = K(
913
+ g?.validIds ?? e.getState().getShadowMetadata(t, i)?.arrayKeys
914
+ ), a = e.getState().getShadowValue(c, g?.validIds);
929
915
  if (!n)
930
916
  throw new Error("No array keys found for mapping");
931
- const d = l({
917
+ const d = u({
932
918
  currentState: a,
933
- path: o,
934
- componentId: T,
935
- meta: S
919
+ path: i,
920
+ componentId: M,
921
+ meta: g
936
922
  });
937
- return a.map((w, m) => {
938
- const P = n[m]?.split(".").slice(1), R = l({
939
- currentState: w,
940
- path: P,
941
- componentId: T,
942
- meta: S
923
+ return a.map((l, p) => {
924
+ const E = n[p]?.split(".").slice(1), D = u({
925
+ currentState: l,
926
+ path: E,
927
+ componentId: M,
928
+ meta: g
943
929
  });
944
930
  return s(
945
- R,
946
- m,
931
+ D,
932
+ p,
947
933
  d
948
934
  );
949
935
  });
950
936
  };
951
- if (v === "$stateMap")
952
- return (s) => le(Ge, {
937
+ if (m === "$stateMap")
938
+ return (s) => lt(Bt, {
953
939
  proxy: {
954
- _stateKey: e,
955
- _path: o,
940
+ _stateKey: t,
941
+ _path: i,
956
942
  _mapFn: s,
957
- _meta: S
943
+ _meta: g
958
944
  },
959
- rebuildStateShape: l
945
+ rebuildStateShape: u
960
946
  });
961
- if (v === "stateFind")
947
+ if (m === "stateFind")
962
948
  return (s) => {
963
- const n = S?.validIds ?? t.getState().getShadowMetadata(e, o)?.arrayKeys;
949
+ const n = g?.validIds ?? e.getState().getShadowMetadata(t, i)?.arrayKeys;
964
950
  if (n)
965
951
  for (let r = 0; r < n.length; r++) {
966
952
  const a = n[r];
967
953
  if (!a) continue;
968
- const d = t.getState().getShadowValue(a);
954
+ const d = e.getState().getShadowValue(a);
969
955
  if (s(d, r)) {
970
- const w = a.split(".").slice(1);
971
- return l({
956
+ const l = a.split(".").slice(1);
957
+ return u({
972
958
  currentState: d,
973
- path: w,
974
- componentId: T,
975
- meta: S
959
+ path: l,
960
+ componentId: M,
961
+ meta: g
976
962
  // Pass along meta for potential further chaining
977
963
  });
978
964
  }
979
965
  }
980
966
  };
981
- if (v === "stateFilter")
967
+ if (m === "stateFilter")
982
968
  return (s) => {
983
- const n = S?.validIds ?? t.getState().getShadowMetadata(e, o)?.arrayKeys;
969
+ const n = g?.validIds ?? e.getState().getShadowMetadata(t, i)?.arrayKeys;
984
970
  if (!n)
985
971
  throw new Error("No array keys found for filtering.");
986
- const r = [], a = b.filter(
987
- (d, w) => s(d, w) ? (r.push(n[w]), !0) : !1
972
+ const r = [], a = w.filter(
973
+ (d, l) => s(d, l) ? (r.push(n[l]), !0) : !1
988
974
  );
989
- return l({
975
+ return u({
990
976
  currentState: a,
991
- path: o,
992
- componentId: T,
977
+ path: i,
978
+ componentId: M,
993
979
  meta: {
994
980
  validIds: r,
995
981
  transforms: [
996
- ...S?.transforms || [],
982
+ ...g?.transforms || [],
997
983
  {
998
984
  type: "filter",
999
985
  fn: s
@@ -1002,421 +988,455 @@ function De(e, i, f, g) {
1002
988
  }
1003
989
  });
1004
990
  };
1005
- if (v === "stateSort")
991
+ if (m === "stateSort")
1006
992
  return (s) => {
1007
- const n = S?.validIds ?? t.getState().getShadowMetadata(e, o)?.arrayKeys;
993
+ const n = g?.validIds ?? e.getState().getShadowMetadata(t, i)?.arrayKeys;
1008
994
  if (!n)
1009
995
  throw new Error("No array keys found for sorting");
1010
- const r = b.map((a, d) => ({
996
+ const r = w.map((a, d) => ({
1011
997
  item: a,
1012
998
  key: n[d]
1013
999
  }));
1014
- return r.sort((a, d) => s(a.item, d.item)).filter(Boolean), l({
1000
+ return r.sort((a, d) => s(a.item, d.item)).filter(Boolean), u({
1015
1001
  currentState: r.map((a) => a.item),
1016
- path: o,
1017
- componentId: T,
1002
+ path: i,
1003
+ componentId: M,
1018
1004
  meta: {
1019
1005
  validIds: r.map((a) => a.key),
1020
1006
  transforms: [
1021
- ...S?.transforms || [],
1007
+ ...g?.transforms || [],
1022
1008
  { type: "sort", fn: s }
1023
1009
  ]
1024
1010
  }
1025
1011
  });
1026
1012
  };
1027
- if (v === "stream")
1013
+ if (m === "stream")
1028
1014
  return function(s = {}) {
1029
1015
  const {
1030
1016
  bufferSize: n = 100,
1031
1017
  flushInterval: r = 100,
1032
1018
  bufferStrategy: a = "accumulate",
1033
1019
  store: d,
1034
- onFlush: w
1020
+ onFlush: l
1035
1021
  } = s;
1036
- let m = [], P = !1, R = null;
1037
- const D = (N) => {
1038
- if (!P) {
1039
- if (a === "sliding" && m.length >= n)
1040
- m.shift();
1041
- else if (a === "dropping" && m.length >= n)
1022
+ let p = [], E = !1, D = null;
1023
+ const U = (L) => {
1024
+ if (!E) {
1025
+ if (a === "sliding" && p.length >= n)
1026
+ p.shift();
1027
+ else if (a === "dropping" && p.length >= n)
1042
1028
  return;
1043
- m.push(N), m.length >= n && j();
1029
+ p.push(L), p.length >= n && C();
1044
1030
  }
1045
- }, j = () => {
1046
- if (m.length === 0) return;
1047
- const N = [...m];
1048
- if (m = [], d) {
1049
- const B = d(N);
1050
- B !== void 0 && (Array.isArray(B) ? B : [B]).forEach((E) => {
1051
- i(E, o, {
1031
+ }, C = () => {
1032
+ if (p.length === 0) return;
1033
+ const L = [...p];
1034
+ if (p = [], d) {
1035
+ const b = d(L);
1036
+ b !== void 0 && (Array.isArray(b) ? b : [b]).forEach((P) => {
1037
+ o(P, i, {
1052
1038
  updateType: "insert"
1053
1039
  });
1054
1040
  });
1055
1041
  } else
1056
- N.forEach((B) => {
1057
- i(B, o, {
1042
+ L.forEach((b) => {
1043
+ o(b, i, {
1058
1044
  updateType: "insert"
1059
1045
  });
1060
1046
  });
1061
- w?.(N);
1047
+ l?.(L);
1062
1048
  };
1063
- r > 0 && (R = setInterval(j, r));
1064
- const F = te(), O = t.getState().getShadowMetadata(e, o) || {}, L = O.streams || /* @__PURE__ */ new Map();
1065
- return L.set(F, { buffer: m, flushTimer: R }), t.getState().setShadowMetadata(e, o, {
1066
- ...O,
1067
- streams: L
1049
+ r > 0 && (D = setInterval(C, r));
1050
+ const F = rt(), x = e.getState().getShadowMetadata(t, i) || {}, j = x.streams || /* @__PURE__ */ new Map();
1051
+ return j.set(F, { buffer: p, flushTimer: D }), e.getState().setShadowMetadata(t, i, {
1052
+ ...x,
1053
+ streams: j
1068
1054
  }), {
1069
- write: (N) => D(N),
1070
- writeMany: (N) => N.forEach(D),
1071
- flush: () => j(),
1055
+ write: (L) => U(L),
1056
+ writeMany: (L) => L.forEach(U),
1057
+ flush: () => C(),
1072
1058
  pause: () => {
1073
- P = !0;
1059
+ E = !0;
1074
1060
  },
1075
1061
  resume: () => {
1076
- P = !1, m.length > 0 && j();
1062
+ E = !1, p.length > 0 && C();
1077
1063
  },
1078
1064
  close: () => {
1079
- j(), R && clearInterval(R);
1080
- const N = t.getState().getShadowMetadata(e, o);
1081
- N?.streams && N.streams.delete(F);
1065
+ C(), D && clearInterval(D);
1066
+ const L = e.getState().getShadowMetadata(t, i);
1067
+ L?.streams && L.streams.delete(F);
1082
1068
  }
1083
1069
  };
1084
1070
  };
1085
- if (v === "stateList")
1086
- return (s) => /* @__PURE__ */ re(() => {
1087
- const r = G(/* @__PURE__ */ new Map()), a = S?.transforms && S.transforms.length > 0 ? `${T}-${qe(S.transforms)}` : `${T}-base`, [d, w] = X({}), { validIds: m, arrayValues: P } = ge(() => {
1088
- const D = t.getState().getShadowMetadata(e, o)?.transformCaches?.get(a);
1089
- let j;
1090
- D && D.validIds ? j = D.validIds : (j = Ie(
1091
- e,
1092
- o,
1093
- S?.transforms
1094
- ), t.getState().setTransformCache(e, o, a, {
1095
- validIds: j,
1071
+ if (m === "stateList")
1072
+ return (s) => /* @__PURE__ */ ot(() => {
1073
+ const r = q(/* @__PURE__ */ new Map()), a = g?.transforms && g.transforms.length > 0 ? `${M}-${zt(g.transforms)}` : `${M}-base`, [d, l] = K({}), { validIds: p, arrayValues: E } = ft(() => {
1074
+ const U = e.getState().getShadowMetadata(t, i)?.transformCaches?.get(a);
1075
+ let C;
1076
+ U && U.validIds ? C = U.validIds : (C = It(
1077
+ t,
1078
+ i,
1079
+ g?.transforms
1080
+ ), e.getState().setTransformCache(t, i, a, {
1081
+ validIds: C,
1096
1082
  computedAt: Date.now(),
1097
- transforms: S?.transforms || []
1083
+ transforms: g?.transforms || []
1098
1084
  }));
1099
- const F = t.getState().getShadowValue(c, j);
1085
+ const F = e.getState().getShadowValue(c, C);
1100
1086
  return {
1101
- validIds: j,
1087
+ validIds: C,
1102
1088
  arrayValues: F || []
1103
1089
  };
1104
1090
  }, [a, d]);
1105
- if (console.log("freshValues", m, P), Q(() => {
1106
- const D = t.getState().subscribeToPath(c, (j) => {
1107
- if (j.type === "GET_SELECTED")
1091
+ if (console.log("freshValues", p, E), Z(() => {
1092
+ const U = e.getState().subscribeToPath(c, (C) => {
1093
+ if (C.type === "GET_SELECTED")
1108
1094
  return;
1109
- const O = t.getState().getShadowMetadata(e, o)?.transformCaches;
1110
- if (O)
1111
- for (const L of O.keys())
1112
- L.startsWith(T) && O.delete(L);
1113
- (j.type === "INSERT" || j.type === "REMOVE" || j.type === "CLEAR_SELECTION") && (console.log("sssssssssssssssssssssssssssss", j), w({}));
1095
+ const x = e.getState().getShadowMetadata(t, i)?.transformCaches;
1096
+ if (x)
1097
+ for (const j of x.keys())
1098
+ j.startsWith(M) && x.delete(j);
1099
+ (C.type === "INSERT" || C.type === "REMOVE" || C.type === "CLEAR_SELECTION") && l({});
1114
1100
  });
1115
1101
  return () => {
1116
- D();
1102
+ U();
1117
1103
  };
1118
- }, [T, c]), !Array.isArray(P))
1104
+ }, [M, c]), !Array.isArray(E))
1119
1105
  return null;
1120
- const R = l({
1121
- currentState: P,
1122
- path: o,
1123
- componentId: T,
1106
+ const D = u({
1107
+ currentState: E,
1108
+ path: i,
1109
+ componentId: M,
1124
1110
  meta: {
1125
- ...S,
1126
- validIds: m
1111
+ ...g,
1112
+ validIds: p
1127
1113
  }
1128
1114
  });
1129
- return console.log("sssssssssssssssssssssssssssss", R), /* @__PURE__ */ re(ke, { children: P.map((D, j) => {
1130
- const F = m[j];
1115
+ return console.log("sssssssssssssssssssssssssssss", D), /* @__PURE__ */ ot(Ut, { children: E.map((U, C) => {
1116
+ const F = p[C];
1131
1117
  if (!F)
1132
1118
  return null;
1133
- let O = r.current.get(F);
1134
- O || (O = te(), r.current.set(F, O));
1135
- const L = F.split(".").slice(1);
1136
- return le(Me, {
1119
+ let x = r.current.get(F);
1120
+ x || (x = rt(), r.current.set(F, x));
1121
+ const j = F.split(".").slice(1);
1122
+ return lt(Mt, {
1137
1123
  key: F,
1138
- stateKey: e,
1139
- itemComponentId: O,
1140
- itemPath: L,
1141
- localIndex: j,
1142
- arraySetter: R,
1143
- rebuildStateShape: l,
1124
+ stateKey: t,
1125
+ itemComponentId: x,
1126
+ itemPath: j,
1127
+ localIndex: C,
1128
+ arraySetter: D,
1129
+ rebuildStateShape: u,
1144
1130
  renderFn: s
1145
1131
  });
1146
1132
  }) });
1147
1133
  }, {});
1148
- if (v === "stateFlattenOn")
1134
+ if (m === "stateFlattenOn")
1149
1135
  return (s) => {
1150
- const n = b;
1151
- h.clear(), p++;
1136
+ const n = w;
1137
+ y.clear(), I++;
1152
1138
  const r = n.flatMap(
1153
1139
  (a) => a[s] ?? []
1154
1140
  );
1155
- return l({
1141
+ return u({
1156
1142
  currentState: r,
1157
- path: [...o, "[*]", s],
1158
- componentId: T,
1159
- meta: S
1143
+ path: [...i, "[*]", s],
1144
+ componentId: M,
1145
+ meta: g
1160
1146
  });
1161
1147
  };
1162
- if (v === "index")
1148
+ if (m === "index")
1163
1149
  return (s) => {
1164
- const r = t.getState().getShadowMetadata(e, o)?.arrayKeys?.filter(
1165
- (w) => !S?.validIds || S?.validIds && S?.validIds?.includes(w)
1150
+ const r = e.getState().getShadowMetadata(t, i)?.arrayKeys?.filter(
1151
+ (l) => !g?.validIds || g?.validIds && g?.validIds?.includes(l)
1166
1152
  )?.[s];
1167
1153
  if (!r) return;
1168
- const a = t.getState().getShadowValue(r, S?.validIds);
1169
- return l({
1154
+ const a = e.getState().getShadowValue(r, g?.validIds);
1155
+ return u({
1170
1156
  currentState: a,
1171
1157
  path: r.split(".").slice(1),
1172
- componentId: T,
1173
- meta: S
1158
+ componentId: M,
1159
+ meta: g
1174
1160
  });
1175
1161
  };
1176
- if (v === "last")
1162
+ if (m === "last")
1177
1163
  return () => {
1178
- const s = t.getState().getShadowValue(e, o);
1164
+ const s = e.getState().getShadowValue(t, i);
1179
1165
  if (s.length === 0) return;
1180
- const n = s.length - 1, r = s[n], a = [...o, n.toString()];
1181
- return l({
1166
+ const n = s.length - 1, r = s[n], a = [...i, n.toString()];
1167
+ return u({
1182
1168
  currentState: r,
1183
1169
  path: a,
1184
- componentId: T,
1185
- meta: S
1170
+ componentId: M,
1171
+ meta: g
1186
1172
  });
1187
1173
  };
1188
- if (v === "insert")
1189
- return (s, n) => (i(s, o, { updateType: "insert" }), l({
1190
- currentState: t.getState().getShadowValue(e, o),
1191
- path: o,
1192
- componentId: T,
1193
- meta: S
1174
+ if (m === "insert")
1175
+ return (s, n) => (o(s, i, { updateType: "insert" }), u({
1176
+ currentState: e.getState().getShadowValue(t, i),
1177
+ path: i,
1178
+ componentId: M,
1179
+ meta: g
1194
1180
  }));
1195
- if (v === "uniqueInsert")
1181
+ if (m === "uniqueInsert")
1196
1182
  return (s, n, r) => {
1197
- const a = t.getState().getShadowValue(e, o), d = ee(s) ? s(a) : s;
1198
- let w = null;
1199
- if (!a.some((P) => {
1200
- const R = n ? n.every(
1201
- (D) => se(P[D], d[D])
1202
- ) : se(P, d);
1203
- return R && (w = P), R;
1183
+ const a = e.getState().getShadowValue(t, i), d = at(s) ? s(a) : s;
1184
+ let l = null;
1185
+ if (!a.some((E) => {
1186
+ const D = n ? n.every(
1187
+ (U) => st(E[U], d[U])
1188
+ ) : st(E, d);
1189
+ return D && (l = E), D;
1204
1190
  }))
1205
- M(o), i(d, o, { updateType: "insert" });
1206
- else if (r && w) {
1207
- const P = r(w), R = a.map(
1208
- (D) => se(D, w) ? P : D
1191
+ V(i), o(d, i, { updateType: "insert" });
1192
+ else if (r && l) {
1193
+ const E = r(l), D = a.map(
1194
+ (U) => st(U, l) ? E : U
1209
1195
  );
1210
- M(o), i(R, o, {
1196
+ V(i), o(D, i, {
1211
1197
  updateType: "update"
1212
1198
  });
1213
1199
  }
1214
1200
  };
1215
- if (v === "cut")
1201
+ if (m === "cut")
1216
1202
  return (s, n) => {
1217
- const r = S?.validIds ?? t.getState().getShadowMetadata(e, o)?.arrayKeys;
1203
+ const r = g?.validIds ?? e.getState().getShadowMetadata(t, i)?.arrayKeys;
1218
1204
  if (!r || r.length === 0) return;
1219
1205
  const a = s == -1 ? r.length - 1 : s !== void 0 ? s : r.length - 1, d = r[a];
1220
1206
  if (!d) return;
1221
- const w = d.split(".").slice(1);
1222
- i(b, w, {
1207
+ const l = d.split(".").slice(1);
1208
+ o(w, l, {
1223
1209
  updateType: "cut"
1224
1210
  });
1225
1211
  };
1226
- if (v === "cutSelected")
1212
+ if (m === "cutSelected")
1227
1213
  return () => {
1228
- t.getState().getShadowMetadata(e, o)?.arrayKeys;
1229
- const s = Ie(
1230
- e,
1231
- o,
1232
- S?.transforms
1214
+ const s = It(
1215
+ t,
1216
+ i,
1217
+ g?.transforms
1233
1218
  );
1234
- if (console.log("validKeys", s), !s || s.length === 0) return;
1235
- const n = t.getState().selectedIndicesMap.get(c);
1219
+ if (!s || s.length === 0) return;
1220
+ const n = e.getState().selectedIndicesMap.get(c);
1236
1221
  let r = s.findIndex(
1237
1222
  (d) => d === n
1238
1223
  );
1239
- console.log("indexToCut", r);
1240
1224
  const a = s[r == -1 ? s.length - 1 : r]?.split(".").slice(1);
1241
- console.log("pathForCut", a), i(b, a, {
1225
+ e.getState().clearSelectedIndex({ arrayKey: c }), o(w, a, {
1242
1226
  updateType: "cut"
1243
1227
  });
1244
1228
  };
1245
- if (v === "cutByValue")
1229
+ if (m === "cutByValue")
1246
1230
  return (s) => {
1247
- const n = t.getState().getShadowMetadata(e, o), r = S?.validIds ?? n?.arrayKeys;
1231
+ const n = e.getState().getShadowMetadata(t, i), r = g?.validIds ?? n?.arrayKeys;
1248
1232
  if (!r) return;
1249
1233
  let a = null;
1250
1234
  for (const d of r)
1251
- if (t.getState().getShadowValue(d) === s) {
1235
+ if (e.getState().getShadowValue(d) === s) {
1252
1236
  a = d;
1253
1237
  break;
1254
1238
  }
1255
1239
  if (a) {
1256
1240
  const d = a.split(".").slice(1);
1257
- i(null, d, { updateType: "cut" });
1241
+ o(null, d, { updateType: "cut" });
1258
1242
  }
1259
1243
  };
1260
- if (v === "toggleByValue")
1244
+ if (m === "toggleByValue")
1261
1245
  return (s) => {
1262
- const n = t.getState().getShadowMetadata(e, o), r = S?.validIds ?? n?.arrayKeys;
1246
+ const n = e.getState().getShadowMetadata(t, i), r = g?.validIds ?? n?.arrayKeys;
1263
1247
  if (!r) return;
1264
1248
  let a = null;
1265
1249
  for (const d of r) {
1266
- const w = t.getState().getShadowValue(d);
1267
- if (console.log("itemValue sdasdasdasd", w), w === s) {
1250
+ const l = e.getState().getShadowValue(d);
1251
+ if (console.log("itemValue sdasdasdasd", l), l === s) {
1268
1252
  a = d;
1269
1253
  break;
1270
1254
  }
1271
1255
  }
1272
1256
  if (console.log("itemValue keyToCut", a), a) {
1273
1257
  const d = a.split(".").slice(1);
1274
- console.log("itemValue keyToCut", a), i(s, d, {
1258
+ console.log("itemValue keyToCut", a), o(s, d, {
1275
1259
  updateType: "cut"
1276
1260
  });
1277
1261
  } else
1278
- i(s, o, { updateType: "insert" });
1262
+ o(s, i, { updateType: "insert" });
1279
1263
  };
1280
- if (v === "findWith")
1264
+ if (m === "findWith")
1281
1265
  return (s, n) => {
1282
- const r = t.getState().getShadowMetadata(e, o)?.arrayKeys;
1266
+ const r = e.getState().getShadowMetadata(t, i)?.arrayKeys;
1283
1267
  if (!r)
1284
1268
  throw new Error("No array keys found for sorting");
1285
1269
  let a = null, d = [];
1286
- for (const w of r) {
1287
- let m = t.getState().getShadowValue(w, S?.validIds);
1288
- if (m && m[s] === n) {
1289
- a = m, d = w.split(".").slice(1);
1270
+ for (const l of r) {
1271
+ let p = e.getState().getShadowValue(l, g?.validIds);
1272
+ if (p && p[s] === n) {
1273
+ a = p, d = l.split(".").slice(1);
1290
1274
  break;
1291
1275
  }
1292
1276
  }
1293
- return l({
1277
+ return u({
1294
1278
  currentState: a,
1295
1279
  path: d,
1296
- componentId: T,
1297
- meta: S
1280
+ componentId: M,
1281
+ meta: g
1298
1282
  });
1299
1283
  };
1300
1284
  }
1301
- if (v === "cut") {
1302
- let s = t.getState().getShadowValue(o.join("."));
1285
+ if (m === "cut") {
1286
+ let s = e.getState().getShadowValue(i.join("."));
1303
1287
  return () => {
1304
- i(s, o, { updateType: "cut" });
1288
+ o(s, i, { updateType: "cut" });
1305
1289
  };
1306
1290
  }
1307
- if (v === "get")
1308
- return () => (Ve(e, T, o), t.getState().getShadowValue(c, S?.validIds));
1309
- if (v === "$derive")
1310
- return (s) => Ae({
1311
- _stateKey: e,
1312
- _path: o,
1291
+ if (m === "get")
1292
+ return () => (Et(t, M, i), e.getState().getShadowValue(c, g?.validIds));
1293
+ if (m === "getState")
1294
+ return () => e.getState().getShadowValue(c, g?.validIds);
1295
+ if (m === "$derive")
1296
+ return (s) => bt({
1297
+ _stateKey: t,
1298
+ _path: i,
1313
1299
  _effect: s.toString(),
1314
- _meta: S
1300
+ _meta: g
1315
1301
  });
1316
- if (v === "$get")
1317
- return () => Ae({ _stateKey: e, _path: o, _meta: S });
1318
- if (v === "lastSynced") {
1319
- const s = `${e}:${o.join(".")}`;
1320
- return t.getState().getSyncInfo(s);
1302
+ if (m === "$get")
1303
+ return () => bt({ _stateKey: t, _path: i, _meta: g });
1304
+ if (m === "lastSynced") {
1305
+ const s = `${t}:${i.join(".")}`;
1306
+ return e.getState().getSyncInfo(s);
1321
1307
  }
1322
- if (v == "getLocalStorage")
1323
- return (s) => Se(g + "-" + e + "-" + s);
1324
- if (v === "isSelected") {
1325
- const s = [e, ...o].slice(0, -1);
1326
- if (ye(e, o, void 0), Array.isArray(
1327
- t.getState().getShadowValue(s.join("."), S?.validIds)
1308
+ if (m == "getLocalStorage")
1309
+ return (s) => gt(S + "-" + t + "-" + s);
1310
+ if (m === "isSelected") {
1311
+ const s = [t, ...i].slice(0, -1);
1312
+ if (pt(t, i, void 0), Array.isArray(
1313
+ e.getState().getShadowValue(s.join("."), g?.validIds)
1328
1314
  )) {
1329
- o[o.length - 1];
1330
- const n = s.join("."), r = t.getState().selectedIndicesMap.get(n), a = e + "." + o.join(".");
1315
+ i[i.length - 1];
1316
+ const n = s.join("."), r = e.getState().selectedIndicesMap.get(n), a = t + "." + i.join(".");
1331
1317
  return r === a;
1332
1318
  }
1333
1319
  return;
1334
1320
  }
1335
- if (v === "setSelected")
1321
+ if (m === "setSelected")
1336
1322
  return (s) => {
1337
- const n = o.slice(0, -1), r = e + "." + n.join("."), a = e + "." + o.join(".");
1338
- ye(e, n, void 0), t.getState().selectedIndicesMap.get(r), s && t.getState().setSelectedIndex(r, a);
1323
+ const n = i.slice(0, -1), r = t + "." + n.join("."), a = t + "." + i.join(".");
1324
+ pt(t, n, void 0), e.getState().selectedIndicesMap.get(r), s && e.getState().setSelectedIndex(r, a);
1339
1325
  };
1340
- if (v === "toggleSelected")
1326
+ if (m === "toggleSelected")
1341
1327
  return () => {
1342
- const s = o.slice(0, -1), n = e + "." + s.join("."), r = e + "." + o.join(".");
1343
- t.getState().selectedIndicesMap.get(n) === r ? t.getState().clearSelectedIndex({ arrayKey: n }) : t.getState().setSelectedIndex(n, r);
1328
+ const s = i.slice(0, -1), n = t + "." + s.join("."), r = t + "." + i.join(".");
1329
+ e.getState().selectedIndicesMap.get(n) === r ? e.getState().clearSelectedIndex({ arrayKey: n }) : e.getState().setSelectedIndex(n, r);
1344
1330
  };
1345
- if (v === "_componentId")
1346
- return T;
1347
- if (o.length == 0) {
1348
- if (v === "addValidation")
1331
+ if (m === "_componentId")
1332
+ return M;
1333
+ if (i.length == 0) {
1334
+ if (m === "addValidation")
1349
1335
  return (s) => {
1350
- const n = t.getState().getInitialOptions(e)?.validation;
1336
+ const n = e.getState().getInitialOptions(t)?.validation;
1351
1337
  if (!n?.key) throw new Error("Validation key not found");
1352
- ne(n.key), s.forEach((r) => {
1338
+ ut(n.key), s.forEach((r) => {
1353
1339
  const a = [n.key, ...r.path].join(".");
1354
- ve(a, r.message);
1355
- }), ie(e);
1340
+ yt(a, r.message);
1341
+ }), it(t);
1356
1342
  };
1357
- if (v === "applyJsonPatch")
1343
+ if (m === "applyJsonPatch")
1358
1344
  return (s) => {
1359
- const n = t.getState(), r = (a) => !a || a === "/" ? [] : a.split("/").slice(1).map((d) => d.replace(/~1/g, "/").replace(/~0/g, "~"));
1360
- for (const a of s) {
1361
- const d = r(a.path);
1362
- switch (a.op) {
1345
+ const n = e.getState(), r = n.getShadowMetadata(t, []);
1346
+ if (!r?.components) return;
1347
+ const a = (l) => !l || l === "/" ? [] : l.split("/").slice(1).map((p) => p.replace(/~1/g, "/").replace(/~0/g, "~")), d = /* @__PURE__ */ new Set();
1348
+ for (const l of s) {
1349
+ const p = a(l.path);
1350
+ switch (l.op) {
1363
1351
  case "add":
1364
1352
  case "replace": {
1365
- const { value: w } = a;
1366
- n.updateShadowAtPath(e, d, w), n.markAsDirty(e, d, { bubble: !0 });
1353
+ const { value: E } = l;
1354
+ n.updateShadowAtPath(t, p, E), n.markAsDirty(t, p, { bubble: !0 });
1355
+ let D = [...p];
1356
+ for (; ; ) {
1357
+ const U = n.getShadowMetadata(
1358
+ t,
1359
+ D
1360
+ );
1361
+ if (console.log("pathMeta", U), U?.pathComponents && U.pathComponents.forEach((C) => {
1362
+ if (!d.has(C)) {
1363
+ const F = r.components?.get(C);
1364
+ F && (F.forceUpdate(), d.add(C));
1365
+ }
1366
+ }), D.length === 0) break;
1367
+ D.pop();
1368
+ }
1367
1369
  break;
1368
1370
  }
1369
1371
  case "remove": {
1370
- n.removeShadowArrayElement(e, d);
1371
- const w = d.slice(0, -1);
1372
- n.markAsDirty(e, w, { bubble: !0 });
1372
+ const E = p.slice(0, -1);
1373
+ n.removeShadowArrayElement(t, p), n.markAsDirty(t, E, { bubble: !0 });
1374
+ let D = [...E];
1375
+ for (; ; ) {
1376
+ const U = n.getShadowMetadata(
1377
+ t,
1378
+ D
1379
+ );
1380
+ if (U?.pathComponents && U.pathComponents.forEach((C) => {
1381
+ if (!d.has(C)) {
1382
+ const F = r.components?.get(C);
1383
+ F && (F.forceUpdate(), d.add(C));
1384
+ }
1385
+ }), D.length === 0) break;
1386
+ D.pop();
1387
+ }
1373
1388
  break;
1374
1389
  }
1375
1390
  }
1376
1391
  }
1377
1392
  };
1378
- if (v === "validateZodSchema")
1393
+ if (m === "validateZodSchema")
1379
1394
  return () => {
1380
- const s = t.getState().getInitialOptions(e)?.validation;
1381
- if (!s?.zodSchema || !s?.key)
1382
- throw new Error("Zod schema or validation key not found");
1383
- ne(s.key);
1384
- const n = t.getState().getShadowValue(e), r = s.zodSchema.safeParse(n);
1385
- return r.success ? !0 : (r.error.errors.forEach((a) => {
1386
- const d = [s.key, ...a.path].join(".");
1387
- ve(d, a.message);
1388
- }), ie(e), !1);
1395
+ const s = e.getState().getInitialOptions(t)?.validation, n = s?.zodSchemaV4 || s?.zodSchemaV3;
1396
+ if (!n || !s?.key)
1397
+ throw new Error(
1398
+ "Zod schema (v3 or v4) or validation key not found"
1399
+ );
1400
+ ut(s.key);
1401
+ const r = e.getState().getShadowValue(t), a = n.safeParse(r);
1402
+ return a.success ? !0 : ("issues" in a.error ? a.error.issues.forEach((d) => {
1403
+ const l = [s.key, ...d.path].join(".");
1404
+ yt(l, d.message);
1405
+ }) : a.error.errors.forEach((d) => {
1406
+ const l = [s.key, ...d.path].join(".");
1407
+ yt(l, d.message);
1408
+ }), it(t), !1);
1389
1409
  };
1390
- if (v === "getComponents")
1391
- return () => t.getState().getShadowMetadata(e, [])?.components;
1392
- if (v === "getAllFormRefs")
1393
- return () => pe.getState().getFormRefsByStateKey(e);
1410
+ if (m === "getComponents")
1411
+ return () => e.getState().getShadowMetadata(t, [])?.components;
1412
+ if (m === "getAllFormRefs")
1413
+ return () => vt.getState().getFormRefsByStateKey(t);
1394
1414
  }
1395
- if (v === "getFormRef")
1396
- return () => pe.getState().getFormRef(e + "." + o.join("."));
1397
- if (v === "validationWrapper")
1415
+ if (m === "getFormRef")
1416
+ return () => vt.getState().getFormRef(t + "." + i.join("."));
1417
+ if (m === "validationWrapper")
1398
1418
  return ({
1399
1419
  children: s,
1400
1420
  hideMessage: n
1401
- }) => /* @__PURE__ */ re(
1402
- be,
1421
+ }) => /* @__PURE__ */ ot(
1422
+ kt,
1403
1423
  {
1404
1424
  formOpts: n ? { validation: { message: "" } } : void 0,
1405
- path: o,
1406
- stateKey: e,
1425
+ path: i,
1426
+ stateKey: t,
1407
1427
  children: s
1408
1428
  }
1409
1429
  );
1410
- if (v === "_stateKey") return e;
1411
- if (v === "_path") return o;
1412
- if (v === "update")
1413
- return (s) => (i(s, o, { updateType: "update" }), {
1430
+ if (m === "_stateKey") return t;
1431
+ if (m === "_path") return i;
1432
+ if (m === "update")
1433
+ return (s) => (o(s, i, { updateType: "update" }), {
1414
1434
  /**
1415
1435
  * Marks this specific item, which was just updated, as 'synced' (not dirty).
1416
1436
  */
1417
1437
  synced: () => {
1418
- const n = t.getState().getShadowMetadata(e, o);
1419
- t.getState().setShadowMetadata(e, o, {
1438
+ const n = e.getState().getShadowMetadata(t, i);
1439
+ e.getState().setShadowMetadata(t, i, {
1420
1440
  ...n,
1421
1441
  isDirty: !1,
1422
1442
  // EXPLICITLY set to false, not just undefined
@@ -1425,385 +1445,461 @@ function De(e, i, f, g) {
1425
1445
  lastServerSync: Date.now()
1426
1446
  // Add timestamp
1427
1447
  });
1428
- const r = [e, ...o].join(".");
1429
- t.getState().notifyPathSubscribers(r, {
1448
+ const r = [t, ...i].join(".");
1449
+ e.getState().notifyPathSubscribers(r, {
1430
1450
  type: "SYNC_STATUS_CHANGE",
1431
1451
  isDirty: !1
1432
1452
  });
1433
1453
  }
1434
1454
  });
1435
- if (v === "toggle") {
1436
- const s = t.getState().getShadowValue([e, ...o].join("."));
1437
- if (console.log("currentValueAtPath", s), typeof b != "boolean")
1455
+ if (m === "toggle") {
1456
+ const s = e.getState().getShadowValue([t, ...i].join("."));
1457
+ if (console.log("currentValueAtPath", s), typeof w != "boolean")
1438
1458
  throw new Error("toggle() can only be used on boolean values");
1439
1459
  return () => {
1440
- i(!s, o, {
1460
+ o(!s, i, {
1441
1461
  updateType: "update"
1442
1462
  });
1443
1463
  };
1444
1464
  }
1445
- if (v === "formElement")
1446
- return (s, n) => /* @__PURE__ */ re(
1447
- be,
1465
+ if (m === "formElement")
1466
+ return (s, n) => /* @__PURE__ */ ot(
1467
+ Yt,
1448
1468
  {
1469
+ stateKey: t,
1470
+ path: i,
1471
+ rebuildStateShape: u,
1472
+ setState: o,
1449
1473
  formOpts: n,
1450
- path: o,
1451
- stateKey: e,
1452
- children: /* @__PURE__ */ re(
1453
- Ze,
1454
- {
1455
- stateKey: e,
1456
- path: o,
1457
- rebuildStateShape: l,
1458
- setState: i,
1459
- formOpts: n,
1460
- renderFn: s
1461
- }
1462
- )
1474
+ renderFn: s
1463
1475
  }
1464
1476
  );
1465
- const oe = [...o, v], fe = t.getState().getShadowValue(e, oe);
1466
- return l({
1467
- currentState: fe,
1468
- path: oe,
1469
- componentId: T,
1470
- meta: S
1477
+ const Q = [...i, m], X = e.getState().getShadowValue(t, Q);
1478
+ return u({
1479
+ currentState: X,
1480
+ path: Q,
1481
+ componentId: M,
1482
+ meta: g
1471
1483
  });
1472
1484
  }
1473
- }, J = new Proxy(x, Y);
1474
- return h.set(W, {
1475
- proxy: J,
1476
- stateVersion: p
1477
- }), J;
1485
+ }, H = new Proxy(B, J);
1486
+ return y.set(z, {
1487
+ proxy: H,
1488
+ stateVersion: I
1489
+ }), H;
1478
1490
  }
1479
- const u = {
1480
- removeValidation: (b) => {
1481
- b?.validationKey && ne(b.validationKey);
1491
+ const f = {
1492
+ removeValidation: (w) => {
1493
+ w?.validationKey && ut(w.validationKey);
1482
1494
  },
1483
- revertToInitialState: (b) => {
1484
- const o = t.getState().getInitialOptions(e)?.validation;
1485
- o?.key && ne(o.key), b?.validationKey && ne(b.validationKey);
1486
- const S = t.getState().getShadowMetadata(e, []);
1487
- S?.stateSource === "server" && S.baseServerState ? S.baseServerState : t.getState().initialStateGlobal[e];
1488
- const T = t.getState().initialStateGlobal[e];
1489
- t.getState().clearSelectedIndexesForState(e), h.clear(), p++, t.getState().initializeShadowState(e, T), l({
1490
- currentState: T,
1495
+ revertToInitialState: (w) => {
1496
+ const i = e.getState().getInitialOptions(t)?.validation;
1497
+ i?.key && ut(i.key), w?.validationKey && ut(w.validationKey);
1498
+ const g = e.getState().getShadowMetadata(t, []);
1499
+ g?.stateSource === "server" && g.baseServerState ? g.baseServerState : e.getState().initialStateGlobal[t];
1500
+ const M = e.getState().initialStateGlobal[t];
1501
+ e.getState().clearSelectedIndexesForState(t), y.clear(), I++, e.getState().initializeShadowState(t, M), u({
1502
+ currentState: M,
1491
1503
  path: [],
1492
- componentId: f
1504
+ componentId: h
1493
1505
  });
1494
- const W = ae(e), c = ee(W?.localStorage?.key) ? W?.localStorage?.key(T) : W?.localStorage?.key, x = `${g}-${e}-${c}`;
1495
- x && localStorage.removeItem(x);
1496
- const Y = t.getState().getShadowMetadata(e, []);
1497
- return Y && Y?.components?.forEach((J) => {
1498
- J.forceUpdate();
1499
- }), T;
1506
+ const z = nt(t), c = at(z?.localStorage?.key) ? z?.localStorage?.key(M) : z?.localStorage?.key, B = `${S}-${t}-${c}`;
1507
+ B && localStorage.removeItem(B);
1508
+ const J = e.getState().getShadowMetadata(t, []);
1509
+ return J && J?.components?.forEach((H) => {
1510
+ H.forceUpdate();
1511
+ }), M;
1500
1512
  },
1501
- updateInitialState: (b) => {
1502
- h.clear(), p++;
1503
- const o = De(
1504
- e,
1505
- i,
1506
- f,
1507
- g
1508
- ), S = t.getState().initialStateGlobal[e], T = ae(e), W = ee(T?.localStorage?.key) ? T?.localStorage?.key(S) : T?.localStorage?.key, c = `${g}-${e}-${W}`;
1509
- return localStorage.getItem(c) && localStorage.removeItem(c), je(() => {
1510
- $e(e, b), t.getState().initializeShadowState(e, b);
1511
- const x = t.getState().getShadowMetadata(e, []);
1512
- x && x?.components?.forEach((Y) => {
1513
- Y.forceUpdate();
1513
+ updateInitialState: (w) => {
1514
+ y.clear(), I++;
1515
+ const i = _t(
1516
+ t,
1517
+ o,
1518
+ h,
1519
+ S
1520
+ ), g = e.getState().initialStateGlobal[t], M = nt(t), z = at(M?.localStorage?.key) ? M?.localStorage?.key(g) : M?.localStorage?.key, c = `${S}-${t}-${z}`;
1521
+ return localStorage.getItem(c) && localStorage.removeItem(c), Rt(() => {
1522
+ Dt(t, w), e.getState().initializeShadowState(t, w);
1523
+ const B = e.getState().getShadowMetadata(t, []);
1524
+ B && B?.components?.forEach((J) => {
1525
+ J.forceUpdate();
1514
1526
  });
1515
1527
  }), {
1516
- fetchId: (x) => o.get()[x]
1528
+ fetchId: (B) => i.get()[B]
1517
1529
  };
1518
1530
  }
1519
1531
  };
1520
- return l({
1521
- currentState: t.getState().getShadowValue(e, []),
1522
- componentId: f,
1532
+ return u({
1533
+ currentState: e.getState().getShadowValue(t, []),
1534
+ componentId: h,
1523
1535
  path: []
1524
1536
  });
1525
1537
  }
1526
- function Ae(e) {
1527
- return le(ze, { proxy: e });
1538
+ function bt(t) {
1539
+ return lt(qt, { proxy: t });
1528
1540
  }
1529
- function Ge({
1530
- proxy: e,
1531
- rebuildStateShape: i
1541
+ function Bt({
1542
+ proxy: t,
1543
+ rebuildStateShape: o
1532
1544
  }) {
1533
- const f = G(null), g = G(`map-${crypto.randomUUID()}`), h = G(!1), p = G(/* @__PURE__ */ new Map());
1534
- Q(() => {
1535
- const l = f.current;
1536
- if (!l || h.current) return;
1537
- const u = setTimeout(() => {
1538
- const A = t.getState().getShadowMetadata(e._stateKey, e._path) || {}, b = A.mapWrappers || [];
1539
- b.push({
1540
- instanceId: g.current,
1541
- mapFn: e._mapFn,
1542
- containerRef: l,
1543
- rebuildStateShape: i,
1544
- path: e._path,
1545
- componentId: g.current,
1546
- meta: e._meta
1547
- }), t.getState().setShadowMetadata(e._stateKey, e._path, {
1545
+ const h = q(null), S = q(`map-${crypto.randomUUID()}`), y = q(!1), I = q(/* @__PURE__ */ new Map());
1546
+ Z(() => {
1547
+ const u = h.current;
1548
+ if (!u || y.current) return;
1549
+ const f = setTimeout(() => {
1550
+ const A = e.getState().getShadowMetadata(t._stateKey, t._path) || {}, w = A.mapWrappers || [];
1551
+ w.push({
1552
+ instanceId: S.current,
1553
+ mapFn: t._mapFn,
1554
+ containerRef: u,
1555
+ rebuildStateShape: o,
1556
+ path: t._path,
1557
+ componentId: S.current,
1558
+ meta: t._meta
1559
+ }), e.getState().setShadowMetadata(t._stateKey, t._path, {
1548
1560
  ...A,
1549
- mapWrappers: b
1550
- }), h.current = !0, M();
1561
+ mapWrappers: w
1562
+ }), y.current = !0, V();
1551
1563
  }, 0);
1552
1564
  return () => {
1553
- if (clearTimeout(u), g.current) {
1554
- const A = t.getState().getShadowMetadata(e._stateKey, e._path) || {};
1565
+ if (clearTimeout(f), S.current) {
1566
+ const A = e.getState().getShadowMetadata(t._stateKey, t._path) || {};
1555
1567
  A.mapWrappers && (A.mapWrappers = A.mapWrappers.filter(
1556
- (b) => b.instanceId !== g.current
1557
- ), t.getState().setShadowMetadata(e._stateKey, e._path, A));
1568
+ (w) => w.instanceId !== S.current
1569
+ ), e.getState().setShadowMetadata(t._stateKey, t._path, A));
1558
1570
  }
1559
- p.current.forEach((A) => A.unmount());
1571
+ I.current.forEach((A) => A.unmount());
1560
1572
  };
1561
1573
  }, []);
1562
- const M = () => {
1563
- const l = f.current;
1564
- if (!l) return;
1565
- const u = t.getState().getShadowValue(
1566
- [e._stateKey, ...e._path].join("."),
1567
- e._meta?.validIds
1574
+ const V = () => {
1575
+ const u = h.current;
1576
+ if (!u) return;
1577
+ const f = e.getState().getShadowValue(
1578
+ [t._stateKey, ...t._path].join("."),
1579
+ t._meta?.validIds
1568
1580
  );
1569
- if (!Array.isArray(u)) return;
1570
- const A = e._meta?.validIds ?? t.getState().getShadowMetadata(e._stateKey, e._path)?.arrayKeys ?? [], b = i({
1571
- currentState: u,
1572
- path: e._path,
1573
- componentId: g.current,
1574
- meta: e._meta
1581
+ if (!Array.isArray(f)) return;
1582
+ const A = t._meta?.validIds ?? e.getState().getShadowMetadata(t._stateKey, t._path)?.arrayKeys ?? [], w = o({
1583
+ currentState: f,
1584
+ path: t._path,
1585
+ componentId: S.current,
1586
+ meta: t._meta
1575
1587
  });
1576
- u.forEach((o, S) => {
1577
- const T = A[S];
1578
- if (!T) return;
1579
- const W = te(), c = document.createElement("div");
1580
- c.setAttribute("data-item-path", T), l.appendChild(c);
1581
- const x = Ce(c);
1582
- p.current.set(T, x);
1583
- const Y = T.split(".").slice(1);
1584
- x.render(
1585
- le(Me, {
1586
- stateKey: e._stateKey,
1587
- itemComponentId: W,
1588
- itemPath: Y,
1589
- localIndex: S,
1590
- arraySetter: b,
1591
- rebuildStateShape: i,
1592
- renderFn: e._mapFn
1588
+ f.forEach((i, g) => {
1589
+ const M = A[g];
1590
+ if (!M) return;
1591
+ const z = rt(), c = document.createElement("div");
1592
+ c.setAttribute("data-item-path", M), u.appendChild(c);
1593
+ const B = At(c);
1594
+ I.current.set(M, B);
1595
+ const J = M.split(".").slice(1);
1596
+ B.render(
1597
+ lt(Mt, {
1598
+ stateKey: t._stateKey,
1599
+ itemComponentId: z,
1600
+ itemPath: J,
1601
+ localIndex: g,
1602
+ arraySetter: w,
1603
+ rebuildStateShape: o,
1604
+ renderFn: t._mapFn
1593
1605
  })
1594
1606
  );
1595
1607
  });
1596
1608
  };
1597
- return /* @__PURE__ */ re("div", { ref: f, "data-map-container": g.current });
1609
+ return /* @__PURE__ */ ot("div", { ref: h, "data-map-container": S.current });
1598
1610
  }
1599
- function ze({
1600
- proxy: e
1611
+ function qt({
1612
+ proxy: t
1601
1613
  }) {
1602
- const i = G(null), f = G(null), g = G(!1), h = `${e._stateKey}-${e._path.join(".")}`, p = t.getState().getShadowValue(
1603
- [e._stateKey, ...e._path].join("."),
1604
- e._meta?.validIds
1614
+ const o = q(null), h = q(null), S = q(!1), y = `${t._stateKey}-${t._path.join(".")}`, I = e.getState().getShadowValue(
1615
+ [t._stateKey, ...t._path].join("."),
1616
+ t._meta?.validIds
1605
1617
  );
1606
- return Q(() => {
1607
- const M = i.current;
1608
- if (!M || g.current) return;
1609
- const l = setTimeout(() => {
1610
- if (!M.parentElement) {
1611
- console.warn("Parent element not found for signal", h);
1618
+ return Z(() => {
1619
+ const V = o.current;
1620
+ if (!V || S.current) return;
1621
+ const u = setTimeout(() => {
1622
+ if (!V.parentElement) {
1623
+ console.warn("Parent element not found for signal", y);
1612
1624
  return;
1613
1625
  }
1614
- const u = M.parentElement, b = Array.from(u.childNodes).indexOf(M);
1615
- let o = u.getAttribute("data-parent-id");
1616
- o || (o = `parent-${crypto.randomUUID()}`, u.setAttribute("data-parent-id", o)), f.current = `instance-${crypto.randomUUID()}`;
1617
- const S = t.getState().getShadowMetadata(e._stateKey, e._path) || {}, T = S.signals || [];
1618
- T.push({
1619
- instanceId: f.current,
1620
- parentId: o,
1621
- position: b,
1622
- effect: e._effect
1623
- }), t.getState().setShadowMetadata(e._stateKey, e._path, {
1624
- ...S,
1625
- signals: T
1626
+ const f = V.parentElement, w = Array.from(f.childNodes).indexOf(V);
1627
+ let i = f.getAttribute("data-parent-id");
1628
+ i || (i = `parent-${crypto.randomUUID()}`, f.setAttribute("data-parent-id", i)), h.current = `instance-${crypto.randomUUID()}`;
1629
+ const g = e.getState().getShadowMetadata(t._stateKey, t._path) || {}, M = g.signals || [];
1630
+ M.push({
1631
+ instanceId: h.current,
1632
+ parentId: i,
1633
+ position: w,
1634
+ effect: t._effect
1635
+ }), e.getState().setShadowMetadata(t._stateKey, t._path, {
1636
+ ...g,
1637
+ signals: M
1626
1638
  });
1627
- let W = p;
1628
- if (e._effect)
1639
+ let z = I;
1640
+ if (t._effect)
1629
1641
  try {
1630
- W = new Function(
1642
+ z = new Function(
1631
1643
  "state",
1632
- `return (${e._effect})(state)`
1633
- )(p);
1634
- } catch (x) {
1635
- console.error("Error evaluating effect function:", x);
1644
+ `return (${t._effect})(state)`
1645
+ )(I);
1646
+ } catch (B) {
1647
+ console.error("Error evaluating effect function:", B);
1636
1648
  }
1637
- W !== null && typeof W == "object" && (W = JSON.stringify(W));
1638
- const c = document.createTextNode(String(W ?? ""));
1639
- M.replaceWith(c), g.current = !0;
1649
+ z !== null && typeof z == "object" && (z = JSON.stringify(z));
1650
+ const c = document.createTextNode(String(z ?? ""));
1651
+ V.replaceWith(c), S.current = !0;
1640
1652
  }, 0);
1641
1653
  return () => {
1642
- if (clearTimeout(l), f.current) {
1643
- const u = t.getState().getShadowMetadata(e._stateKey, e._path) || {};
1644
- u.signals && (u.signals = u.signals.filter(
1645
- (A) => A.instanceId !== f.current
1646
- ), t.getState().setShadowMetadata(e._stateKey, e._path, u));
1654
+ if (clearTimeout(u), h.current) {
1655
+ const f = e.getState().getShadowMetadata(t._stateKey, t._path) || {};
1656
+ f.signals && (f.signals = f.signals.filter(
1657
+ (A) => A.instanceId !== h.current
1658
+ ), e.getState().setShadowMetadata(t._stateKey, t._path, f));
1647
1659
  }
1648
1660
  };
1649
- }, []), le("span", {
1650
- ref: i,
1661
+ }, []), lt("span", {
1662
+ ref: o,
1651
1663
  style: { display: "contents" },
1652
- "data-signal-id": h
1664
+ "data-signal-id": y
1653
1665
  });
1654
1666
  }
1655
- const Me = Re(
1656
- Ye,
1657
- (e, i) => e.itemPath.join(".") === i.itemPath.join(".") && e.stateKey === i.stateKey && e.itemComponentId === i.itemComponentId && e.localIndex === i.localIndex
1658
- ), Je = (e) => {
1659
- const [i, f] = X(!1);
1660
- return de(() => {
1661
- if (!e.current) {
1662
- f(!0);
1667
+ const Mt = Ot(
1668
+ Jt,
1669
+ (t, o) => t.itemPath.join(".") === o.itemPath.join(".") && t.stateKey === o.stateKey && t.itemComponentId === o.itemComponentId && t.localIndex === o.localIndex
1670
+ ), Gt = (t) => {
1671
+ const [o, h] = K(!1);
1672
+ return dt(() => {
1673
+ if (!t.current) {
1674
+ h(!0);
1663
1675
  return;
1664
1676
  }
1665
- const g = Array.from(e.current.querySelectorAll("img"));
1666
- if (g.length === 0) {
1667
- f(!0);
1677
+ const S = Array.from(t.current.querySelectorAll("img"));
1678
+ if (S.length === 0) {
1679
+ h(!0);
1668
1680
  return;
1669
1681
  }
1670
- let h = 0;
1671
- const p = () => {
1672
- h++, h === g.length && f(!0);
1682
+ let y = 0;
1683
+ const I = () => {
1684
+ y++, y === S.length && h(!0);
1673
1685
  };
1674
- return g.forEach((M) => {
1675
- M.complete ? p() : (M.addEventListener("load", p), M.addEventListener("error", p));
1686
+ return S.forEach((V) => {
1687
+ V.complete ? I() : (V.addEventListener("load", I), V.addEventListener("error", I));
1676
1688
  }), () => {
1677
- g.forEach((M) => {
1678
- M.removeEventListener("load", p), M.removeEventListener("error", p);
1689
+ S.forEach((V) => {
1690
+ V.removeEventListener("load", I), V.removeEventListener("error", I);
1679
1691
  });
1680
1692
  };
1681
- }, [e.current]), i;
1693
+ }, [t.current]), o;
1682
1694
  };
1683
- function Ye({
1684
- stateKey: e,
1685
- itemComponentId: i,
1686
- itemPath: f,
1687
- localIndex: g,
1688
- arraySetter: h,
1689
- rebuildStateShape: p,
1690
- renderFn: M
1695
+ function Jt({
1696
+ stateKey: t,
1697
+ itemComponentId: o,
1698
+ itemPath: h,
1699
+ localIndex: S,
1700
+ arraySetter: y,
1701
+ rebuildStateShape: I,
1702
+ renderFn: V
1691
1703
  }) {
1692
- const [, l] = X({}), { ref: u, inView: A } = Le(), b = G(null), o = Je(b), S = G(!1), T = [e, ...f].join(".");
1693
- Ue(e, i, l);
1694
- const W = ce(
1695
- (U) => {
1696
- b.current = U, u(U);
1704
+ const [, u] = K({}), { ref: f, inView: A } = Lt(), w = q(null), i = Gt(w), g = q(!1), M = [t, ...h].join(".");
1705
+ $t(t, o, u);
1706
+ const z = ct(
1707
+ (W) => {
1708
+ w.current = W, f(W);
1697
1709
  },
1698
- [u]
1710
+ [f]
1699
1711
  );
1700
- Q(() => {
1701
- t.getState().subscribeToPath(T, (U) => {
1702
- l({});
1712
+ Z(() => {
1713
+ e.getState().subscribeToPath(M, (W) => {
1714
+ u({});
1703
1715
  });
1704
- }, []), Q(() => {
1705
- if (!A || !o || S.current)
1716
+ }, []), Z(() => {
1717
+ if (!A || !i || g.current)
1706
1718
  return;
1707
- const U = b.current;
1708
- if (U && U.offsetHeight > 0) {
1709
- S.current = !0;
1710
- const v = U.offsetHeight;
1711
- t.getState().setShadowMetadata(e, f, {
1719
+ const W = w.current;
1720
+ if (W && W.offsetHeight > 0) {
1721
+ g.current = !0;
1722
+ const m = W.offsetHeight;
1723
+ e.getState().setShadowMetadata(t, h, {
1712
1724
  virtualizer: {
1713
- itemHeight: v,
1714
- domRef: U
1725
+ itemHeight: m,
1726
+ domRef: W
1715
1727
  }
1716
1728
  });
1717
- const K = f.slice(0, -1), oe = [e, ...K].join(".");
1718
- t.getState().notifyPathSubscribers(oe, {
1729
+ const Y = h.slice(0, -1), Q = [t, ...Y].join(".");
1730
+ e.getState().notifyPathSubscribers(Q, {
1719
1731
  type: "ITEMHEIGHT",
1720
- itemKey: f.join("."),
1721
- ref: b.current
1732
+ itemKey: h.join("."),
1733
+ ref: w.current
1722
1734
  });
1723
1735
  }
1724
- }, [A, o, e, f]);
1725
- const c = [e, ...f].join("."), x = t.getState().getShadowValue(c);
1726
- if (x === void 0)
1736
+ }, [A, i, t, h]);
1737
+ const c = [t, ...h].join("."), B = e.getState().getShadowValue(c);
1738
+ if (B === void 0)
1727
1739
  return null;
1728
- const Y = p({
1729
- currentState: x,
1730
- path: f,
1731
- componentId: i
1732
- }), J = M(Y, g, h);
1733
- return /* @__PURE__ */ re("div", { ref: W, children: J });
1740
+ const J = I({
1741
+ currentState: B,
1742
+ path: h,
1743
+ componentId: o
1744
+ }), H = V(J, S, y);
1745
+ return /* @__PURE__ */ ot("div", { ref: z, children: H });
1734
1746
  }
1735
- function Ze({
1736
- stateKey: e,
1737
- path: i,
1738
- rebuildStateShape: f,
1739
- renderFn: g,
1740
- formOpts: h,
1741
- setState: p
1747
+ function Yt({
1748
+ stateKey: t,
1749
+ path: o,
1750
+ rebuildStateShape: h,
1751
+ renderFn: S,
1752
+ formOpts: y,
1753
+ setState: I
1742
1754
  }) {
1743
- const [M] = X(() => te()), [, l] = X({}), u = [e, ...i].join(".");
1744
- Ue(e, M, l);
1745
- const A = t.getState().getShadowValue(u), [b, o] = X(A), S = G(!1), T = G(null);
1746
- Q(() => {
1747
- !S.current && !se(A, b) && o(A);
1748
- }, [A]), Q(() => {
1749
- const J = t.getState().subscribeToPath(u, (U) => {
1750
- l({});
1755
+ const [V] = K(() => rt()), [, u] = K({}), f = [t, ...o].join(".");
1756
+ $t(t, V, u);
1757
+ const A = e.getState().getShadowValue(f), [w, i] = K(A), g = q(!1), M = q(null);
1758
+ Z(() => {
1759
+ !g.current && !st(A, w) && i(A);
1760
+ }, [A]), Z(() => {
1761
+ const H = e.getState().subscribeToPath(f, (W) => {
1762
+ !g.current && w !== W && u({});
1751
1763
  });
1752
1764
  return () => {
1753
- J(), T.current && (clearTimeout(T.current), S.current = !1);
1765
+ H(), M.current && (clearTimeout(M.current), g.current = !1);
1754
1766
  };
1755
1767
  }, []);
1756
- const W = ce(
1757
- (J) => {
1758
- o(J), S.current = !0, T.current && clearTimeout(T.current);
1759
- const U = h?.debounceTime ?? 200;
1760
- T.current = setTimeout(() => {
1761
- S.current = !1, p(J, i, { updateType: "update" });
1762
- }, U);
1768
+ const z = ct(
1769
+ (H) => {
1770
+ typeof A === "number" && typeof H == "string" && (H = H === "" ? 0 : Number(H)), i(H), g.current = !0, M.current && clearTimeout(M.current);
1771
+ const m = y?.debounceTime ?? 200;
1772
+ M.current = setTimeout(() => {
1773
+ g.current = !1, I(H, o, { updateType: "update" });
1774
+ const { getInitialOptions: Y, setShadowMetadata: Q, getShadowMetadata: X } = e.getState(), s = Y(t)?.validation, n = s?.zodSchemaV4 || s?.zodSchemaV3;
1775
+ if (n) {
1776
+ const r = e.getState().getShadowValue(t), a = n.safeParse(r), d = X(t, o) || {};
1777
+ if (a.success)
1778
+ Q(t, o, {
1779
+ ...d,
1780
+ validation: {
1781
+ status: "VALID_LIVE",
1782
+ validatedValue: H
1783
+ }
1784
+ });
1785
+ else {
1786
+ const p = ("issues" in a.error ? a.error.issues : a.error.errors).filter(
1787
+ (E) => JSON.stringify(E.path) === JSON.stringify(o)
1788
+ );
1789
+ p.length > 0 ? Q(t, o, {
1790
+ ...d,
1791
+ validation: {
1792
+ status: "INVALID_LIVE",
1793
+ message: p[0]?.message,
1794
+ validatedValue: H
1795
+ }
1796
+ }) : Q(t, o, {
1797
+ ...d,
1798
+ validation: {
1799
+ status: "VALID_LIVE",
1800
+ validatedValue: H
1801
+ }
1802
+ });
1803
+ }
1804
+ }
1805
+ }, m), u({});
1763
1806
  },
1764
- [p, i, h?.debounceTime]
1765
- ), c = ce(() => {
1766
- T.current && (clearTimeout(T.current), S.current = !1, p(b, i, { updateType: "update" }));
1767
- }, [p, i, b]), x = f({
1807
+ [I, o, y?.debounceTime, t]
1808
+ ), c = ct(async () => {
1809
+ console.log("handleBlur triggered"), M.current && (clearTimeout(M.current), M.current = null, g.current = !1, I(w, o, { updateType: "update" }));
1810
+ const { getInitialOptions: H } = e.getState(), W = H(t)?.validation, m = W?.zodSchemaV4 || W?.zodSchemaV3;
1811
+ if (!m) return;
1812
+ const Y = e.getState().getShadowMetadata(t, o);
1813
+ e.getState().setShadowMetadata(t, o, {
1814
+ ...Y,
1815
+ validation: {
1816
+ status: "DIRTY",
1817
+ validatedValue: w
1818
+ }
1819
+ });
1820
+ const Q = e.getState().getShadowValue(t), X = m.safeParse(Q);
1821
+ if (console.log("result ", X), X.success)
1822
+ e.getState().setShadowMetadata(t, o, {
1823
+ ...Y,
1824
+ validation: {
1825
+ status: "VALID_PENDING_SYNC",
1826
+ validatedValue: w
1827
+ }
1828
+ });
1829
+ else {
1830
+ const s = "issues" in X.error ? X.error.issues : X.error.errors;
1831
+ console.log("All validation errors:", s), console.log("Current blur path:", o);
1832
+ const n = s.filter((r) => {
1833
+ if (console.log("Processing error:", r), o.some((d) => d.startsWith("id:"))) {
1834
+ console.log("Detected array path with ULID");
1835
+ const d = o[0].startsWith("id:") ? [] : o.slice(0, -1);
1836
+ console.log("Parent path:", d);
1837
+ const l = e.getState().getShadowMetadata(t, d);
1838
+ if (console.log("Array metadata:", l), l?.arrayKeys) {
1839
+ const p = [t, ...o.slice(0, -1)].join("."), E = l.arrayKeys.indexOf(p);
1840
+ console.log("Item key:", p, "Index:", E);
1841
+ const D = [...d, E, ...o.slice(-1)], U = JSON.stringify(r.path) === JSON.stringify(D);
1842
+ return console.log("Zod path comparison:", {
1843
+ zodPath: D,
1844
+ errorPath: r.path,
1845
+ match: U
1846
+ }), U;
1847
+ }
1848
+ }
1849
+ const a = JSON.stringify(r.path) === JSON.stringify(o);
1850
+ return console.log("Direct path comparison:", {
1851
+ errorPath: r.path,
1852
+ currentPath: o,
1853
+ match: a
1854
+ }), a;
1855
+ });
1856
+ console.log("Filtered path errors:", n), e.getState().setShadowMetadata(t, o, {
1857
+ ...Y,
1858
+ validation: {
1859
+ status: "VALIDATION_FAILED",
1860
+ message: n[0]?.message,
1861
+ validatedValue: w
1862
+ }
1863
+ });
1864
+ }
1865
+ u({});
1866
+ }, [t, o, w, I]), B = h({
1768
1867
  currentState: A,
1769
- path: i,
1770
- componentId: M
1771
- }), Y = new Proxy(x, {
1772
- get(J, U) {
1773
- return U === "inputProps" ? {
1774
- value: b ?? "",
1775
- onChange: (v) => {
1776
- W(v.target.value);
1868
+ path: o,
1869
+ componentId: V
1870
+ }), J = new Proxy(B, {
1871
+ get(H, W) {
1872
+ return W === "inputProps" ? {
1873
+ value: w ?? "",
1874
+ onChange: (m) => {
1875
+ z(m.target.value);
1777
1876
  },
1877
+ // 5. Wire the new onBlur handler to the input props.
1778
1878
  onBlur: c,
1779
- ref: pe.getState().getFormRef(e + "." + i.join("."))
1780
- } : J[U];
1879
+ ref: vt.getState().getFormRef(t + "." + o.join("."))
1880
+ } : H[W];
1781
1881
  }
1782
1882
  });
1783
- return /* @__PURE__ */ re(ke, { children: g(Y) });
1883
+ return /* @__PURE__ */ ot(kt, { formOpts: y, path: o, stateKey: t, children: S(J) });
1784
1884
  }
1785
- function Ue(e, i, f) {
1786
- const g = `${e}////${i}`;
1787
- de(() => {
1788
- const h = t.getState().getShadowMetadata(e, []), p = h?.components || /* @__PURE__ */ new Map();
1789
- return p.set(g, {
1790
- forceUpdate: () => f({}),
1885
+ function $t(t, o, h) {
1886
+ const S = `${t}////${o}`;
1887
+ dt(() => {
1888
+ const { registerComponent: y, unregisterComponent: I } = e.getState();
1889
+ return y(t, S, {
1890
+ forceUpdate: () => h({}),
1791
1891
  paths: /* @__PURE__ */ new Set(),
1792
1892
  reactiveType: ["component"]
1793
- }), t.getState().setShadowMetadata(e, [], {
1794
- ...h,
1795
- components: p
1796
1893
  }), () => {
1797
- const M = t.getState().getShadowMetadata(e, []);
1798
- M?.components && M.components.delete(g);
1894
+ I(t, S);
1799
1895
  };
1800
- }, [e, g]);
1896
+ }, [t, S]);
1801
1897
  }
1802
1898
  export {
1803
- Ae as $cogsSignal,
1804
- it as addStateOptions,
1805
- ct as createCogsState,
1806
- lt as notifyComponent,
1807
- Be as useCogsStateFn
1899
+ bt as $cogsSignal,
1900
+ se as addStateOptions,
1901
+ ie as createCogsState,
1902
+ le as notifyComponent,
1903
+ xt as useCogsStateFn
1808
1904
  };
1809
1905
  //# sourceMappingURL=CogsState.jsx.map