cogsbox-state 0.5.476 → 0.5.479

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