cogsbox-state 0.5.435 → 0.5.437

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