cogsbox-state 0.5.434 → 0.5.436

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