cogsbox-state 0.5.459 → 0.5.460

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