cogsbox-state 0.5.487 → 0.5.488

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