cogsbox-state 0.5.475-canary.0 → 0.5.475-canary.10

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