cogsbox-state 0.5.488 → 0.5.490

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