cogsbox-state 0.5.483 → 0.5.485

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