cogsbox-state 0.5.202 → 0.5.204

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.
@@ -1,40 +1,40 @@
1
1
  "use client";
2
- import { jsx as Se } from "react/jsx-runtime";
3
- import { useState as le, useRef as Q, useEffect as de, useLayoutEffect as pe, useMemo as ke, createElement as te, useSyncExternalStore as Ae, startTransition as Te } from "react";
4
- import { transformStateFunc as Ne, isDeepEqual as M, isFunction as z, getNestedValue as W, getDifferences as he, debounce as Ve } from "./utility.js";
2
+ import { jsx as me } from "react/jsx-runtime";
3
+ import { useState as le, useRef as Q, useEffect as de, useLayoutEffect as $e, useMemo as ke, createElement as te, useSyncExternalStore as Te, startTransition as Ne } from "react";
4
+ import { transformStateFunc as Ae, isDeepEqual as G, isFunction as J, getNestedValue as W, getDifferences as ue, debounce as Ve } from "./utility.js";
5
5
  import { pushFunc as se, updateFn as Y, cutFunc as X, ValidationWrapper as _e, FormControlComponent as be } from "./Functions.jsx";
6
- import Pe from "superjson";
7
- import { v4 as ue } from "uuid";
6
+ import Ce from "superjson";
7
+ import { v4 as ge } from "uuid";
8
8
  import "zod";
9
- import { getGlobalStore as n, formRefStore as me } from "./store.js";
9
+ import { getGlobalStore as n, formRefStore as ye } from "./store.js";
10
10
  import { useCogsConfig as we } from "./CogsStateClient.jsx";
