cogsbox-state 0.5.489 → 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,460 +1,422 @@
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,
17
- getShadowValue: _,
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 ?? [] };
13
+ getInitialOptions: _,
14
+ updateInitialStateGlobal: pe,
15
+ getShadowMetadata: $,
16
+ setShadowMetadata: ee,
17
+ getShadowValue: O,
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 S = a[s];
48
- if (S)
49
- return { key: S, index: s, value: e[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];
45
+ if (f)
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) || {}, S = l[e] || {};
66
- let u = { ...S, ...s }, h = !1;
67
- if (a) {
68
- const y = (t, o) => {
62
+ const a = _(e) || {}, f = c[e] || {};
63
+ let l = { ...f, ...a }, h = !1;
64
+ if (r) {
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]) || (y(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 = y(u, a);
70
+ l = m(l, r);
74
71
  }
75
- if (u.validation && (a?.validation?.hasOwnProperty("onBlur") || s?.validation?.hasOwnProperty("onBlur") || S?.validation?.hasOwnProperty("onBlur") || (u.validation.onBlur = "error", h = !0)), h) {
76
- ye(e, u);
77
- const y = u.validation?.zodSchemaV4, t = u.validation?.zodSchemaV3;
78
- (y !== s?.validation?.zodSchemaV4 || t !== s?.validation?.zodSchemaV3) && (y || t) && (y ? H(e, y, "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 }, [S, u] = ke(s);
89
- Object.keys(S).forEach((o) => {
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);
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);
104
- }), Object.keys(S).forEach((o) => {
105
- K(o, S[o]);
100
+ Object.keys(E).length > 0 && fe(o, E);
101
+ }), Object.keys(f).forEach((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, []) || S[o], O = 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, O), () => {
129
- L.getState().unregisterStateHandler(o);
130
- }), [o, O]), re(() => {
131
- const D = U(o)?.validation;
132
- D?.zodSchemaV4 ? H(o, D.zodSchemaV4, "zod4") : D?.zodSchemaV3 && H(o, D.zodSchemaV3, "zod3");
133
- }), O;
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
- function y(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);
130
+ function m(o, d) {
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(S).forEach(($) => {
147
- y($, o);
141
+ Object.keys(f).forEach((P) => {
142
+ m(P, o);
148
143
  });
149
144
  }
150
145
  return {
151
146
  useCogsState: h,
152
- setCogsOptionsByKey: y,
147
+ setCogsOptionsByKey: m,
153
148
  setCogsOptions: t
154
149
  };
155
- }, Ze = (e, a, l, s, S) => {
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}`;
165
- let y;
157
+ const l = W(c?.localStorage?.key) ? c.localStorage?.key(e) : c?.localStorage?.key;
158
+ if (l && a) {
159
+ const h = `${a}-${r}-${l}`;
160
+ let m;
166
161
  try {
167
- y = 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: y,
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 = _(e, []), { sessionId: s } = Me(), S = W(a?.localStorage?.key) ? a.localStorage.key(l) : a?.localStorage?.key;
199
- if (S && s) {
200
- const u = ce(
201
- `${s}-${e}-${S}`
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 S = E(e, a);
219
- if (B(e, a, {
220
- ...S,
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, y) => {
227
- const t = [...a, h], o = l[y];
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], y = l[u];
237
- oe(e, h, y, 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: S }) => {
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 y = a;
253
- if (S && 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
- y = new Function("state", `return (${S})(state)`)(
256
- a
226
+ m = new Function("state", `return (${f})(state)`)(
227
+ r
257
228
  );
258
229
  } catch (t) {
259
230
  console.error("Error evaluating effect function:", t);
260
231
  }
261
- y !== null && typeof y == "object" && (y = JSON.stringify(y)), h[s].textContent = String(y ?? "");
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
- const S = /* @__PURE__ */ new Set();
269
- if (l.type === "update") {
270
- let u = [...a];
239
+ const f = /* @__PURE__ */ new Set();
240
+ if (c.type === "update") {
241
+ let l = [...r];
271
242
  for (; ; ) {
272
- const h = E(e, u);
273
- if (h?.pathComponents && h.pathComponents.forEach((y) => {
274
- const t = s.components?.get(y);
275
- t && ((Array.isArray(t.reactiveType) ? t.reactiveType : [t.reactiveType || "component"]).includes("none") || S.add(t));
276
- }), u.length === 0) break;
277
- u.pop();
243
+ const h = $(e, l);
244
+ if (h?.pathComponents && h.pathComponents.forEach((m) => {
245
+ const t = a.components?.get(m);
246
+ t && ((Array.isArray(t.reactiveType) ? t.reactiveType : [t.reactiveType || "component"]).includes("none") || f.add(t));
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((y) => {
280
- const t = y.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") || S.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 y = E(e, h);
290
- if (y?.pathComponents && y.pathComponents.forEach((t) => {
291
- const o = s.components?.get(t);
292
- o && S.add(o);
260
+ const m = $(e, h);
261
+ if (m?.pathComponents && m.pathComponents.forEach((t) => {
262
+ const o = a.components?.get(t);
263
+ o && f.add(o);
293
264
  }), h.length === 0) break;
294
265
  h.pop();
295
266
  }
296
267
  }
297
- return S;
268
+ return f;
298
269
  }
299
- function rt(e, a, l) {
300
- const s = M.getState().getShadowValue(e, a), S = W(l) ? l(s) : l;
301
- if (X(s, S))
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, S), 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,
308
- newValue: S,
309
- shadowMeta: u
278
+ oldValue: a,
279
+ newValue: f,
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, S) {
323
- let u;
324
- if (W(l)) {
325
- const { value: o } = j(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
- S
335
- );
336
- ie(e, a, { bubble: !0 });
337
- const y = E(e, a);
305
+ f
306
+ ), m = $(e, r);
338
307
  let t;
339
- return y?.arrayKeys && s !== void 0 && s > 0 && (t = y.arrayKeys[s - 1]), {
308
+ return m?.arrayKeys && a !== void 0 && a > 0 && (t = m.arrayKeys[a - 1]), {
340
309
  type: "insert",
341
- newValue: u,
342
- shadowMeta: y,
343
- path: a,
310
+ newValue: l,
311
+ shadowMeta: m,
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 = _(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 S = s, u = S.type === "cut" ? null : S.newValue;
360
- S.shadowMeta?.signals?.length > 0 && a.push({ shadowMeta: S.shadowMeta, displayValue: u }), tt(
361
- S.stateKey,
362
- S.path,
363
- S
364
- ).forEach((y) => {
365
- e.add(y);
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(
330
+ f.stateKey,
331
+ f.path,
332
+ f
333
+ ).forEach((m) => {
334
+ e.add(m);
366
335
  });
367
336
  }
368
- l.length > 0 && xe(l), a.forEach(({ shadowMeta: s, displayValue: S }) => {
369
- et(s, S);
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 (S, u, h) => {
376
- s(e, u, S, h);
343
+ function rt(e, r, c) {
344
+ return (f, l, h) => {
345
+ a(e, l, f, h);
377
346
  };
378
- function s(S, u, h, y) {
347
+ function a(f, l, h, m) {
379
348
  let t;
380
- switch (y.updateType) {
349
+ switch (m.updateType) {
381
350
  case "update":
382
- t = rt(S, u, h);
351
+ t = Xe(f, l, h);
383
352
  break;
384
353
  case "insert":
385
- t = at(
386
- S,
387
- u,
354
+ t = et(
355
+ f,
356
+ l,
388
357
  h,
389
- y.index,
390
- y.itemId
358
+ m.index,
359
+ m.itemId
391
360
  );
392
361
  break;
393
362
  case "insert_many":
394
- t = nt(S, u, h);
363
+ t = Ke(f, l, h);
395
364
  break;
396
365
  case "cut":
397
- t = ot(S, u);
366
+ t = tt(f, l);
398
367
  break;
399
368
  }
400
369
  if (t === null)
401
370
  return;
402
- t.stateKey = S, 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
- stateKey: S,
406
- path: u,
407
- updateType: y.updateType,
374
+ stateKey: f,
375
+ path: l,
376
+ updateType: m.updateType,
408
377
  status: "new",
409
378
  oldValue: t.oldValue,
410
379
  newValue: t.newValue ?? null,
411
380
  itemId: t.itemId,
412
381
  insertAfterId: t.insertAfterId,
413
- metaData: y.metaData
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
- S,
418
- l.current,
419
- a
420
- ), l.current?.middleware && l.current.middleware({ update: o }), ze(o, y.validationTrigger || "programmatic"), Ye(o);
386
+ f,
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,
427
- reactiveDeps: S,
428
- reactiveType: u,
392
+ function at(e, {
393
+ stateKey: r,
394
+ localStorage: c,
395
+ formElements: a,
396
+ reactiveDeps: f,
397
+ reactiveType: l,
429
398
  componentId: h,
430
- defaultState: y,
431
- dependencies: t,
432
- serverState: o
399
+ defaultState: m,
400
+ dependencies: t
433
401
  } = {}) {
434
- const [d, $] = ne({}), { sessionId: b } = Me();
435
- let P = !a;
436
- const [v] = ne(a ?? ae()), O = 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 || y || 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 f = W(r.localStorage.key) ? r.localStorage.key(n) : r.localStorage.key, c = ce(
451
- `${b}-${v}-${f}`
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 (c && c.lastUpdated > (r?.serverState?.timestamp || 0))
415
+ if (s)
454
416
  return {
455
- value: c.state,
417
+ value: s.state,
456
418
  source: "localStorage",
457
- timestamp: c.lastUpdated
419
+ timestamp: s.lastUpdated
458
420
  };
459
421
  }
460
422
  return {
@@ -463,388 +425,325 @@ function ct(e, {
463
425
  timestamp: Date.now()
464
426
  };
465
427
  },
466
- [v, y, 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, i = _(v, []), f = r.data;
477
- if (n && n.strategy === "append" && "key" in n && Array.isArray(i) && Array.isArray(f)) {
478
- const c = n.key;
479
- if (!c) {
480
- console.error(
481
- "CogsState: Merge strategy 'append' requires a 'key' field."
482
- );
483
- return;
484
- }
485
- const m = new Set(
486
- i.map((A) => A[c])
487
- ), w = f.filter(
488
- (A) => !m.has(A[c])
489
- );
490
- w.length > 0 && be(v, [], w);
491
- const p = _(v, []);
492
- oe(
493
- v,
494
- [],
495
- p,
496
- r.timestamp || Date.now()
497
- );
498
- } else
499
- K(v, f), oe(
500
- v,
501
- [],
502
- f,
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 || y !== void 0) {
518
- const m = C?.defaultState || y;
519
- C?.defaultState || ue(v, {
520
- defaultState: m
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: i, timestamp: f } = J();
524
- K(v, n);
525
- const c = U(v)?.validation;
526
- c?.zodSchemaV4 ? H(v, c.zodSchemaV4, "zod4") : c?.zodSchemaV3 && H(v, c.zodSchemaV3, "zod3"), B(v, [], {
527
- stateSource: i,
528
- lastServerSync: i === "server" ? f : void 0,
529
- isDirty: i === "server" ? !1 : void 0,
530
- baseServerState: i === "server" ? n : void 0
531
- }), i === "server" && o && me(v, o), N(v);
532
- }, [v, ...t || []]), re(() => {
533
- P && ue(v, {
534
- formElements: s,
535
- defaultState: y,
536
- localStorage: l,
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,
449
+ defaultState: m,
450
+ localStorage: c,
537
451
  middleware: D.current?.middleware
538
452
  });
539
- const k = `${v}////${O.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
- depsFunction: S || void 0,
545
- deps: S ? S(_(v, [])) : [],
546
- prevDeps: S ? S(_(v, [])) : []
547
- }), B(v, [], {
548
- ...C,
549
- components: r
550
- }), $({}), () => {
551
- const n = E(v, []), i = n?.components?.get(k);
552
- i?.paths && i.paths.forEach((f) => {
553
- const m = f.split(".").slice(1), w = M.getState().getShadowMetadata(v, m);
554
- w?.pathComponents && w.pathComponents.size === 0 && (delete w.pathComponents, M.getState().setShadowMetadata(v, m, w));
555
- }), n?.components && B(v, [], n);
458
+ depsFunction: f || void 0,
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
- O.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 S = l?.transforms;
573
- if (!S || S.length === 0)
574
- return s;
575
- for (const u of S)
576
- if (u.type === "filter") {
484
+ const ot = (e, r, c) => {
485
+ let a = $(e, r)?.arrayKeys || [];
486
+ const f = c?.transforms;
487
+ if (!f || f.length === 0)
488
+ return a;
489
+ for (const l of f)
490
+ if (l.type === "filter") {
577
491
  const h = [];
578
- s.forEach((y, t) => {
579
- const o = _(e, [...a, y]);
580
- u.fn(o, t) && h.push(y);
581
- }), s = h;
582
- } else u.type === "sort" && s.sort((h, y) => {
583
- const t = _(e, [...a, h]), o = _(e, [...a, y]);
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, []), S = /* @__PURE__ */ new Set();
592
- s?.components && s.components.forEach((h, y) => {
593
- (Array.isArray(h.reactiveType) ? h.reactiveType : [h.reactiveType || "component"]).includes("all") && (h.forceUpdate(), S.add(y));
594
- }), E(e, [
595
- ...a,
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) => {
507
+ (Array.isArray(h.reactiveType) ? h.reactiveType : [h.reactiveType || "component"]).includes("all") && (h.forceUpdate(), f.add(m));
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 y = h + ".selected", t = E(e, y.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 j(e, a, l) {
609
- const s = E(e, a), S = a.length > 0 ? a.join(".") : "root", u = l?.arrayViews?.[S];
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 = _(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((S) => {
634
- S.domRef?.current && s.push(S.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));
561
+ });
562
+ }
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();
647
573
  });
648
574
  }
649
- function Te(e, a, l, s) {
650
- const S = /* @__PURE__ */ new Map();
651
- function u({
575
+ function Pe(e, r, c, a) {
576
+ const f = /* @__PURE__ */ new Map();
577
+ function l({
652
578
  path: t = [],
653
579
  meta: o,
654
580
  componentId: d
655
581
  }) {
656
- if (E(e, t)?.isRaw)
657
- return _(e, t);
658
- const b = o ? JSON.stringify(o.arrayViews || o.transforms) : "", P = t.join(".") + ":" + d + ":" + b;
659
- if (S.has(P))
660
- return S.get(P);
661
- const v = [e, ...t].join("."), O = () => {
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), _(e, t, n);
582
+ if ($(e, t)?.isRaw)
583
+ return O(e, t);
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 } = j(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 } = j(e, t, o);
691
- if (r !== null && typeof r == "object" && !Array.isArray(r) && Object.prototype.hasOwnProperty.call(r, g)) {
692
- const f = [...t, g];
693
- return u({
694
- path: f,
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 i = [...t, g];
700
- return u({
701
- path: i,
625
+ const s = [...t, u];
626
+ return l({
627
+ path: s,
702
628
  componentId: d,
703
629
  meta: o
704
630
  });
705
631
  }
706
- if (typeof g == "string" && g.startsWith("$")) {
707
- const r = g.slice(1), { value: n } = j(e, t, o), i = L.getState().registeredPlugins;
708
- for (const f of i) {
709
- const c = f.chainMethods?.[r];
710
- if (c && ut(t, c.pathPattern) && dt(n, c.target))
711
- return (...m) => {
712
- const w = L.getState(), p = w.pluginOptions.get(e)?.get(f.name), A = w.getHookResult(e, f.name);
713
- return c.handler(
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(
714
640
  {
715
641
  stateKey: e,
716
642
  path: t,
717
- pluginName: f.name,
718
- options: p,
719
- hookData: A,
720
- $get: () => j(e, t, o).value,
721
- $update: (V) => (a(V, t, {
643
+ pluginName: g.name,
644
+ options: A,
645
+ hookData: p,
646
+ $get: () => N(e, t, o).value,
647
+ $update: (M) => (r(M, t, {
722
648
  updateType: "update"
723
649
  }), {
724
- synced: () => {
725
- const T = M.getState().getShadowMetadata(e, t);
726
- B(e, t, {
727
- ...T,
728
- isDirty: !1,
729
- stateSource: "server",
730
- lastServerSync: Date.now()
731
- });
650
+ syned: () => {
732
651
  }
733
652
  }),
734
- $applyOperation: (V, T) => {
735
- a(V.newValue, V.path, {
736
- updateType: V.updateType,
737
- itemId: V.itemId,
653
+ $applyOperation: (M, T) => {
654
+ r(M.newValue, M.path, {
655
+ updateType: M.updateType,
656
+ itemId: M.itemId,
738
657
  metaData: T
739
658
  });
740
659
  },
741
- getFieldMetaData: () => he(e, t)?.get(f.name),
742
- setFieldMetaData: (V) => ve(e, t, f.name, V),
743
- removeFieldMetaData: () => we(e, t, f.name),
744
- getFieldRefs: () => Ee(e, t),
745
- getFieldElements: () => $e(e, t),
746
- 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)
747
677
  },
748
- ...m
678
+ ...y
749
679
  );
750
680
  };
751
681
  }
752
682
  }
753
- if (g === "$_rebuildStateShape")
754
- return u;
755
- if (g === "$sync" && t.length === 0)
756
- return async function() {
757
- const r = M.getState().getInitialOptions(e), n = r?.sync;
758
- if (!n)
759
- return console.error(`No mutation defined for state key "${e}"`), { success: !1, error: "No mutation defined" };
760
- const i = M.getState().getShadowValue(e, []), f = r?.validation?.key;
761
- try {
762
- const c = await n.action(i);
763
- if (c && !c.success && c.errors, c?.success) {
764
- const m = M.getState().getShadowMetadata(e, []);
765
- B(e, [], {
766
- ...m,
767
- isDirty: !1,
768
- lastServerSync: Date.now(),
769
- stateSource: "server",
770
- baseServerState: i
771
- // Update base server state
772
- }), n.onSuccess && n.onSuccess(c.data);
773
- } else !c?.success && n.onError && n.onError(c.error);
774
- return c;
775
- } catch (c) {
776
- return n.onError && n.onError(c), { success: !1, error: c };
777
- }
778
- };
779
- if (g === "$_status" || g === "$getStatus") {
780
- const r = () => {
781
- const { shadowMeta: n, value: i } = j(e, t, o);
782
- return n?.isDirty === !0 ? "dirty" : n?.stateSource === "server" || n?.isDirty === !1 ? "synced" : n?.stateSource === "localStorage" ? "restored" : n?.stateSource === "default" || i !== void 0 ? "fresh" : "unknown";
783
- };
784
- return g === "$_status" ? r() : r;
785
- }
786
- if (g === "$removeStorage")
683
+ if (u === "$_rebuildStateShape")
684
+ return l;
685
+ if (u === "$removeStorage")
787
686
  return () => {
788
- const r = M.getState().initialStateGlobal[e], n = U(e), i = W(n?.localStorage?.key) ? n.localStorage.key(r) : n?.localStorage?.key, f = s && i ? `${s}-${e}-${i}` : void 0;
789
- de(f);
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);
790
689
  };
791
- if (g === "$setRaw")
792
- return (r) => {
793
- const n = E(e, t) || {};
794
- 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" });
795
694
  };
796
- if (g === "$validate")
797
- return (r) => {
798
- const n = M.getState();
799
- if (r) {
800
- const w = r.map((p) => {
801
- const A = [...t, p], V = n.getShadowValue(e, A), T = n.getShadowMetadata(e, A) || {}, F = T.typeInfo?.schema;
802
- if (!F)
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)
803
702
  return {
804
- key: p,
805
- path: A,
703
+ key: A,
704
+ path: p,
806
705
  success: !0,
807
- data: V
706
+ data: M
808
707
  };
809
- const I = F.safeParse(V), R = I.error?.issues || I.error?.errors || [];
810
- return n.setShadowMetadata(e, A, {
708
+ const I = j.safeParse(M), L = I.error?.issues || I.error?.errors || [];
709
+ return i.setShadowMetadata(e, p, {
811
710
  ...T,
812
711
  validation: {
813
712
  status: I.success ? "VALID" : "INVALID",
814
713
  errors: I.success ? [] : [
815
714
  {
816
715
  source: "client",
817
- message: R[0]?.message || "Invalid value",
716
+ message: L[0]?.message || "Invalid value",
818
717
  severity: "error",
819
- code: R[0]?.code
718
+ code: L[0]?.code
820
719
  }
821
720
  ],
822
721
  lastValidated: Date.now(),
823
- validatedValue: V
722
+ validatedValue: M
824
723
  }
825
- }), n.notifyPathSubscribers([e, ...A].join("."), {
724
+ }), i.notifyPathSubscribers([e, ...p].join("."), {
826
725
  type: "VALIDATION_UPDATE"
827
726
  }), {
828
- key: p,
829
- path: A,
727
+ key: A,
728
+ path: p,
830
729
  success: I.success,
831
730
  data: I.success ? I.data : void 0,
832
731
  error: I.success ? void 0 : I.error
833
732
  };
834
733
  });
835
- return N(e), {
836
- success: w.every((p) => p.success),
837
- results: w
734
+ return F(e), {
735
+ success: v.every((A) => A.success),
736
+ results: v
838
737
  };
839
738
  }
840
- const { value: i } = j(e, t, o), f = n.getInitialOptions(e), c = f?.validation?.zodSchemaV4 || f?.validation?.zodSchemaV3;
841
- if (!c)
842
- return { success: !0, data: i };
843
- const m = c.safeParse(i);
844
- if (m.success) {
845
- const w = n.getShadowMetadata(e, t) || {};
846
- n.setShadowMetadata(e, t, {
847
- ...w,
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);
743
+ if (y.success) {
744
+ const v = i.getShadowMetadata(e, t) || {};
745
+ i.setShadowMetadata(e, t, {
746
+ ...v,
848
747
  validation: {
849
748
  status: "VALID",
850
749
  errors: [],
@@ -852,785 +751,768 @@ function Te(e, a, l, s) {
852
751
  }
853
752
  });
854
753
  } else
855
- (m.error?.issues || m.error?.errors || []).forEach((p) => {
856
- const A = [...t, ...p.path.map(String)], V = n.getShadowMetadata(e, A) || {};
857
- n.setShadowMetadata(e, A, {
858
- ...V,
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,
859
758
  validation: {
860
759
  status: "INVALID",
861
760
  errors: [
862
761
  {
863
762
  source: "client",
864
- message: p.message,
763
+ message: A.message,
865
764
  severity: "error",
866
- code: p.code
765
+ code: A.code
867
766
  }
868
767
  ],
869
768
  lastValidated: Date.now(),
870
- validatedValue: n.getShadowValue(e, A)
769
+ validatedValue: i.getShadowValue(e, p)
871
770
  }
872
771
  });
873
772
  });
874
- return N(e), m;
773
+ return F(e), y;
875
774
  };
876
- if (g === "$showValidationErrors")
775
+ if (u === "$showValidationErrors")
877
776
  return () => {
878
- const { shadowMeta: r } = j(e, t, o);
879
- 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) : [];
880
779
  };
881
- if (g === "$validationErrors")
882
- return (r) => {
883
- const n = M.getState(), i = (c) => {
884
- const m = n.getShadowMetadata(e, c)?.validation, w = (m?.errors || []).map((T) => ({
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) => ({
885
784
  ...T,
886
- path: c
887
- })), p = w.filter((T) => T.severity === "error"), A = w.filter(
785
+ path: S
786
+ })), A = v.filter((T) => T.severity === "error"), p = v.filter(
888
787
  (T) => T.severity === "warning"
889
- ), V = p.length > 0 ? "error" : A.length > 0 ? "warning" : void 0;
788
+ ), M = A.length > 0 ? "error" : p.length > 0 ? "warning" : void 0;
890
789
  return {
891
- status: m?.status || "NOT_VALIDATED",
892
- severity: V,
893
- hasErrors: p.length > 0,
894
- hasWarnings: A.length > 0,
895
- message: p[0]?.message || A[0]?.message || "",
896
- errors: p.map((T) => T.message),
897
- warnings: A.map((T) => T.message),
898
- allErrors: w,
899
- path: c,
900
- getData: () => n.getShadowValue(e, c)
790
+ status: y?.status || "NOT_VALIDATED",
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)
901
800
  };
902
801
  };
903
- return (r ?? Object.keys(n.getShadowNode(e, t) ?? {}).filter(
904
- (c) => c !== "_meta"
905
- )).map((c) => i([...t, c]));
802
+ return (n ?? Object.keys(i.getShadowNode(e, t) ?? {}).filter(
803
+ (S) => S !== "_meta"
804
+ )).map((S) => s([...t, S]));
906
805
  };
907
- if (g === "$getSelected")
806
+ if (u === "$getSelected")
908
807
  return () => {
909
- const r = [e, ...t].join(".");
910
- te(e, d, [
808
+ const n = [e, ...t].join(".");
809
+ Q(e, d, [
911
810
  ...t,
912
811
  "getSelected"
913
812
  ]);
914
- const n = M.getState().selectedIndicesMap.get(r);
915
- if (!n)
813
+ const i = V.getState().selectedIndicesMap.get(n);
814
+ if (!i)
916
815
  return;
917
- const i = t.join("."), f = o?.arrayViews?.[i], c = n.split(".").pop();
918
- if (!(f && !f.includes(c) || _(
816
+ const s = t.join("."), g = o?.arrayViews?.[s], S = i.split(".").pop();
817
+ if (!(g && !g.includes(S) || O(
919
818
  e,
920
- n.split(".").slice(1)
819
+ i.split(".").slice(1)
921
820
  ) === void 0))
922
- return u({
923
- path: n.split(".").slice(1),
821
+ return l({
822
+ path: i.split(".").slice(1),
924
823
  componentId: d,
925
824
  meta: o
926
825
  });
927
826
  };
928
- if (g === "$getSelectedIndex")
827
+ if (u === "$getSelectedIndex")
929
828
  return () => {
930
- const r = e + "." + t.join(".");
829
+ const n = e + "." + t.join(".");
931
830
  t.join(".");
932
- const n = M.getState().selectedIndicesMap.get(r);
933
- if (!n)
934
- return -1;
935
- const { keys: i } = z(e, t, o);
831
+ const i = V.getState().selectedIndicesMap.get(n);
936
832
  if (!i)
937
833
  return -1;
938
- const f = n.split(".").pop();
939
- return i.indexOf(f);
834
+ const { keys: s } = U(e, t, o);
835
+ if (!s)
836
+ return -1;
837
+ const g = i.split(".").pop();
838
+ return s.indexOf(g);
940
839
  };
941
- if (g === "$clearSelected")
942
- return Y(e, t), () => {
943
- Je({
840
+ if (u === "$clearSelected")
841
+ return Z(e, t), () => {
842
+ qe({
944
843
  arrayKey: e + "." + t.join(".")
945
844
  });
946
845
  };
947
- if (g === "$map")
948
- return (r) => {
949
- const { value: n, keys: i } = z(
846
+ if (u === "$map")
847
+ return (n) => {
848
+ const { value: i, keys: s } = U(
950
849
  e,
951
850
  t,
952
851
  o
953
852
  );
954
- if (te(e, d, t), !i || !Array.isArray(n))
853
+ if (Q(e, d, t), !s || !Array.isArray(i))
955
854
  return [];
956
- const f = u({
855
+ const g = l({
957
856
  path: t,
958
857
  componentId: d,
959
858
  meta: o
960
859
  });
961
- return n.map((c, m) => {
962
- const w = i[m];
963
- if (!w) return;
964
- const p = [...t, w], A = u({
965
- 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,
966
865
  // This now correctly points to the item in the shadow store.
967
866
  componentId: d,
968
867
  meta: o
969
868
  });
970
- return r(A, m, f);
869
+ return n(p, y, g);
971
870
  });
972
871
  };
973
- if (g === "$filter")
974
- return (r) => {
975
- const n = t.length > 0 ? t.join(".") : "root", { keys: i, value: f } = z(
872
+ if (u === "$filter")
873
+ return (n) => {
874
+ const i = t.length > 0 ? t.join(".") : "root", { keys: s, value: g } = U(
976
875
  e,
977
876
  t,
978
877
  o
979
878
  );
980
- if (!Array.isArray(f))
879
+ if (!Array.isArray(g))
981
880
  throw new Error("filter can only be used on arrays");
982
- const c = [];
983
- return f.forEach((m, w) => {
984
- if (r(m, w)) {
985
- const p = i[w];
986
- p && c.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);
987
886
  }
988
- }), u({
887
+ }), l({
989
888
  path: t,
990
889
  componentId: d,
991
890
  meta: {
992
891
  ...o,
993
892
  arrayViews: {
994
893
  ...o?.arrayViews || {},
995
- [n]: c
894
+ [i]: S
996
895
  },
997
896
  transforms: [
998
897
  ...o?.transforms || [],
999
- { type: "filter", fn: r, path: t }
898
+ { type: "filter", fn: n, path: t }
1000
899
  ]
1001
900
  }
1002
901
  });
1003
902
  };
1004
- if (g === "$sort")
1005
- return (r) => {
1006
- const n = t.length > 0 ? t.join(".") : "root", { value: i, keys: f } = z(
903
+ if (u === "$sort")
904
+ return (n) => {
905
+ const i = t.length > 0 ? t.join(".") : "root", { value: s, keys: g } = U(
1007
906
  e,
1008
907
  t,
1009
908
  o
1010
909
  );
1011
- if (!Array.isArray(i) || !f)
910
+ if (!Array.isArray(s) || !g)
1012
911
  throw new Error("No array keys found for sorting");
1013
- const c = i.map((w, p) => ({
1014
- item: w,
1015
- key: f[p]
912
+ const S = s.map((v, A) => ({
913
+ item: v,
914
+ key: g[A]
1016
915
  }));
1017
- c.sort((w, p) => r(w.item, p.item));
1018
- const m = c.map((w) => w.key);
1019
- return u({
916
+ S.sort((v, A) => n(v.item, A.item));
917
+ const y = S.map((v) => v.key);
918
+ return l({
1020
919
  path: t,
1021
920
  componentId: d,
1022
921
  meta: {
1023
922
  ...o,
1024
923
  arrayViews: {
1025
924
  ...o?.arrayViews || {},
1026
- [n]: m
925
+ [i]: y
1027
926
  },
1028
927
  transforms: [
1029
928
  ...o?.transforms || [],
1030
- { type: "sort", fn: r, path: t }
929
+ { type: "sort", fn: n, path: t }
1031
930
  ]
1032
931
  }
1033
932
  });
1034
933
  };
1035
- if (g === "$list")
1036
- return (r) => /* @__PURE__ */ Q(() => {
1037
- const i = x(/* @__PURE__ */ new Map()), [f, c] = ne({}), m = 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(() => ({
1038
937
  ...o,
1039
938
  arrayViews: {
1040
939
  ...o?.arrayViews || {},
1041
- [m]: w
940
+ [y]: v
1042
941
  }
1043
- }), [o, m, w]), { value: A } = z(
942
+ }), [o, y, v]), { value: p } = U(
1044
943
  e,
1045
944
  t,
1046
- p
945
+ A
1047
946
  );
1048
- if (Z(() => {
1049
- const F = M.getState().subscribeToPath(v, (I) => {
947
+ if (ue(() => {
948
+ const j = V.getState().subscribeToPath(b, (I) => {
1050
949
  if (I.type === "GET_SELECTED")
1051
950
  return;
1052
- const G = M.getState().getShadowMetadata(e, t)?.transformCaches;
1053
- if (G)
1054
- for (const ee of G.keys())
1055
- ee.startsWith(d) && G.delete(ee);
1056
- (I.type === "INSERT" || I.type === "INSERT_MANY" || I.type === "REMOVE" || I.type === "CLEAR_SELECTION" || I.type === "SERVER_STATE_UPDATE" && !o?.serverStateIsUpStream) && c({});
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({});
1057
956
  });
1058
957
  return () => {
1059
- F();
958
+ j();
1060
959
  };
1061
- }, [d, v]), !Array.isArray(A))
960
+ }, [d, b]), !Array.isArray(p))
1062
961
  return null;
1063
- const V = u({
962
+ const M = l({
1064
963
  path: t,
1065
964
  componentId: d,
1066
- meta: p
965
+ meta: A
1067
966
  // Use updated meta here
1068
- }), T = A.map((F, I) => {
1069
- const R = w[I];
1070
- if (!R)
967
+ }), T = p.map((j, I) => {
968
+ const L = v[I];
969
+ if (!L)
1071
970
  return null;
1072
- let G = i.current.get(R);
1073
- G || (G = ae(), i.current.set(R, G));
1074
- const ee = [...t, R];
1075
- return Se(Ue, {
1076
- 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,
1077
976
  stateKey: e,
1078
- itemComponentId: G,
1079
- itemPath: ee,
977
+ itemComponentId: B,
978
+ itemPath: X,
1080
979
  localIndex: I,
1081
- arraySetter: V,
1082
- rebuildStateShape: u,
1083
- renderFn: r
980
+ arraySetter: M,
981
+ rebuildStateShape: l,
982
+ renderFn: n
1084
983
  });
1085
984
  });
1086
- return /* @__PURE__ */ Q(Pe, { children: T });
985
+ return /* @__PURE__ */ H($e, { children: T });
1087
986
  }, {});
1088
- if (g === "$stateFlattenOn")
1089
- return (r) => {
1090
- const n = t.length > 0 ? t.join(".") : "root", i = o?.arrayViews?.[n], f = M.getState().getShadowValue(e, t, i);
1091
- return Array.isArray(f) ? u({
1092
- 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],
1093
992
  componentId: d,
1094
993
  meta: o
1095
994
  }) : [];
1096
995
  };
1097
- if (g === "$index")
1098
- return (r) => {
1099
- const n = t.length > 0 ? t.join(".") : "root", i = o?.arrayViews?.[n];
1100
- if (i) {
1101
- const m = i[r];
1102
- return m ? u({
1103
- path: [...t, m],
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({
1002
+ path: [...t, y],
1104
1003
  componentId: d,
1105
1004
  meta: o
1106
1005
  }) : void 0;
1107
1006
  }
1108
- const f = E(e, t);
1109
- if (!f?.arrayKeys) return;
1110
- const c = f.arrayKeys[r];
1111
- if (c)
1112
- return u({
1113
- path: [...t, c],
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],
1114
1013
  componentId: d,
1115
1014
  meta: o
1116
1015
  });
1117
1016
  };
1118
- if (g === "$last")
1017
+ if (u === "$last")
1119
1018
  return () => {
1120
- const { keys: r } = z(e, t, o);
1121
- if (!r || r.length === 0)
1019
+ const { keys: n } = U(e, t, o);
1020
+ if (!n || n.length === 0)
1122
1021
  return;
1123
- const n = r[r.length - 1];
1124
- if (!n)
1022
+ const i = n[n.length - 1];
1023
+ if (!i)
1125
1024
  return;
1126
- const i = [...t, n];
1127
- return u({
1128
- path: i,
1025
+ const s = [...t, i];
1026
+ return l({
1027
+ path: s,
1129
1028
  componentId: d,
1130
1029
  meta: o
1131
1030
  });
1132
1031
  };
1133
- if (g === "$insert")
1134
- return (r, n) => {
1135
- a(r, t, {
1032
+ if (u === "$insert")
1033
+ return (n, i) => {
1034
+ r(n, t, {
1136
1035
  updateType: "insert",
1137
- index: n
1036
+ index: i
1138
1037
  });
1139
1038
  };
1140
- if (g === "$insertMany")
1141
- return (r) => {
1142
- a(r, t, {
1039
+ if (u === "$insertMany")
1040
+ return (n) => {
1041
+ r(n, t, {
1143
1042
  updateType: "insert_many"
1144
1043
  });
1145
1044
  };
1146
- if (g === "$uniqueInsert")
1147
- return (r, n, i) => {
1148
- const { value: f } = j(
1045
+ if (u === "$uniqueInsert")
1046
+ return (n, i, s) => {
1047
+ const { value: g } = N(
1149
1048
  e,
1150
1049
  t,
1151
1050
  o
1152
- ), c = W(r) ? r(f) : r;
1153
- let m = null;
1154
- if (!f.some((p) => {
1155
- const A = n ? n.every(
1156
- (V) => X(p[V], c[V])
1157
- ) : X(p, c);
1158
- return A && (m = p), A;
1051
+ ), S = W(n) ? n(g) : n;
1052
+ let y = null;
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;
1159
1058
  }))
1160
- a(c, t, { updateType: "insert" });
1161
- else if (i && m) {
1162
- const p = i(m), A = f.map(
1163
- (V) => X(V, m) ? p : V
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
1164
1063
  );
1165
- a(A, t, {
1064
+ r(p, t, {
1166
1065
  updateType: "update"
1167
1066
  });
1168
1067
  }
1169
1068
  };
1170
- if (g === "$cut")
1171
- return (r, n) => {
1172
- const i = E(e, t);
1173
- if (!i?.arrayKeys || i.arrayKeys.length === 0)
1069
+ if (u === "$cut")
1070
+ return (n, i) => {
1071
+ const s = $(e, t);
1072
+ if (!s?.arrayKeys || s.arrayKeys.length === 0)
1174
1073
  return;
1175
- const f = r === -1 ? i.arrayKeys.length - 1 : r !== void 0 ? r : i.arrayKeys.length - 1, c = i.arrayKeys[f];
1176
- c && a(null, [...t, c], {
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], {
1177
1076
  updateType: "cut"
1178
1077
  });
1179
1078
  };
1180
- if (g === "$cutSelected")
1079
+ if (u === "$cutSelected")
1181
1080
  return () => {
1182
- const r = [e, ...t].join("."), { keys: n } = z(e, t, o);
1183
- if (!n || n.length === 0)
1081
+ const n = [e, ...t].join("."), { keys: i } = U(e, t, o);
1082
+ if (!i || i.length === 0)
1184
1083
  return;
1185
- const i = M.getState().selectedIndicesMap.get(r);
1186
- if (!i)
1084
+ const s = V.getState().selectedIndicesMap.get(n);
1085
+ if (!s)
1187
1086
  return;
1188
- const f = i.split(".").pop();
1189
- if (!n.includes(f))
1087
+ const g = s.split(".").pop();
1088
+ if (!i.includes(g))
1190
1089
  return;
1191
- const c = i.split(".").slice(1);
1192
- M.getState().clearSelectedIndex({ arrayKey: r });
1193
- const m = c.slice(0, -1);
1194
- Y(e, m), a(null, c, {
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, {
1195
1094
  updateType: "cut"
1196
1095
  });
1197
1096
  };
1198
- if (g === "$cutByValue")
1199
- return (r) => {
1097
+ if (u === "$cutByValue")
1098
+ return (n) => {
1200
1099
  const {
1201
- isArray: n,
1202
- value: i,
1203
- keys: f
1204
- } = z(e, t, o);
1205
- if (!n) return;
1206
- const c = le(i, f, (m) => m === r);
1207
- c && a(null, [...t, c.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], {
1208
1107
  updateType: "cut"
1209
1108
  });
1210
1109
  };
1211
- if (g === "$toggleByValue")
1212
- return (r) => {
1110
+ if (u === "$toggleByValue")
1111
+ return (n) => {
1213
1112
  const {
1214
- isArray: n,
1215
- value: i,
1216
- keys: f
1217
- } = z(e, t, o);
1218
- if (!n) return;
1219
- const c = le(i, f, (m) => m === r);
1220
- if (c) {
1221
- const m = [...t, c.key];
1222
- a(null, m, {
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, {
1223
1122
  updateType: "cut"
1224
1123
  });
1225
1124
  } else
1226
- a(r, t, { updateType: "insert" });
1125
+ r(n, t, { updateType: "insert" });
1227
1126
  };
1228
- if (g === "$findWith")
1229
- return (r, n) => {
1230
- const { isArray: i, value: f, keys: c } = z(e, t, o);
1231
- if (!i)
1127
+ if (u === "$findWith")
1128
+ return (n, i) => {
1129
+ const { isArray: s, value: g, keys: S } = U(e, t, o);
1130
+ if (!s)
1232
1131
  throw new Error("findWith can only be used on arrays");
1233
- const m = le(
1234
- f,
1235
- c,
1236
- (w) => w?.[r] === n
1132
+ const y = ie(
1133
+ g,
1134
+ S,
1135
+ (v) => v?.[n] === i
1237
1136
  );
1238
- return m ? u({
1239
- path: [...t, m.key],
1137
+ return y ? l({
1138
+ path: [...t, y.key],
1240
1139
  componentId: d,
1241
1140
  meta: o
1242
1141
  }) : null;
1243
1142
  };
1244
- if (g === "$cutThis") {
1245
- const { value: r } = j(e, t, o), n = t.slice(0, -1);
1246
- return Y(e, n), () => {
1247
- 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" });
1248
1147
  };
1249
1148
  }
1250
- if (g === "$get")
1149
+ if (u === "$get")
1251
1150
  return () => {
1252
- te(e, d, t);
1253
- const { value: r } = j(e, t, o);
1254
- return r;
1151
+ Q(e, d, t);
1152
+ const { value: n } = N(e, t, o);
1153
+ return n;
1255
1154
  };
1256
- if (g === "$$derive")
1257
- return (r) => Ae({
1155
+ if (u === "$$derive")
1156
+ return (n) => he({
1258
1157
  _stateKey: e,
1259
1158
  _path: t,
1260
- _effect: r.toString(),
1159
+ _effect: n.toString(),
1261
1160
  _meta: o
1262
1161
  });
1263
- if (g === "$$get")
1264
- return () => Ae({ _stateKey: e, _path: t, _meta: o });
1265
- if (g === "$lastSynced") {
1266
- const r = `${e}:${t.join(".")}`;
1267
- return He(r);
1268
- }
1269
- if (g == "getLocalStorage")
1270
- return (r) => ce(s + "-" + e + "-" + r);
1271
- if (g === "$isSelected") {
1272
- const r = t.slice(0, -1);
1273
- if (E(e, r)?.arrayKeys) {
1274
- const i = e + "." + r.join("."), f = M.getState().selectedIndicesMap.get(i), c = e + "." + t.join(".");
1275
- return f === c;
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;
1276
1171
  }
1277
1172
  return;
1278
1173
  }
1279
- if (g === "$setSelected")
1280
- return (r) => {
1281
- const n = t.slice(0, -1), i = e + "." + n.join("."), f = e + "." + t.join(".");
1282
- Y(e, n, void 0), M.getState().selectedIndicesMap.get(i), r && M.getState().setSelectedIndex(i, f);
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);
1283
1178
  };
1284
- if (g === "$toggleSelected")
1179
+ if (u === "$toggleSelected")
1285
1180
  return () => {
1286
- const r = t.slice(0, -1), n = e + "." + r.join("."), i = e + "." + t.join(".");
1287
- M.getState().selectedIndicesMap.get(n) === i ? M.getState().clearSelectedIndex({ arrayKey: n }) : M.getState().setSelectedIndex(n, i), 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);
1288
1183
  };
1289
- if (g === "$clearValidation")
1290
- return (r) => {
1291
- const n = r ? [...t, ...r] : t, i = M.getState(), f = i.getShadowNode(e, n);
1292
- if (console.log("startNode ", f), !f) return;
1293
- const c = [[f, n]];
1294
- for (console.log("stack ", c); c.length > 0; ) {
1295
- const [m, w] = c.pop();
1296
- if (console.log("while (stack.length ", m, w), !m || typeof m != "object") continue;
1297
- if (m._meta?.validation) {
1298
- m._meta.validation = {
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;
1192
+ if (y._meta?.validation) {
1193
+ y._meta.validation = {
1299
1194
  status: "NOT_VALIDATED",
1300
1195
  errors: [],
1301
1196
  lastValidated: Date.now(),
1302
1197
  validatedValue: void 0
1303
1198
  };
1304
- const A = [e, ...w].join(".");
1305
- i.notifyPathSubscribers(A, {
1199
+ const p = [e, ...v].join(".");
1200
+ s.notifyPathSubscribers(p, {
1306
1201
  type: "VALIDATION_CLEAR"
1307
1202
  });
1308
1203
  }
1309
- const p = Object.keys(m);
1310
- for (const A of p)
1311
- A !== "_meta" && c.push([m[A], [...w, A]]);
1204
+ const A = Object.keys(y);
1205
+ for (const p of A)
1206
+ p !== "_meta" && S.push([y[p], [...v, p]]);
1312
1207
  }
1313
- N(e);
1208
+ F(e);
1314
1209
  };
1315
1210
  if (t.length == 0) {
1316
- if (g === "$_componentId")
1211
+ if (u === "$_componentId")
1317
1212
  return d;
1318
- if (g === "$setOptions")
1319
- return (r) => {
1320
- fe({ stateKey: e, options: r, initialOptionsPart: {} });
1213
+ if (u === "$setOptions")
1214
+ return (n) => {
1215
+ ce({ stateKey: e, options: n, initialOptionsPart: {} });
1321
1216
  };
1322
- if (g === "$_applyUpdate")
1323
- return (r, n, i = "update") => {
1324
- a(r, n, { updateType: i });
1217
+ if (u === "$_applyUpdate")
1218
+ return (n, i, s = "update") => {
1219
+ r(n, i, { updateType: s });
1325
1220
  };
1326
- if (g === "$_getEffectiveSetState")
1327
- return a;
1328
- if (g === "$getPluginMetaData")
1329
- return (r) => he(e, t)?.get(r);
1330
- if (g === "$addPluginMetaData")
1331
- return (r, n) => ve(e, t, r, n);
1332
- if (g === "$removePluginMetaData")
1333
- return (r) => we(e, t, r);
1334
- if (g === "$addZodValidation")
1335
- return (r, n) => {
1336
- const i = M.getState();
1337
- r.forEach((f) => {
1338
- const c = f.path.map(String), m = i.getShadowMetadata(e, c) || {};
1339
- i.setShadowMetadata(e, c, {
1340
- ...m,
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, {
1235
+ ...y,
1341
1236
  validation: {
1342
1237
  status: "INVALID",
1343
1238
  errors: [
1344
1239
  {
1345
- source: n || "client",
1346
- message: f.message,
1240
+ source: i || "client",
1241
+ message: g.message,
1347
1242
  severity: "error",
1348
- code: f.code
1243
+ code: g.code
1349
1244
  }
1350
1245
  ],
1351
1246
  lastValidated: Date.now(),
1352
1247
  validatedValue: void 0
1353
1248
  }
1354
- }), i.notifyPathSubscribers(
1355
- [e, ...c].join("."),
1249
+ }), s.notifyPathSubscribers(
1250
+ [e, ...S].join("."),
1356
1251
  { type: "VALIDATION_UPDATE" }
1357
1252
  );
1358
- }), N(e);
1253
+ }), F(e);
1359
1254
  };
1360
- if (g === "$clearZodValidationPaths")
1361
- return (r) => {
1362
- const n = M.getState();
1363
- r.forEach((i) => {
1364
- const f = n.getShadowMetadata(e, i) || {};
1365
- f.validation && (n.setShadowMetadata(e, i, {
1366
- ...f,
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,
1367
1262
  validation: {
1368
1263
  status: "NOT_VALIDATED",
1369
1264
  errors: [],
1370
1265
  lastValidated: Date.now(),
1371
1266
  validatedValue: void 0
1372
1267
  }
1373
- }), n.notifyPathSubscribers(
1374
- [e, ...i].join("."),
1268
+ }), i.notifyPathSubscribers(
1269
+ [e, ...s].join("."),
1375
1270
  { type: "VALIDATION_CLEAR" }
1376
1271
  ));
1377
- }), N(e);
1272
+ }), F(e);
1378
1273
  };
1379
- if (g === "$applyOperation")
1380
- return (r, n) => {
1381
- let i;
1382
- if (r.insertAfterId && r.updateType === "insert") {
1383
- const f = E(e, r.path);
1384
- if (f?.arrayKeys) {
1385
- const c = f.arrayKeys.indexOf(
1386
- 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
1387
1282
  );
1388
- c !== -1 && (i = c + 1);
1283
+ S !== -1 && (s = S + 1);
1389
1284
  }
1390
1285
  }
1391
- a(r.newValue, r.path, {
1392
- updateType: r.updateType,
1393
- itemId: r.itemId,
1394
- index: i,
1286
+ r(n.newValue, n.path, {
1287
+ updateType: n.updateType,
1288
+ itemId: n.itemId,
1289
+ index: s,
1395
1290
  // Pass the calculated index
1396
- metaData: n
1291
+ metaData: i
1397
1292
  });
1398
1293
  };
1399
- if (g === "$applyJsonPatch")
1400
- return (r) => {
1401
- const n = M.getState(), i = n.getShadowMetadata(e, []);
1402
- if (!i?.components) return;
1403
- const f = (m) => !m || m === "/" ? [] : m.split("/").slice(1).map((w) => w.replace(/~1/g, "/").replace(/~0/g, "~")), c = /* @__PURE__ */ new Set();
1404
- for (const m of r) {
1405
- const w = f(m.path);
1406
- switch (m.op) {
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);
1301
+ switch (y.op) {
1407
1302
  case "add":
1408
1303
  case "replace": {
1409
- const { value: p } = m;
1410
- n.updateShadowAtPath(e, w, p), n.markAsDirty(e, w, { bubble: !0 });
1411
- let A = [...w];
1304
+ const { value: A } = y;
1305
+ i.updateShadowAtPath(e, v, A);
1306
+ let p = [...v];
1412
1307
  for (; ; ) {
1413
- const V = n.getShadowMetadata(
1308
+ const M = i.getShadowMetadata(
1414
1309
  e,
1415
- A
1310
+ p
1416
1311
  );
1417
- if (V?.pathComponents && V.pathComponents.forEach((T) => {
1418
- if (!c.has(T)) {
1419
- const F = i.components?.get(T);
1420
- F && (F.forceUpdate(), c.add(T));
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));
1421
1316
  }
1422
- }), A.length === 0) break;
1423
- A.pop();
1317
+ }), p.length === 0) break;
1318
+ p.pop();
1424
1319
  }
1425
1320
  break;
1426
1321
  }
1427
1322
  case "remove": {
1428
- const p = w.slice(0, -1);
1429
- n.removeShadowArrayElement(e, w), n.markAsDirty(e, p, { bubble: !0 });
1430
- let A = [...p];
1323
+ const A = v.slice(0, -1);
1324
+ i.removeShadowArrayElement(e, v);
1325
+ let p = [...A];
1431
1326
  for (; ; ) {
1432
- const V = n.getShadowMetadata(
1327
+ const M = i.getShadowMetadata(
1433
1328
  e,
1434
- A
1329
+ p
1435
1330
  );
1436
- if (V?.pathComponents && V.pathComponents.forEach((T) => {
1437
- if (!c.has(T)) {
1438
- const F = i.components?.get(T);
1439
- F && (F.forceUpdate(), c.add(T));
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));
1440
1335
  }
1441
- }), A.length === 0) break;
1442
- A.pop();
1336
+ }), p.length === 0) break;
1337
+ p.pop();
1443
1338
  }
1444
1339
  break;
1445
1340
  }
1446
1341
  }
1447
1342
  }
1448
1343
  };
1449
- if (g === "$getComponents")
1450
- return () => E(e, [])?.components;
1344
+ if (u === "$getComponents")
1345
+ return () => $(e, [])?.components;
1451
1346
  }
1452
- if (g === "$validationWrapper")
1347
+ if (u === "$validationWrapper")
1453
1348
  return ({
1454
- children: r,
1455
- hideMessage: n
1456
- }) => /* @__PURE__ */ Q(
1457
- Oe,
1349
+ children: n,
1350
+ hideMessage: i
1351
+ }) => /* @__PURE__ */ H(
1352
+ De,
1458
1353
  {
1459
- formOpts: n ? { validation: { message: "" } } : void 0,
1354
+ formOpts: i ? { validation: { message: "" } } : void 0,
1460
1355
  path: t,
1461
1356
  stateKey: e,
1462
- children: r
1357
+ children: n
1463
1358
  }
1464
1359
  );
1465
- if (g === "$_stateKey") return e;
1466
- if (g === "$_path") return t;
1467
- if (g === "$update")
1468
- 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" }), {
1469
1364
  synced: () => {
1470
- const n = M.getState().getShadowMetadata(e, t);
1471
- B(e, t, {
1472
- ...n,
1473
- isDirty: !1,
1474
- stateSource: "server",
1475
- lastServerSync: Date.now()
1476
- });
1477
- const i = [e, ...t].join(".");
1478
- Qe(i, {
1479
- type: "SYNC_STATUS_CHANGE",
1480
- isDirty: !1
1481
- });
1482
1365
  }
1483
1366
  });
1484
- if (g === "$toggle") {
1485
- const { value: r } = j(
1367
+ if (u === "$toggle") {
1368
+ const { value: n } = N(
1486
1369
  e,
1487
1370
  t,
1488
1371
  o
1489
1372
  );
1490
- if (typeof r != "boolean")
1373
+ if (typeof n != "boolean")
1491
1374
  throw new Error("toggle() can only be used on boolean values");
1492
1375
  return () => {
1493
- a(!r, t, {
1376
+ r(!n, t, {
1494
1377
  updateType: "update"
1495
1378
  });
1496
1379
  };
1497
1380
  }
1498
- if (g === "$isolate")
1499
- return (r, n) => {
1500
- const i = Array.isArray(r), f = i ? r : void 0, c = i ? n : r;
1501
- if (!c || typeof c != "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")
1502
1385
  throw new Error(
1503
1386
  "CogsState: $isolate requires a render function."
1504
1387
  );
1505
- return /* @__PURE__ */ Q(
1506
- je,
1388
+ return /* @__PURE__ */ H(
1389
+ Ce,
1507
1390
  {
1508
1391
  stateKey: e,
1509
1392
  path: t,
1510
- dependencies: f,
1511
- rebuildStateShape: u,
1512
- renderFn: c
1393
+ dependencies: g,
1394
+ rebuildStateShape: l,
1395
+ renderFn: S
1513
1396
  }
1514
1397
  );
1515
1398
  };
1516
- if (g === "$formElement")
1517
- return (r, n) => /* @__PURE__ */ Q(
1518
- Ne,
1399
+ if (u === "$formElement")
1400
+ return (n, i) => /* @__PURE__ */ H(
1401
+ Oe,
1519
1402
  {
1520
1403
  stateKey: e,
1521
1404
  path: t,
1522
- rebuildStateShape: u,
1523
- setState: a,
1524
- formOpts: n,
1525
- renderFn: r
1405
+ rebuildStateShape: l,
1406
+ setState: r,
1407
+ formOpts: i,
1408
+ renderFn: n
1526
1409
  }
1527
1410
  );
1528
- const C = [...t, g];
1529
- return u({
1411
+ const C = [...t, u];
1412
+ return l({
1530
1413
  path: C,
1531
1414
  componentId: d,
1532
1415
  meta: o
1533
1416
  });
1534
1417
  }
1535
- }, J = new Proxy(O, D);
1536
- return S.set(P, J), J;
1418
+ }, q = new Proxy(D, k);
1419
+ return f.set(w, q), q;
1537
1420
  }
1538
1421
  const h = {
1539
1422
  $revertToInitialState: (t) => {
1540
- const o = M.getState().getShadowMetadata(e, []);
1541
- let d;
1542
- 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({
1543
1425
  path: [],
1544
- componentId: l
1426
+ componentId: c
1545
1427
  });
1546
- const $ = U(e), b = W($?.localStorage?.key) ? $?.localStorage?.key(d) : $?.localStorage?.key, P = s && b ? `${s}-${e}-${b}` : void 0;
1547
- 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;
1548
1430
  },
1549
1431
  $initializeAndMergeShadowState: (t) => {
1550
- Re(e, t), N(e);
1432
+ Fe(e, t), F(e);
1551
1433
  },
1552
1434
  $updateInitialState: (t) => {
1553
- const o = Te(
1435
+ const o = Pe(
1554
1436
  e,
1555
- a,
1556
- l,
1557
- s
1558
- ), d = M.getState().initialStateGlobal[e], $ = U(e), b = W($?.localStorage?.key) ? $?.localStorage?.key(d) : $?.localStorage?.key, P = s && b ? `${s}-${e}-${b}` : void 0;
1559
- return de(P), De(() => {
1560
- Ve(e, t), K(e, t);
1561
- const v = M.getState().getShadowMetadata(e, []);
1562
- v && v?.components?.forEach((O) => {
1563
- O.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();
1564
1446
  });
1565
1447
  }), {
1566
- fetchId: (v) => o.$get()[v]
1448
+ fetchId: (b) => o.$get()[b]
1567
1449
  };
1568
1450
  }
1569
1451
  };
1570
- return u({
1571
- componentId: l,
1452
+ return l({
1453
+ componentId: c,
1572
1454
  path: []
1573
1455
  });
1574
1456
  }
1575
- function Ae(e) {
1576
- return Se(gt, { proxy: e });
1457
+ function he(e) {
1458
+ return de(ut, { proxy: e });
1577
1459
  }
1578
- function gt({
1460
+ function ut({
1579
1461
  proxy: e
1580
1462
  }) {
1581
- const a = x(null), l = x(null), s = x(!1), S = `${e._stateKey}-${e._path.join(".")}`, u = e._path.length > 0 ? e._path.join(".") : "root", h = e._meta?.arrayViews?.[u], y = _(e._stateKey, e._path, h);
1582
- return Z(() => {
1583
- const t = a.current;
1584
- 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;
1585
1467
  const o = setTimeout(() => {
1586
1468
  if (!t.parentElement) {
1587
- console.warn("Parent element not found for signal", S);
1469
+ console.warn("Parent element not found for signal", f);
1588
1470
  return;
1589
1471
  }
1590
- const d = t.parentElement, b = Array.from(d.childNodes).indexOf(t);
1591
- let P = d.getAttribute("data-parent-id");
1592
- P || (P = `parent-${crypto.randomUUID()}`, d.setAttribute("data-parent-id", P)), l.current = `instance-${crypto.randomUUID()}`;
1593
- const v = M.getState().getShadowMetadata(e._stateKey, e._path) || {}, O = v.signals || [];
1594
- O.push({
1595
- instanceId: l.current,
1596
- parentId: P,
1597
- 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,
1598
1480
  effect: e._effect
1599
- }), M.getState().setShadowMetadata(e._stateKey, e._path, {
1600
- ...v,
1601
- signals: O
1481
+ }), V.getState().setShadowMetadata(e._stateKey, e._path, {
1482
+ ...b,
1483
+ signals: D
1602
1484
  });
1603
- let D = y;
1485
+ let k = m;
1604
1486
  if (e._effect)
1605
1487
  try {
1606
- D = new Function(
1488
+ k = new Function(
1607
1489
  "state",
1608
1490
  `return (${e._effect})(state)`
1609
- )(y);
1610
- } catch (q) {
1611
- console.error("Error evaluating effect function:", q);
1491
+ )(m);
1492
+ } catch (x) {
1493
+ console.error("Error evaluating effect function:", x);
1612
1494
  }
1613
- D !== null && typeof D == "object" && (D = JSON.stringify(D));
1614
- const J = document.createTextNode(String(D ?? ""));
1615
- 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;
1616
1498
  }, 0);
1617
1499
  return () => {
1618
- if (clearTimeout(o), l.current) {
1619
- 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) || {};
1620
1502
  d.signals && (d.signals = d.signals.filter(
1621
- ($) => $.instanceId !== l.current
1622
- ), M.getState().setShadowMetadata(e._stateKey, e._path, d));
1503
+ (P) => P.instanceId !== c.current
1504
+ ), V.getState().setShadowMetadata(e._stateKey, e._path, d));
1623
1505
  }
1624
1506
  };
1625
- }, []), Se("span", {
1626
- ref: a,
1507
+ }, []), de("span", {
1508
+ ref: r,
1627
1509
  style: { display: "contents" },
1628
- "data-signal-id": S
1510
+ "data-signal-id": f
1629
1511
  });
1630
1512
  }
1631
1513
  export {
1632
- Ae as $cogsSignal,
1633
- bt as createCogsState,
1634
- ct as useCogsStateFn
1514
+ he as $cogsSignal,
1515
+ Mt as createCogsState,
1516
+ at as useCogsStateFn
1635
1517
  };
1636
1518
  //# sourceMappingURL=CogsState.js.map