cogsbox-state 0.5.289 → 0.5.290

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,17 +1,17 @@
1
1
  "use client";
2
- import { jsx as St } from "react/jsx-runtime";
3
- import { useState as K, useRef as H, useEffect as st, useLayoutEffect as it, useMemo as Tt, createElement as rt, useSyncExternalStore as Ct, startTransition as Vt, useCallback as vt } from "react";
4
- import { transformStateFunc as bt, isDeepEqual as L, isFunction as J, getNestedValue as z, getDifferences as mt, debounce as xt } from "./utility.js";
5
- import { pushFunc as gt, updateFn as nt, cutFunc as at, ValidationWrapper as Pt, FormControlComponent as _t } from "./Functions.jsx";
2
+ import { jsx as mt } from "react/jsx-runtime";
3
+ import { useState as K, useRef as q, useEffect as st, useLayoutEffect as it, useMemo as Tt, createElement as rt, useSyncExternalStore as Ct, startTransition as Vt, useCallback as ft } from "react";
4
+ import { transformStateFunc as bt, isDeepEqual as L, isFunction as J, getNestedValue as z, getDifferences as ht, debounce as xt } from "./utility.js";
5
+ import { pushFunc as St, updateFn as nt, cutFunc as ot, ValidationWrapper as Pt, FormControlComponent as _t } from "./Functions.jsx";
6
6
  import Mt from "superjson";
7
- import { v4 as ht } from "uuid";
7
+ import { v4 as yt } from "uuid";
8
8
  import "zod";
9
- import { getGlobalStore as o, formRefStore as It } from "./store.js";
9
+ import { getGlobalStore as r, formRefStore as It } from "./store.js";
10
10
  import { useCogsConfig as $t } from "./CogsStateClient.jsx";
11
11
  import { applyPatch as jt } from "fast-json-patch";
12
12
  import Ot from "react-use-measure";
