cogsbox-state 0.5.303 → 0.5.304

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,61 +1,61 @@
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 et, useRef as B, useEffect as lt, useLayoutEffect as dt, useMemo as It, createElement as ot, useSyncExternalStore as _t, startTransition as Mt, useCallback as At } from "react";
4
+ import { transformStateFunc as jt, isDeepEqual as H, isFunction as Y, getNestedValue as z, getDifferences as pt, debounce as Ot } from "./utility.js";
5
+ import { pushFunc as vt, updateFn as rt, cutFunc as ct, ValidationWrapper as Rt, FormControlComponent as Ft } from "./Functions.jsx";
6
+ import Ut 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 Dt } from "fast-json-patch";
12
+ import Wt 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 };
26
- let I = !1;
25
+ const f = tt(t) || {}, y = m[t] || {}, $ = o.getState().setInitialStateOptions, w = { ...y, ...f };
26
+ let p = !1;
27
27
  if (c)
28
28
  for (const a in c)
29
- p.hasOwnProperty(a) ? (a == "localStorage" && c[a] && p[a].key !== c[a]?.key && (I = !0, p[a] = c[a]), a == "initialState" && c[a] && p[a] !== c[a] && // Different references
30
- !q(p[a], c[a]) && (I = !0, p[a] = c[a])) : (I = !0, p[a] = c[a]);
31
- I && k(t, p);
29
+ w.hasOwnProperty(a) ? (a == "localStorage" && c[a] && w[a].key !== c[a]?.key && (p = !0, w[a] = c[a]), a == "initialState" && c[a] && w[a] !== c[a] && // Different references
30
+ !H(w[a], c[a]) && (p = !0, w[a] = c[a])) : (p = !0, w[a] = c[a]);
31
+ p && $(t, w);
32
32
  }
