cogsbox-state 0.5.468 → 0.5.470

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