13
13
  function pt(t, c) {
14
- const h = o.getState().getInitialOptions, g = o.getState().setInitialStateOptions, v = h(t) || {};
14
+ const h = r.getState().getInitialOptions, g = r.getState().setInitialStateOptions, v = h(t) || {};
15
15
  g(t, {
16
16
  ...v,
17
17
  ...c
@@ -22,18 +22,18 @@ function wt({
22
22
  options: c,
23
23
  initialOptionsPart: h
24
24
  }) {
25
- const g = Q(t) || {}, v = h[t] || {}, k = o.getState().setInitialStateOptions, p = { ...v, ...g };
25
+ const g = Q(t) || {}, v = h[t] || {}, $ = r.getState().setInitialStateOptions, p = { ...v, ...g };
26
26
  let I = !1;
27
27
  if (c)
28
- for (const a in c)
29
- p.hasOwnProperty(a) ? (a == "localStorage" && c[a] && p[a].key !== c[a]?.key && (I = !0, p[a] = c[a]), a == "initialState" && c[a] && p[a] !== c[a] && // Different references
30
- !L(p[a], c[a]) && (I = !0, p[a] = c[a])) : (I = !0, p[a] = c[a]);
31
- I && k(t, p);
28
+ for (const o in c)
29
+ p.hasOwnProperty(o) ? (o == "localStorage" && c[o] && p[o].key !== c[o]?.key && (I = !0, p[o] = c[o]), o == "initialState" && c[o] && p[o] !== c[o] && // Different references
30
+ !L(p[o], c[o]) && (I = !0, p[o] = c[o])) : (I = !0, p[o] = c[o]);
31
+ I && $(t, p);
32
32
  }
33
- function oe(t, { formElements: c, validation: h }) {
33
+ function ae(t, { formElements: c, validation: h }) {
34
34
  return { initialState: t, formElements: c, validation: h };
35
35
  }
36
- const ae = (t, c) => {
36
+ const oe = (t, c) => {
37
37
  let h = t;
38
38
  const [g, v] = bt(h);
39
39
  (Object.keys(v).length > 0 || c && Object.keys(c).length > 0) && Object.keys(v).forEach((I) => {
@@ -43,68 +43,68 @@ const ae = (t, c) => {
43
43
  ...c?.validation,
44
44
  ...v[I].formElements || {}
45
45
  // State-specific overrides
46
- }, Q(I) || o.getState().setInitialStateOptions(I, v[I]);
47
- }), o.getState().setInitialStates(g), o.getState().setCreatedState(g);
48
- const k = (I, a) => {
49
- const [y] = K(a?.componentId ?? ht());
46
+ }, Q(I) || r.getState().setInitialStateOptions(I, v[I]);
47
+ }), r.getState().setInitialStates(g), r.getState().setCreatedState(g);
48
+ const $ = (I, o) => {
49
+ const [y] = K(o?.componentId ?? yt());
50
50
  wt({
51
51
  stateKey: I,
52
- options: a,
52
+ options: o,
53
53
  initialOptionsPart: v
54
54
  });
55
- const r = o.getState().cogsStateStore[I] || g[I], S = a?.modifyState ? a.modifyState(r) : r, [W, O] = Lt(
55
+ const a = r.getState().cogsStateStore[I] || g[I], S = o?.modifyState ? o.modifyState(a) : a, [D, O] = Lt(
56
56
  S,
57
57
  {
58
58
  stateKey: I,
59
- syncUpdate: a?.syncUpdate,
59
+ syncUpdate: o?.syncUpdate,
60
60
  componentId: y,
61
- localStorage: a?.localStorage,
62
- middleware: a?.middleware,
63
- enabledSync: a?.enabledSync,
64
- reactiveType: a?.reactiveType,
65
- reactiveDeps: a?.reactiveDeps,
66
- initialState: a?.initialState,
67
- dependencies: a?.dependencies,
68
- serverState: a?.serverState
61
+ localStorage: o?.localStorage,
62
+ middleware: o?.middleware,
63
+ enabledSync: o?.enabledSync,
64
+ reactiveType: o?.reactiveType,
65
+ reactiveDeps: o?.reactiveDeps,
66
+ initialState: o?.initialState,
67
+ dependencies: o?.dependencies,
68
+ serverState: o?.serverState
69
69
  }
70
70
  );
71
71
  return O;
72
72
  };
73
- function p(I, a) {
74
- wt({ stateKey: I, options: a, initialOptionsPart: v }), a.localStorage && Wt(I, a), ut(I);
73
+ function p(I, o) {
74
+ wt({ stateKey: I, options: o, initialOptionsPart: v }), o.localStorage && Wt(I, o), ut(I);
75
75
  }
76
- return { useCogsState: k, setCogsOptions: p };
76
+ return { useCogsState: $, setCogsOptions: p };
77
77
  }, {
78
78
  setUpdaterState: ct,
79
79
  setState: Z,
80
80
  getInitialOptions: Q,
81
81
  getKeyState: kt,
82
- getValidationErrors: Rt,
83
- setStateLog: Ft,
84
- updateInitialStateGlobal: yt,
85
- addValidationError: Ut,
86
- removeValidationError: q,
82
+ getValidationErrors: Ut,
83
+ setStateLog: Rt,
84
+ updateInitialStateGlobal: vt,
85
+ addValidationError: Ft,
86
+ removeValidationError: H,
87
87
  setServerSyncActions: Dt
88
- } = o.getState(), Et = (t, c, h, g, v) => {
88
+ } = r.getState(), Et = (t, c, h, g, v) => {
89
89
  h?.log && console.log(
90
90
  "saving to localstorage",
91
91
  c,
92
92
  h.localStorage?.key,
93
93
  g
94
94
  );
95
- const k = J(h?.localStorage?.key) ? h.localStorage?.key(t) : h?.localStorage?.key;
96
- if (k && g) {
97
- const p = `${g}-${c}-${k}`;
95
+ const $ = J(h?.localStorage?.key) ? h.localStorage?.key(t) : h?.localStorage?.key;
96
+ if ($ && g) {
97
+ const p = `${g}-${c}-${$}`;
98
98
  let I;
99
99
  try {
100
100
  I = dt(p)?.lastSyncedWithServer;
101
101
  } catch {
102
102
  }
103
- const a = {
103
+ const o = {
104
104
  state: t,
105
105
  lastUpdated: Date.now(),
106
106
  lastSyncedWithServer: v ?? I
107
- }, y = Mt.serialize(a);
107
+ }, y = Mt.serialize(o);
108
108
  window.localStorage.setItem(
109
109
  p,
110
110
  JSON.stringify(y.json)
@@ -119,29 +119,29 @@ const ae = (t, c) => {
119
119
  return console.error("Error loading from localStorage:", c), null;
120
120
  }
121
121
  }, Wt = (t, c) => {
122
- const h = o.getState().cogsStateStore[t], { sessionId: g } = $t(), v = J(c?.localStorage?.key) ? c.localStorage.key(h) : c?.localStorage?.key;
122
+ const h = r.getState().cogsStateStore[t], { sessionId: g } = $t(), v = J(c?.localStorage?.key) ? c.localStorage.key(h) : c?.localStorage?.key;
123
123
  if (v && g) {
124
- const k = dt(
124
+ const $ = dt(
125
125
  `${g}-${t}-${v}`
126
126
  );
127
- if (k && k.lastUpdated > (k.lastSyncedWithServer || 0))
128
- return Z(t, k.state), ut(t), !0;
127
+ if ($ && $.lastUpdated > ($.lastSyncedWithServer || 0))
128
+ return Z(t, $.state), ut(t), !0;
129
129
  }
130
130
  return !1;
131
- }, Nt = (t, c, h, g, v, k) => {
131
+ }, Nt = (t, c, h, g, v, $) => {
132
132
  const p = {
133
133
  initialState: c,
134
134
  updaterState: lt(
135
135
  t,
136
136
  g,
137
137
  v,
138
- k
138
+ $
139
139
  ),
140
140
  state: h
141
141
  };
142
- yt(t, p.initialState), ct(t, p.updaterState), Z(t, p.state);
142
+ vt(t, p.initialState), ct(t, p.updaterState), Z(t, p.state);
143
143
  }, ut = (t) => {
144
- const c = o.getState().stateComponents.get(t);
144
+ const c = r.getState().stateComponents.get(t);
145
145
  if (!c) return;
146
146
  const h = /* @__PURE__ */ new Set();
147
147
  c.components.forEach((g) => {
@@ -150,7 +150,7 @@ const ae = (t, c) => {
150
150
  h.forEach((g) => g());
151
151
  });
152
152
  }, se = (t, c) => {
153
- const h = o.getState().stateComponents.get(t);
153
+ const h = r.getState().stateComponents.get(t);
154
154
  if (h) {
155
155
  const g = `${t}////${c}`, v = h.components.get(g);
156
156
  if ((v ? Array.isArray(v.reactiveType) ? v.reactiveType : [v.reactiveType || "component"] : null)?.includes("none"))
@@ -185,17 +185,17 @@ function Lt(t, {
185
185
  serverSync: h,
186
186
  localStorage: g,
187
187
  formElements: v,
188
- reactiveDeps: k,
188
+ reactiveDeps: $,
189
189
  reactiveType: p,
190
190
  componentId: I,
191
- initialState: a,
191
+ initialState: o,
192
192
  syncUpdate: y,
193
- dependencies: r,
193
+ dependencies: a,
194
194
  serverState: S
195
195
  } = {}) {
196
- const [W, O] = K({}), { sessionId: R } = $t();
197
- let G = !c;
198
- const [m] = K(c ?? ht()), l = o.getState().stateLog[m], ot = H(/* @__PURE__ */ new Set()), Y = H(I ?? ht()), j = H(
196
+ const [D, O] = K({}), { sessionId: U } = $t();
197
+ let W = !c;
198
+ const [m] = K(c ?? yt()), l = r.getState().stateLog[m], at = q(/* @__PURE__ */ new Set()), Y = q(I ?? yt()), j = q(
199
199
  null
200
200
  );
201
201
  j.current = Q(m) ?? null, st(() => {
@@ -205,168 +205,182 @@ function Lt(t, {
205
205
  [y.path[0]]: y.newValue
206
206
  }));
207
207
  const e = `${y.stateKey}:${y.path.join(".")}`;
208
- o.getState().setSyncInfo(e, {
208
+ r.getState().setSyncInfo(e, {
209
209
  timeStamp: y.timeStamp,
210
210
  userId: y.userId
211
211
  });
212
212
  }
213
213
  }, [y]), st(() => {
214
- if (a) {
214
+ if (o) {
215
215
  pt(m, {
216
- initialState: a
216
+ initialState: o
217
217
  });
218
- const e = j.current, s = e?.serverState?.id !== void 0 && e?.serverState?.status === "success" && e?.serverState?.data, i = o.getState().initialStateGlobal[m];
219
- if (!(i && !L(i, a) || !i) && !s)
218
+ const e = j.current, s = e?.serverState?.id !== void 0 && e?.serverState?.status === "success" && e?.serverState?.data, i = r.getState().initialStateGlobal[m];
219
+ if (!(i && !L(i, o) || !i) && !s)
220
220
  return;
221
- let u = null;
222
- const E = J(e?.localStorage?.key) ? e?.localStorage?.key(a) : e?.localStorage?.key;
223
- E && R && (u = dt(`${R}-${m}-${E}`));
224
- let w = a, T = !1;
225
- const x = s ? Date.now() : 0, C = u?.lastUpdated || 0, V = u?.lastSyncedWithServer || 0;
226
- s && x > C ? (w = e.serverState.data, T = !0) : u && C > V && (w = u.state, e?.localStorage?.onChange && e?.localStorage?.onChange(w)), o.getState().initializeShadowState(m, a), Nt(
221
+ let d = null;
222
+ const k = J(e?.localStorage?.key) ? e?.localStorage?.key(o) : e?.localStorage?.key;
223
+ k && U && (d = dt(`${U}-${m}-${k}`));
224
+ let w = o, T = !1;
225
+ const x = s ? Date.now() : 0, C = d?.lastUpdated || 0, V = d?.lastSyncedWithServer || 0;
226
+ s && x > C ? (w = e.serverState.data, T = !0) : d && C > V && (w = d.state, e?.localStorage?.onChange && e?.localStorage?.onChange(w)), r.getState().initializeShadowState(m, o), Nt(
227
227
  m,
228
- a,
228
+ o,
229
229
  w,
230
230
  tt,
231
231
  Y.current,
232
- R
233
- ), T && E && R && Et(w, m, e, R, Date.now()), ut(m), (Array.isArray(p) ? p : [p || "component"]).includes("none") || O({});
232
+ U
233
+ ), T && k && U && Et(w, m, e, U, Date.now()), ut(m), (Array.isArray(p) ? p : [p || "component"]).includes("none") || O({});
234
234
  }
235
235
  }, [
236
- a,
236
+ o,
237
237
  S?.status,
238
238
  S?.data,
239
- ...r || []
239
+ ...a || []
240
240
  ]), it(() => {
241
- G && pt(m, {
241
+ W && pt(m, {
242
242
  serverSync: h,
243
243
  formElements: v,
244
- initialState: a,
244
+ initialState: o,
245
245
  localStorage: g,
246
246
  middleware: j.current?.middleware
247
247
  });
248
- const e = `${m}////${Y.current}`, n = o.getState().stateComponents.get(m) || {
248
+ const e = `${m}////${Y.current}`, n = r.getState().stateComponents.get(m) || {
249
249
  components: /* @__PURE__ */ new Map()
250
250
  };
251
251
  return n.components.set(e, {
252
252
  forceUpdate: () => O({}),
253
253
  paths: /* @__PURE__ */ new Set(),
254
254
  deps: [],
255
- depsFunction: k || void 0,
255
+ depsFunction: $ || void 0,
256
256
  reactiveType: p ?? ["component", "deps"]
257
- }), o.getState().stateComponents.set(m, n), O({}), () => {
258
- n && (n.components.delete(e), n.components.size === 0 && o.getState().stateComponents.delete(m));
257
+ }), r.getState().stateComponents.set(m, n), O({}), () => {
258
+ n && (n.components.delete(e), n.components.size === 0 && r.getState().stateComponents.delete(m));
259
259
  };
260
260
  }, []);
261
261
  const tt = (e, n, s, i) => {
262
262
  if (Array.isArray(n)) {
263
- const u = `${m}-${n.join(".")}`;
264
- ot.current.add(u);
263
+ const f = `${m}-${n.join(".")}`;
264
+ at.current.add(f);
265
265
  }
266
- const f = o.getState();
267
- Z(m, (u) => {
268
- const E = J(e) ? e(u) : e, w = `${m}-${n.join(".")}`;
269
- if (w) {
270
- let _ = !1, N = f.signalDomElements.get(w);
266
+ Z(m, (f) => {
267
+ const d = J(e) ? e(f) : e, k = `${m}-${n.join(".")}`;
268
+ if (k) {
269
+ let _ = !1, N = r.getState().signalDomElements.get(k);
271
270
  if ((!N || N.size === 0) && (s.updateType === "insert" || s.updateType === "cut")) {
272
- const $ = n.slice(0, -1), M = z(E, $);
273
- if (Array.isArray(M)) {
271
+ const A = n.slice(0, -1), P = z(d, A);
272
+ if (Array.isArray(P)) {
274
273
  _ = !0;
275
- const A = `${m}-${$.join(".")}`;
276
- N = f.signalDomElements.get(A);
274
+ const E = `${m}-${A.join(".")}`;
275
+ N = r.getState().signalDomElements.get(E);
277
276
  }
278
277
  }
279
278
  if (N) {
280
- const $ = _ ? z(E, n.slice(0, -1)) : z(E, n);
281
- N.forEach(({ parentId: M, position: A, effect: P }) => {
279
+ const A = _ ? z(d, n.slice(0, -1)) : z(d, n);
280
+ N.forEach(({ parentId: P, position: E, effect: M }) => {
282
281
  const b = document.querySelector(
283
- `[data-parent-id="${M}"]`
282
+ `[data-parent-id="${P}"]`
284
283
  );
285
284
  if (b) {
286
- const U = Array.from(b.childNodes);
287
- if (U[A]) {
288
- const F = P ? new Function("state", `return (${P})(state)`)($) : $;
289
- U[A].textContent = String(F);
285
+ const F = Array.from(b.childNodes);
286
+ if (F[E]) {
287
+ const R = M ? new Function("state", `return (${M})(state)`)(A) : A;
288
+ F[E].textContent = String(R);
290
289
  }
291
290
  }
292
291
  });
293
292
  }
294
293
  }
295
- console.log("shadowState", f.shadowStateStore), s.updateType === "update" && (i || j.current?.validation?.key) && n && q(
294
+ (() => {
295
+ const _ = r.getState();
296
+ switch (s.updateType) {
297
+ case "update":
298
+ _.updateShadowAtPath(m, n, d);
299
+ break;
300
+ case "insert":
301
+ const N = n.slice(0, -1);
302
+ _.insertShadowArrayElement(m, N);
303
+ break;
304
+ case "cut":
305
+ const A = n.slice(0, -1), P = parseInt(n[n.length - 1]);
306
+ _.removeShadowArrayElement(m, A, P);
307
+ break;
308
+ }
309
+ })(), console.log("shadowState", r.getState().shadowStateStore), s.updateType === "update" && (i || j.current?.validation?.key) && n && H(
296
310
  (i || j.current?.validation?.key) + "." + n.join(".")
297
311
  );
298
312
  const T = n.slice(0, n.length - 1);
299
- s.updateType === "cut" && j.current?.validation?.key && q(
313
+ s.updateType === "cut" && j.current?.validation?.key && H(
300
314
  j.current?.validation?.key + "." + T.join(".")
301
- ), s.updateType === "insert" && j.current?.validation?.key && Rt(
315
+ ), s.updateType === "insert" && j.current?.validation?.key && Ut(
302
316
  j.current?.validation?.key + "." + T.join(".")
303
- ).filter(([N, $]) => {
304
- let M = N?.split(".").length;
305
- if (N == T.join(".") && M == T.length - 1) {
306
- let A = N + "." + T;
307
- q(N), Ut(A, $);
317
+ ).filter(([N, A]) => {
318
+ let P = N?.split(".").length;
319
+ if (N == T.join(".") && P == T.length - 1) {
320
+ let E = N + "." + T;
321
+ H(N), Ft(E, A);
308
322
  }
309
323
  });
310
- const x = f.stateComponents.get(m);
324
+ const x = r.getState().stateComponents.get(m);
311
325
  if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", x), x) {
312
- const _ = mt(u, E), N = new Set(_), $ = s.updateType === "update" ? n.join(".") : n.slice(0, -1).join(".") || "";
326
+ const _ = ht(f, d), N = new Set(_), A = s.updateType === "update" ? n.join(".") : n.slice(0, -1).join(".") || "";
313
327
  for (const [
314
- M,
315
- A
328
+ P,
329
+ E
316
330
  ] of x.components.entries()) {
317
- let P = !1;
318
- const b = Array.isArray(A.reactiveType) ? A.reactiveType : [A.reactiveType || "component"];
319
- if (console.log("component", A), !b.includes("none")) {
331
+ let M = !1;
332
+ const b = Array.isArray(E.reactiveType) ? E.reactiveType : [E.reactiveType || "component"];
333
+ if (console.log("component", E), !b.includes("none")) {
320
334
  if (b.includes("all")) {
321
- A.forceUpdate();
335
+ E.forceUpdate();
322
336
  continue;
323
337
  }
324
- if (b.includes("component") && ((A.paths.has($) || A.paths.has("")) && (P = !0), !P))
325
- for (const U of N) {
326
- let F = U;
338
+ if (b.includes("component") && ((E.paths.has(A) || E.paths.has("")) && (M = !0), !M))
339
+ for (const F of N) {
340
+ let R = F;
327
341
  for (; ; ) {
328
- if (A.paths.has(F)) {
329
- P = !0;
342
+ if (E.paths.has(R)) {
343
+ M = !0;
330
344
  break;
331
345
  }
332
- const X = F.lastIndexOf(".");
346
+ const X = R.lastIndexOf(".");
333
347
  if (X !== -1) {
334
- const et = F.substring(
348
+ const et = R.substring(
335
349
  0,
336
350
  X
337
351
  );
338
352
  if (!isNaN(
339
- Number(F.substring(X + 1))
340
- ) && A.paths.has(et)) {
341
- P = !0;
353
+ Number(R.substring(X + 1))
354
+ ) && E.paths.has(et)) {
355
+ M = !0;
342
356
  break;
343
357
  }
344
- F = et;
358
+ R = et;
345
359
  } else
346
- F = "";
347
- if (F === "")
360
+ R = "";
361
+ if (R === "")
348
362
  break;
349
363
  }
350
- if (P) break;
364
+ if (M) break;
351
365
  }
352
- if (!P && b.includes("deps") && A.depsFunction) {
353
- const U = A.depsFunction(E);
354
- let F = !1;
355
- typeof U == "boolean" ? U && (F = !0) : L(A.deps, U) || (A.deps = U, F = !0), F && (P = !0);
366
+ if (!M && b.includes("deps") && E.depsFunction) {
367
+ const F = E.depsFunction(d);
368
+ let R = !1;
369
+ typeof F == "boolean" ? F && (R = !0) : L(E.deps, F) || (E.deps = F, R = !0), R && (M = !0);
356
370
  }
357
- P && A.forceUpdate();
371
+ M && E.forceUpdate();
358
372
  }
359
373
  }
360
374
  }
361
375
  const C = Date.now();
362
376
  n = n.map((_, N) => {
363
- const $ = n.slice(0, -1), M = z(E, $);
364
- return N === n.length - 1 && ["insert", "cut"].includes(s.updateType) ? (M.length - 1).toString() : _;
377
+ const A = n.slice(0, -1), P = z(d, A);
378
+ return N === n.length - 1 && ["insert", "cut"].includes(s.updateType) ? (P.length - 1).toString() : _;
365
379
  });
366
- const { oldValue: V, newValue: D } = Gt(
380
+ const { oldValue: V, newValue: G } = Gt(
367
381
  s.updateType,
368
- u,
369
- E,
382
+ f,
383
+ d,
370
384
  n
371
385
  ), B = {
372
386
  timeStamp: C,
@@ -375,132 +389,119 @@ function Lt(t, {
375
389
  updateType: s.updateType,
376
390
  status: "new",
377
391
  oldValue: V,
378
- newValue: D
392
+ newValue: G
379
393
  };
380
- switch (s.updateType) {
381
- case "update":
382
- f.updateShadowAtPath(m, n, E);
383
- break;
384
- case "insert":
385
- const _ = n.slice(0, -1);
386
- f.insertShadowArrayElement(m, _, D);
387
- break;
388
- case "cut":
389
- const N = n.slice(0, -1), $ = parseInt(n[n.length - 1]);
390
- f.removeShadowArrayElement(m, N, $);
391
- break;
392
- }
393
- if (Ft(m, (_) => {
394
- const $ = [..._ ?? [], B].reduce((M, A) => {
395
- const P = `${A.stateKey}:${JSON.stringify(A.path)}`, b = M.get(P);
396
- return b ? (b.timeStamp = Math.max(b.timeStamp, A.timeStamp), b.newValue = A.newValue, b.oldValue = b.oldValue ?? A.oldValue, b.updateType = A.updateType) : M.set(P, { ...A }), M;
394
+ if (Rt(m, (_) => {
395
+ const A = [..._ ?? [], B].reduce((P, E) => {
396
+ const M = `${E.stateKey}:${JSON.stringify(E.path)}`, b = P.get(M);
397
+ return b ? (b.timeStamp = Math.max(b.timeStamp, E.timeStamp), b.newValue = E.newValue, b.oldValue = b.oldValue ?? E.oldValue, b.updateType = E.updateType) : P.set(M, { ...E }), P;
397
398
  }, /* @__PURE__ */ new Map());
398
- return Array.from($.values());
399
+ return Array.from(A.values());
399
400
  }), Et(
400
- E,
401
+ d,
401
402
  m,
402
403
  j.current,
403
- R
404
+ U
404
405
  ), j.current?.middleware && j.current.middleware({
405
406
  updateLog: l,
406
407
  update: B
407
408
  }), j.current?.serverSync) {
408
- const _ = f.serverState[m], N = j.current?.serverSync;
409
+ const _ = r.getState().serverState[m], N = j.current?.serverSync;
409
410
  Dt(m, {
410
- syncKey: typeof N.syncKey == "string" ? N.syncKey : N.syncKey({ state: E }),
411
+ syncKey: typeof N.syncKey == "string" ? N.syncKey : N.syncKey({ state: d }),
411
412
  rollBackState: _,
412
413
  actionTimeStamp: Date.now() + (N.debounce ?? 3e3),
413
414
  status: "waiting"
414
415
  });
415
416
  }
416
- return E;
417
+ return d;
417
418
  });
418
419
  };
419
- o.getState().updaterState[m] || (ct(
420
+ r.getState().updaterState[m] || (ct(
420
421
  m,
421
422
  lt(
422
423
  m,
423
424
  tt,
424
425
  Y.current,
425
- R
426
+ U
426
427
  )
427
- ), o.getState().cogsStateStore[m] || Z(m, t), o.getState().initialStateGlobal[m] || yt(m, t));
428
- const d = Tt(() => lt(
428
+ ), r.getState().cogsStateStore[m] || Z(m, t), r.getState().initialStateGlobal[m] || vt(m, t));
429
+ const u = Tt(() => lt(
429
430
  m,
430
431
  tt,
431
432
  Y.current,
432
- R
433
- ), [m, R]);
434
- return [kt(m), d];
433
+ U
434
+ ), [m, U]);
435
+ return [kt(m), u];
435
436
  }
436
437
  function lt(t, c, h, g) {
437
438
  const v = /* @__PURE__ */ new Map();
438
- let k = 0;
439
+ let $ = 0;
439
440
  const p = (y) => {
440
- const r = y.join(".");
441
+ const a = y.join(".");
441
442
  for (const [S] of v)
442
- (S === r || S.startsWith(r + ".")) && v.delete(S);
443
- k++;
443
+ (S === a || S.startsWith(a + ".")) && v.delete(S);
444
+ $++;
444
445
  }, I = {
445
446
  removeValidation: (y) => {
446
- y?.validationKey && q(y.validationKey);
447
+ y?.validationKey && H(y.validationKey);
447
448
  },
448
449
  revertToInitialState: (y) => {
449
- const r = o.getState().getInitialOptions(t)?.validation;
450
- r?.key && q(r?.key), y?.validationKey && q(y.validationKey);
451
- const S = o.getState().initialStateGlobal[t];
452
- o.getState().clearSelectedIndexesForState(t), v.clear(), k++;
453
- const W = a(S, []), O = Q(t), R = J(O?.localStorage?.key) ? O?.localStorage?.key(S) : O?.localStorage?.key, G = `${g}-${t}-${R}`;
454
- G && localStorage.removeItem(G), ct(t, W), Z(t, S);
455
- const m = o.getState().stateComponents.get(t);
450
+ const a = r.getState().getInitialOptions(t)?.validation;
451
+ a?.key && H(a?.key), y?.validationKey && H(y.validationKey);
452
+ const S = r.getState().initialStateGlobal[t];
453
+ r.getState().clearSelectedIndexesForState(t), v.clear(), $++;
454
+ const D = o(S, []), O = Q(t), U = J(O?.localStorage?.key) ? O?.localStorage?.key(S) : O?.localStorage?.key, W = `${g}-${t}-${U}`;
455
+ W && localStorage.removeItem(W), ct(t, D), Z(t, S);
456
+ const m = r.getState().stateComponents.get(t);
456
457
  return m && m.components.forEach((l) => {
457
458
  l.forceUpdate();
458
459
  }), S;
459
460
  },
460
461
  updateInitialState: (y) => {
461
- v.clear(), k++;
462
- const r = lt(
462
+ v.clear(), $++;
463
+ const a = lt(
463
464
  t,
464
465
  c,
465
466
  h,
466
467
  g
467
- ), S = o.getState().initialStateGlobal[t], W = Q(t), O = J(W?.localStorage?.key) ? W?.localStorage?.key(S) : W?.localStorage?.key, R = `${g}-${t}-${O}`;
468
- return localStorage.getItem(R) && localStorage.removeItem(R), Vt(() => {
469
- yt(t, y), o.getState().initializeShadowState(t, y), ct(t, r), Z(t, y);
470
- const G = o.getState().stateComponents.get(t);
471
- G && G.components.forEach((m) => {
468
+ ), S = r.getState().initialStateGlobal[t], D = Q(t), O = J(D?.localStorage?.key) ? D?.localStorage?.key(S) : D?.localStorage?.key, U = `${g}-${t}-${O}`;
469
+ return localStorage.getItem(U) && localStorage.removeItem(U), Vt(() => {
470
+ vt(t, y), r.getState().initializeShadowState(t, y), ct(t, a), Z(t, y);
471
+ const W = r.getState().stateComponents.get(t);
472
+ W && W.components.forEach((m) => {
472
473
  m.forceUpdate();
473
474
  });
474
475
  }), {
475
- fetchId: (G) => r.get()[G]
476
+ fetchId: (W) => a.get()[W]
476
477
  };
477
478
  },
478
- _initialState: o.getState().initialStateGlobal[t],
479
- _serverState: o.getState().serverState[t],
480
- _isLoading: o.getState().isLoadingGlobal[t],
479
+ _initialState: r.getState().initialStateGlobal[t],
480
+ _serverState: r.getState().serverState[t],
481
+ _isLoading: r.getState().isLoadingGlobal[t],
481
482
  _isServerSynced: () => {
482
- const y = o.getState().serverState[t];
483
+ const y = r.getState().serverState[t];
483
484
  return !!(y && L(y, kt(t)));
484
485
  }
485
486
  };
486
- function a(y, r = [], S) {
487
- const W = r.map(String).join(".");
488
- v.get(W);
487
+ function o(y, a = [], S) {
488
+ const D = a.map(String).join(".");
489
+ v.get(D);
489
490
  const O = function() {
490
- return o().getNestedState(t, r);
491
+ return r().getNestedState(t, a);
491
492
  };
492
493
  Object.keys(I).forEach((m) => {
493
494
  O[m] = I[m];
494
495
  });
495
- const R = {
496
- apply(m, l, ot) {
496
+ const U = {
497
+ apply(m, l, at) {
497
498
  return console.log(
498
- `PROXY APPLY TRAP HIT: stateKey=${t}, path=${r.join(".")}`
499
- ), console.trace("Apply trap stack trace"), o().getNestedState(t, r);
499
+ `PROXY APPLY TRAP HIT: stateKey=${t}, path=${a.join(".")}`
500
+ ), console.trace("Apply trap stack trace"), r().getNestedState(t, a);
500
501
  },
501
502
  get(m, l) {
502
503
  S?.validIndices && !Array.isArray(y) && (S = { ...S, validIndices: void 0 });
503
- const ot = /* @__PURE__ */ new Set([
504
+ const at = /* @__PURE__ */ new Set([
504
505
  "insert",
505
506
  "cut",
506
507
  "cutByValue",
@@ -523,12 +524,12 @@ function lt(t, c, h, g) {
523
524
  "_stateKey",
524
525
  "getComponents"
525
526
  ]);
526
- if (console.log("prop", m, r, l), l && l !== "then" && !l.startsWith("$") && l !== "stateMapNoRender" && !ot.has(l)) {
527
- const d = `${t}////${h}`, e = o.getState().stateComponents.get(t);
527
+ if (l !== "then" && !l.startsWith("$") && l !== "stateMapNoRender" && !at.has(l)) {
528
+ const u = `${t}////${h}`, e = r.getState().stateComponents.get(t);
528
529
  if (e) {
529
- const n = e.components.get(d);
530
+ const n = e.components.get(u);
530
531
  if (n && !n.paths.has("")) {
531
- const s = r.join(".");
532
+ const s = a.join(".");
532
533
  let i = !0;
533
534
  for (const f of n.paths)
534
535
  if (s.startsWith(f) && (s === f || s[f.length] === ".")) {
@@ -540,26 +541,26 @@ function lt(t, c, h, g) {
540
541
  }
541
542
  }
542
543
  if (l === "getDifferences")
543
- return () => mt(
544
- o.getState().cogsStateStore[t],
545
- o.getState().initialStateGlobal[t]
544
+ return () => ht(
545
+ r.getState().cogsStateStore[t],
546
+ r.getState().initialStateGlobal[t]
546
547
  );
547
- if (l === "sync" && r.length === 0)
548
+ if (l === "sync" && a.length === 0)
548
549
  return async function() {
549
- const d = o.getState().getInitialOptions(t), e = d?.sync;
550
+ const u = r.getState().getInitialOptions(t), e = u?.sync;
550
551
  if (!e)
551
552
  return console.error(`No mutation defined for state key "${t}"`), { success: !1, error: "No mutation defined" };
552
- const n = o.getState().getNestedState(t, []), s = d?.validation?.key;
553
+ const n = r.getState().getNestedState(t, []), s = u?.validation?.key;
553
554
  try {
554
555
  const i = await e.action(n);
555
556
  if (i && !i.success && i.errors && s) {
556
- o.getState().removeValidationError(s), i.errors.forEach((u) => {
557
- const E = [s, ...u.path].join(".");
558
- o.getState().addValidationError(E, u.message);
557
+ r.getState().removeValidationError(s), i.errors.forEach((d) => {
558
+ const k = [s, ...d.path].join(".");
559
+ r.getState().addValidationError(k, d.message);
559
560
  });
560
- const f = o.getState().stateComponents.get(t);
561
- f && f.components.forEach((u) => {
562
- u.forceUpdate();
561
+ const f = r.getState().stateComponents.get(t);
562
+ f && f.components.forEach((d) => {
563
+ d.forceUpdate();
563
564
  });
564
565
  }
565
566
  return i?.success && e.onSuccess ? e.onSuccess(i.data) : !i?.success && e.onError && e.onError(i.error), i;
@@ -568,117 +569,119 @@ function lt(t, c, h, g) {
568
569
  }
569
570
  };
570
571
  if (l === "_status") {
571
- const d = o.getState().getNestedState(t, r), e = o.getState().initialStateGlobal[t], n = z(e, r);
572
- return L(d, n) ? "fresh" : "stale";
572
+ const u = r.getState().getNestedState(t, a), e = r.getState().initialStateGlobal[t], n = z(e, a);
573
+ return L(u, n) ? "fresh" : "stale";
573
574
  }
574
575
  if (l === "getStatus")
575
576
  return function() {
576
- const d = o().getNestedState(
577
+ const u = r().getNestedState(
577
578
  t,
578
- r
579
- ), e = o.getState().initialStateGlobal[t], n = z(e, r);
580
- return L(d, n) ? "fresh" : "stale";
579
+ a
580
+ ), e = r.getState().initialStateGlobal[t], n = z(e, a);
581
+ return L(u, n) ? "fresh" : "stale";
581
582
  };
582
583
  if (l === "removeStorage")
583
584
  return () => {
584
- const d = o.getState().initialStateGlobal[t], e = Q(t), n = J(e?.localStorage?.key) ? e?.localStorage?.key(d) : e?.localStorage?.key, s = `${g}-${t}-${n}`;
585
+ const u = r.getState().initialStateGlobal[t], e = Q(t), n = J(e?.localStorage?.key) ? e?.localStorage?.key(u) : e?.localStorage?.key, s = `${g}-${t}-${n}`;
585
586
  s && localStorage.removeItem(s);
586
587
  };
587
588
  if (l === "showValidationErrors")
588
589
  return () => {
589
- const d = o.getState().getInitialOptions(t)?.validation;
590
- if (!d?.key)
590
+ const u = r.getState().getInitialOptions(t)?.validation;
591
+ if (!u?.key)
591
592
  throw new Error("Validation key not found");
592
- return o.getState().getValidationErrors(d.key + "." + r.join("."));
593
+ return r.getState().getValidationErrors(u.key + "." + a.join("."));
593
594
  };
594
595
  if (Array.isArray(y)) {
595
- const d = () => S?.validIndices ? y.map((n, s) => ({
596
+ const u = () => S?.validIndices ? y.map((n, s) => ({
596
597
  item: n,
597
598
  originalIndex: S.validIndices[s]
598
- })) : o.getState().getNestedState(t, r).map((n, s) => ({
599
+ })) : r.getState().getNestedState(t, a).map((n, s) => ({
599
600
  item: n,
600
601
  originalIndex: s
601
602
  }));
602
603
  if (l === "getSelected")
603
604
  return () => {
604
- const e = o.getState().getSelectedIndex(t, r.join("."));
605
+ const e = r.getState().getSelectedIndex(t, a.join("."));
605
606
  if (e !== void 0)
606
- return a(
607
+ return o(
607
608
  y[e],
608
- [...r, e.toString()],
609
+ [...a, e.toString()],
609
610
  S
610
611
  );
611
612
  };
612
613
  if (l === "clearSelected")
613
614
  return () => {
614
- o.getState().clearSelectedIndex({ stateKey: t, path: r });
615
+ r.getState().clearSelectedIndex({ stateKey: t, path: a });
615
616
  };
616
617
  if (l === "getSelectedIndex")
617
- return () => o.getState().getSelectedIndex(t, r.join(".")) ?? -1;
618
+ return () => r.getState().getSelectedIndex(t, a.join(".")) ?? -1;
618
619
  if (l === "useVirtualView")
619
620
  return (e) => {
620
621
  const {
621
622
  itemHeight: n,
622
623
  overscan: s = 5,
623
624
  stickToBottom: i = !1
624
- } = e, f = H(null), [u, E] = K({
625
+ } = e, f = q(null), [d, k] = K({
625
626
  startIndex: 0,
626
627
  endIndex: 10
627
- }), w = H(i), T = H(0), x = H(!0), C = o().getNestedState(
628
+ });
629
+ ft((A) => r.getState().getShadowMetadata(t, [...a, A.toString()])?.virtualizer?.itemHeight || e.itemHeight, []);
630
+ const w = q(i), T = q(0), x = q(!0), C = r().getNestedState(
628
631
  t,
629
- r
630
- ), V = C.length, D = Tt(() => {
631
- const $ = Math.max(0, u.startIndex), M = Math.min(V, u.endIndex), A = Array.from(
632
- { length: M - $ },
633
- (b, U) => $ + U
634
- ), P = A.map((b) => C[b]);
635
- return a(P, r, {
632
+ a
633
+ ), V = C.length, G = Tt(() => {
634
+ const A = Math.max(0, d.startIndex), P = Math.min(V, d.endIndex), E = Array.from(
635
+ { length: P - A },
636
+ (b, F) => A + F
637
+ ), M = E.map((b) => C[b]);
638
+ return o(M, a, {
636
639
  ...S,
637
- validIndices: A
640
+ validIndices: E
638
641
  });
639
- }, [u.startIndex, u.endIndex, C, V]);
642
+ }, [d.startIndex, d.endIndex, C, V]);
640
643
  it(() => {
641
- const $ = f.current;
642
- if (!$) return;
643
- const M = w.current, A = V > T.current;
644
+ const A = f.current;
645
+ if (!A) return;
646
+ const P = w.current, E = V > T.current;
644
647
  T.current = V;
645
- const P = () => {
646
- const { scrollTop: b, clientHeight: U, scrollHeight: F } = $;
647
- w.current = F - b - U < 10;
648
+ const M = () => {
649
+ const { scrollTop: b, clientHeight: F, scrollHeight: R } = A;
650
+ w.current = R - b - F < 10;
648
651
  const X = Math.max(
649
652
  0,
650
653
  Math.floor(b / n) - s
651
654
  ), et = Math.min(
652
655
  V,
653
- Math.ceil((b + U) / n) + s
656
+ Math.ceil((b + F) / n) + s
654
657
  );
655
- E((ft) => ft.startIndex !== X || ft.endIndex !== et ? { startIndex: X, endIndex: et } : ft);
658
+ k((gt) => gt.startIndex !== X || gt.endIndex !== et ? { startIndex: X, endIndex: et } : gt);
656
659
  };
657
- return $.addEventListener("scroll", P, {
660
+ return A.addEventListener("scroll", M, {
658
661
  passive: !0
659
- }), i && (x.current ? $.scrollTo({
660
- top: $.scrollHeight,
662
+ }), i && (x.current ? A.scrollTo({
663
+ top: A.scrollHeight,
661
664
  behavior: "auto"
662
- }) : M && A && requestAnimationFrame(() => {
663
- $.scrollTo({
664
- top: $.scrollHeight,
665
+ }) : P && E && requestAnimationFrame(() => {
666
+ A.scrollTo({
667
+ top: A.scrollHeight,
665
668
  behavior: "smooth"
666
669
  });
667
- })), x.current = !1, P(), () => $.removeEventListener("scroll", P);
670
+ })), x.current = !1, M(), () => A.removeEventListener("scroll", M);
668
671
  }, [V, n, s, i]);
669
- const B = vt(
670
- ($ = "smooth") => {
672
+ const B = ft(
673
+ (A = "smooth") => {
671
674
  f.current && f.current.scrollTo({
672
675
  top: f.current.scrollHeight,
673
- behavior: $
676
+ behavior: A
674
677
  });
675
678
  },
676
679
  []
677
- ), _ = vt(
678
- ($, M = "smooth") => {
680
+ ), _ = ft(
681
+ (A, P = "smooth") => {
679
682
  f.current && f.current.scrollTo({
680
- top: $ * n,
681
- behavior: M
683
+ top: A * n,
684
+ behavior: P
682
685
  });
683
686
  },
684
687
  [n]
@@ -695,12 +698,12 @@ function lt(t, c, h, g) {
695
698
  },
696
699
  list: {
697
700
  style: {
698
- transform: `translateY(${u.startIndex * n}px)`
701
+ transform: `translateY(${d.startIndex * n}px)`
699
702
  }
700
703
  }
701
704
  };
702
705
  return {
703
- virtualState: D,
706
+ virtualState: G,
704
707
  virtualizerProps: N,
705
708
  scrollToBottom: B,
706
709
  scrollToIndex: _
@@ -708,45 +711,45 @@ function lt(t, c, h, g) {
708
711
  };
709
712
  if (l === "stateSort")
710
713
  return (e) => {
711
- const s = [...d()].sort(
712
- (u, E) => e(u.item, E.item)
713
- ), i = s.map(({ item: u }) => u), f = {
714
+ const s = [...u()].sort(
715
+ (d, k) => e(d.item, k.item)
716
+ ), i = s.map(({ item: d }) => d), f = {
714
717
  ...S,
715
718
  validIndices: s.map(
716
- ({ originalIndex: u }) => u
719
+ ({ originalIndex: d }) => d
717
720
  )
718
721
  };
719
- return a(i, r, f);
722
+ return o(i, a, f);
720
723
  };
721
724
  if (l === "stateFilter")
722
725
  return (e) => {
723
- const s = d().filter(
724
- ({ item: u }, E) => e(u, E)
725
- ), i = s.map(({ item: u }) => u), f = {
726
+ const s = u().filter(
727
+ ({ item: d }, k) => e(d, k)
728
+ ), i = s.map(({ item: d }) => d), f = {
726
729
  ...S,
727
730
  validIndices: s.map(
728
- ({ originalIndex: u }) => u
731
+ ({ originalIndex: d }) => d
729
732
  )
730
733
  };
731
- return a(i, r, f);
734
+ return o(i, a, f);
732
735
  };
733
736
  if (l === "stateMap")
734
737
  return (e) => {
735
- const n = o.getState().getNestedState(t, r);
738
+ const n = r.getState().getNestedState(t, a);
736
739
  return Array.isArray(n) ? (S?.validIndices || Array.from({ length: n.length }, (i, f) => f)).map((i, f) => {
737
- const u = n[i], E = [...r, i.toString()], w = a(u, E, S);
738
- return e(u, w, {
740
+ const d = n[i], k = [...a, i.toString()], w = o(d, k, S);
741
+ return e(d, w, {
739
742
  register: () => {
740
- const [, x] = K({}), C = `${h}-${r.join(".")}-${i}`;
743
+ const [, x] = K({}), C = `${h}-${a.join(".")}-${i}`;
741
744
  it(() => {
742
- const V = `${t}////${C}`, D = o.getState().stateComponents.get(t) || {
745
+ const V = `${t}////${C}`, G = r.getState().stateComponents.get(t) || {
743
746
  components: /* @__PURE__ */ new Map()
744
747
  };
745
- return D.components.set(V, {
748
+ return G.components.set(V, {
746
749
  forceUpdate: () => x({}),
747
- paths: /* @__PURE__ */ new Set([E.join(".")])
748
- }), o.getState().stateComponents.set(t, D), () => {
749
- const B = o.getState().stateComponents.get(t);
750
+ paths: /* @__PURE__ */ new Set([k.join(".")])
751
+ }), r.getState().stateComponents.set(t, G), () => {
752
+ const B = r.getState().stateComponents.get(t);
750
753
  B && B.components.delete(V);
751
754
  };
752
755
  }, [t, C]);
@@ -755,7 +758,7 @@ function lt(t, c, h, g) {
755
758
  originalIndex: i
756
759
  });
757
760
  }) : (console.warn(
758
- `stateMap called on a non-array value at path: ${r.join(".")}. The current value is:`,
761
+ `stateMap called on a non-array value at path: ${a.join(".")}. The current value is:`,
759
762
  n
760
763
  ), null);
761
764
  };
@@ -763,143 +766,143 @@ function lt(t, c, h, g) {
763
766
  return (e) => y.map((s, i) => {
764
767
  let f;
765
768
  S?.validIndices && S.validIndices[i] !== void 0 ? f = S.validIndices[i] : f = i;
766
- const u = [...r, f.toString()], E = a(s, u, S);
769
+ const d = [...a, f.toString()], k = o(s, d, S);
767
770
  return e(
768
771
  s,
769
- E,
772
+ k,
770
773
  i,
771
774
  y,
772
- a(y, r, S)
775
+ o(y, a, S)
773
776
  );
774
777
  });
775
778
  if (l === "$stateMap")
776
779
  return (e) => rt(zt, {
777
780
  proxy: {
778
781
  _stateKey: t,
779
- _path: r,
782
+ _path: a,
780
783
  _mapFn: e
781
784
  // Pass the actual function, not string
782
785
  },
783
- rebuildStateShape: a
786
+ rebuildStateShape: o
784
787
  });
785
788
  if (l === "stateList")
786
789
  return (e) => {
787
- const n = o.getState().getNestedState(t, r);
790
+ const n = r.getState().getNestedState(t, a);
788
791
  return Array.isArray(n) ? (S?.validIndices || Array.from({ length: n.length }, (i, f) => f)).map((i, f) => {
789
- const u = n[i], E = [...r, i.toString()], w = a(u, E, S), T = `${h}-${r.join(".")}-${i}`;
790
- return rt(qt, {
792
+ const d = n[i], k = [...a, i.toString()], w = o(d, k, S), T = `${h}-${a.join(".")}-${i}`;
793
+ return rt(Ht, {
791
794
  key: i,
792
795
  stateKey: t,
793
796
  itemComponentId: T,
794
- itemPath: E,
797
+ itemPath: k,
795
798
  children: e(
796
- u,
799
+ d,
797
800
  w,
798
801
  f,
799
802
  n,
800
- a(n, r, S)
803
+ o(n, a, S)
801
804
  )
802
805
  });
803
806
  }) : (console.warn(
804
- `stateList called on a non-array value at path: ${r.join(".")}.`
807
+ `stateList called on a non-array value at path: ${a.join(".")}.`
805
808
  ), null);
806
809
  };
807
810
  if (l === "stateFlattenOn")
808
811
  return (e) => {
809
812
  const n = y;
810
- v.clear(), k++;
813
+ v.clear(), $++;
811
814
  const s = n.flatMap(
812
815
  (i) => i[e] ?? []
813
816
  );
814
- return a(
817
+ return o(
815
818
  s,
816
- [...r, "[*]", e],
819
+ [...a, "[*]", e],
817
820
  S
818
821
  );
819
822
  };
820
823
  if (l === "index")
821
824
  return (e) => {
822
825
  const n = y[e];
823
- return a(n, [...r, e.toString()]);
826
+ return o(n, [...a, e.toString()]);
824
827
  };
825
828
  if (l === "last")
826
829
  return () => {
827
- const e = o.getState().getNestedState(t, r);
830
+ const e = r.getState().getNestedState(t, a);
828
831
  if (e.length === 0) return;
829
- const n = e.length - 1, s = e[n], i = [...r, n.toString()];
830
- return a(s, i);
832
+ const n = e.length - 1, s = e[n], i = [...a, n.toString()];
833
+ return o(s, i);
831
834
  };
832
835
  if (l === "insert")
833
- return (e) => (p(r), gt(c, e, r, t), a(
834
- o.getState().getNestedState(t, r),
835
- r
836
+ return (e) => (p(a), St(c, e, a, t), o(
837
+ r.getState().getNestedState(t, a),
838
+ a
836
839
  ));
837
840
  if (l === "uniqueInsert")
838
841
  return (e, n, s) => {
839
- const i = o.getState().getNestedState(t, r), f = J(e) ? e(i) : e;
840
- let u = null;
842
+ const i = r.getState().getNestedState(t, a), f = J(e) ? e(i) : e;
843
+ let d = null;
841
844
  if (!i.some((w) => {
842
845
  if (n) {
843
846
  const x = n.every(
844
847
  (C) => L(w[C], f[C])
845
848
  );
846
- return x && (u = w), x;
849
+ return x && (d = w), x;
847
850
  }
848
851
  const T = L(w, f);
849
- return T && (u = w), T;
852
+ return T && (d = w), T;
850
853
  }))
851
- p(r), gt(c, f, r, t);
852
- else if (s && u) {
853
- const w = s(u), T = i.map(
854
- (x) => L(x, u) ? w : x
854
+ p(a), St(c, f, a, t);
855
+ else if (s && d) {
856
+ const w = s(d), T = i.map(
857
+ (x) => L(x, d) ? w : x
855
858
  );
856
- p(r), nt(c, T, r);
859
+ p(a), nt(c, T, a);
857
860
  }
858
861
  };
859
862
  if (l === "cut")
860
863
  return (e, n) => {
861
864
  if (!n?.waitForSync)
862
- return p(r), at(c, r, t, e), a(
863
- o.getState().getNestedState(t, r),
864
- r
865
+ return p(a), ot(c, a, t, e), o(
866
+ r.getState().getNestedState(t, a),
867
+ a
865
868
  );
866
869
  };
867
870
  if (l === "cutByValue")
868
871
  return (e) => {
869
872
  for (let n = 0; n < y.length; n++)
870
- y[n] === e && at(c, r, t, n);
873
+ y[n] === e && ot(c, a, t, n);
871
874
  };
872
875
  if (l === "toggleByValue")
873
876
  return (e) => {
874
877
  const n = y.findIndex((s) => s === e);
875
- n > -1 ? at(c, r, t, n) : gt(c, e, r, t);
878
+ n > -1 ? ot(c, a, t, n) : St(c, e, a, t);
876
879
  };
877
880
  if (l === "stateFind")
878
881
  return (e) => {
879
- const s = d().find(
880
- ({ item: f }, u) => e(f, u)
882
+ const s = u().find(
883
+ ({ item: f }, d) => e(f, d)
881
884
  );
882
885
  if (!s) return;
883
- const i = [...r, s.originalIndex.toString()];
884
- return a(s.item, i, S);
886
+ const i = [...a, s.originalIndex.toString()];
887
+ return o(s.item, i, S);
885
888
  };
886
889
  if (l === "findWith")
887
890
  return (e, n) => {
888
- const i = d().find(
889
- ({ item: u }) => u[e] === n
891
+ const i = u().find(
892
+ ({ item: d }) => d[e] === n
890
893
  );
891
894
  if (!i) return;
892
- const f = [...r, i.originalIndex.toString()];
893
- return a(i.item, f, S);
895
+ const f = [...a, i.originalIndex.toString()];
896
+ return o(i.item, f, S);
894
897
  };
895
898
  }
896
- const Y = r[r.length - 1];
899
+ const Y = a[a.length - 1];
897
900
  if (!isNaN(Number(Y))) {
898
- const d = r.slice(0, -1), e = o.getState().getNestedState(t, d);
901
+ const u = a.slice(0, -1), e = r.getState().getNestedState(t, u);
899
902
  if (Array.isArray(e) && l === "cut")
900
- return () => at(
903
+ return () => ot(
901
904
  c,
902
- d,
905
+ u,
903
906
  t,
904
907
  Number(Y)
905
908
  );
@@ -907,67 +910,67 @@ function lt(t, c, h, g) {
907
910
  if (l === "get")
908
911
  return () => {
909
912
  if (S?.validIndices && Array.isArray(y)) {
910
- const d = o.getState().getNestedState(t, r);
911
- return S.validIndices.map((e) => d[e]);
913
+ const u = r.getState().getNestedState(t, a);
914
+ return S.validIndices.map((e) => u[e]);
912
915
  }
913
- return o.getState().getNestedState(t, r);
916
+ return r.getState().getNestedState(t, a);
914
917
  };
915
918
  if (l === "$derive")
916
- return (d) => At({
919
+ return (u) => At({
917
920
  _stateKey: t,
918
- _path: r,
919
- _effect: d.toString()
921
+ _path: a,
922
+ _effect: u.toString()
920
923
  });
921
924
  if (l === "$get")
922
925
  return () => At({
923
926
  _stateKey: t,
924
- _path: r
927
+ _path: a
925
928
  });
926
929
  if (l === "lastSynced") {
927
- const d = `${t}:${r.join(".")}`;
928
- return o.getState().getSyncInfo(d);
930
+ const u = `${t}:${a.join(".")}`;
931
+ return r.getState().getSyncInfo(u);
929
932
  }
930
933
  if (l == "getLocalStorage")
931
- return (d) => dt(g + "-" + t + "-" + d);
934
+ return (u) => dt(g + "-" + t + "-" + u);
932
935
  if (l === "_selected") {
933
- const d = r.slice(0, -1), e = d.join("."), n = o.getState().getNestedState(t, d);
934
- return Array.isArray(n) ? Number(r[r.length - 1]) === o.getState().getSelectedIndex(t, e) : void 0;
936
+ const u = a.slice(0, -1), e = u.join("."), n = r.getState().getNestedState(t, u);
937
+ return Array.isArray(n) ? Number(a[a.length - 1]) === r.getState().getSelectedIndex(t, e) : void 0;
935
938
  }
936
939
  if (l === "setSelected")
937
- return (d) => {
938
- const e = r.slice(0, -1), n = Number(r[r.length - 1]), s = e.join(".");
939
- d ? o.getState().setSelectedIndex(t, s, n) : o.getState().setSelectedIndex(t, s, void 0);
940
- const i = o.getState().getNestedState(t, [...e]);
940
+ return (u) => {
941
+ const e = a.slice(0, -1), n = Number(a[a.length - 1]), s = e.join(".");
942
+ u ? r.getState().setSelectedIndex(t, s, n) : r.getState().setSelectedIndex(t, s, void 0);
943
+ const i = r.getState().getNestedState(t, [...e]);
941
944
  nt(c, i, e), p(e);
942
945
  };
943
946
  if (l === "toggleSelected")
944
947
  return () => {
945
- const d = r.slice(0, -1), e = Number(r[r.length - 1]), n = d.join("."), s = o.getState().getSelectedIndex(t, n);
946
- o.getState().setSelectedIndex(
948
+ const u = a.slice(0, -1), e = Number(a[a.length - 1]), n = u.join("."), s = r.getState().getSelectedIndex(t, n);
949
+ r.getState().setSelectedIndex(
947
950
  t,
948
951
  n,
949
952
  s === e ? void 0 : e
950
953
  );
951
- const i = o.getState().getNestedState(t, [...d]);
952
- nt(c, i, d), p(d);
954
+ const i = r.getState().getNestedState(t, [...u]);
955
+ nt(c, i, u), p(u);
953
956
  };
954
- if (r.length == 0) {
957
+ if (a.length == 0) {
955
958
  if (l === "applyJsonPatch")
956
- return (d) => {
957
- const e = o.getState().cogsStateStore[t], s = jt(e, d).newDocument;
959
+ return (u) => {
960
+ const e = r.getState().cogsStateStore[t], s = jt(e, u).newDocument;
958
961
  Nt(
959
962
  t,
960
- o.getState().initialStateGlobal[t],
963
+ r.getState().initialStateGlobal[t],
961
964
  s,
962
965
  c,
963
966
  h,
964
967
  g
965
968
  );
966
- const i = o.getState().stateComponents.get(t);
969
+ const i = r.getState().stateComponents.get(t);
967
970
  if (i) {
968
- const f = mt(e, s), u = new Set(f);
971
+ const f = ht(e, s), d = new Set(f);
969
972
  for (const [
970
- E,
973
+ k,
971
974
  w
972
975
  ] of i.components.entries()) {
973
976
  let T = !1;
@@ -978,15 +981,15 @@ function lt(t, c, h, g) {
978
981
  continue;
979
982
  }
980
983
  if (x.includes("component") && (w.paths.has("") && (T = !0), !T))
981
- for (const C of u) {
984
+ for (const C of d) {
982
985
  if (w.paths.has(C)) {
983
986
  T = !0;
984
987
  break;
985
988
  }
986
989
  let V = C.lastIndexOf(".");
987
990
  for (; V !== -1; ) {
988
- const D = C.substring(0, V);
989
- if (w.paths.has(D)) {
991
+ const G = C.substring(0, V);
992
+ if (w.paths.has(G)) {
990
993
  T = !0;
991
994
  break;
992
995
  }
@@ -994,9 +997,9 @@ function lt(t, c, h, g) {
994
997
  V + 1
995
998
  );
996
999
  if (!isNaN(Number(B))) {
997
- const _ = D.lastIndexOf(".");
1000
+ const _ = G.lastIndexOf(".");
998
1001
  if (_ !== -1) {
999
- const N = D.substring(
1002
+ const N = G.substring(
1000
1003
  0,
1001
1004
  _
1002
1005
  );
@@ -1006,7 +1009,7 @@ function lt(t, c, h, g) {
1006
1009
  }
1007
1010
  }
1008
1011
  }
1009
- V = D.lastIndexOf(".");
1012
+ V = G.lastIndexOf(".");
1010
1013
  }
1011
1014
  if (T) break;
1012
1015
  }
@@ -1022,21 +1025,21 @@ function lt(t, c, h, g) {
1022
1025
  };
1023
1026
  if (l === "validateZodSchema")
1024
1027
  return () => {
1025
- const d = o.getState().getInitialOptions(t)?.validation, e = o.getState().addValidationError;
1026
- if (!d?.zodSchema)
1028
+ const u = r.getState().getInitialOptions(t)?.validation, e = r.getState().addValidationError;
1029
+ if (!u?.zodSchema)
1027
1030
  throw new Error("Zod schema not found");
1028
- if (!d?.key)
1031
+ if (!u?.key)
1029
1032
  throw new Error("Validation key not found");
1030
- q(d.key);
1031
- const n = o.getState().cogsStateStore[t];
1033
+ H(u.key);
1034
+ const n = r.getState().cogsStateStore[t];
1032
1035
  try {
1033
- const s = o.getState().getValidationErrors(d.key);
1036
+ const s = r.getState().getValidationErrors(u.key);
1034
1037
  s && s.length > 0 && s.forEach(([f]) => {
1035
- f && f.startsWith(d.key) && q(f);
1038
+ f && f.startsWith(u.key) && H(f);
1036
1039
  });
1037
- const i = d.zodSchema.safeParse(n);
1038
- return i.success ? !0 : (i.error.errors.forEach((u) => {
1039
- const E = u.path, w = u.message, T = [d.key, ...E].join(".");
1040
+ const i = u.zodSchema.safeParse(n);
1041
+ return i.success ? !0 : (i.error.errors.forEach((d) => {
1042
+ const k = d.path, w = d.message, T = [u.key, ...k].join(".");
1040
1043
  e(T, w);
1041
1044
  }), ut(t), !1);
1042
1045
  } catch (s) {
@@ -1045,77 +1048,77 @@ function lt(t, c, h, g) {
1045
1048
  };
1046
1049
  if (l === "_componentId") return h;
1047
1050
  if (l === "getComponents")
1048
- return () => o().stateComponents.get(t);
1051
+ return () => r().stateComponents.get(t);
1049
1052
  if (l === "getAllFormRefs")
1050
1053
  return () => It.getState().getFormRefsByStateKey(t);
1051
1054
  if (l === "_initialState")
1052
- return o.getState().initialStateGlobal[t];
1055
+ return r.getState().initialStateGlobal[t];
1053
1056
  if (l === "_serverState")
1054
- return o.getState().serverState[t];
1057
+ return r.getState().serverState[t];
1055
1058
  if (l === "_isLoading")
1056
- return o.getState().isLoadingGlobal[t];
1059
+ return r.getState().isLoadingGlobal[t];
1057
1060
  if (l === "revertToInitialState")
1058
1061
  return I.revertToInitialState;
1059
1062
  if (l === "updateInitialState") return I.updateInitialState;
1060
1063
  if (l === "removeValidation") return I.removeValidation;
1061
1064
  }
1062
1065
  if (l === "getFormRef")
1063
- return () => It.getState().getFormRef(t + "." + r.join("."));
1066
+ return () => It.getState().getFormRef(t + "." + a.join("."));
1064
1067
  if (l === "validationWrapper")
1065
1068
  return ({
1066
- children: d,
1069
+ children: u,
1067
1070
  hideMessage: e
1068
- }) => /* @__PURE__ */ St(
1071
+ }) => /* @__PURE__ */ mt(
1069
1072
  Pt,
1070
1073
  {
1071
1074
  formOpts: e ? { validation: { message: "" } } : void 0,
1072
- path: r,
1073
- validationKey: o.getState().getInitialOptions(t)?.validation?.key || "",
1075
+ path: a,
1076
+ validationKey: r.getState().getInitialOptions(t)?.validation?.key || "",
1074
1077
  stateKey: t,
1075
1078
  validIndices: S?.validIndices,
1076
- children: d
1079
+ children: u
1077
1080
  }
1078
1081
  );
1079
1082
  if (l === "_stateKey") return t;
1080
- if (l === "_path") return r;
1083
+ if (l === "_path") return a;
1081
1084
  if (l === "_isServerSynced") return I._isServerSynced;
1082
1085
  if (l === "update")
1083
- return (d, e) => {
1086
+ return (u, e) => {
1084
1087
  if (e?.debounce)
1085
1088
  xt(() => {
1086
- nt(c, d, r, "");
1087
- const n = o.getState().getNestedState(t, r);
1089
+ nt(c, u, a, "");
1090
+ const n = r.getState().getNestedState(t, a);
1088
1091
  e?.afterUpdate && e.afterUpdate(n);
1089
1092
  }, e.debounce);
1090
1093
  else {
1091
- nt(c, d, r, "");
1092
- const n = o.getState().getNestedState(t, r);
1094
+ nt(c, u, a, "");
1095
+ const n = r.getState().getNestedState(t, a);
1093
1096
  e?.afterUpdate && e.afterUpdate(n);
1094
1097
  }
1095
- p(r);
1098
+ p(a);
1096
1099
  };
1097
1100
  if (l === "formElement")
1098
- return (d, e) => /* @__PURE__ */ St(
1101
+ return (u, e) => /* @__PURE__ */ mt(
1099
1102
  _t,
1100
1103
  {
1101
1104
  setState: c,
1102
1105
  stateKey: t,
1103
- path: r,
1104
- child: d,
1106
+ path: a,
1107
+ child: u,
1105
1108
  formOpts: e
1106
1109
  }
1107
1110
  );
1108
- const j = [...r, l], tt = o.getState().getNestedState(t, j);
1109
- return a(tt, j, S);
1111
+ const j = [...a, l], tt = r.getState().getNestedState(t, j);
1112
+ return o(tt, j, S);
1110
1113
  }
1111
- }, G = new Proxy(O, R);
1112
- return v.set(W, {
1113
- proxy: G,
1114
- stateVersion: k
1115
- }), G;
1114
+ }, W = new Proxy(O, U);
1115
+ return v.set(D, {
1116
+ proxy: W,
1117
+ stateVersion: $
1118
+ }), W;
1116
1119
  }
1117
- return a(
1118
- o.getState().getNestedState(t, [])
1120
+ return o(
1121
+ r.getState().getNestedState(t, [])
1119
1122
  );
1120
1123
  }
1121
1124
  function At(t) {
@@ -1125,18 +1128,18 @@ function zt({
1125
1128
  proxy: t,
1126
1129
  rebuildStateShape: c
1127
1130
  }) {
1128
- const h = o().getNestedState(t._stateKey, t._path);
1131
+ const h = r().getNestedState(t._stateKey, t._path);
1129
1132
  return Array.isArray(h) ? c(
1130
1133
  h,
1131
1134
  t._path
1132
1135
  ).stateMapNoRender(
1133
- (v, k, p, I, a) => t._mapFn(v, k, p, I, a)
1136
+ (v, $, p, I, o) => t._mapFn(v, $, p, I, o)
1134
1137
  ) : null;
1135
1138
  }
1136
1139
  function Bt({
1137
1140
  proxy: t
1138
1141
  }) {
1139
- const c = H(null), h = `${t._stateKey}-${t._path.join(".")}`;
1142
+ const c = q(null), h = `${t._stateKey}-${t._path.join(".")}`;
1140
1143
  return st(() => {
1141
1144
  const g = c.current;
1142
1145
  if (!g || !g.parentElement) return;
@@ -1149,23 +1152,23 @@ function Bt({
1149
1152
  position: p,
1150
1153
  effect: t._effect
1151
1154
  };
1152
- o.getState().addSignalElement(h, y);
1153
- const r = o.getState().getNestedState(t._stateKey, t._path);
1155
+ r.getState().addSignalElement(h, y);
1156
+ const a = r.getState().getNestedState(t._stateKey, t._path);
1154
1157
  let S;
1155
1158
  if (t._effect)
1156
1159
  try {
1157
1160
  S = new Function(
1158
1161
  "state",
1159
1162
  `return (${t._effect})(state)`
1160
- )(r);
1163
+ )(a);
1161
1164
  } catch (O) {
1162
- console.error("Error evaluating effect function during mount:", O), S = r;
1165
+ console.error("Error evaluating effect function during mount:", O), S = a;
1163
1166
  }
1164
1167
  else
1165
- S = r;
1168
+ S = a;
1166
1169
  S !== null && typeof S == "object" && (S = JSON.stringify(S));
1167
- const W = document.createTextNode(String(S));
1168
- g.replaceWith(W);
1170
+ const D = document.createTextNode(String(S));
1171
+ g.replaceWith(D);
1169
1172
  }, [t._stateKey, t._path.join("."), t._effect]), rt("span", {
1170
1173
  ref: c,
1171
1174
  style: { display: "none" },
@@ -1175,7 +1178,7 @@ function Bt({
1175
1178
  function ie(t) {
1176
1179
  const c = Ct(
1177
1180
  (h) => {
1178
- const g = o.getState().stateComponents.get(t._stateKey) || {
1181
+ const g = r.getState().stateComponents.get(t._stateKey) || {
1179
1182
  components: /* @__PURE__ */ new Map()
1180
1183
  };
1181
1184
  return g.components.set(t._stateKey, {
@@ -1183,41 +1186,41 @@ function ie(t) {
1183
1186
  paths: /* @__PURE__ */ new Set([t._path.join(".")])
1184
1187
  }), () => g.components.delete(t._stateKey);
1185
1188
  },
1186
- () => o.getState().getNestedState(t._stateKey, t._path)
1189
+ () => r.getState().getNestedState(t._stateKey, t._path)
1187
1190
  );
1188
1191
  return rt("text", {}, String(c));
1189
1192
  }
1190
- function qt({
1193
+ function Ht({
1191
1194
  stateKey: t,
1192
1195
  itemComponentId: c,
1193
1196
  itemPath: h,
1194
1197
  children: g
1195
1198
  }) {
1196
- const [, v] = K({}), [k, p] = Ot();
1199
+ const [, v] = K({}), [$, p] = Ot();
1197
1200
  return st(() => {
1198
- p.height > 0 && o.getState().setShadowMetadata(t, h, {
1201
+ p.height > 0 && r.getState().setShadowMetadata(t, h, {
1199
1202
  virtualizer: {
1200
1203
  itemHeight: p.height
1201
1204
  }
1202
1205
  });
1203
1206
  }, [p.height]), it(() => {
1204
- const I = `${t}////${c}`, a = o.getState().stateComponents.get(t) || {
1207
+ const I = `${t}////${c}`, o = r.getState().stateComponents.get(t) || {
1205
1208
  components: /* @__PURE__ */ new Map()
1206
1209
  };
1207
- return a.components.set(I, {
1210
+ return o.components.set(I, {
1208
1211
  forceUpdate: () => v({}),
1209
1212
  paths: /* @__PURE__ */ new Set([h.join(".")])
1210
- }), o.getState().stateComponents.set(t, a), () => {
1211
- const y = o.getState().stateComponents.get(t);
1213
+ }), r.getState().stateComponents.set(t, o), () => {
1214
+ const y = r.getState().stateComponents.get(t);
1212
1215
  y && y.components.delete(I);
1213
1216
  };
1214
- }, [t, c, h.join(".")]), /* @__PURE__ */ St("div", { ref: k, children: g });
1217
+ }, [t, c, h.join(".")]), /* @__PURE__ */ mt("div", { ref: $, children: g });
1215
1218
  }
1216
1219
  export {
1217
1220
  At as $cogsSignal,
1218
1221
  ie as $cogsSignalStore,
1219
- oe as addStateOptions,
1220
- ae as createCogsState,
1222
+ ae as addStateOptions,
1223
+ oe as createCogsState,
1221
1224
  se as notifyComponent,
1222
1225
  Lt as useCogsStateFn
1223
1226
  };