cogsbox-state 0.5.475-canary.6 → 0.5.475-canary.9

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