cogsbox-state 0.5.423 → 0.5.424

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