cogsbox-state 0.5.303 → 0.5.305

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