cogsbox-state 0.5.273 → 0.5.274

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