11
- import { applyPatch as Ce } from "fast-json-patch";
12
- function ye(e, i) {
13
- const m = n.getState().getInitialOptions, u = n.getState().setInitialStateOptions, f = m(e) || {};
11
+ import { applyPatch as Pe } from "fast-json-patch";
12
+ function ve(e, i) {
13
+ const S = n.getState().getInitialOptions, u = n.getState().setInitialStateOptions, f = S(e) || {};
14
14
  u(e, {
15
15
  ...f,
16
16
  ...i
17
17
  });
18
18
  }
19
- function ve({
19
+ function Ie({
20
20
  stateKey: e,
21
21
  options: i,
22
- initialOptionsPart: m
22
+ initialOptionsPart: S
23
23
  }) {
24
- const u = Z(e) || {}, f = m[e] || {}, A = n.getState().setInitialStateOptions, p = { ...f, ...u };
24
+ const u = Z(e) || {}, f = S[e] || {}, T = n.getState().setInitialStateOptions, h = { ...f, ...u };
25
25
  let v = !1;
26
26
  if (i)
27
27
  for (const s in i)
28
- p.hasOwnProperty(s) ? (s == "localStorage" && i[s] && p[s].key !== i[s]?.key && (v = !0, p[s] = i[s]), s == "initialState" && i[s] && p[s] !== i[s] && // Different references
29
- !M(p[s], i[s]) && (v = !0, p[s] = i[s])) : (v = !0, p[s] = i[s]);
30
- v && A(e, p);
28
+ h.hasOwnProperty(s) ? (s == "localStorage" && i[s] && h[s].key !== i[s]?.key && (v = !0, h[s] = i[s]), s == "initialState" && i[s] && h[s] !== i[s] && // Different references
29
+ !G(h[s], i[s]) && (v = !0, h[s] = i[s])) : (v = !0, h[s] = i[s]);
30
+ v && T(e, h);
31
31
  }
32
- function Qe(e, { formElements: i, validation: m }) {
33
- return { initialState: e, formElements: i, validation: m };
32
+ function Qe(e, { formElements: i, validation: S }) {
33
+ return { initialState: e, formElements: i, validation: S };
34
34
  }
35
35
  const Ke = (e, i) => {
36
- let m = e;
37
- const [u, f] = Ne(m);
36
+ let S = e;
37
+ const [u, f] = Ae(S);
38
38
  (Object.keys(f).length > 0 || i && Object.keys(i).length > 0) && Object.keys(f).forEach((v) => {
39
39
  f[v] = f[v] || {}, f[v].formElements = {
40
40
  ...i?.formElements,
@@ -44,19 +44,19 @@ const Ke = (e, i) => {
44
44
  // State-specific overrides
45
45
  }, Z(v) || n.getState().setInitialStateOptions(v, f[v]);
46
46
  }), n.getState().setInitialStates(u), n.getState().setCreatedState(u);
47
- const A = (v, s) => {
48
- const [y] = le(s?.componentId ?? ue());
49
- ve({
47
+ const T = (v, s) => {
48
+ const [m] = le(s?.componentId ?? ge());
49
+ Ie({
50
50
  stateKey: v,
51
51
  options: s,
52
52
  initialOptionsPart: f
53
53
  });
54
- const t = n.getState().cogsStateStore[v] || u[v], I = s?.modifyState ? s.modifyState(t) : t, [j, V] = De(
54
+ const t = n.getState().cogsStateStore[v] || u[v], I = s?.modifyState ? s.modifyState(t) : t, [j, A] = De(
55
55
  I,
56
56
  {
57
57
  stateKey: v,
58
58
  syncUpdate: s?.syncUpdate,
59
- componentId: y,
59
+ componentId: m,
60
60
  localStorage: s?.localStorage,
61
61
  middleware: s?.middleware,
62
62
  enabledSync: s?.enabledSync,
@@ -67,46 +67,46 @@ const Ke = (e, i) => {
67
67
  serverState: s?.serverState
68
68
  }
69
69
  );
70
- return V;
70
+ return A;
71
71
  };
72
- function p(v, s) {
73
- ve({ stateKey: v, options: s, initialOptionsPart: f }), s.localStorage && Re(v, s), re(v);
72
+ function h(v, s) {
73
+ Ie({ stateKey: v, options: s, initialOptionsPart: f }), s.localStorage && je(v, s), re(v);
74
74
  }
75
- return { useCogsState: A, setCogsOptions: p };
75
+ return { useCogsState: T, setCogsOptions: h };
76
76
  }, {
77
77
  setUpdaterState: K,
78
- setState: J,
78
+ setState: B,
79
79
  getInitialOptions: Z,
80
80
  getKeyState: Ee,
81
81
  getValidationErrors: xe,
82
82
  setStateLog: Oe,
83
- updateInitialStateGlobal: ge,
83
+ updateInitialStateGlobal: fe,
84
84
  addValidationError: Fe,
85
- removeValidationError: q,
86
- setServerSyncActions: je
87
- } = n.getState(), Ie = (e, i, m, u, f) => {
88
- m?.log && console.log(
85
+ removeValidationError: z,
86
+ setServerSyncActions: Re
87
+ } = n.getState(), he = (e, i, S, u, f) => {
88
+ S?.log && console.log(
89
89
  "saving to localstorage",
90
90
  i,
91
- m.localStorage?.key,
91
+ S.localStorage?.key,
92
92
  u
93
93
  );
94
- const A = z(m?.localStorage?.key) ? m.localStorage?.key(e) : m?.localStorage?.key;
95
- if (A && u) {
96
- const p = `${u}-${i}-${A}`;
94
+ const T = J(S?.localStorage?.key) ? S.localStorage?.key(e) : S?.localStorage?.key;
95
+ if (T && u) {
96
+ const h = `${u}-${i}-${T}`;
97
97
  let v;
98
98
  try {
99
- v = ne(p)?.lastSyncedWithServer;
99
+ v = ne(h)?.lastSyncedWithServer;
100
100
  } catch {
101
101
  }
102
102
  const s = {
103
103
  state: e,
104
104
  lastUpdated: Date.now(),
105
105
  lastSyncedWithServer: f ?? v
106
- }, y = Pe.serialize(s);
106
+ }, m = Ce.serialize(s);
107
107
  window.localStorage.setItem(
108
- p,
109
- JSON.stringify(y.json)
108
+ h,
109
+ JSON.stringify(m.json)
110
110
  );
111
111
  }
112
112
  }, ne = (e) => {
@@ -117,57 +117,57 @@ const Ke = (e, i) => {
117
117
  } catch (i) {
118
118
  return console.error("Error loading from localStorage:", i), null;
119
119
  }
120
- }, Re = (e, i) => {
121
- const m = n.getState().cogsStateStore[e], { sessionId: u } = we(), f = z(i?.localStorage?.key) ? i.localStorage.key(m) : i?.localStorage?.key;
120
+ }, je = (e, i) => {
121
+ const S = n.getState().cogsStateStore[e], { sessionId: u } = we(), f = J(i?.localStorage?.key) ? i.localStorage.key(S) : i?.localStorage?.key;
122
122
  if (f && u) {
123
- const A = ne(
123
+ const T = ne(
124
124
  `${u}-${e}-${f}`
125
125
  );
126
- if (A && A.lastUpdated > (A.lastSyncedWithServer || 0))
127
- return J(e, A.state), re(e), !0;
126
+ if (T && T.lastUpdated > (T.lastSyncedWithServer || 0))
127
+ return B(e, T.state), re(e), !0;
128
128
  }
129
129
  return !1;
130
- }, $e = (e, i, m, u, f, A) => {
131
- const p = {
130
+ }, pe = (e, i, S, u, f, T) => {
131
+ const h = {
132
132
  initialState: i,
133
133
  updaterState: ee(
134
134
  e,
135
135
  u,
136
136
  f,
137
- A
137
+ T
138
138
  ),
139
- state: m
139
+ state: S
140
140
  };
141
- ge(e, p.initialState), K(e, p.updaterState), J(e, p.state);
141
+ fe(e, h.initialState), K(e, h.updaterState), B(e, h.state);
142
142
  }, re = (e) => {
143
143
  const i = n.getState().stateComponents.get(e);
144
144
  if (!i) return;
145
- const m = /* @__PURE__ */ new Set();
145
+ const S = /* @__PURE__ */ new Set();
146
146
  i.components.forEach((u) => {
147
- (u ? Array.isArray(u.reactiveType) ? u.reactiveType : [u.reactiveType || "component"] : null)?.includes("none") || m.add(() => u.forceUpdate());
147
+ (u ? Array.isArray(u.reactiveType) ? u.reactiveType : [u.reactiveType || "component"] : null)?.includes("none") || S.add(() => u.forceUpdate());
148
148
  }), queueMicrotask(() => {
149
- m.forEach((u) => u());
149
+ S.forEach((u) => u());
150
150
  });
151
151
  }, et = (e, i) => {
152
- const m = n.getState().stateComponents.get(e);
153
- if (m) {
154
- const u = `${e}////${i}`, f = m.components.get(u);
152
+ const S = n.getState().stateComponents.get(e);
153
+ if (S) {
154
+ const u = `${e}////${i}`, f = S.components.get(u);
155
155
  if ((f ? Array.isArray(f.reactiveType) ? f.reactiveType : [f.reactiveType || "component"] : null)?.includes("none"))
156
156
  return;
157
157
  f && f.forceUpdate();
158
158
  }
159
- }, Ue = (e, i, m, u) => {
159
+ }, Ue = (e, i, S, u) => {
160
160
  switch (e) {
161
161
  case "update":
162
162
  return {
163
163
  oldValue: W(i, u),
164
- newValue: W(m, u)
164
+ newValue: W(S, u)
165
165
  };
166
166
  case "insert":
167
167
  return {
168
168
  oldValue: null,
169
169
  // or undefined
170
- newValue: W(m, u)
170
+ newValue: W(S, u)
171
171
  };
172
172
  case "cut":
173
173
  return {
@@ -181,310 +181,310 @@ const Ke = (e, i) => {
181
181
  };
182
182
  function De(e, {
183
183
  stateKey: i,
184
- serverSync: m,
184
+ serverSync: S,
185
185
  localStorage: u,
186
186
  formElements: f,
187
- reactiveDeps: A,
188
- reactiveType: p,
187
+ reactiveDeps: T,
188
+ reactiveType: h,
189
189
  componentId: v,
190
190
  initialState: s,
191
- syncUpdate: y,
191
+ syncUpdate: m,
192
192
  dependencies: t,
193
193
  serverState: I
194
194
  } = {}) {
195
- const [j, V] = le({}), { sessionId: _ } = we();
196
- let R = !i;
197
- const [g] = le(i ?? ue()), c = n.getState().stateLog[g], H = Q(/* @__PURE__ */ new Set()), L = Q(v ?? ue()), b = Q(
195
+ const [j, A] = le({}), { sessionId: V } = we();
196
+ let U = !i;
197
+ const [g] = le(i ?? ge()), c = n.getState().stateLog[g], H = Q(/* @__PURE__ */ new Set()), q = Q(v ?? ge()), _ = Q(
198
198
  null
199
199
  );
200
- b.current = Z(g) ?? null, de(() => {
201
- if (y && y.stateKey === g && y.path?.[0]) {
202
- J(g, (a) => ({
200
+ _.current = Z(g) ?? null, de(() => {
201
+ if (m && m.stateKey === g && m.path?.[0]) {
202
+ B(g, (a) => ({
203
203
  ...a,
204
- [y.path[0]]: y.newValue
204
+ [m.path[0]]: m.newValue
205
205
  }));
206
- const o = `${y.stateKey}:${y.path.join(".")}`;
206
+ const o = `${m.stateKey}:${m.path.join(".")}`;
207
207
  n.getState().setSyncInfo(o, {
208
- timeStamp: y.timeStamp,
209
- userId: y.userId
208
+ timeStamp: m.timeStamp,
209
+ userId: m.userId
210
210
  });
211
211
  }
212
- }, [y]), de(() => {
212
+ }, [m]), de(() => {
213
213
  if (s) {
214
- ye(g, {
214
+ ve(g, {
215
215
  initialState: s
216
216
  });
217
- const o = b.current, d = o?.serverState?.id !== void 0 && o?.serverState?.status === "success" && o?.serverState?.data, w = n.getState().initialStateGlobal[g];
218
- if (!(w && !M(w, s) || !w) && !d)
217
+ const o = _.current, l = o?.serverState?.id !== void 0 && o?.serverState?.status === "success" && o?.serverState?.data, w = n.getState().initialStateGlobal[g];
218
+ if (!(w && !G(w, s) || !w) && !l)
219
219
  return;
220
- let h = null;
221
- const E = z(o?.localStorage?.key) ? o?.localStorage?.key(s) : o?.localStorage?.key;
222
- E && _ && (h = ne(`${_}-${g}-${E}`));
223
- let $ = s, N = !1;
224
- const P = d ? Date.now() : 0, B = h?.lastUpdated || 0, ae = h?.lastSyncedWithServer || 0;
225
- d && P > B ? ($ = o.serverState.data, N = !0) : h && B > ae && ($ = h.state, o?.localStorage?.onChange && o?.localStorage?.onChange($)), $e(
220
+ let E = null;
221
+ const p = J(o?.localStorage?.key) ? o?.localStorage?.key(s) : o?.localStorage?.key;
222
+ p && V && (E = ne(`${V}-${g}-${p}`));
223
+ let k = s, b = !1;
224
+ const P = l ? Date.now() : 0, x = E?.lastUpdated || 0, ae = E?.lastSyncedWithServer || 0;
225
+ l && P > x ? (k = o.serverState.data, b = !0) : E && x > ae && (k = E.state, o?.localStorage?.onChange && o?.localStorage?.onChange(k)), pe(
226
226
  g,
227
227
  s,
228
- $,
229
- l,
230
- L.current,
231
- _
232
- ), N && E && _ && Ie($, g, o, _, Date.now()), re(g), (Array.isArray(p) ? p : [p || "component"]).includes("none") || V({});
228
+ k,
229
+ d,
230
+ q.current,
231
+ V
232
+ ), b && p && V && he(k, g, o, V, Date.now()), re(g), (Array.isArray(h) ? h : [h || "component"]).includes("none") || A({});
233
233
  }
234
234
  }, [
235
235
  s,
236
236
  I?.status,
237
237
  I?.data,
238
238
  ...t || []
239
- ]), pe(() => {
240
- R && ye(g, {
241
- serverSync: m,
239
+ ]), $e(() => {
240
+ U && ve(g, {
241
+ serverSync: S,
242
242
  formElements: f,
243
243
  initialState: s,
244
244
  localStorage: u,
245
- middleware: b.current?.middleware
245
+ middleware: _.current?.middleware
246
246
  });
247
- const o = `${g}////${L.current}`, a = n.getState().stateComponents.get(g) || {
247
+ const o = `${g}////${q.current}`, a = n.getState().stateComponents.get(g) || {
248
248
  components: /* @__PURE__ */ new Map()
249
249
  };
250
250
  return a.components.set(o, {
251
- forceUpdate: () => V({}),
251
+ forceUpdate: () => A({}),
252
252
  paths: /* @__PURE__ */ new Set(),
253
253
  deps: [],
254
- depsFunction: A || void 0,
255
- reactiveType: p ?? ["component", "deps"]
256
- }), n.getState().stateComponents.set(g, a), V({}), () => {
257
- const d = `${g}////${L.current}`;
258
- a && (a.components.delete(d), a.components.size === 0 && n.getState().stateComponents.delete(g));
254
+ depsFunction: T || void 0,
255
+ reactiveType: h ?? ["component", "deps"]
256
+ }), n.getState().stateComponents.set(g, a), A({}), () => {
257
+ const l = `${g}////${q.current}`;
258
+ a && (a.components.delete(l), a.components.size === 0 && n.getState().stateComponents.delete(g));
259
259
  };
260
260
  }, []);
261
- const l = (o, a, d, w) => {
261
+ const d = (o, a, l, w) => {
262
262
  if (Array.isArray(a)) {
263
- const S = `${g}-${a.join(".")}`;
264
- H.current.add(S);
263
+ const y = `${g}-${a.join(".")}`;
264
+ H.current.add(y);
265
265
  }
266
- J(g, (S) => {
267
- const h = z(o) ? o(S) : o, E = `${g}-${a.join(".")}`;
268
- if (E) {
269
- let D = !1, T = n.getState().signalDomElements.get(E);
270
- if ((!T || T.size === 0) && (d.updateType === "insert" || d.updateType === "cut")) {
271
- const O = a.slice(0, -1), U = W(h, O);
272
- if (Array.isArray(U)) {
273
- D = !0;
274
- const k = `${g}-${O.join(".")}`;
275
- T = n.getState().signalDomElements.get(k);
266
+ B(g, (y) => {
267
+ const E = J(o) ? o(y) : o, p = `${g}-${a.join(".")}`;
268
+ if (p) {
269
+ let M = !1, N = n.getState().signalDomElements.get(p);
270
+ if ((!N || N.size === 0) && (l.updateType === "insert" || l.updateType === "cut")) {
271
+ const F = a.slice(0, -1), D = W(E, F);
272
+ if (Array.isArray(D)) {
273
+ M = !0;
274
+ const $ = `${g}-${F.join(".")}`;
275
+ N = n.getState().signalDomElements.get($);
276
276
  }
277
277
  }
278
- if (T) {
279
- const O = D ? W(h, a.slice(0, -1)) : W(h, a);
280
- T.forEach(({ parentId: U, position: k, effect: x }) => {
278
+ if (N) {
279
+ const F = M ? W(E, a.slice(0, -1)) : W(E, a);
280
+ N.forEach(({ parentId: D, position: $, effect: O }) => {
281
281
  const C = document.querySelector(
282
- `[data-parent-id="${U}"]`
282
+ `[data-parent-id="${D}"]`
283
283
  );
284
284
  if (C) {
285
- const G = Array.from(C.childNodes);
286
- if (G[k]) {
287
- const F = x ? new Function("state", `return (${x})(state)`)(O) : O;
288
- G[k].textContent = String(F);
285
+ const L = Array.from(C.childNodes);
286
+ if (L[$]) {
287
+ const R = O ? new Function("state", `return (${O})(state)`)(F) : F;
288
+ L[$].textContent = String(R);
289
289
  }
290
290
  }
291
291
  });
292
292
  }
293
293
  }
294
- d.updateType === "update" && (w || b.current?.validation?.key) && a && q(
295
- (w || b.current?.validation?.key) + "." + a.join(".")
294
+ l.updateType === "update" && (w || _.current?.validation?.key) && a && z(
295
+ (w || _.current?.validation?.key) + "." + a.join(".")
296
296
  );
297
- const $ = a.slice(0, a.length - 1);
298
- d.updateType === "cut" && b.current?.validation?.key && q(
299
- b.current?.validation?.key + "." + $.join(".")
300
- ), d.updateType === "insert" && b.current?.validation?.key && xe(
301
- b.current?.validation?.key + "." + $.join(".")
302
- ).filter(([T, O]) => {
303
- let U = T?.split(".").length;
304
- if (T == $.join(".") && U == $.length - 1) {
305
- let k = T + "." + $;
306
- q(T), Fe(k, O);
297
+ const k = a.slice(0, a.length - 1);
298
+ l.updateType === "cut" && _.current?.validation?.key && z(
299
+ _.current?.validation?.key + "." + k.join(".")
300
+ ), l.updateType === "insert" && _.current?.validation?.key && xe(
301
+ _.current?.validation?.key + "." + k.join(".")
302
+ ).filter(([N, F]) => {
303
+ let D = N?.split(".").length;
304
+ if (N == k.join(".") && D == k.length - 1) {
305
+ let $ = N + "." + k;
306
+ z(N), Fe($, F);
307
307
  }
308
308
  });
309
- const N = n.getState().stateComponents.get(g);
310
- if (N) {
311
- const D = he(S, h), T = new Set(D), O = d.updateType === "update" ? a.join(".") : a.slice(0, -1).join(".") || "";
309
+ const b = n.getState().stateComponents.get(g);
310
+ if (b) {
311
+ const M = ue(y, E), N = new Set(M), F = l.updateType === "update" ? a.join(".") : a.slice(0, -1).join(".") || "";
312
312
  for (const [
313
- U,
314
- k
315
- ] of N.components.entries()) {
316
- let x = !1;
317
- const C = Array.isArray(k.reactiveType) ? k.reactiveType : [k.reactiveType || "component"];
313
+ D,
314
+ $
315
+ ] of b.components.entries()) {
316
+ let O = !1;
317
+ const C = Array.isArray($.reactiveType) ? $.reactiveType : [$.reactiveType || "component"];
318
318
  if (!C.includes("none")) {
319
319
  if (C.includes("all")) {
320
- k.forceUpdate();
320
+ $.forceUpdate();
321
321
  continue;
322
322
  }
323
- if (C.includes("component") && ((k.paths.has(O) || k.paths.has("")) && (x = !0), !x))
324
- for (const G of T) {
325
- let F = G;
323
+ if (C.includes("component") && (($.paths.has(F) || $.paths.has("")) && (O = !0), !O))
324
+ for (const L of N) {
325
+ let R = L;
326
326
  for (; ; ) {
327
- if (k.paths.has(F)) {
328
- x = !0;
327
+ if ($.paths.has(R)) {
328
+ O = !0;
329
329
  break;
330
330
  }
331
- const ie = F.lastIndexOf(".");
331
+ const ie = R.lastIndexOf(".");
332
332
  if (ie !== -1) {
333
- const fe = F.substring(
333
+ const Se = R.substring(
334
334
  0,
335
335
  ie
336
336
  );
337
337
  if (!isNaN(
338
- Number(F.substring(ie + 1))
339
- ) && k.paths.has(fe)) {
340
- x = !0;
338
+ Number(R.substring(ie + 1))
339
+ ) && $.paths.has(Se)) {
340
+ O = !0;
341
341
  break;
342
342
  }
343
- F = fe;
343
+ R = Se;
344
344
  } else
345
- F = "";
346
- if (F === "")
345
+ R = "";
346
+ if (R === "")
347
347
  break;
348
348
  }
349
- if (x) break;
349
+ if (O) break;
350
350
  }
351
- if (!x && C.includes("deps") && k.depsFunction) {
352
- const G = k.depsFunction(h);
353
- let F = !1;
354
- typeof G == "boolean" ? G && (F = !0) : M(k.deps, G) || (k.deps = G, F = !0), F && (x = !0);
351
+ if (!O && C.includes("deps") && $.depsFunction) {
352
+ const L = $.depsFunction(E);
353
+ let R = !1;
354
+ typeof L == "boolean" ? L && (R = !0) : G($.deps, L) || ($.deps = L, R = !0), R && (O = !0);
355
355
  }
356
- x && k.forceUpdate();
356
+ O && $.forceUpdate();
357
357
  }
358
358
  }
359
359
  }
360
360
  const P = Date.now();
361
- a = a.map((D, T) => {
362
- const O = a.slice(0, -1), U = W(h, O);
363
- return T === a.length - 1 && ["insert", "cut"].includes(d.updateType) ? (U.length - 1).toString() : D;
361
+ a = a.map((M, N) => {
362
+ const F = a.slice(0, -1), D = W(E, F);
363
+ return N === a.length - 1 && ["insert", "cut"].includes(l.updateType) ? (D.length - 1).toString() : M;
364
364
  }), console.log(
365
365
  "mmmmmmmmmmmmmmmmm22222222222222",
366
- d.updateType,
367
- S,
368
- h,
366
+ l.updateType,
367
+ y,
368
+ E,
369
369
  a
370
370
  );
371
- const { oldValue: B, newValue: ae } = Ue(
372
- d.updateType,
373
- S,
374
- h,
371
+ const { oldValue: x, newValue: ae } = Ue(
372
+ l.updateType,
373
+ y,
374
+ E,
375
375
  a
376
376
  ), oe = {
377
377
  timeStamp: P,
378
378
  stateKey: g,
379
379
  path: a,
380
- updateType: d.updateType,
380
+ updateType: l.updateType,
381
381
  status: "new",
382
- oldValue: B,
382
+ oldValue: x,
383
383
  newValue: ae
384
384
  };
385
- if (Oe(g, (D) => {
386
- const O = [...D ?? [], oe].reduce((U, k) => {
387
- const x = `${k.stateKey}:${JSON.stringify(k.path)}`, C = U.get(x);
388
- return C ? (C.timeStamp = Math.max(C.timeStamp, k.timeStamp), C.newValue = k.newValue, C.oldValue = C.oldValue ?? k.oldValue, C.updateType = k.updateType) : U.set(x, { ...k }), U;
385
+ if (Oe(g, (M) => {
386
+ const F = [...M ?? [], oe].reduce((D, $) => {
387
+ const O = `${$.stateKey}:${JSON.stringify($.path)}`, C = D.get(O);
388
+ return C ? (C.timeStamp = Math.max(C.timeStamp, $.timeStamp), C.newValue = $.newValue, C.oldValue = C.oldValue ?? $.oldValue, C.updateType = $.updateType) : D.set(O, { ...$ }), D;
389
389
  }, /* @__PURE__ */ new Map());
390
- return Array.from(O.values());
391
- }), Ie(
392
- h,
390
+ return Array.from(F.values());
391
+ }), he(
392
+ E,
393
393
  g,
394
- b.current,
395
- _
396
- ), b.current?.middleware && b.current.middleware({
394
+ _.current,
395
+ V
396
+ ), _.current?.middleware && _.current.middleware({
397
397
  updateLog: c,
398
398
  update: oe
399
- }), b.current?.serverSync) {
400
- const D = n.getState().serverState[g], T = b.current?.serverSync;
401
- je(g, {
402
- syncKey: typeof T.syncKey == "string" ? T.syncKey : T.syncKey({ state: h }),
403
- rollBackState: D,
404
- actionTimeStamp: Date.now() + (T.debounce ?? 3e3),
399
+ }), _.current?.serverSync) {
400
+ const M = n.getState().serverState[g], N = _.current?.serverSync;
401
+ Re(g, {
402
+ syncKey: typeof N.syncKey == "string" ? N.syncKey : N.syncKey({ state: E }),
403
+ rollBackState: M,
404
+ actionTimeStamp: Date.now() + (N.debounce ?? 3e3),
405
405
  status: "waiting"
406
406
  });
407
407
  }
408
- return h;
408
+ return E;
409
409
  });
410
410
  };
411
411
  n.getState().updaterState[g] || (K(
412
412
  g,
413
413
  ee(
414
414
  g,
415
- l,
416
- L.current,
417
- _
415
+ d,
416
+ q.current,
417
+ V
418
418
  )
419
- ), n.getState().cogsStateStore[g] || J(g, e), n.getState().initialStateGlobal[g] || ge(g, e));
419
+ ), n.getState().cogsStateStore[g] || B(g, e), n.getState().initialStateGlobal[g] || fe(g, e));
420
420
  const r = ke(() => ee(
421
421
  g,
422
- l,
423
- L.current,
424
- _
422
+ d,
423
+ q.current,
424
+ V
425
425
  ), [g]);
426
426
  return [Ee(g), r];
427
427
  }
428
- function ee(e, i, m, u) {
428
+ function ee(e, i, S, u) {
429
429
  const f = /* @__PURE__ */ new Map();
430
- let A = 0;
431
- const p = (y) => {
432
- const t = y.join(".");
430
+ let T = 0;
431
+ const h = (m) => {
432
+ const t = m.join(".");
433
433
  for (const [I] of f)
434
434
  (I === t || I.startsWith(t + ".")) && f.delete(I);
435
- A++;
435
+ T++;
436
436
  }, v = {
437
- removeValidation: (y) => {
438
- y?.validationKey && q(y.validationKey);
437
+ removeValidation: (m) => {
438
+ m?.validationKey && z(m.validationKey);
439
439
  },
440
- revertToInitialState: (y) => {
440
+ revertToInitialState: (m) => {
441
441
  const t = n.getState().getInitialOptions(e)?.validation;
442
- t?.key && q(t?.key), y?.validationKey && q(y.validationKey);
442
+ t?.key && z(t?.key), m?.validationKey && z(m.validationKey);
443
443
  const I = n.getState().initialStateGlobal[e];
444
- n.getState().clearSelectedIndexesForState(e), f.clear(), A++;
445
- const j = s(I, []), V = Z(e), _ = z(V?.localStorage?.key) ? V?.localStorage?.key(I) : V?.localStorage?.key, R = `${u}-${e}-${_}`;
446
- R && localStorage.removeItem(R), K(e, j), J(e, I);
444
+ n.getState().clearSelectedIndexesForState(e), f.clear(), T++;
445
+ const j = s(I, []), A = Z(e), V = J(A?.localStorage?.key) ? A?.localStorage?.key(I) : A?.localStorage?.key, U = `${u}-${e}-${V}`;
446
+ U && localStorage.removeItem(U), K(e, j), B(e, I);
447
447
  const g = n.getState().stateComponents.get(e);
448
448
  return g && g.components.forEach((c) => {
449
449
  c.forceUpdate();
450
450
  }), I;
451
451
  },
452
- updateInitialState: (y) => {
453
- f.clear(), A++;
452
+ updateInitialState: (m) => {
453
+ f.clear(), T++;
454
454
  const t = ee(
455
455
  e,
456
456
  i,
457
- m,
457
+ S,
458
458
  u
459
- ), I = n.getState().initialStateGlobal[e], j = Z(e), V = z(j?.localStorage?.key) ? j?.localStorage?.key(I) : j?.localStorage?.key, _ = `${u}-${e}-${V}`;
460
- return console.log("removing storage", _), localStorage.getItem(_) && localStorage.removeItem(_), Te(() => {
461
- ge(e, y), K(e, t), J(e, y);
462
- const R = n.getState().stateComponents.get(e);
463
- R && R.components.forEach((g) => {
459
+ ), I = n.getState().initialStateGlobal[e], j = Z(e), A = J(j?.localStorage?.key) ? j?.localStorage?.key(I) : j?.localStorage?.key, V = `${u}-${e}-${A}`;
460
+ return console.log("removing storage", V), localStorage.getItem(V) && localStorage.removeItem(V), Ne(() => {
461
+ fe(e, m), K(e, t), B(e, m);
462
+ const U = n.getState().stateComponents.get(e);
463
+ U && U.components.forEach((g) => {
464
464
  g.forceUpdate();
465
465
  });
466
466
  }), {
467
- fetchId: (R) => t.get()[R]
467
+ fetchId: (U) => t.get()[U]
468
468
  };
469
469
  },
470
470
  _initialState: n.getState().initialStateGlobal[e],
471
471
  _serverState: n.getState().serverState[e],
472
472
  _isLoading: n.getState().isLoadingGlobal[e],
473
473
  _isServerSynced: () => {
474
- const y = n.getState().serverState[e];
475
- return !!(y && M(y, Ee(e)));
474
+ const m = n.getState().serverState[e];
475
+ return !!(m && G(m, Ee(e)));
476
476
  }
477
477
  };
478
- function s(y, t = [], I) {
478
+ function s(m, t = [], I) {
479
479
  const j = t.map(String).join(".");
480
- f.get(j);
481
- const V = function() {
480
+ console.log("rebuildStateShape", t), f.get(j);
481
+ const A = function() {
482
482
  return n().getNestedState(e, t);
483
483
  };
484
484
  Object.keys(v).forEach((g) => {
485
- V[g] = v[g];
485
+ A[g] = v[g];
486
486
  });
487
- const _ = {
487
+ const V = {
488
488
  apply(g, c, H) {
489
489
  return console.log(
490
490
  `PROXY APPLY TRAP HIT: stateKey=${e}, path=${t.join(".")}`
@@ -492,66 +492,66 @@ function ee(e, i, m, u) {
492
492
  },
493
493
  get(g, c) {
494
494
  if (c !== "then" && !c.startsWith("$") && c !== "stateMapNoRender") {
495
- const l = t.join("."), r = `${e}////${m}`, o = n.getState().stateComponents.get(e);
495
+ const d = t.join("."), r = `${e}////${S}`, o = n.getState().stateComponents.get(e);
496
496
  if (o) {
497
497
  const a = o.components.get(r);
498
- a && (t.length > 0 || c === "get") && a.paths.add(l);
498
+ a && (t.length > 0 || c === "get") && a.paths.add(d);
499
499
  }
500
500
  }
501
501
  if (c === "getDifferences")
502
- return () => he(
502
+ return () => ue(
503
503
  n.getState().cogsStateStore[e],
504
504
  n.getState().initialStateGlobal[e]
505
505
  );
506
506
  if (c === "sync" && t.length === 0)
507
507
  return async function() {
508
- const l = n.getState().getInitialOptions(e), r = l?.sync;
508
+ const d = n.getState().getInitialOptions(e), r = d?.sync;
509
509
  if (!r)
510
510
  return console.error(`No mutation defined for state key "${e}"`), { success: !1, error: "No mutation defined" };
511
- const o = n.getState().getNestedState(e, []), a = l?.validation?.key;
511
+ const o = n.getState().getNestedState(e, []), a = d?.validation?.key;
512
512
  try {
513
- const d = await r.action(o);
514
- if (d && !d.success && d.errors && a) {
515
- n.getState().removeValidationError(a), d.errors.forEach((S) => {
516
- const h = [a, ...S.path].join(".");
517
- n.getState().addValidationError(h, S.message);
513
+ const l = await r.action(o);
514
+ if (l && !l.success && l.errors && a) {
515
+ n.getState().removeValidationError(a), l.errors.forEach((y) => {
516
+ const E = [a, ...y.path].join(".");
517
+ n.getState().addValidationError(E, y.message);
518
518
  });
519
519
  const w = n.getState().stateComponents.get(e);
520
- w && w.components.forEach((S) => {
521
- S.forceUpdate();
520
+ w && w.components.forEach((y) => {
521
+ y.forceUpdate();
522
522
  });
523
523
  }
524
- return d?.success && r.onSuccess ? r.onSuccess(d.data) : !d?.success && r.onError && r.onError(d.error), d;
525
- } catch (d) {
526
- return r.onError && r.onError(d), { success: !1, error: d };
524
+ return l?.success && r.onSuccess ? r.onSuccess(l.data) : !l?.success && r.onError && r.onError(l.error), l;
525
+ } catch (l) {
526
+ return r.onError && r.onError(l), { success: !1, error: l };
527
527
  }
528
528
  };
529
529
  if (c === "_status") {
530
- const l = n.getState().getNestedState(e, t), r = n.getState().initialStateGlobal[e], o = W(r, t);
531
- return M(l, o) ? "fresh" : "stale";
530
+ const d = n.getState().getNestedState(e, t), r = n.getState().initialStateGlobal[e], o = W(r, t);
531
+ return G(d, o) ? "fresh" : "stale";
532
532
  }
533
533
  if (c === "getStatus")
534
534
  return function() {
535
- const l = n().getNestedState(
535
+ const d = n().getNestedState(
536
536
  e,
537
537
  t
538
538
  ), r = n.getState().initialStateGlobal[e], o = W(r, t);
539
- return M(l, o) ? "fresh" : "stale";
539
+ return G(d, o) ? "fresh" : "stale";
540
540
  };
541
541
  if (c === "removeStorage")
542
542
  return () => {
543
- const l = n.getState().initialStateGlobal[e], r = Z(e), o = z(r?.localStorage?.key) ? r?.localStorage?.key(l) : r?.localStorage?.key, a = `${u}-${e}-${o}`;
543
+ const d = n.getState().initialStateGlobal[e], r = Z(e), o = J(r?.localStorage?.key) ? r?.localStorage?.key(d) : r?.localStorage?.key, a = `${u}-${e}-${o}`;
544
544
  console.log("removing storage", a), a && localStorage.removeItem(a);
545
545
  };
546
546
  if (c === "showValidationErrors")
547
547
  return () => {
548
- const l = n.getState().getInitialOptions(e)?.validation;
549
- if (!l?.key)
548
+ const d = n.getState().getInitialOptions(e)?.validation;
549
+ if (!d?.key)
550
550
  throw new Error("Validation key not found");
551
- return n.getState().getValidationErrors(l.key + "." + t.join("."));
551
+ return n.getState().getValidationErrors(d.key + "." + t.join("."));
552
552
  };
553
- if (Array.isArray(y)) {
554
- const l = () => I?.validIndices ? y.map((o, a) => ({
553
+ if (Array.isArray(m)) {
554
+ const d = () => I?.validIndices ? m.map((o, a) => ({
555
555
  item: o,
556
556
  originalIndex: I.validIndices[a]
557
557
  })) : n.getState().getNestedState(e, t).map((o, a) => ({
@@ -563,7 +563,7 @@ function ee(e, i, m, u) {
563
563
  const r = n.getState().getSelectedIndex(e, t.join("."));
564
564
  if (r !== void 0)
565
565
  return s(
566
- y[r],
566
+ m[r],
567
567
  [...t, r.toString()],
568
568
  I
569
569
  );
@@ -576,39 +576,39 @@ function ee(e, i, m, u) {
576
576
  return () => n.getState().getSelectedIndex(e, t.join(".")) ?? -1;
577
577
  if (c === "stateSort")
578
578
  return (r) => {
579
- const a = [...l()].sort(
580
- (S, h) => r(S.item, h.item)
581
- ), d = a.map(({ item: S }) => S), w = {
579
+ const a = [...d()].sort(
580
+ (y, E) => r(y.item, E.item)
581
+ ), l = a.map(({ item: y }) => y), w = {
582
582
  ...I,
583
583
  validIndices: a.map(
584
- ({ originalIndex: S }) => S
584
+ ({ originalIndex: y }) => y
585
585
  )
586
586
  };
587
- return s(d, t, w);
587
+ return s(l, t, w);
588
588
  };
589
589
  if (c === "stateFilter")
590
590
  return (r) => {
591
- const a = l().filter(
592
- ({ item: S }, h) => r(S, h)
593
- ), d = a.map(({ item: S }) => S), w = {
591
+ const a = d().filter(
592
+ ({ item: y }, E) => r(y, E)
593
+ ), l = a.map(({ item: y }) => y), w = {
594
594
  ...I,
595
595
  validIndices: a.map(
596
- ({ originalIndex: S }) => S
596
+ ({ originalIndex: y }) => y
597
597
  )
598
598
  };
599
- return s(d, t, w);
599
+ return s(l, t, w);
600
600
  };
601
601
  if (c === "stateMap" || c === "stateMapNoRender")
602
- return (r) => y.map((a, d) => {
602
+ return (r) => m.map((a, l) => {
603
603
  let w;
604
- I?.validIndices && I.validIndices[d] !== void 0 ? w = I.validIndices[d] : w = d;
605
- const S = [...t, w.toString()], h = s(a, S, I);
604
+ I?.validIndices && I.validIndices[l] !== void 0 ? w = I.validIndices[l] : w = l;
605
+ const y = [...t, w.toString()], E = s(a, y, I);
606
606
  return r(
607
607
  a,
608
- h,
609
- d,
610
- y,
611
- s(y, t, I)
608
+ E,
609
+ l,
610
+ m,
611
+ s(m, t, I)
612
612
  );
613
613
  });
614
614
  if (c === "$stateMap")
@@ -623,10 +623,10 @@ function ee(e, i, m, u) {
623
623
  });
624
624
  if (c === "stateFlattenOn")
625
625
  return (r) => {
626
- const o = y;
627
- f.clear(), A++;
626
+ const o = m;
627
+ f.clear(), T++;
628
628
  const a = o.flatMap(
629
- (d) => d[r] ?? []
629
+ (l) => l[r] ?? []
630
630
  );
631
631
  return s(
632
632
  a,
@@ -636,87 +636,87 @@ function ee(e, i, m, u) {
636
636
  };
637
637
  if (c === "index")
638
638
  return (r) => {
639
- const o = y[r];
639
+ const o = m[r];
640
640
  return s(o, [...t, r.toString()]);
641
641
  };
642
642
  if (c === "last")
643
643
  return () => {
644
644
  const r = n.getState().getNestedState(e, t);
645
645
  if (r.length === 0) return;
646
- const o = r.length - 1, a = r[o], d = [...t, o.toString()];
647
- return s(a, d);
646
+ const o = r.length - 1, a = r[o], l = [...t, o.toString()];
647
+ return s(a, l);
648
648
  };
649
649
  if (c === "insert")
650
- return (r) => (p(t), se(i, r, t, e), s(
650
+ return (r) => (h(t), se(i, r, t, e), s(
651
651
  n.getState().getNestedState(e, t),
652
652
  t
653
653
  ));
654
654
  if (c === "uniqueInsert")
655
655
  return (r, o, a) => {
656
- const d = n.getState().getNestedState(e, t), w = z(r) ? r(d) : r;
657
- let S = null;
658
- if (!d.some((E) => {
656
+ const l = n.getState().getNestedState(e, t), w = J(r) ? r(l) : r;
657
+ let y = null;
658
+ if (!l.some((p) => {
659
659
  if (o) {
660
- const N = o.every(
661
- (P) => M(E[P], w[P])
660
+ const b = o.every(
661
+ (P) => G(p[P], w[P])
662
662
  );
663
- return N && (S = E), N;
663
+ return b && (y = p), b;
664
664
  }
665
- const $ = M(E, w);
666
- return $ && (S = E), $;
665
+ const k = G(p, w);
666
+ return k && (y = p), k;
667
667
  }))
668
- p(t), se(i, w, t, e);
669
- else if (a && S) {
670
- const E = a(S), $ = d.map(
671
- (N) => M(N, S) ? E : N
668
+ h(t), se(i, w, t, e);
669
+ else if (a && y) {
670
+ const p = a(y), k = l.map(
671
+ (b) => G(b, y) ? p : b
672
672
  );
673
- p(t), Y(i, $, t);
673
+ h(t), Y(i, k, t);
674
674
  }
675
675
  };
676
676
  if (c === "cut")
677
677
  return (r, o) => {
678
678
  if (!o?.waitForSync)
679
- return p(t), X(i, t, e, r), s(
679
+ return h(t), X(i, t, e, r), s(
680
680
  n.getState().getNestedState(e, t),
681
681
  t
682
682
  );
683
683
  };
684
684
  if (c === "cutByValue")
685
685
  return (r) => {
686
- for (let o = 0; o < y.length; o++)
687
- y[o] === r && X(i, t, e, o);
686
+ for (let o = 0; o < m.length; o++)
687
+ m[o] === r && X(i, t, e, o);
688
688
  };
689
689
  if (c === "toggleByValue")
690
690
  return (r) => {
691
- const o = y.findIndex((a) => a === r);
691
+ const o = m.findIndex((a) => a === r);
692
692
  o > -1 ? X(i, t, e, o) : se(i, r, t, e);
693
693
  };
694
694
  if (c === "stateFind")
695
695
  return (r) => {
696
- const a = l().find(
697
- ({ item: w }, S) => r(w, S)
696
+ const a = d().find(
697
+ ({ item: w }, y) => r(w, y)
698
698
  );
699
699
  if (!a) return;
700
- const d = [...t, a.originalIndex.toString()];
701
- return s(a.item, d, I);
700
+ const l = [...t, a.originalIndex.toString()];
701
+ return s(a.item, l, I);
702
702
  };
703
703
  if (c === "findWith")
704
704
  return (r, o) => {
705
- const d = l().find(
706
- ({ item: S }) => S[r] === o
705
+ const l = d().find(
706
+ ({ item: y }) => y[r] === o
707
707
  );
708
- if (!d) return;
709
- const w = [...t, d.originalIndex.toString()];
710
- return s(d.item, w, I);
708
+ if (!l) return;
709
+ const w = [...t, l.originalIndex.toString()];
710
+ return s(l.item, w, I);
711
711
  };
712
712
  }
713
713
  const H = t[t.length - 1];
714
714
  if (!isNaN(Number(H))) {
715
- const l = t.slice(0, -1), r = n.getState().getNestedState(e, l);
715
+ const d = t.slice(0, -1), r = n.getState().getNestedState(e, d);
716
716
  if (Array.isArray(r) && c === "cut")
717
717
  return () => X(
718
718
  i,
719
- l,
719
+ d,
720
720
  e,
721
721
  Number(H)
722
722
  );
@@ -724,16 +724,16 @@ function ee(e, i, m, u) {
724
724
  if (c === "get")
725
725
  return () => n.getState().getNestedState(e, t);
726
726
  if (c === "$derive")
727
- return (l) => ce({
727
+ return (d) => ce({
728
728
  _stateKey: e,
729
729
  _path: t,
730
- _effect: l.toString()
730
+ _effect: d.toString()
731
731
  });
732
732
  if (c === "$derive")
733
- return (l) => ce({
733
+ return (d) => ce({
734
734
  _stateKey: e,
735
735
  _path: t,
736
- _effect: l.toString()
736
+ _effect: d.toString()
737
737
  });
738
738
  if (c === "$get")
739
739
  return () => ce({
@@ -741,125 +741,114 @@ function ee(e, i, m, u) {
741
741
  _path: t
742
742
  });
743
743
  if (c === "lastSynced") {
744
- const l = `${e}:${t.join(".")}`;
745
- return n.getState().getSyncInfo(l);
744
+ const d = `${e}:${t.join(".")}`;
745
+ return n.getState().getSyncInfo(d);
746
746
  }
747
747
  if (c == "getLocalStorage")
748
- return (l) => ne(u + "-" + e + "-" + l);
748
+ return (d) => ne(u + "-" + e + "-" + d);
749
749
  if (c === "_selected") {
750
- const l = t.slice(0, -1), r = l.join("."), o = n.getState().getNestedState(e, l);
750
+ const d = t.slice(0, -1), r = d.join("."), o = n.getState().getNestedState(e, d);
751
751
  return Array.isArray(o) ? Number(t[t.length - 1]) === n.getState().getSelectedIndex(e, r) : void 0;
752
752
  }
753
753
  if (c === "setSelected")
754
- return (l) => {
754
+ return (d) => {
755
755
  const r = t.slice(0, -1), o = Number(t[t.length - 1]), a = r.join(".");
756
- l ? n.getState().setSelectedIndex(e, a, o) : n.getState().setSelectedIndex(e, a, void 0);
757
- const d = n.getState().getNestedState(e, [...r]);
758
- Y(i, d, r), p(r);
756
+ d ? n.getState().setSelectedIndex(e, a, o) : n.getState().setSelectedIndex(e, a, void 0);
757
+ const l = n.getState().getNestedState(e, [...r]);
758
+ Y(i, l, r), h(r);
759
759
  };
760
760
  if (c === "toggleSelected")
761
761
  return () => {
762
- const l = t.slice(0, -1), r = Number(t[t.length - 1]), o = l.join("."), a = n.getState().getSelectedIndex(e, o);
762
+ const d = t.slice(0, -1), r = Number(t[t.length - 1]), o = d.join("."), a = n.getState().getSelectedIndex(e, o);
763
763
  n.getState().setSelectedIndex(
764
764
  e,
765
765
  o,
766
766
  a === r ? void 0 : r
767
767
  );
768
- const d = n.getState().getNestedState(e, [...l]);
769
- Y(i, d, l), p(l);
768
+ const l = n.getState().getNestedState(e, [...d]);
769
+ Y(i, l, d), h(d);
770
770
  };
771
771
  if (t.length == 0) {
772
772
  if (c === "applyJsonPatch")
773
- return (l) => {
774
- const r = n.getState().cogsStateStore[e], a = Ce(r, l).newDocument;
775
- $e(
773
+ return (d) => {
774
+ const r = n.getState().cogsStateStore[e], a = Pe(r, d).newDocument;
775
+ pe(
776
776
  e,
777
777
  n.getState().initialStateGlobal[e],
778
778
  a,
779
779
  i,
780
- m,
780
+ S,
781
781
  u
782
782
  );
783
- const d = n.getState().stateComponents.get(e);
784
- if (!d) return;
785
- const w = /* @__PURE__ */ new Set(), S = (h, E) => {
786
- w.add(h), Array.isArray(E) ? E.forEach(($, N) => {
787
- S(`${h}.${N}`, $);
788
- }) : typeof E == "object" && E !== null && Object.keys(E).forEach(($) => {
789
- S(`${h}.${$}`, E[$]);
790
- });
791
- };
792
- l.forEach((h) => {
793
- const E = h.path.slice(1).replace(/\//g, ".");
794
- w.add(E);
795
- let $ = E;
796
- for (; $.includes("."); )
797
- $ = $.substring(
798
- 0,
799
- $.lastIndexOf(".")
800
- ), w.add($);
801
- w.add(""), (h.op === "add" || h.op === "replace") && h.value && S(E, h.value);
802
- });
803
- for (const [
804
- h,
805
- E
806
- ] of d.components.entries()) {
807
- let $ = !1;
808
- const N = Array.isArray(E.reactiveType) ? E.reactiveType : [E.reactiveType || "component"];
809
- if (!N.includes("none")) {
810
- if (N.includes("all")) {
811
- E.forceUpdate();
812
- continue;
813
- }
814
- if (N.includes("component"))
815
- for (const P of E.paths) {
816
- if (w.has(P)) {
817
- $ = !0;
818
- break;
819
- }
820
- for (const B of w)
821
- if (P.startsWith(B + ".")) {
822
- $ = !0;
783
+ const l = n.getState().stateComponents.get(e);
784
+ if (l) {
785
+ const w = ue(r, a), y = new Set(w);
786
+ for (const [
787
+ E,
788
+ p
789
+ ] of l.components.entries()) {
790
+ let k = !1;
791
+ const b = Array.isArray(p.reactiveType) ? p.reactiveType : [p.reactiveType || "component"];
792
+ if (!b.includes("none")) {
793
+ if (b.includes("all")) {
794
+ p.forceUpdate();
795
+ continue;
796
+ }
797
+ if (b.includes("component") && (p.paths.has("") && (k = !0), !k))
798
+ for (const P of y) {
799
+ if (p.paths.has(P)) {
800
+ k = !0;
823
801
  break;
824
802
  }
825
- if ($) break;
803
+ let x = P;
804
+ for (; x.includes("."); )
805
+ if (x = x.substring(
806
+ 0,
807
+ x.lastIndexOf(".")
808
+ ), p.paths.has(x)) {
809
+ k = !0;
810
+ break;
811
+ }
812
+ if (k) break;
813
+ }
814
+ if (!k && b.includes("deps") && p.depsFunction) {
815
+ const P = p.depsFunction(a);
816
+ let x = !1;
817
+ typeof P == "boolean" ? P && (x = !0) : G(p.deps, P) || (p.deps = P, x = !0), x && (k = !0);
826
818
  }
827
- if (!$ && N.includes("deps") && E.depsFunction) {
828
- const P = E.depsFunction(a);
829
- (typeof P == "boolean" ? P : !M(E.deps, P)) && (E.deps = Array.isArray(P) ? P : [], $ = !0);
819
+ k && p.forceUpdate();
830
820
  }
831
- $ && E.forceUpdate();
832
821
  }
833
822
  }
834
823
  };
835
824
  if (c === "validateZodSchema")
836
825
  return () => {
837
- const l = n.getState().getInitialOptions(e)?.validation, r = n.getState().addValidationError;
838
- if (!l?.zodSchema)
826
+ const d = n.getState().getInitialOptions(e)?.validation, r = n.getState().addValidationError;
827
+ if (!d?.zodSchema)
839
828
  throw new Error("Zod schema not found");
840
- if (!l?.key)
829
+ if (!d?.key)
841
830
  throw new Error("Validation key not found");
842
- q(l.key);
831
+ z(d.key);
843
832
  const o = n.getState().cogsStateStore[e];
844
833
  try {
845
- const a = n.getState().getValidationErrors(l.key);
834
+ const a = n.getState().getValidationErrors(d.key);
846
835
  a && a.length > 0 && a.forEach(([w]) => {
847
- w && w.startsWith(l.key) && q(w);
836
+ w && w.startsWith(d.key) && z(w);
848
837
  });
849
- const d = l.zodSchema.safeParse(o);
850
- return d.success ? !0 : (d.error.errors.forEach((S) => {
851
- const h = S.path, E = S.message, $ = [l.key, ...h].join(".");
852
- r($, E);
838
+ const l = d.zodSchema.safeParse(o);
839
+ return l.success ? !0 : (l.error.errors.forEach((y) => {
840
+ const E = y.path, p = y.message, k = [d.key, ...E].join(".");
841
+ r(k, p);
853
842
  }), re(e), !1);
854
843
  } catch (a) {
855
844
  return console.error("Zod schema validation failed", a), !1;
856
845
  }
857
846
  };
858
- if (c === "_componentId") return m;
847
+ if (c === "_componentId") return S;
859
848
  if (c === "getComponents")
860
849
  return () => n().stateComponents.get(e);
861
850
  if (c === "getAllFormRefs")
862
- return () => me.getState().getFormRefsByStateKey(e);
851
+ return () => ye.getState().getFormRefsByStateKey(e);
863
852
  if (c === "_initialState")
864
853
  return n.getState().initialStateGlobal[e];
865
854
  if (c === "_serverState")
@@ -872,12 +861,12 @@ function ee(e, i, m, u) {
872
861
  if (c === "removeValidation") return v.removeValidation;
873
862
  }
874
863
  if (c === "getFormRef")
875
- return () => me.getState().getFormRef(e + "." + t.join("."));
864
+ return () => ye.getState().getFormRef(e + "." + t.join("."));
876
865
  if (c === "validationWrapper")
877
866
  return ({
878
- children: l,
867
+ children: d,
879
868
  hideMessage: r
880
- }) => /* @__PURE__ */ Se(
869
+ }) => /* @__PURE__ */ me(
881
870
  _e,
882
871
  {
883
872
  formOpts: r ? { validation: { message: "" } } : void 0,
@@ -885,83 +874,83 @@ function ee(e, i, m, u) {
885
874
  validationKey: n.getState().getInitialOptions(e)?.validation?.key || "",
886
875
  stateKey: e,
887
876
  validIndices: I?.validIndices,
888
- children: l
877
+ children: d
889
878
  }
890
879
  );
891
880
  if (c === "_stateKey") return e;
892
881
  if (c === "_path") return t;
893
882
  if (c === "_isServerSynced") return v._isServerSynced;
894
883
  if (c === "update")
895
- return (l, r) => {
884
+ return (d, r) => {
896
885
  if (r?.debounce)
897
886
  Ve(() => {
898
- Y(i, l, t, "");
887
+ Y(i, d, t, "");
899
888
  const o = n.getState().getNestedState(e, t);
900
889
  r?.afterUpdate && r.afterUpdate(o);
901
890
  }, r.debounce);
902
891
  else {
903
- Y(i, l, t, "");
892
+ Y(i, d, t, "");
904
893
  const o = n.getState().getNestedState(e, t);
905
894
  r?.afterUpdate && r.afterUpdate(o);
906
895
  }
907
- p(t);
896
+ h(t);
908
897
  };
909
898
  if (c === "formElement")
910
- return (l, r) => /* @__PURE__ */ Se(
899
+ return (d, r) => /* @__PURE__ */ me(
911
900
  be,
912
901
  {
913
902
  setState: i,
914
903
  stateKey: e,
915
904
  path: t,
916
- child: l,
905
+ child: d,
917
906
  formOpts: r
918
907
  }
919
908
  );
920
- const L = [...t, c], b = n.getState().getNestedState(e, L);
921
- return s(b, L, I);
909
+ const q = [...t, c], _ = n.getState().getNestedState(e, q);
910
+ return s(_, q, I);
922
911
  }
923
- }, R = new Proxy(V, _);
912
+ }, U = new Proxy(A, V);
924
913
  return f.set(j, {
925
- proxy: R,
926
- stateVersion: A
927
- }), R;
914
+ proxy: U,
915
+ stateVersion: T
916
+ }), U;
928
917
  }
929
918
  return s(
930
919
  n.getState().getNestedState(e, [])
931
920
  );
932
921
  }
933
922
  function ce(e) {
934
- return te(We, { proxy: e });
923
+ return te(Ge, { proxy: e });
935
924
  }
936
925
  function Me({
937
926
  proxy: e,
938
927
  rebuildStateShape: i
939
928
  }) {
940
- const m = n().getNestedState(e._stateKey, e._path);
941
- return Array.isArray(m) ? i(
942
- m,
929
+ const S = n().getNestedState(e._stateKey, e._path);
930
+ return Array.isArray(S) ? i(
931
+ S,
943
932
  e._path
944
933
  ).stateMapNoRender(
945
- (f, A, p, v, s) => e._mapFn(f, A, p, v, s)
934
+ (f, T, h, v, s) => e._mapFn(f, T, h, v, s)
946
935
  ) : null;
947
936
  }
948
- function We({
937
+ function Ge({
949
938
  proxy: e
950
939
  }) {
951
- const i = Q(null), m = `${e._stateKey}-${e._path.join(".")}`;
940
+ const i = Q(null), S = `${e._stateKey}-${e._path.join(".")}`;
952
941
  return de(() => {
953
942
  const u = i.current;
954
943
  if (!u || !u.parentElement) return;
955
- const f = u.parentElement, p = Array.from(f.childNodes).indexOf(u);
944
+ const f = u.parentElement, h = Array.from(f.childNodes).indexOf(u);
956
945
  let v = f.getAttribute("data-parent-id");
957
946
  v || (v = `parent-${crypto.randomUUID()}`, f.setAttribute("data-parent-id", v));
958
- const y = {
947
+ const m = {
959
948
  instanceId: `instance-${crypto.randomUUID()}`,
960
949
  parentId: v,
961
- position: p,
950
+ position: h,
962
951
  effect: e._effect
963
952
  };
964
- n.getState().addSignalElement(m, y);
953
+ n.getState().addSignalElement(S, m);
965
954
  const t = n.getState().getNestedState(e._stateKey, e._path);
966
955
  let I;
967
956
  if (e._effect)
@@ -970,8 +959,8 @@ function We({
970
959
  "state",
971
960
  `return (${e._effect})(state)`
972
961
  )(t);
973
- } catch (V) {
974
- console.error("Error evaluating effect function during mount:", V), I = t;
962
+ } catch (A) {
963
+ console.error("Error evaluating effect function during mount:", A), I = t;
975
964
  }
976
965
  else
977
966
  I = t;
@@ -981,17 +970,17 @@ function We({
981
970
  }, [e._stateKey, e._path.join("."), e._effect]), te("span", {
982
971
  ref: i,
983
972
  style: { display: "none" },
984
- "data-signal-id": m
973
+ "data-signal-id": S
985
974
  });
986
975
  }
987
976
  function tt(e) {
988
- const i = Ae(
989
- (m) => {
977
+ const i = Te(
978
+ (S) => {
990
979
  const u = n.getState().stateComponents.get(e._stateKey) || {
991
980
  components: /* @__PURE__ */ new Map()
992
981
  };
993
982
  return u.components.set(e._stateKey, {
994
- forceUpdate: m,
983
+ forceUpdate: S,
995
984
  paths: /* @__PURE__ */ new Set([e._path.join(".")])
996
985
  }), () => u.components.delete(e._stateKey);
997
986
  },