cogsbox-state 0.5.471 → 0.5.473

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