cogsbox-state 0.5.475-canary.1 → 0.5.475-canary.2

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