cogsbox-state 0.5.386 → 0.5.388

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