cogsbox-state 0.5.462 → 0.5.463

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