cogsbox-state 0.5.476 → 0.5.478

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