cogsbox-state 0.5.459 → 0.5.461

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