cogsbox-state 0.5.463 → 0.5.464

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