cogsbox-state 0.5.460 → 0.5.462

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