33
- function Se(t, { formElements: c, validation: m }) {
33
+ function le(t, { formElements: c, validation: m }) {
34
34
  return { initialState: t, formElements: c, validation: m };
35
35
  }
36
- const me = (t, c) => {
36
+ const de = (t, c) => {
37
37
  let m = t;
38
- const [f, y] = Dt(m);
39
- (Object.keys(y).length > 0 || c && Object.keys(c).length > 0) && Object.keys(y).forEach((I) => {
40
- y[I] = y[I] || {}, y[I].formElements = {
38
+ const [f, y] = jt(m);
39
+ (Object.keys(y).length > 0 || c && Object.keys(c).length > 0) && Object.keys(y).forEach((p) => {
40
+ y[p] = y[p] || {}, y[p].formElements = {
41
41
  ...c?.formElements,
42
42
  // Global defaults first
43
43
  ...c?.validation,
44
- ...y[I].formElements || {}
44
+ ...y[p].formElements || {}
45
45
  // State-specific overrides
46
- }, st(I) || o.getState().setInitialStateOptions(I, y[I]);
46
+ }, tt(p) || o.getState().setInitialStateOptions(p, y[p]);
47
47
  }), o.getState().setInitialStates(f), o.getState().setCreatedState(f);
48
- const k = (I, a) => {
49
- const [v] = ot(a?.componentId ?? kt());
50
- Ct({
51
- stateKey: I,
48
+ const $ = (p, a) => {
49
+ const [v] = et(a?.componentId ?? wt());
50
+ kt({
51
+ stateKey: p,
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[p] || f[p], S = a?.modifyState ? a.modifyState(n) : n, [G, R] = Jt(
56
56
  S,
57
57
  {
58
- stateKey: I,
58
+ stateKey: p,
59
59
  syncUpdate: a?.syncUpdate,
60
60
  componentId: v,
61
61
  localStorage: a?.localStorage,
@@ -68,49 +68,49 @@ const me = (t, c) => {
68
68
  serverState: a?.serverState
69
69
  }
70
70
  );
71
- return O;
71
+ return R;
72
72
  };
73
- function p(I, a) {
74
- Ct({ stateKey: I, options: a, initialOptionsPart: y }), a.localStorage && Xt(I, a), yt(I);
73
+ function w(p, a) {
74
+ kt({ stateKey: p, options: a, initialOptionsPart: y }), a.localStorage && Bt(p, a), St(p);
75
75
  }
76
- return { useCogsState: k, setCogsOptions: p };
76
+ return { useCogsState: $, setCogsOptions: w };
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: K,
80
+ getInitialOptions: tt,
81
+ getKeyState: Vt,
82
+ getValidationErrors: Gt,
83
+ setStateLog: Lt,
84
+ updateInitialStateGlobal: Et,
85
+ addValidationError: Ht,
86
+ removeValidationError: J,
87
+ setServerSyncActions: zt
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;
96
- if (k && f) {
97
- const p = `${f}-${c}-${k}`;
98
- let I;
95
+ const $ = Y(m?.localStorage?.key) ? m.localStorage?.key(t) : m?.localStorage?.key;
96
+ if ($ && f) {
97
+ const w = `${f}-${c}-${$}`;
98
+ let p;
99
99
  try {
100
- I = vt(p)?.lastSyncedWithServer;
100
+ p = ft(w)?.lastSyncedWithServer;
101
101
  } catch {
102
102
  }
103
103
  const a = {
104
104
  state: t,
105
105
  lastUpdated: Date.now(),
106
- lastSyncedWithServer: y ?? I
107
- }, v = Ht.serialize(a);
106
+ lastSyncedWithServer: y ?? p
107
+ }, v = Ut.serialize(a);
108
108
  window.localStorage.setItem(
109
- p,
109
+ w,
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,29 +118,29 @@ 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 = Y(c?.localStorage?.key) ? c.localStorage.key(m) : c?.localStorage?.key;
123
123
  if (y && f) {
124
- const k = vt(
124
+ const $ = ft(
125
125
  `${f}-${t}-${y}`
126
126
  );
127
- if (k && k.lastUpdated > (k.lastSyncedWithServer || 0))
128
- return at(t, k.state), yt(t), !0;
127
+ if ($ && $.lastUpdated > ($.lastSyncedWithServer || 0))
128
+ return K(t, $.state), St(t), !0;
129
129
  }
130
130
  return !1;
131
- }, Rt = (t, c, m, f, y, k) => {
132
- const p = {
131
+ }, xt = (t, c, m, f, y, $) => {
132
+ const w = {
133
133
  initialState: c,
134
- updaterState: ht(
134
+ updaterState: gt(
135
135
  t,
136
136
  f,
137
137
  y,
138
- k
138
+ $
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
+ Et(t, w.initialState), ut(t, w.updaterState), K(t, w.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
+ }, ue = (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
+ }, qt = (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: z(c, f),
165
+ newValue: z(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: z(m, f)
172
172
  };
173
173
  case "cut":
174
174
  return {
175
- oldValue: Y(c, f),
175
+ oldValue: z(c, f),
176
176
  newValue: null
177
177
  // or undefined
178
178
  };
@@ -180,27 +180,27 @@ const me = (t, c) => {
180
180
  return { oldValue: null, newValue: null };
181
181
  }
182
182
  };
183
- function Kt(t, {
183
+ function Jt(t, {
184
184
  stateKey: c,
185
185
  serverSync: m,
186
186
  localStorage: f,
187
187
  formElements: y,
188
- reactiveDeps: k,
189
- reactiveType: p,
190
- componentId: I,
188
+ reactiveDeps: $,
189
+ reactiveType: w,
190
+ componentId: p,
191
191
  initialState: a,
192
192
  syncUpdate: v,
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, R] = et({}), { sessionId: F } = Ct();
197
+ let L = !c;
198
+ const [h] = et(c ?? wt()), l = o.getState().stateLog[h], at = B(/* @__PURE__ */ new Set()), Z = B(p ?? wt()), j = B(
199
199
  null
200
200
  );
201
- M.current = st(h) ?? null, lt(() => {
201
+ j.current = tt(h) ?? null, lt(() => {
202
202
  if (v && v.stateKey === h && v.path?.[0]) {
203
- at(h, (r) => ({
203
+ K(h, (r) => ({
204
204
  ...r,
205
205
  [v.path[0]]: v.newValue
206
206
  }));
@@ -212,267 +212,267 @@ function Kt(t, {
212
212
  }
213
213
  }, [v]), lt(() => {
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 && !H(i, a) || !i) && !s)
220
220
  return;
221
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(
222
+ const A = Y(e?.localStorage?.key) ? e?.localStorage?.key(a) : e?.localStorage?.key;
223
+ A && F && (g = ft(`${F}-${h}-${A}`));
224
+ let E = a, T = !1;
225
+ const x = s ? Date.now() : 0, b = g?.lastUpdated || 0, k = g?.lastSyncedWithServer || 0;
226
+ s && x > b ? (E = e.serverState.data, T = !0) : g && b > k && (E = g.state, e?.localStorage?.onChange && e?.localStorage?.onChange(E)), o.getState().initializeShadowState(h, a), xt(
227
227
  h,
228
228
  a,
229
- T,
230
- it,
231
- K.current,
229
+ E,
230
+ nt,
231
+ Z.current,
232
232
  F
233
- ), $ && E && F && xt(T, h, e, F, Date.now()), yt(h), (Array.isArray(p) ? p : [p || "component"]).includes("none") || O({});
233
+ ), T && A && F && Nt(E, h, e, F, Date.now()), St(h), (Array.isArray(w) ? w : [w || "component"]).includes("none") || R({});
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}////${Z.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: () => R({}),
253
253
  paths: /* @__PURE__ */ new Set(),
254
254
  deps: [],
255
- depsFunction: k || void 0,
256
- reactiveType: p ?? ["component", "deps"]
257
- }), o.getState().stateComponents.set(h, r), O({}), () => {
255
+ depsFunction: $ || void 0,
256
+ reactiveType: w ?? ["component", "deps"]
257
+ }), o.getState().stateComponents.set(h, r), R({}), () => {
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 nt = (e, r, s, i) => {
262
262
  if (Array.isArray(r)) {
263
263
  const g = `${h}-${r.join(".")}`;
264
- ut.current.add(g);
264
+ at.current.add(g);
265
265
  }
266
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);
267
+ K(h, (g) => {
268
+ const A = Y(e) ? e(g) : e, E = `${h}-${r.join(".")}`;
269
+ if (E) {
270
+ let M = !1, N = u.signalDomElements.get(E);
271
+ if ((!N || N.size === 0) && (s.updateType === "insert" || s.updateType === "cut")) {
272
+ const O = r.slice(0, -1), D = z(A, O);
273
+ if (Array.isArray(D)) {
274
+ M = !0;
275
+ const I = `${h}-${O.join(".")}`;
276
+ N = u.signalDomElements.get(I);
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 = M ? z(A, r.slice(0, -1)) : z(A, r);
281
+ N.forEach(({ parentId: D, position: I, effect: C }) => {
282
+ const V = document.querySelector(
283
+ `[data-parent-id="${D}"]`
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 (V) {
286
+ const _ = Array.from(V.childNodes);
287
+ if (_[I]) {
288
+ const P = C ? new Function("state", `return (${C})(state)`)(O) : O;
289
+ _[I].textContent = String(P);
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", u.shadowStateStore), s.updateType === "update" && (i || j.current?.validation?.key) && r && J(
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 && J(
300
+ j.current?.validation?.key + "." + T.join(".")
301
+ ), s.updateType === "insert" && j.current?.validation?.key && Gt(
302
+ j.current?.validation?.key + "." + T.join(".")
303
+ ).filter(([N, O]) => {
304
+ let D = N?.split(".").length;
305
+ if (N == T.join(".") && D == T.length - 1) {
306
+ let I = N + "." + T;
307
+ J(N), Ht(I, 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 x = u.stateComponents.get(h);
311
+ if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", x), x) {
312
+ const M = pt(g, A), N = new Set(M), 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
+ D,
315
+ I
316
+ ] of x.components.entries()) {
317
+ let C = !1;
318
+ const V = Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"];
319
+ if (console.log("component", I), !V.includes("none")) {
320
+ if (V.includes("all")) {
321
+ I.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 (V.includes("component") && ((I.paths.has(O) || I.paths.has("")) && (C = !0), !C))
325
+ for (const _ of N) {
326
+ let P = _;
327
327
  for (; ; ) {
328
- if (A.paths.has(D)) {
329
- U = !0;
328
+ if (I.paths.has(P)) {
329
+ C = !0;
330
330
  break;
331
331
  }
332
- const b = D.lastIndexOf(".");
333
- if (b !== -1) {
334
- const W = D.substring(
332
+ const q = P.lastIndexOf(".");
333
+ if (q !== -1) {
334
+ const st = P.substring(
335
335
  0,
336
- b
336
+ q
337
337
  );
338
338
  if (!isNaN(
339
- Number(D.substring(b + 1))
340
- ) && A.paths.has(W)) {
341
- U = !0;
339
+ Number(P.substring(q + 1))
340
+ ) && I.paths.has(st)) {
341
+ C = !0;
342
342
  break;
343
343
  }
344
- D = W;
344
+ P = st;
345
345
  } else
346
- D = "";
347
- if (D === "")
346
+ P = "";
347
+ if (P === "")
348
348
  break;
349
349
  }
350
- if (U) break;
350
+ if (C) 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 (!C && V.includes("deps") && I.depsFunction) {
353
+ const _ = I.depsFunction(A);
354
+ let P = !1;
355
+ typeof _ == "boolean" ? _ && (P = !0) : H(I.deps, _) || (I.deps = _, P = !0), P && (C = !0);
356
356
  }
357
- U && A.forceUpdate();
357
+ C && I.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 b = Date.now();
362
+ r = r.map((M, N) => {
363
+ const O = r.slice(0, -1), D = z(A, O);
364
+ return N === r.length - 1 && ["insert", "cut"].includes(s.updateType) ? (D.length - 1).toString() : M;
365
365
  });
366
- const { oldValue: P, newValue: _ } = Qt(
366
+ const { oldValue: k, newValue: W } = qt(
367
367
  s.updateType,
368
368
  g,
369
- E,
369
+ A,
370
370
  r
371
- ), z = {
372
- timeStamp: V,
371
+ ), U = {
372
+ timeStamp: b,
373
373
  stateKey: h,
374
374
  path: r,
375
375
  updateType: s.updateType,
376
376
  status: "new",
377
- oldValue: P,
378
- newValue: _
377
+ oldValue: k,
378
+ newValue: W
379
379
  };
380
380
  switch (s.updateType) {
381
381
  case "update":
382
- u.updateShadowAtPath(h, r, E);
382
+ u.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 M = r.slice(0, -1);
386
+ u.insertShadowArrayElement(h, M, W);
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
+ u.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 (Lt(h, (M) => {
394
+ const O = [...M ?? [], U].reduce((D, I) => {
395
+ const C = `${I.stateKey}:${JSON.stringify(I.path)}`, V = D.get(C);
396
+ return V ? (V.timeStamp = Math.max(V.timeStamp, I.timeStamp), V.newValue = I.newValue, V.oldValue = V.oldValue ?? I.oldValue, V.updateType = I.updateType) : D.set(C, { ...I }), D;
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,
402
+ j.current,
403
403
  F
404
- ), M.current?.middleware && M.current.middleware({
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: U
407
+ }), j.current?.serverSync) {
408
+ const M = u.serverState[h], N = j.current?.serverSync;
409
+ zt(h, {
410
+ syncKey: typeof N.syncKey == "string" ? N.syncKey : N.syncKey({ state: A }),
411
+ rollBackState: M,
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,
423
+ nt,
424
+ Z.current,
425
425
  F
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] || K(h, t), o.getState().initialStateGlobal[h] || Et(h, t));
428
+ const d = It(() => gt(
429
429
  h,
430
- it,
431
- K.current,
430
+ nt,
431
+ Z.current,
432
432
  F
433
433
  ), [h, F]);
434
- return [Mt(h), d];
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
- let k = 0;
439
- const p = (v) => {
438
+ let $ = 0;
439
+ const w = (v) => {
440
440
  const n = v.join(".");
441
441
  for (const [S] of y)
442
442
  (S === n || S.startsWith(n + ".")) && y.delete(S);
443
- k++;
444
- }, I = {
443
+ $++;
444
+ }, p = {
445
445
  removeValidation: (v) => {
446
- v?.validationKey && X(v.validationKey);
446
+ v?.validationKey && J(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 && J(n?.key), v?.validationKey && J(v.validationKey);
451
451
  const S = o.getState().initialStateGlobal[t];
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);
452
+ o.getState().clearSelectedIndexesForState(t), y.clear(), $++;
453
+ const G = a(S, []), R = tt(t), F = Y(R?.localStorage?.key) ? R?.localStorage?.key(S) : R?.localStorage?.key, L = `${f}-${t}-${F}`;
454
+ L && localStorage.removeItem(L), ut(t, G), K(t, S);
455
455
  const h = o.getState().stateComponents.get(t);
456
456
  return h && h.components.forEach((l) => {
457
457
  l.forceUpdate();
458
458
  }), S;
459
459
  },
460
460
  updateInitialState: (v) => {
461
- y.clear(), k++;
462
- const n = ht(
461
+ y.clear(), $++;
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 = tt(t), R = Y(G?.localStorage?.key) ? G?.localStorage?.key(S) : G?.localStorage?.key, F = `${f}-${t}-${R}`;
468
+ return localStorage.getItem(F) && localStorage.removeItem(F), Mt(() => {
469
+ Et(t, v), o.getState().initializeShadowState(t, v), ut(t, n), K(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 && H(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 R = function() {
490
490
  return o().getNestedState(t, n);
491
491
  };
492
- Object.keys(I).forEach((h) => {
493
- O[h] = I[h];
492
+ Object.keys(p).forEach((h) => {
493
+ R[h] = p[h];
494
494
  });
495
495
  const F = {
496
- apply(h, l, ut) {
496
+ apply(h, l, at) {
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 at = /* @__PURE__ */ new Set([
504
504
  "insert",
505
505
  "cut",
506
506
  "cutByValue",
@@ -523,7 +523,7 @@ 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" && !at.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);
@@ -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
  );
@@ -554,8 +554,8 @@ function ht(t, c, m, f) {
554
554
  const i = await e.action(r);
555
555
  if (i && !i.success && i.errors && s) {
556
556
  o.getState().removeValidationError(s), i.errors.forEach((g) => {
557
- const E = [s, ...g.path].join(".");
558
- o.getState().addValidationError(E, g.message);
557
+ const A = [s, ...g.path].join(".");
558
+ o.getState().addValidationError(A, g.message);
559
559
  });
560
560
  const u = o.getState().stateComponents.get(t);
561
561
  u && u.components.forEach((g) => {
@@ -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 = z(e, n);
572
+ return H(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 = z(e, n);
580
+ return H(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 = tt(t), r = Y(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,117 +621,82 @@ 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, u = B(null), [g, A] = et({
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
+ }), E = B(i), T = B(0), x = B(!0), b = 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
+ ), k = b.length, { totalHeight: W, positions: U } = It(() => {
631
+ const I = o.getState().getShadowMetadata(t, n) || [];
632
+ let C = 0;
633
+ const V = [];
634
+ for (let _ = 0; _ < k; _++) {
635
+ V[_] = C;
636
+ const P = I[_]?.virtualizer?.itemHeight;
637
+ C += P || r;
649
638
  }
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, {
639
+ return { totalHeight: C, positions: V };
640
+ }, [k, t, n.join("."), r]), M = It(() => {
641
+ const I = Math.max(0, g.startIndex), C = Math.min(k, g.endIndex), V = Array.from(
642
+ { length: C - I },
643
+ (P, q) => I + q
644
+ ), _ = V.map((P) => b[P]);
645
+ return a(_, n, {
661
646
  ...S,
662
- validIndices: tt
647
+ validIndices: V
663
648
  });
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;
649
+ }, [g.startIndex, g.endIndex, b, k]);
650
+ dt(() => {
651
+ const I = u.current;
652
+ if (!I) return;
653
+ const C = E.current, V = k > T.current;
654
+ T.current = k;
655
+ const _ = () => {
656
+ const { scrollTop: P, clientHeight: q, scrollHeight: st } = I;
657
+ E.current = st - P - q < 10;
658
+ let mt = 0, it = k - 1;
659
+ for (; mt <= it; ) {
660
+ const Q = Math.floor((mt + it) / 2);
661
+ U[Q] < P ? mt = Q + 1 : it = Q - 1;
677
662
  }
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);
663
+ const ht = Math.max(0, it - s);
664
+ let X = ht;
665
+ const Pt = P + q;
666
+ for (; X < k && U[X] < Pt; )
667
+ X++;
668
+ X = Math.min(k, X + s), A((Q) => Q.startIndex !== ht || Q.endIndex !== X ? { startIndex: ht, endIndex: X } : Q);
684
669
  };
685
- if (b.addEventListener("scroll", Z, {
670
+ return I.addEventListener("scroll", _, {
686
671
  passive: !0
687
- }), i)
688
- if (_.current && !z.current) {
689
- if (A && w > 0)
690
- b.scrollTo({
691
- top: b.scrollHeight,
692
- 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"
707
- });
672
+ }), i ? x.current && k > 0 ? (setTimeout(() => {
673
+ u.current && u.current.scrollTo({
674
+ top: u.current.scrollHeight,
675
+ behavior: "auto"
708
676
  });
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") => {
677
+ }, 0), x.current = !1) : C && V && requestAnimationFrame(() => {
678
+ I.scrollTo({
679
+ top: I.scrollHeight,
680
+ behavior: "smooth"
681
+ });
682
+ }) : x.current = !1, _(), () => I.removeEventListener("scroll", _);
683
+ }, [k, U, s, i]);
684
+ const N = At(
685
+ (I = "smooth") => {
721
686
  u.current && u.current.scrollTo({
722
687
  top: u.current.scrollHeight,
723
- behavior: b
688
+ behavior: I
724
689
  });
725
690
  },
726
691
  []
727
- ), B = wt(
728
- (b, W = "smooth") => {
729
- u.current && x[b] !== void 0 && u.current.scrollTo({
730
- top: x[b],
731
- behavior: W
692
+ ), O = At(
693
+ (I, C = "smooth") => {
694
+ u.current && U[I] !== void 0 && u.current.scrollTo({
695
+ top: U[I],
696
+ behavior: C
732
697
  });
733
698
  },
734
- [x]
699
+ [U]
735
700
  ), D = {
736
701
  outer: {
737
702
  ref: u,
@@ -739,27 +704,27 @@ function ht(t, c, m, f) {
739
704
  },
740
705
  inner: {
741
706
  style: {
742
- height: `${R}px`,
707
+ height: `${W}px`,
743
708
  position: "relative"
744
709
  }
745
710
  },
746
711
  list: {
747
712
  style: {
748
- transform: `translateY(${x[g.startIndex] || 0}px)`
713
+ transform: `translateY(${U[g.startIndex] || 0}px)`
749
714
  }
750
715
  }
751
716
  };
752
717
  return {
753
- virtualState: U,
718
+ virtualState: M,
754
719
  virtualizerProps: D,
755
- scrollToBottom: j,
756
- scrollToIndex: B
720
+ scrollToBottom: N,
721
+ scrollToIndex: O
757
722
  };
758
723
  };
759
724
  if (l === "stateSort")
760
725
  return (e) => {
761
726
  const s = [...d()].sort(
762
- (g, E) => e(g.item, E.item)
727
+ (g, A) => e(g.item, A.item)
763
728
  ), i = s.map(({ item: g }) => g), u = {
764
729
  ...S,
765
730
  validIndices: s.map(
@@ -771,7 +736,7 @@ function ht(t, c, m, f) {
771
736
  if (l === "stateFilter")
772
737
  return (e) => {
773
738
  const s = d().filter(
774
- ({ item: g }, E) => e(g, E)
739
+ ({ item: g }, A) => e(g, A)
775
740
  ), i = s.map(({ item: g }) => g), u = {
776
741
  ...S,
777
742
  validIndices: s.map(
@@ -784,22 +749,22 @@ function ht(t, c, m, f) {
784
749
  return (e) => {
785
750
  const r = o.getState().getNestedState(t, n);
786
751
  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, {
752
+ const g = r[i], A = [...n, i.toString()], E = a(g, A, S);
753
+ return e(g, E, {
789
754
  register: () => {
790
- const [, N] = ot({}), V = `${m}-${n.join(".")}-${i}`;
791
- St(() => {
792
- const P = `${t}////${V}`, _ = o.getState().stateComponents.get(t) || {
755
+ const [, x] = et({}), b = `${m}-${n.join(".")}-${i}`;
756
+ dt(() => {
757
+ const k = `${t}////${b}`, W = o.getState().stateComponents.get(t) || {
793
758
  components: /* @__PURE__ */ new Map()
794
759
  };
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);
760
+ return W.components.set(k, {
761
+ forceUpdate: () => x({}),
762
+ paths: /* @__PURE__ */ new Set([A.join(".")])
763
+ }), o.getState().stateComponents.set(t, W), () => {
764
+ const U = o.getState().stateComponents.get(t);
765
+ U && U.components.delete(k);
801
766
  };
802
- }, [t, V]);
767
+ }, [t, b]);
803
768
  },
804
769
  index: u,
805
770
  originalIndex: i
@@ -813,17 +778,17 @@ function ht(t, c, m, f) {
813
778
  return (e) => v.map((s, i) => {
814
779
  let u;
815
780
  S?.validIndices && S.validIndices[i] !== void 0 ? u = S.validIndices[i] : u = i;
816
- const g = [...n, u.toString()], E = a(s, g, S);
781
+ const g = [...n, u.toString()], A = a(s, g, S);
817
782
  return e(
818
783
  s,
819
- E,
784
+ A,
820
785
  i,
821
786
  v,
822
787
  a(v, n, S)
823
788
  );
824
789
  });
825
790
  if (l === "$stateMap")
826
- return (e) => dt(te, {
791
+ return (e) => ot(Yt, {
827
792
  proxy: {
828
793
  _stateKey: t,
829
794
  _path: n,
@@ -836,15 +801,15 @@ function ht(t, c, m, f) {
836
801
  return (e) => {
837
802
  const r = o.getState().getNestedState(t, n);
838
803
  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, {
804
+ const g = r[i], A = [...n, i.toString()], E = a(g, A, S), T = `${m}-${n.join(".")}-${i}`;
805
+ return ot(Xt, {
841
806
  key: i,
842
807
  stateKey: t,
843
- itemComponentId: $,
844
- itemPath: E,
808
+ itemComponentId: T,
809
+ itemPath: A,
845
810
  children: e(
846
811
  g,
847
- T,
812
+ E,
848
813
  u,
849
814
  r,
850
815
  a(r, n, S)
@@ -857,7 +822,7 @@ function ht(t, c, m, f) {
857
822
  if (l === "stateFlattenOn")
858
823
  return (e) => {
859
824
  const r = v;
860
- y.clear(), k++;
825
+ y.clear(), $++;
861
826
  const s = r.flatMap(
862
827
  (i) => i[e] ?? []
863
828
  );
@@ -880,36 +845,36 @@ function ht(t, c, m, f) {
880
845
  return a(s, i);
881
846
  };
882
847
  if (l === "insert")
883
- return (e) => (p(n), Tt(c, e, n, t), a(
848
+ return (e) => (w(n), vt(c, e, n, t), a(
884
849
  o.getState().getNestedState(t, n),
885
850
  n
886
851
  ));
887
852
  if (l === "uniqueInsert")
888
853
  return (e, r, s) => {
889
- const i = o.getState().getNestedState(t, n), u = Q(e) ? e(i) : e;
854
+ const i = o.getState().getNestedState(t, n), u = Y(e) ? e(i) : e;
890
855
  let g = null;
891
- if (!i.some((T) => {
856
+ if (!i.some((E) => {
892
857
  if (r) {
893
- const N = r.every(
894
- (V) => q(T[V], u[V])
858
+ const x = r.every(
859
+ (b) => H(E[b], u[b])
895
860
  );
896
- return N && (g = T), N;
861
+ return x && (g = E), x;
897
862
  }
898
- const $ = q(T, u);
899
- return $ && (g = T), $;
863
+ const T = H(E, u);
864
+ return T && (g = E), T;
900
865
  }))
901
- p(n), Tt(c, u, n, t);
866
+ w(n), vt(c, u, n, t);
902
867
  else if (s && g) {
903
- const T = s(g), $ = i.map(
904
- (N) => q(N, g) ? T : N
868
+ const E = s(g), T = i.map(
869
+ (x) => H(x, g) ? E : x
905
870
  );
906
- p(n), ct(c, $, n);
871
+ w(n), rt(c, T, n);
907
872
  }
908
873
  };
909
874
  if (l === "cut")
910
875
  return (e, r) => {
911
876
  if (!r?.waitForSync)
912
- return p(n), ft(c, n, t, e), a(
877
+ return w(n), ct(c, n, t, e), a(
913
878
  o.getState().getNestedState(t, n),
914
879
  n
915
880
  );
@@ -917,12 +882,12 @@ function ht(t, c, m, f) {
917
882
  if (l === "cutByValue")
918
883
  return (e) => {
919
884
  for (let r = 0; r < v.length; r++)
920
- v[r] === e && ft(c, n, t, r);
885
+ v[r] === e && ct(c, n, t, r);
921
886
  };
922
887
  if (l === "toggleByValue")
923
888
  return (e) => {
924
889
  const r = v.findIndex((s) => s === e);
925
- r > -1 ? ft(c, n, t, r) : Tt(c, e, n, t);
890
+ r > -1 ? ct(c, n, t, r) : vt(c, e, n, t);
926
891
  };
927
892
  if (l === "stateFind")
928
893
  return (e) => {
@@ -943,15 +908,15 @@ function ht(t, c, m, f) {
943
908
  return a(i.item, u, S);
944
909
  };
945
910
  }
946
- const K = n[n.length - 1];
947
- if (!isNaN(Number(K))) {
911
+ const Z = n[n.length - 1];
912
+ if (!isNaN(Number(Z))) {
948
913
  const d = n.slice(0, -1), e = o.getState().getNestedState(t, d);
949
914
  if (Array.isArray(e) && l === "cut")
950
- return () => ft(
915
+ return () => ct(
951
916
  c,
952
917
  d,
953
918
  t,
954
- Number(K)
919
+ Number(Z)
955
920
  );
956
921
  }
957
922
  if (l === "get")
@@ -963,13 +928,13 @@ function ht(t, c, m, f) {
963
928
  return o.getState().getNestedState(t, n);
964
929
  };
965
930
  if (l === "$derive")
966
- return (d) => Pt({
931
+ return (d) => bt({
967
932
  _stateKey: t,
968
933
  _path: n,
969
934
  _effect: d.toString()
970
935
  });
971
936
  if (l === "$get")
972
- return () => Pt({
937
+ return () => bt({
973
938
  _stateKey: t,
974
939
  _path: n
975
940
  });
@@ -978,7 +943,7 @@ function ht(t, c, m, f) {
978
943
  return o.getState().getSyncInfo(d);
979
944
  }
980
945
  if (l == "getLocalStorage")
981
- return (d) => vt(f + "-" + t + "-" + d);
946
+ return (d) => ft(f + "-" + t + "-" + d);
982
947
  if (l === "_selected") {
983
948
  const d = n.slice(0, -1), e = d.join("."), r = o.getState().getNestedState(t, d);
984
949
  return Array.isArray(r) ? Number(n[n.length - 1]) === o.getState().getSelectedIndex(t, e) : void 0;
@@ -988,7 +953,7 @@ function ht(t, c, m, f) {
988
953
  const e = n.slice(0, -1), r = Number(n[n.length - 1]), s = e.join(".");
989
954
  d ? o.getState().setSelectedIndex(t, s, r) : o.getState().setSelectedIndex(t, s, void 0);
990
955
  const i = o.getState().getNestedState(t, [...e]);
991
- ct(c, i, e), p(e);
956
+ rt(c, i, e), w(e);
992
957
  };
993
958
  if (l === "toggleSelected")
994
959
  return () => {
@@ -999,13 +964,13 @@ function ht(t, c, m, f) {
999
964
  s === e ? void 0 : e
1000
965
  );
1001
966
  const i = o.getState().getNestedState(t, [...d]);
1002
- ct(c, i, d), p(d);
967
+ rt(c, i, d), w(d);
1003
968
  };
1004
969
  if (n.length == 0) {
1005
970
  if (l === "applyJsonPatch")
1006
971
  return (d) => {
1007
- const e = o.getState().cogsStateStore[t], s = zt(e, d).newDocument;
1008
- Rt(
972
+ const e = o.getState().cogsStateStore[t], s = Dt(e, d).newDocument;
973
+ xt(
1009
974
  t,
1010
975
  o.getState().initialStateGlobal[t],
1011
976
  s,
@@ -1015,57 +980,57 @@ function ht(t, c, m, f) {
1015
980
  );
1016
981
  const i = o.getState().stateComponents.get(t);
1017
982
  if (i) {
1018
- const u = $t(e, s), g = new Set(u);
983
+ const u = pt(e, s), g = new Set(u);
1019
984
  for (const [
1020
- E,
1021
- T
985
+ A,
986
+ E
1022
987
  ] 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();
988
+ let T = !1;
989
+ const x = Array.isArray(E.reactiveType) ? E.reactiveType : [E.reactiveType || "component"];
990
+ if (!x.includes("none")) {
991
+ if (x.includes("all")) {
992
+ E.forceUpdate();
1028
993
  continue;
1029
994
  }
1030
- if (N.includes("component") && (T.paths.has("") && ($ = !0), !$))
1031
- for (const V of g) {
1032
- if (T.paths.has(V)) {
1033
- $ = !0;
995
+ if (x.includes("component") && (E.paths.has("") && (T = !0), !T))
996
+ for (const b of g) {
997
+ if (E.paths.has(b)) {
998
+ T = !0;
1034
999
  break;
1035
1000
  }
1036
- let P = V.lastIndexOf(".");
1037
- for (; P !== -1; ) {
1038
- const _ = V.substring(0, P);
1039
- if (T.paths.has(_)) {
1040
- $ = !0;
1001
+ let k = b.lastIndexOf(".");
1002
+ for (; k !== -1; ) {
1003
+ const W = b.substring(0, k);
1004
+ if (E.paths.has(W)) {
1005
+ T = !0;
1041
1006
  break;
1042
1007
  }
1043
- const z = V.substring(
1044
- P + 1
1008
+ const U = b.substring(
1009
+ k + 1
1045
1010
  );
1046
- if (!isNaN(Number(z))) {
1047
- const C = _.lastIndexOf(".");
1048
- if (C !== -1) {
1049
- const w = _.substring(
1011
+ if (!isNaN(Number(U))) {
1012
+ const M = W.lastIndexOf(".");
1013
+ if (M !== -1) {
1014
+ const N = W.substring(
1050
1015
  0,
1051
- C
1016
+ M
1052
1017
  );
1053
- if (T.paths.has(w)) {
1054
- $ = !0;
1018
+ if (E.paths.has(N)) {
1019
+ T = !0;
1055
1020
  break;
1056
1021
  }
1057
1022
  }
1058
1023
  }
1059
- P = _.lastIndexOf(".");
1024
+ k = W.lastIndexOf(".");
1060
1025
  }
1061
- if ($) break;
1026
+ if (T) break;
1062
1027
  }
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);
1028
+ if (!T && x.includes("deps") && E.depsFunction) {
1029
+ const b = E.depsFunction(s);
1030
+ let k = !1;
1031
+ typeof b == "boolean" ? b && (k = !0) : H(E.deps, b) || (E.deps = b, k = !0), k && (T = !0);
1067
1032
  }
1068
- $ && T.forceUpdate();
1033
+ T && E.forceUpdate();
1069
1034
  }
1070
1035
  }
1071
1036
  }
@@ -1077,18 +1042,18 @@ function ht(t, c, m, f) {
1077
1042
  throw new Error("Zod schema not found");
1078
1043
  if (!d?.key)
1079
1044
  throw new Error("Validation key not found");
1080
- X(d.key);
1045
+ J(d.key);
1081
1046
  const r = o.getState().cogsStateStore[t];
1082
1047
  try {
1083
1048
  const s = o.getState().getValidationErrors(d.key);
1084
1049
  s && s.length > 0 && s.forEach(([u]) => {
1085
- u && u.startsWith(d.key) && X(u);
1050
+ u && u.startsWith(d.key) && J(u);
1086
1051
  });
1087
1052
  const i = d.zodSchema.safeParse(r);
1088
1053
  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);
1054
+ const A = g.path, E = g.message, T = [d.key, ...A].join(".");
1055
+ e(T, E);
1056
+ }), St(t), !1);
1092
1057
  } catch (s) {
1093
1058
  return console.error("Zod schema validation failed", s), !1;
1094
1059
  }
@@ -1097,7 +1062,7 @@ function ht(t, c, m, f) {
1097
1062
  if (l === "getComponents")
1098
1063
  return () => o().stateComponents.get(t);
1099
1064
  if (l === "getAllFormRefs")
1100
- return () => Vt.getState().getFormRefsByStateKey(t);
1065
+ return () => Tt.getState().getFormRefsByStateKey(t);
1101
1066
  if (l === "_initialState")
1102
1067
  return o.getState().initialStateGlobal[t];
1103
1068
  if (l === "_serverState")
@@ -1105,18 +1070,18 @@ function ht(t, c, m, f) {
1105
1070
  if (l === "_isLoading")
1106
1071
  return o.getState().isLoadingGlobal[t];
1107
1072
  if (l === "revertToInitialState")
1108
- return I.revertToInitialState;
1109
- if (l === "updateInitialState") return I.updateInitialState;
1110
- if (l === "removeValidation") return I.removeValidation;
1073
+ return p.revertToInitialState;
1074
+ if (l === "updateInitialState") return p.updateInitialState;
1075
+ if (l === "removeValidation") return p.removeValidation;
1111
1076
  }
1112
1077
  if (l === "getFormRef")
1113
- return () => Vt.getState().getFormRef(t + "." + n.join("."));
1078
+ return () => Tt.getState().getFormRef(t + "." + n.join("."));
1114
1079
  if (l === "validationWrapper")
1115
1080
  return ({
1116
1081
  children: d,
1117
1082
  hideMessage: e
1118
- }) => /* @__PURE__ */ Et(
1119
- Gt,
1083
+ }) => /* @__PURE__ */ yt(
1084
+ Rt,
1120
1085
  {
1121
1086
  formOpts: e ? { validation: { message: "" } } : void 0,
1122
1087
  path: n,
@@ -1128,25 +1093,25 @@ function ht(t, c, m, f) {
1128
1093
  );
1129
1094
  if (l === "_stateKey") return t;
1130
1095
  if (l === "_path") return n;
1131
- if (l === "_isServerSynced") return I._isServerSynced;
1096
+ if (l === "_isServerSynced") return p._isServerSynced;
1132
1097
  if (l === "update")
1133
1098
  return (d, e) => {
1134
1099
  if (e?.debounce)
1135
- Wt(() => {
1136
- ct(c, d, n, "");
1100
+ Ot(() => {
1101
+ rt(c, d, n, "");
1137
1102
  const r = o.getState().getNestedState(t, n);
1138
1103
  e?.afterUpdate && e.afterUpdate(r);
1139
1104
  }, e.debounce);
1140
1105
  else {
1141
- ct(c, d, n, "");
1106
+ rt(c, d, n, "");
1142
1107
  const r = o.getState().getNestedState(t, n);
1143
1108
  e?.afterUpdate && e.afterUpdate(r);
1144
1109
  }
1145
- p(n);
1110
+ w(n);
1146
1111
  };
1147
1112
  if (l === "formElement")
1148
- return (d, e) => /* @__PURE__ */ Et(
1149
- Lt,
1113
+ return (d, e) => /* @__PURE__ */ yt(
1114
+ Ft,
1150
1115
  {
1151
1116
  setState: c,
1152
1117
  stateKey: t,
@@ -1155,23 +1120,23 @@ function ht(t, c, m, f) {
1155
1120
  formOpts: e
1156
1121
  }
1157
1122
  );
1158
- const M = [...n, l], it = o.getState().getNestedState(t, M);
1159
- return a(it, M, S);
1123
+ const j = [...n, l], nt = o.getState().getNestedState(t, j);
1124
+ return a(nt, j, S);
1160
1125
  }
1161
- }, H = new Proxy(O, F);
1162
- return y.set(L, {
1163
- proxy: H,
1164
- stateVersion: k
1165
- }), H;
1126
+ }, L = new Proxy(R, F);
1127
+ return y.set(G, {
1128
+ proxy: L,
1129
+ stateVersion: $
1130
+ }), L;
1166
1131
  }
1167
1132
  return a(
1168
1133
  o.getState().getNestedState(t, [])
1169
1134
  );
1170
1135
  }
1171
- function Pt(t) {
1172
- return dt(ee, { proxy: t });
1136
+ function bt(t) {
1137
+ return ot(Zt, { proxy: t });
1173
1138
  }
1174
- function te({
1139
+ function Yt({
1175
1140
  proxy: t,
1176
1141
  rebuildStateShape: c
1177
1142
  }) {
@@ -1180,23 +1145,23 @@ function te({
1180
1145
  m,
1181
1146
  t._path
1182
1147
  ).stateMapNoRender(
1183
- (y, k, p, I, a) => t._mapFn(y, k, p, I, a)
1148
+ (y, $, w, p, a) => t._mapFn(y, $, w, p, a)
1184
1149
  ) : null;
1185
1150
  }
1186
- function ee({
1151
+ function Zt({
1187
1152
  proxy: t
1188
1153
  }) {
1189
- const c = J(null), m = `${t._stateKey}-${t._path.join(".")}`;
1154
+ const c = B(null), m = `${t._stateKey}-${t._path.join(".")}`;
1190
1155
  return lt(() => {
1191
1156
  const f = c.current;
1192
1157
  if (!f || !f.parentElement) return;
1193
- const y = f.parentElement, p = Array.from(y.childNodes).indexOf(f);
1194
- let I = y.getAttribute("data-parent-id");
1195
- I || (I = `parent-${crypto.randomUUID()}`, y.setAttribute("data-parent-id", I));
1158
+ const y = f.parentElement, w = Array.from(y.childNodes).indexOf(f);
1159
+ let p = y.getAttribute("data-parent-id");
1160
+ p || (p = `parent-${crypto.randomUUID()}`, y.setAttribute("data-parent-id", p));
1196
1161
  const v = {
1197
1162
  instanceId: `instance-${crypto.randomUUID()}`,
1198
- parentId: I,
1199
- position: p,
1163
+ parentId: p,
1164
+ position: w,
1200
1165
  effect: t._effect
1201
1166
  };
1202
1167
  o.getState().addSignalElement(m, v);
@@ -1208,22 +1173,22 @@ function ee({
1208
1173
  "state",
1209
1174
  `return (${t._effect})(state)`
1210
1175
  )(n);
1211
- } catch (O) {
1212
- console.error("Error evaluating effect function during mount:", O), S = n;
1176
+ } catch (R) {
1177
+ console.error("Error evaluating effect function during mount:", R), S = n;
1213
1178
  }
1214
1179
  else
1215
1180
  S = n;
1216
1181
  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", {
1182
+ const G = document.createTextNode(String(S));
1183
+ f.replaceWith(G);
1184
+ }, [t._stateKey, t._path.join("."), t._effect]), ot("span", {
1220
1185
  ref: c,
1221
1186
  style: { display: "none" },
1222
1187
  "data-signal-id": m
1223
1188
  });
1224
1189
  }
1225
- function ve(t) {
1226
- const c = Ft(
1190
+ function ge(t) {
1191
+ const c = _t(
1227
1192
  (m) => {
1228
1193
  const f = o.getState().stateComponents.get(t._stateKey) || {
1229
1194
  components: /* @__PURE__ */ new Map()
@@ -1235,22 +1200,22 @@ function ve(t) {
1235
1200
  },
1236
1201
  () => o.getState().getNestedState(t._stateKey, t._path)
1237
1202
  );
1238
- return dt("text", {}, String(c));
1203
+ return ot("text", {}, String(c));
1239
1204
  }
1240
- function ne({
1205
+ function Xt({
1241
1206
  stateKey: t,
1242
1207
  itemComponentId: c,
1243
1208
  itemPath: m,
1244
1209
  children: f
1245
1210
  }) {
1246
- const [, y] = ot({}), [k, p] = Bt(), I = J(null);
1211
+ const [, y] = et({}), [$, w] = Wt(), p = B(null);
1247
1212
  return lt(() => {
1248
- p.height > 0 && p.height !== I.current && (I.current = p.height, o.getState().setShadowMetadata(t, m, {
1213
+ w.height > 0 && w.height !== p.current && (p.current = w.height, o.getState().setShadowMetadata(t, m, {
1249
1214
  virtualizer: {
1250
- itemHeight: p.height
1215
+ itemHeight: w.height
1251
1216
  }
1252
1217
  }));
1253
- }, [p.height, t, m]), St(() => {
1218
+ }, [w.height, t, m]), dt(() => {
1254
1219
  const a = `${t}////${c}`, v = o.getState().stateComponents.get(t) || {
1255
1220
  components: /* @__PURE__ */ new Map()
1256
1221
  };
@@ -1261,14 +1226,14 @@ function ne({
1261
1226
  const n = o.getState().stateComponents.get(t);
1262
1227
  n && n.components.delete(a);
1263
1228
  };
1264
- }, [t, c, m.join(".")]), /* @__PURE__ */ Et("div", { ref: k, children: f });
1229
+ }, [t, c, m.join(".")]), /* @__PURE__ */ yt("div", { ref: $, children: f });
1265
1230
  }
1266
1231
  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
1232
+ bt as $cogsSignal,
1233
+ ge as $cogsSignalStore,
1234
+ le as addStateOptions,
1235
+ de as createCogsState,
1236
+ ue as notifyComponent,
1237
+ Jt as useCogsStateFn
1273
1238
  };
1274
1239
  //# sourceMappingURL=CogsState.jsx.map