cogsbox-state 0.5.385 → 0.5.387

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,63 +1,63 @@
1
1
  "use client";
2
- import { jsx as we } from "react/jsx-runtime";
3
- import { useState as K, useRef as J, useEffect as oe, useLayoutEffect as le, useMemo as Oe, createElement as de, useSyncExternalStore as je, startTransition as He, useCallback as be } from "react";
4
- import { transformStateFunc as Fe, isDeepEqual as q, isFunction as ee, getNestedValue as Z, getDifferences as _e, debounce as Be } from "./utility.js";
5
- import { pushFunc as Ee, updateFn as ce, cutFunc as me, ValidationWrapper as We, FormControlComponent as ze } from "./Functions.jsx";
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
6
  import qe from "superjson";
7
7
  import { v4 as Ae } from "uuid";
8
8
  import "zod";
9
- import { getGlobalStore as o, formRefStore as Me } from "./store.js";
10
- import { useCogsConfig as Le } from "./CogsStateClient.jsx";
9
+ import { getGlobalStore as o, formRefStore as be } from "./store.js";
10
+ import { useCogsConfig as Ge } from "./CogsStateClient.jsx";
11
11
  import { applyPatch as Je } from "fast-json-patch";
12
12
  import Ye from "react-use-measure";
13
- function xe(e, i) {
14
- const m = o.getState().getInitialOptions, g = o.getState().setInitialStateOptions, T = m(e) || {};
13
+ function Me(e, i) {
14
+ const m = o.getState().getInitialOptions, g = o.getState().setInitialStateOptions, I = m(e) || {};
15
15
  g(e, {
16
- ...T,
16
+ ...I,
17
17
  ...i
18
18
  });
19
19
  }
20
- function Pe({
20
+ function xe({
21
21
  stateKey: e,
22
22
  options: i,
23
23
  initialOptionsPart: m
24
24
  }) {
25
- const g = re(e) || {}, T = m[e] || {}, A = o.getState().setInitialStateOptions, E = { ...T, ...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
- E.hasOwnProperty(s) ? (s == "localStorage" && i[s] && E[s].key !== i[s]?.key && (y = !0, E[s] = i[s]), s == "initialState" && i[s] && E[s] !== i[s] && // Different references
30
- !q(E[s], i[s]) && (y = !0, E[s] = i[s])) : (y = !0, E[s] = i[s]);
31
- y && A(e, E);
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
33
  function It(e, { formElements: i, validation: m }) {
34
34
  return { initialState: e, formElements: i, validation: m };
35
35
  }
36
- const Tt = (e, i) => {
36
+ const vt = (e, i) => {
37
37
  let m = e;
38
- const [g, T] = Fe(m);
39
- (Object.keys(T).length > 0 || i && Object.keys(i).length > 0) && Object.keys(T).forEach((y) => {
40
- T[y] = T[y] || {}, T[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
- ...T[y].formElements || {}
44
+ ...I[v].formElements || {}
45
45
  // State-specific overrides
46
- }, re(y) || o.getState().setInitialStateOptions(y, T[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 [I] = K(s?.componentId ?? Ae());
50
- Pe({
51
- stateKey: y,
48
+ const N = (v, s) => {
49
+ const [T] = K(s?.componentId ?? Ae());
50
+ xe({
51
+ stateKey: v,
52
52
  options: s,
53
- initialOptionsPart: T
53
+ initialOptionsPart: I
54
54
  });
55
- const n = o.getState().cogsStateStore[y] || g[y], S = s?.modifyState ? s.modifyState(n) : n, [B, L] = nt(
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: I,
60
+ componentId: T,
61
61
  localStorage: s?.localStorage,
62
62
  middleware: s?.middleware,
63
63
  enabledSync: s?.enabledSync,
@@ -68,12 +68,12 @@ const Tt = (e, i) => {
68
68
  serverState: s?.serverState
69
69
  }
70
70
  );
71
- return L;
71
+ return U;
72
72
  };
73
- function E(y, s) {
74
- Pe({ stateKey: y, options: s, initialOptionsPart: T }), s.localStorage && et(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: E };
76
+ return { useCogsState: N, setCogsOptions: p };
77
77
  }, {
78
78
  setUpdaterState: he,
79
79
  setState: ne,
@@ -85,32 +85,32 @@ const Tt = (e, i) => {
85
85
  addValidationError: Qe,
86
86
  removeValidationError: Q,
87
87
  setServerSyncActions: Ke
88
- } = o.getState(), Ve = (e, i, m, g, T) => {
88
+ } = o.getState(), Pe = (e, i, m, g, I) => {
89
89
  m?.log && console.log(
90
90
  "saving to localstorage",
91
91
  i,
92
92
  m.localStorage?.key,
93
93
  g
94
94
  );
95
- const A = ee(m?.localStorage?.key) ? m.localStorage?.key(e) : m?.localStorage?.key;
96
- if (A && g) {
97
- const E = `${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 = Te(E)?.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: T ?? y
107
- }, I = qe.serialize(s);
106
+ lastSyncedWithServer: I ?? v
107
+ }, T = qe.serialize(s);
108
108
  window.localStorage.setItem(
109
- E,
110
- JSON.stringify(I.json)
109
+ p,
110
+ JSON.stringify(T.json)
111
111
  );
112
112
  }
113
- }, Te = (e) => {
113
+ }, Ie = (e) => {
114
114
  if (!e) return null;
115
115
  try {
116
116
  const i = window.localStorage.getItem(e);
@@ -119,27 +119,27 @@ const Tt = (e, i) => {
119
119
  return console.error("Error loading from localStorage:", i), null;
120
120
  }
121
121
  }, et = (e, i) => {
122
- const m = o.getState().cogsStateStore[e], { sessionId: g } = Le(), T = ee(i?.localStorage?.key) ? i.localStorage.key(m) : i?.localStorage?.key;
123
- if (T && g) {
124
- const A = Te(
125
- `${g}-${e}-${T}`
122
+ const m = o.getState().cogsStateStore[e], { sessionId: g } = Ge(), I = ee(i?.localStorage?.key) ? i.localStorage.key(m) : i?.localStorage?.key;
123
+ if (I && g) {
124
+ const N = Ie(
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
- }, De = (e, i, m, g, T, A) => {
132
- const E = {
131
+ }, Le = (e, i, m, g, I, N) => {
132
+ const p = {
133
133
  initialState: i,
134
- updaterState: Ie(
134
+ updaterState: Te(
135
135
  e,
136
136
  g,
137
- T,
138
- A
137
+ I,
138
+ N
139
139
  ),
140
140
  state: m
141
141
  };
142
- Ne(e, E.initialState), he(e, E.updaterState), ne(e, E.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,13 +149,13 @@ const Tt = (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
- const g = `${e}////${i}`, T = m.components.get(g);
156
- if ((T ? Array.isArray(T.reactiveType) ? T.reactiveType : [T.reactiveType || "component"] : null)?.includes("none"))
155
+ const g = `${e}////${i}`, I = m.components.get(g);
156
+ if ((I ? Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"] : null)?.includes("none"))
157
157
  return;
158
- T && T.forceUpdate();
158
+ I && I.forceUpdate();
159
159
  }
160
160
  }, tt = (e, i, m, g) => {
161
161
  switch (e) {
@@ -184,323 +184,323 @@ function nt(e, {
184
184
  stateKey: i,
185
185
  serverSync: m,
186
186
  localStorage: g,
187
- formElements: T,
188
- reactiveDeps: A,
189
- reactiveType: E,
190
- componentId: y,
187
+ formElements: I,
188
+ reactiveDeps: N,
189
+ reactiveType: p,
190
+ componentId: v,
191
191
  initialState: s,
192
- syncUpdate: I,
192
+ syncUpdate: T,
193
193
  dependencies: n,
194
194
  serverState: S
195
195
  } = {}) {
196
- const [B, L] = K({}), { sessionId: R } = Le();
197
- let W = !i;
198
- const [h] = K(i ?? Ae()), l = o.getState().stateLog[h], ue = J(/* @__PURE__ */ new Set()), te = 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(h) ?? null, oe(() => {
202
- if (I && I.stateKey === h && I.path?.[0]) {
201
+ R.current = re(h) ?? null, oe(() => {
202
+ if (T && T.stateKey === h && T.path?.[0]) {
203
203
  ne(h, (r) => ({
204
204
  ...r,
205
- [I.path[0]]: I.newValue
205
+ [T.path[0]]: T.newValue
206
206
  }));
207
- const t = `${I.stateKey}:${I.path.join(".")}`;
207
+ const t = `${T.stateKey}:${T.path.join(".")}`;
208
208
  o.getState().setSyncInfo(t, {
209
- timeStamp: I.timeStamp,
210
- userId: I.userId
209
+ timeStamp: T.timeStamp,
210
+ userId: T.userId
211
211
  });
212
212
  }
213
- }, [I]), oe(() => {
213
+ }, [T]), oe(() => {
214
214
  if (s) {
215
- xe(h, {
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[h];
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 w = ee(t?.localStorage?.key) ? t?.localStorage?.key(s) : t?.localStorage?.key;
223
- w && R && (u = Te(`${R}-${h}-${w}`));
224
- let p = s, O = !1;
225
- const N = a ? Date.now() : 0, _ = u?.lastUpdated || 0, k = u?.lastSyncedWithServer || 0;
226
- a && N > _ ? (p = t.serverState.data, O = !0) : u && _ > k && (p = u.state, t?.localStorage?.onChange && t?.localStorage?.onChange(p)), o.getState().initializeShadowState(h, s), De(
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
227
  h,
228
228
  s,
229
- p,
229
+ y,
230
230
  ae,
231
231
  te.current,
232
- R
233
- ), O && w && R && Ve(p, h, t, R, Date.now()), ve(h), (Array.isArray(E) ? E : [E || "component"]).includes("none") || L({});
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,
237
237
  S?.status,
238
238
  S?.data,
239
239
  ...n || []
240
- ]), le(() => {
241
- W && xe(h, {
240
+ ]), ce(() => {
241
+ z && Me(h, {
242
242
  serverSync: m,
243
- formElements: T,
243
+ formElements: I,
244
244
  initialState: s,
245
245
  localStorage: g,
246
- middleware: P.current?.middleware
246
+ middleware: R.current?.middleware
247
247
  });
248
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: () => L({}),
252
+ forceUpdate: () => U({}),
253
253
  paths: /* @__PURE__ */ new Set(),
254
254
  deps: [],
255
- depsFunction: A || void 0,
256
- reactiveType: E ?? ["component", "deps"]
257
- }), o.getState().stateComponents.set(h, r), L({}), () => {
255
+ depsFunction: N || void 0,
256
+ reactiveType: p ?? ["component", "deps"]
257
+ }), o.getState().stateComponents.set(h, r), U({}), () => {
258
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
263
  const u = `${h}-${r.join(".")}`;
264
- ue.current.add(u);
264
+ de.current.add(u);
265
265
  }
266
266
  const f = o.getState();
267
267
  ne(h, (u) => {
268
- const w = ee(t) ? t(u) : t, p = `${h}-${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), D = Z(w, V);
273
- if (Array.isArray(D)) {
274
- b = !0;
275
- const v = `${h}-${V.join(".")}`;
276
- C = f.signalDomElements.get(v);
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(w, r.slice(0, -1)) : Z(w, r);
281
- C.forEach(({ parentId: D, position: v, effect: U }) => {
282
- const $ = document.querySelector(
283
- `[data-parent-id="${D}"]`
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 H = U ? new Function("state", `return (${U})(state)`)(V) : V;
289
- z[v].textContent = String(H);
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 && Q(
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 O = r.slice(0, r.length - 1);
299
- a.updateType === "cut" && P.current?.validation?.key && Q(
300
- P.current?.validation?.key + "." + O.join(".")
301
- ), a.updateType === "insert" && P.current?.validation?.key && Ze(
302
- P.current?.validation?.key + "." + O.join(".")
303
- ).filter(([C, V]) => {
304
- let D = C?.split(".").length;
305
- if (C == O.join(".") && D == O.length - 1) {
306
- let v = C + "." + O;
307
- Q(C), Qe(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(h);
311
- if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", N), N) {
312
- const b = _e(u, w), 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
- D,
315
- v
316
- ] of N.components.entries()) {
317
- let U = !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("")) && (U = !0), !U))
325
- for (const z of C) {
326
- let H = 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(H)) {
329
- U = !0;
328
+ if (O.paths.has(B)) {
329
+ _ = !0;
330
330
  break;
331
331
  }
332
- const se = H.lastIndexOf(".");
332
+ const se = B.lastIndexOf(".");
333
333
  if (se !== -1) {
334
- const ge = H.substring(
334
+ const ue = B.substring(
335
335
  0,
336
336
  se
337
337
  );
338
338
  if (!isNaN(
339
- Number(H.substring(se + 1))
340
- ) && v.paths.has(ge)) {
341
- U = !0;
339
+ Number(B.substring(se + 1))
340
+ ) && O.paths.has(ue)) {
341
+ _ = !0;
342
342
  break;
343
343
  }
344
- H = ge;
344
+ B = ue;
345
345
  } else
346
- H = "";
347
- if (H === "")
346
+ B = "";
347
+ if (B === "")
348
348
  break;
349
349
  }
350
- if (U) break;
350
+ if (_) break;
351
351
  }
352
- if (!U && $.includes("deps") && v.depsFunction) {
353
- const z = v.depsFunction(w);
354
- let H = !1;
355
- typeof z == "boolean" ? z && (H = !0) : q(v.deps, z) || (v.deps = z, H = !0), H && (U = !0);
352
+ if (!_ && D.includes("deps") && O.depsFunction) {
353
+ const G = O.depsFunction(E);
354
+ let B = !1;
355
+ typeof G == "boolean" ? G && (B = !0) : J(O.deps, G) || (O.deps = G, B = !0), B && (_ = !0);
356
356
  }
357
- U && v.forceUpdate();
357
+ _ && O.forceUpdate();
358
358
  }
359
359
  }
360
360
  }
361
- const _ = Date.now();
362
- r = r.map((b, C) => {
363
- const V = r.slice(0, -1), D = Z(w, V);
364
- return C === r.length - 1 && ["insert", "cut"].includes(a.updateType) ? (D.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 } = tt(
366
+ const { oldValue: M, newValue: F } = tt(
367
367
  a.updateType,
368
368
  u,
369
- w,
369
+ E,
370
370
  r
371
371
  ), Y = {
372
- timeStamp: _,
372
+ timeStamp: A,
373
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(h, r, w);
382
+ f.updateShadowAtPath(h, r, E);
383
383
  break;
384
384
  case "insert":
385
- const b = r.slice(0, -1);
386
- f.insertShadowArrayElement(h, 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(h, 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 (Xe(h, (b) => {
394
- const V = [...b ?? [], Y].reduce((D, v) => {
395
- const U = `${v.stateKey}:${JSON.stringify(v.path)}`, $ = D.get(U);
396
- return $ ? ($.timeStamp = Math.max($.timeStamp, v.timeStamp), $.newValue = v.newValue, $.oldValue = $.oldValue ?? v.oldValue, $.updateType = v.updateType) : D.set(U, { ...v }), D;
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());
399
- }), Ve(
400
- w,
398
+ return Array.from(L.values());
399
+ }), Pe(
400
+ E,
401
401
  h,
402
- P.current,
403
- R
404
- ), P.current?.middleware && P.current.middleware({
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[h], C = P.current?.serverSync;
407
+ }), R.current?.serverSync) {
408
+ const x = f.serverState[h], $ = R.current?.serverSync;
409
409
  Ke(h, {
410
- syncKey: typeof C.syncKey == "string" ? C.syncKey : C.syncKey({ state: w }),
411
- rollBackState: b,
412
- actionTimeStamp: Date.now() + (C.debounce ?? 3e3),
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 w;
416
+ return E;
417
417
  });
418
418
  };
419
419
  o.getState().updaterState[h] || (he(
420
420
  h,
421
- Ie(
421
+ Te(
422
422
  h,
423
423
  ae,
424
424
  te.current,
425
- R
425
+ j
426
426
  )
427
427
  ), o.getState().cogsStateStore[h] || ne(h, e), o.getState().initialStateGlobal[h] || Ne(h, e));
428
- const d = Oe(() => Ie(
428
+ const d = we(() => Te(
429
429
  h,
430
430
  ae,
431
431
  te.current,
432
- R
433
- ), [h, R]);
432
+ j
433
+ ), [h, j]);
434
434
  return [Re(h), d];
435
435
  }
436
- function Ie(e, i, m, g) {
437
- const T = /* @__PURE__ */ new Map();
438
- let A = 0;
439
- const E = (I) => {
440
- const n = I.join(".");
441
- for (const [S] of T)
442
- (S === n || S.startsWith(n + ".")) && T.delete(S);
443
- A++;
444
- }, y = {
445
- removeValidation: (I) => {
446
- I?.validationKey && Q(I.validationKey);
436
+ function Te(e, i, m, g) {
437
+ const I = /* @__PURE__ */ new Map();
438
+ let N = 0;
439
+ const p = (T) => {
440
+ const n = T.join(".");
441
+ for (const [S] of I)
442
+ (S === n || S.startsWith(n + ".")) && I.delete(S);
443
+ N++;
444
+ }, v = {
445
+ removeValidation: (T) => {
446
+ T?.validationKey && Q(T.validationKey);
447
447
  },
448
- revertToInitialState: (I) => {
448
+ revertToInitialState: (T) => {
449
449
  const n = o.getState().getInitialOptions(e)?.validation;
450
- n?.key && Q(n?.key), I?.validationKey && Q(I.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), T.clear(), A++;
453
- const B = s(S, []), L = re(e), R = ee(L?.localStorage?.key) ? L?.localStorage?.key(S) : L?.localStorage?.key, W = `${g}-${e}-${R}`;
454
- W && localStorage.removeItem(W), he(e, B), ne(e, S);
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
455
  const h = o.getState().stateComponents.get(e);
456
456
  return h && h.components.forEach((l) => {
457
457
  l.forceUpdate();
458
458
  }), S;
459
459
  },
460
- updateInitialState: (I) => {
461
- T.clear(), A++;
462
- const n = Ie(
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], B = re(e), L = ee(B?.localStorage?.key) ? B?.localStorage?.key(S) : B?.localStorage?.key, R = `${g}-${e}-${L}`;
468
- return localStorage.getItem(R) && localStorage.removeItem(R), He(() => {
469
- Ne(e, I), o.getState().initializeShadowState(e, I), he(e, n), ne(e, I);
470
- const W = o.getState().stateComponents.get(e);
471
- W && W.components.forEach((h) => {
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
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 I = o.getState().serverState[e];
483
- return !!(I && q(I, Re(e)));
482
+ const T = o.getState().serverState[e];
483
+ return !!(T && J(T, Re(e)));
484
484
  }
485
485
  };
486
- function s(I, n = [], S) {
487
- const B = n.map(String).join(".");
488
- T.get(B);
489
- const L = 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((h) => {
493
- L[h] = y[h];
492
+ Object.keys(v).forEach((h) => {
493
+ U[h] = v[h];
494
494
  });
495
- const R = {
496
- apply(h, l, ue) {
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
501
  get(h, l) {
502
- S?.validIndices && !Array.isArray(I) && (S = { ...S, validIndices: void 0 });
503
- const ue = /* @__PURE__ */ new Set([
502
+ S?.validIndices && !Array.isArray(T) && (S = { ...S, validIndices: void 0 });
503
+ const de = /* @__PURE__ */ new Set([
504
504
  "insert",
505
505
  "cut",
506
506
  "cutByValue",
@@ -523,7 +523,7 @@ function Ie(e, i, m, g) {
523
523
  "_stateKey",
524
524
  "getComponents"
525
525
  ]);
526
- if (l !== "then" && !l.startsWith("$") && l !== "stateMapNoRender" && !ue.has(l)) {
526
+ if (l !== "then" && !l.startsWith("$") && l !== "stateMapNoRender" && !de.has(l)) {
527
527
  const d = `${e}////${m}`, t = o.getState().stateComponents.get(e);
528
528
  if (t) {
529
529
  const r = t.components.get(d);
@@ -554,8 +554,8 @@ function Ie(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 w = [a, ...u.path].join(".");
558
- o.getState().addValidationError(w, 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 Ie(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,7 +577,7 @@ function Ie(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 () => {
@@ -591,8 +591,8 @@ function Ie(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(I)) {
595
- const d = () => S?.validIndices ? I.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 Ie(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
- I[t],
607
+ T[t],
608
608
  [...n, t.toString()],
609
609
  S
610
610
  );
@@ -622,174 +622,178 @@ function Ie(e, i, m, g) {
622
622
  overscan: a = 6,
623
623
  stickToBottom: c = !1,
624
624
  dependencies: f = []
625
- } = t, u = J(!1), w = J(null), [p, O] = K({
625
+ } = t, u = q(!1), E = q(null), [y, w] = K({
626
626
  startIndex: 0,
627
627
  endIndex: 10
628
- }), [N, _] = K("IDLE_AT_TOP"), k = J(!1), j = J(0), Y = J(f), b = J(0), [C, V] = K(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 D = o().getNestedState(
632
+ const O = o().getNestedState(
633
633
  e,
634
634
  n
635
- ), v = D.length, { totalHeight: U, positions: $ } = Oe(() => {
636
- const M = o.getState().getShadowMetadata(e, n) || [];
637
- let x = 0;
638
- const F = [];
639
- for (let G = 0; G < v; G++) {
640
- F[G] = x;
641
- const X = M[G]?.virtualizer?.itemHeight;
642
- x += X || r;
635
+ ), _ = O.length, { totalHeight: D, positions: G } = we(() => {
636
+ const k = o.getState().getShadowMetadata(e, n) || [];
637
+ let P = 0;
638
+ const H = [];
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: F };
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 = Oe(() => {
652
- const M = Math.max(0, p.startIndex), x = Math.min(v, p.endIndex), F = Array.from(
653
- { length: x - M },
654
- (X, ie) => M + ie
655
- ), G = F.map((X) => D[X]);
656
- return s(G, 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
- validIndices: F
658
+ validIndices: H
659
659
  });
660
- }, [p.startIndex, p.endIndex, D, v]);
661
- le(() => {
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"), _("IDLE_AT_TOP");
668
- return;
660
+ }, [y.startIndex, y.endIndex, O, _]);
661
+ ce(() => {
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
- ), _("GETTING_HEIGHTS")), j.current = v, Y.current = f;
673
- }, [v, ...f]), le(() => {
674
- const M = w.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
- ), _("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
- ), O({
685
- startIndex: Math.max(0, v - 10 - a),
686
- endIndex: v
687
- }), x = setInterval(() => {
688
- const F = v - 1;
689
- ((o.getState().getShadowMetadata(e, n) || [])[F]?.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
- ), _("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 F = j.current === 0 ? "auto" : "smooth";
698
- M.scrollTo({
699
- top: M.scrollHeight,
700
- behavior: F
704
+ ), M.current = !0;
705
+ const H = F.current === 0 ? "auto" : "smooth";
706
+ k.scrollTo({
707
+ top: k.scrollHeight,
708
+ behavior: H
701
709
  });
702
- const G = 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, _("LOCKED_AT_BOTTOM");
714
+ ), M.current = !1, u.current = !1, A("LOCKED_AT_BOTTOM");
707
715
  },
708
- F === "smooth" ? 500 : 50
716
+ H === "smooth" ? 500 : 50
709
717
  );
710
- return () => clearTimeout(G);
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 = w.current;
717
- if (!M) return;
718
- const x = r, F = () => {
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: G, scrollHeight: X, clientHeight: ie } = M;
722
- console.log(
723
- "scrollTop, scrollHeight, clientHeight ",
724
- G,
725
- X,
726
- ie
727
- );
728
- const Ce = X - G - ie < 10;
729
- if (console.log("isAtBottom", Ce), Ce ? N !== "LOCKED_AT_BOTTOM" && _("LOCKED_AT_BOTTOM") : N !== "IDLE_NOT_AT_BOTTOM" && _("IDLE_NOT_AT_BOTTOM"), Math.abs(G - 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)
730
734
  return;
731
735
  console.log(
732
- `Threshold passed at ${G}px. Recalculating range...`
736
+ `Threshold passed at ${b}px. Recalculating range...`
733
737
  );
734
- let ye = v - 1, pe = 0, $e = 0;
738
+ let ye = _ - 1, pe = 0, Ce = 0;
735
739
  for (; pe <= ye; ) {
736
740
  const Se = Math.floor((pe + ye) / 2);
737
- $[Se] < G ? ($e = Se, pe = Se + 1) : ye = Se - 1;
741
+ G[Se] < b ? (Ce = Se, pe = Se + 1) : ye = Se - 1;
738
742
  }
739
- const ke = Math.max(0, $e - a);
740
- let fe = ke;
741
- const Ue = G + ie;
742
- for (; fe < v && $[fe] < Ue; )
743
+ const $e = Math.max(0, Ce - a);
744
+ let fe = $e;
745
+ const Ue = b + ge;
746
+ for (; fe < _ && G[fe] < Ue; )
743
747
  fe++;
744
- O({
745
- startIndex: ke,
746
- endIndex: Math.min(v, fe + a)
747
- }), b.current = G;
748
+ w({
749
+ startIndex: $e,
750
+ endIndex: Math.min(_, fe + a)
751
+ }), x.current = b;
748
752
  };
749
- return M.addEventListener("scroll", F, {
753
+ return k.addEventListener("scroll", H, {
750
754
  passive: !0
751
- }), () => M.removeEventListener("scroll", F);
752
- }, [v, $, r, a, N]);
753
- const H = be(() => {
755
+ }), () => k.removeEventListener("scroll", H);
756
+ }, [_, G, r, a, C]);
757
+ const se = ke(() => {
754
758
  console.log(
755
759
  "USER ACTION: Clicked scroll button -> SCROLLING_TO_BOTTOM"
756
- ), _("SCROLLING_TO_BOTTOM");
757
- }, []), se = be(
758
- (M, x = "smooth") => {
759
- w.current && $[M] !== void 0 && (_("IDLE_NOT_AT_BOTTOM"), w.current.scrollTo({
760
- top: $[M],
761
- 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
762
766
  }));
763
767
  },
764
- [$]
765
- ), ge = {
768
+ [G]
769
+ ), De = {
766
770
  outer: {
767
- ref: w,
771
+ ref: E,
768
772
  style: { overflowY: "auto", height: "100%" }
769
773
  },
770
774
  inner: {
771
775
  style: {
772
- height: `${U}px`,
776
+ height: `${D}px`,
773
777
  position: "relative"
774
778
  }
775
779
  },
776
780
  list: {
777
781
  style: {
778
- transform: `translateY(${$[p.startIndex] || 0}px)`
782
+ transform: `translateY(${G[y.startIndex] || 0}px)`
779
783
  }
780
784
  }
781
785
  };
782
786
  return {
783
- virtualState: z,
784
- virtualizerProps: ge,
785
- scrollToBottom: H,
786
- scrollToIndex: se
787
+ virtualState: B,
788
+ virtualizerProps: De,
789
+ scrollToBottom: se,
790
+ scrollToIndex: ue
787
791
  };
788
792
  };
789
793
  if (l === "stateSort")
790
794
  return (t) => {
791
795
  const a = [...d()].sort(
792
- (u, w) => t(u.item, w.item)
796
+ (u, E) => t(u.item, E.item)
793
797
  ), c = a.map(({ item: u }) => u), f = {
794
798
  ...S,
795
799
  validIndices: a.map(
@@ -801,7 +805,7 @@ function Ie(e, i, m, g) {
801
805
  if (l === "stateFilter")
802
806
  return (t) => {
803
807
  const a = d().filter(
804
- ({ item: u }, w) => t(u, w)
808
+ ({ item: u }, E) => t(u, E)
805
809
  ), c = a.map(({ item: u }) => u), f = {
806
810
  ...S,
807
811
  validIndices: a.map(
@@ -814,22 +818,22 @@ function Ie(e, i, m, g) {
814
818
  return (t) => {
815
819
  const r = o.getState().getNestedState(e, n);
816
820
  return Array.isArray(r) ? (S?.validIndices || Array.from({ length: r.length }, (c, f) => f)).map((c, f) => {
817
- const u = r[c], w = [...n, c.toString()], p = s(u, w, S);
818
- return t(u, p, {
821
+ const u = r[c], E = [...n, c.toString()], y = s(u, E, S);
822
+ return t(u, y, {
819
823
  register: () => {
820
- const [, N] = K({}), _ = `${m}-${n.join(".")}-${c}`;
821
- le(() => {
822
- const k = `${e}////${_}`, j = o.getState().stateComponents.get(e) || {
824
+ const [, C] = K({}), A = `${m}-${n.join(".")}-${c}`;
825
+ ce(() => {
826
+ const M = `${e}////${A}`, F = o.getState().stateComponents.get(e) || {
823
827
  components: /* @__PURE__ */ new Map()
824
828
  };
825
- return j.components.set(k, {
826
- forceUpdate: () => N({}),
827
- paths: /* @__PURE__ */ new Set([w.join(".")])
828
- }), 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), () => {
829
833
  const Y = o.getState().stateComponents.get(e);
830
- Y && Y.components.delete(k);
834
+ Y && Y.components.delete(M);
831
835
  };
832
- }, [e, _]);
836
+ }, [e, A]);
833
837
  },
834
838
  index: f,
835
839
  originalIndex: c
@@ -840,20 +844,20 @@ function Ie(e, i, m, g) {
840
844
  ), null);
841
845
  };
842
846
  if (l === "stateMapNoRender")
843
- return (t) => I.map((a, c) => {
847
+ return (t) => T.map((a, c) => {
844
848
  let f;
845
849
  S?.validIndices && S.validIndices[c] !== void 0 ? f = S.validIndices[c] : f = c;
846
- const u = [...n, f.toString()], w = s(a, u, S);
850
+ const u = [...n, f.toString()], E = s(a, u, S);
847
851
  return t(
848
852
  a,
849
- w,
853
+ E,
850
854
  c,
851
- I,
852
- s(I, n, S)
855
+ T,
856
+ s(T, n, S)
853
857
  );
854
858
  });
855
859
  if (l === "$stateMap")
856
- return (t) => de(rt, {
860
+ return (t) => le(rt, {
857
861
  proxy: {
858
862
  _stateKey: e,
859
863
  _path: n,
@@ -866,15 +870,15 @@ function Ie(e, i, m, g) {
866
870
  return (t) => {
867
871
  const r = o.getState().getNestedState(e, n);
868
872
  return Array.isArray(r) ? (S?.validIndices || Array.from({ length: r.length }, (c, f) => f)).map((c, f) => {
869
- const u = r[c], w = [...n, c.toString()], p = s(u, w, S), O = `${m}-${n.join(".")}-${c}`;
870
- return de(at, {
873
+ const u = r[c], E = [...n, c.toString()], y = s(u, E, S), w = `${m}-${n.join(".")}-${c}`;
874
+ return le(at, {
871
875
  key: c,
872
876
  stateKey: e,
873
- itemComponentId: O,
874
- itemPath: w,
877
+ itemComponentId: w,
878
+ itemPath: E,
875
879
  children: t(
876
880
  u,
877
- p,
881
+ y,
878
882
  f,
879
883
  r,
880
884
  s(r, n, S)
@@ -886,8 +890,8 @@ function Ie(e, i, m, g) {
886
890
  };
887
891
  if (l === "stateFlattenOn")
888
892
  return (t) => {
889
- const r = I;
890
- T.clear(), A++;
893
+ const r = T;
894
+ I.clear(), N++;
891
895
  const a = r.flatMap(
892
896
  (c) => c[t] ?? []
893
897
  );
@@ -899,7 +903,7 @@ function Ie(e, i, m, g) {
899
903
  };
900
904
  if (l === "index")
901
905
  return (t) => {
902
- const r = I[t];
906
+ const r = T[t];
903
907
  return s(r, [...n, t.toString()]);
904
908
  };
905
909
  if (l === "last")
@@ -910,7 +914,7 @@ function Ie(e, i, m, g) {
910
914
  return s(a, c);
911
915
  };
912
916
  if (l === "insert")
913
- return (t) => (E(n), Ee(i, t, n, e), s(
917
+ return (t) => (p(n), Ee(i, t, n, e), s(
914
918
  o.getState().getNestedState(e, n),
915
919
  n
916
920
  ));
@@ -918,40 +922,40 @@ function Ie(e, i, m, g) {
918
922
  return (t, r, a) => {
919
923
  const c = o.getState().getNestedState(e, n), f = ee(t) ? t(c) : t;
920
924
  let u = null;
921
- if (!c.some((p) => {
925
+ if (!c.some((y) => {
922
926
  if (r) {
923
- const N = r.every(
924
- (_) => q(p[_], f[_])
927
+ const C = r.every(
928
+ (A) => J(y[A], f[A])
925
929
  );
926
- return N && (u = p), N;
930
+ return C && (u = y), C;
927
931
  }
928
- const O = q(p, f);
929
- return O && (u = p), O;
932
+ const w = J(y, f);
933
+ return w && (u = y), w;
930
934
  }))
931
- E(n), Ee(i, f, n, e);
935
+ p(n), Ee(i, f, n, e);
932
936
  else if (a && u) {
933
- const p = a(u), O = c.map(
934
- (N) => q(N, u) ? p : N
937
+ const y = a(u), w = c.map(
938
+ (C) => J(C, u) ? y : C
935
939
  );
936
- E(n), ce(i, O, n);
940
+ p(n), ie(i, w, n);
937
941
  }
938
942
  };
939
943
  if (l === "cut")
940
944
  return (t, r) => {
941
945
  if (!r?.waitForSync)
942
- return E(n), me(i, n, e, t), s(
946
+ return p(n), me(i, n, e, t), s(
943
947
  o.getState().getNestedState(e, n),
944
948
  n
945
949
  );
946
950
  };
947
951
  if (l === "cutByValue")
948
952
  return (t) => {
949
- for (let r = 0; r < I.length; r++)
950
- I[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);
951
955
  };
952
956
  if (l === "toggleByValue")
953
957
  return (t) => {
954
- const r = I.findIndex((a) => a === t);
958
+ const r = T.findIndex((a) => a === t);
955
959
  r > -1 ? me(i, n, e, r) : Ee(i, t, n, e);
956
960
  };
957
961
  if (l === "stateFind")
@@ -986,20 +990,20 @@ function Ie(e, i, m, g) {
986
990
  }
987
991
  if (l === "get")
988
992
  return () => {
989
- if (S?.validIndices && Array.isArray(I)) {
993
+ if (S?.validIndices && Array.isArray(T)) {
990
994
  const d = o.getState().getNestedState(e, n);
991
995
  return S.validIndices.map((t) => d[t]);
992
996
  }
993
997
  return o.getState().getNestedState(e, n);
994
998
  };
995
999
  if (l === "$derive")
996
- return (d) => Ge({
1000
+ return (d) => Ve({
997
1001
  _stateKey: e,
998
1002
  _path: n,
999
1003
  _effect: d.toString()
1000
1004
  });
1001
1005
  if (l === "$get")
1002
- return () => Ge({
1006
+ return () => Ve({
1003
1007
  _stateKey: e,
1004
1008
  _path: n
1005
1009
  });
@@ -1008,7 +1012,7 @@ function Ie(e, i, m, g) {
1008
1012
  return o.getState().getSyncInfo(d);
1009
1013
  }
1010
1014
  if (l == "getLocalStorage")
1011
- return (d) => Te(g + "-" + e + "-" + d);
1015
+ return (d) => Ie(g + "-" + e + "-" + d);
1012
1016
  if (l === "_selected") {
1013
1017
  const d = n.slice(0, -1), t = d.join("."), r = o.getState().getNestedState(e, d);
1014
1018
  return Array.isArray(r) ? Number(n[n.length - 1]) === o.getState().getSelectedIndex(e, t) : void 0;
@@ -1018,7 +1022,7 @@ function Ie(e, i, m, g) {
1018
1022
  const t = n.slice(0, -1), r = Number(n[n.length - 1]), a = t.join(".");
1019
1023
  d ? o.getState().setSelectedIndex(e, a, r) : o.getState().setSelectedIndex(e, a, void 0);
1020
1024
  const c = o.getState().getNestedState(e, [...t]);
1021
- ce(i, c, t), E(t);
1025
+ ie(i, c, t), p(t);
1022
1026
  };
1023
1027
  if (l === "toggleSelected")
1024
1028
  return () => {
@@ -1029,13 +1033,13 @@ function Ie(e, i, m, g) {
1029
1033
  a === t ? void 0 : t
1030
1034
  );
1031
1035
  const c = o.getState().getNestedState(e, [...d]);
1032
- ce(i, c, d), E(d);
1036
+ ie(i, c, d), p(d);
1033
1037
  };
1034
1038
  if (n.length == 0) {
1035
1039
  if (l === "applyJsonPatch")
1036
1040
  return (d) => {
1037
1041
  const t = o.getState().cogsStateStore[e], a = Je(t, d).newDocument;
1038
- De(
1042
+ Le(
1039
1043
  e,
1040
1044
  o.getState().initialStateGlobal[e],
1041
1045
  a,
@@ -1047,55 +1051,55 @@ function Ie(e, i, m, g) {
1047
1051
  if (c) {
1048
1052
  const f = _e(t, a), u = new Set(f);
1049
1053
  for (const [
1050
- w,
1051
- p
1054
+ E,
1055
+ y
1052
1056
  ] of c.components.entries()) {
1053
- let O = !1;
1054
- const N = Array.isArray(p.reactiveType) ? p.reactiveType : [p.reactiveType || "component"];
1055
- if (!N.includes("none")) {
1056
- if (N.includes("all")) {
1057
- 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();
1058
1062
  continue;
1059
1063
  }
1060
- if (N.includes("component") && (p.paths.has("") && (O = !0), !O))
1061
- for (const _ of u) {
1062
- if (p.paths.has(_)) {
1063
- O = !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;
1064
1068
  break;
1065
1069
  }
1066
- let k = _.lastIndexOf(".");
1067
- for (; k !== -1; ) {
1068
- const j = _.substring(0, k);
1069
- if (p.paths.has(j)) {
1070
- O = !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;
1071
1075
  break;
1072
1076
  }
1073
- const Y = _.substring(
1074
- k + 1
1077
+ const Y = A.substring(
1078
+ M + 1
1075
1079
  );
1076
1080
  if (!isNaN(Number(Y))) {
1077
- const b = j.lastIndexOf(".");
1078
- if (b !== -1) {
1079
- const C = j.substring(
1081
+ const x = F.lastIndexOf(".");
1082
+ if (x !== -1) {
1083
+ const $ = F.substring(
1080
1084
  0,
1081
- b
1085
+ x
1082
1086
  );
1083
- if (p.paths.has(C)) {
1084
- O = !0;
1087
+ if (y.paths.has($)) {
1088
+ w = !0;
1085
1089
  break;
1086
1090
  }
1087
1091
  }
1088
1092
  }
1089
- k = j.lastIndexOf(".");
1093
+ M = F.lastIndexOf(".");
1090
1094
  }
1091
- if (O) break;
1095
+ if (w) break;
1092
1096
  }
1093
- if (!O && N.includes("deps") && p.depsFunction) {
1094
- const _ = p.depsFunction(a);
1095
- let k = !1;
1096
- typeof _ == "boolean" ? _ && (k = !0) : q(p.deps, _) || (p.deps = _, k = !0), k && (O = !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);
1097
1101
  }
1098
- O && p.forceUpdate();
1102
+ w && y.forceUpdate();
1099
1103
  }
1100
1104
  }
1101
1105
  }
@@ -1116,8 +1120,8 @@ function Ie(e, i, m, g) {
1116
1120
  });
1117
1121
  const c = d.zodSchema.safeParse(r);
1118
1122
  return c.success ? !0 : (c.error.errors.forEach((u) => {
1119
- const w = u.path, p = u.message, O = [d.key, ...w].join(".");
1120
- t(O, p);
1123
+ const E = u.path, y = u.message, w = [d.key, ...E].join(".");
1124
+ t(w, y);
1121
1125
  }), ve(e), !1);
1122
1126
  } catch (a) {
1123
1127
  return console.error("Zod schema validation failed", a), !1;
@@ -1127,7 +1131,7 @@ function Ie(e, i, m, g) {
1127
1131
  if (l === "getComponents")
1128
1132
  return () => o().stateComponents.get(e);
1129
1133
  if (l === "getAllFormRefs")
1130
- return () => Me.getState().getFormRefsByStateKey(e);
1134
+ return () => be.getState().getFormRefsByStateKey(e);
1131
1135
  if (l === "_initialState")
1132
1136
  return o.getState().initialStateGlobal[e];
1133
1137
  if (l === "_serverState")
@@ -1135,17 +1139,17 @@ function Ie(e, i, m, g) {
1135
1139
  if (l === "_isLoading")
1136
1140
  return o.getState().isLoadingGlobal[e];
1137
1141
  if (l === "revertToInitialState")
1138
- return y.revertToInitialState;
1139
- if (l === "updateInitialState") return y.updateInitialState;
1140
- if (l === "removeValidation") return y.removeValidation;
1142
+ return v.revertToInitialState;
1143
+ if (l === "updateInitialState") return v.updateInitialState;
1144
+ if (l === "removeValidation") return v.removeValidation;
1141
1145
  }
1142
1146
  if (l === "getFormRef")
1143
- return () => Me.getState().getFormRef(e + "." + n.join("."));
1147
+ return () => be.getState().getFormRef(e + "." + n.join("."));
1144
1148
  if (l === "validationWrapper")
1145
1149
  return ({
1146
1150
  children: d,
1147
1151
  hideMessage: t
1148
- }) => /* @__PURE__ */ we(
1152
+ }) => /* @__PURE__ */ Oe(
1149
1153
  We,
1150
1154
  {
1151
1155
  formOpts: t ? { validation: { message: "" } } : void 0,
@@ -1158,24 +1162,24 @@ function Ie(e, i, m, g) {
1158
1162
  );
1159
1163
  if (l === "_stateKey") return e;
1160
1164
  if (l === "_path") return n;
1161
- if (l === "_isServerSynced") return y._isServerSynced;
1165
+ if (l === "_isServerSynced") return v._isServerSynced;
1162
1166
  if (l === "update")
1163
1167
  return (d, t) => {
1164
1168
  if (t?.debounce)
1165
1169
  Be(() => {
1166
- ce(i, d, n, "");
1170
+ ie(i, d, n, "");
1167
1171
  const r = o.getState().getNestedState(e, n);
1168
1172
  t?.afterUpdate && t.afterUpdate(r);
1169
1173
  }, t.debounce);
1170
1174
  else {
1171
- ce(i, d, n, "");
1175
+ ie(i, d, n, "");
1172
1176
  const r = o.getState().getNestedState(e, n);
1173
1177
  t?.afterUpdate && t.afterUpdate(r);
1174
1178
  }
1175
- E(n);
1179
+ p(n);
1176
1180
  };
1177
1181
  if (l === "formElement")
1178
- return (d, t) => /* @__PURE__ */ we(
1182
+ return (d, t) => /* @__PURE__ */ Oe(
1179
1183
  ze,
1180
1184
  {
1181
1185
  setState: i,
@@ -1185,21 +1189,21 @@ function Ie(e, i, m, g) {
1185
1189
  formOpts: t
1186
1190
  }
1187
1191
  );
1188
- const P = [...n, l], ae = o.getState().getNestedState(e, P);
1189
- return s(ae, P, S);
1192
+ const R = [...n, l], ae = o.getState().getNestedState(e, R);
1193
+ return s(ae, R, S);
1190
1194
  }
1191
- }, W = new Proxy(L, R);
1192
- return T.set(B, {
1193
- proxy: W,
1194
- stateVersion: A
1195
- }), W;
1195
+ }, z = new Proxy(U, j);
1196
+ return I.set(W, {
1197
+ proxy: z,
1198
+ stateVersion: N
1199
+ }), z;
1196
1200
  }
1197
1201
  return s(
1198
1202
  o.getState().getNestedState(e, [])
1199
1203
  );
1200
1204
  }
1201
- function Ge(e) {
1202
- return de(ot, { proxy: e });
1205
+ function Ve(e) {
1206
+ return le(ot, { proxy: e });
1203
1207
  }
1204
1208
  function rt({
1205
1209
  proxy: e,
@@ -1210,26 +1214,26 @@ function rt({
1210
1214
  m,
1211
1215
  e._path
1212
1216
  ).stateMapNoRender(
1213
- (T, A, E, y, s) => e._mapFn(T, A, E, y, s)
1217
+ (I, N, p, v, s) => e._mapFn(I, N, p, v, s)
1214
1218
  ) : null;
1215
1219
  }
1216
1220
  function ot({
1217
1221
  proxy: e
1218
1222
  }) {
1219
- const i = J(null), m = `${e._stateKey}-${e._path.join(".")}`;
1223
+ const i = q(null), m = `${e._stateKey}-${e._path.join(".")}`;
1220
1224
  return oe(() => {
1221
1225
  const g = i.current;
1222
1226
  if (!g || !g.parentElement) return;
1223
- const T = g.parentElement, E = Array.from(T.childNodes).indexOf(g);
1224
- let y = T.getAttribute("data-parent-id");
1225
- y || (y = `parent-${crypto.randomUUID()}`, T.setAttribute("data-parent-id", y));
1226
- const I = {
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 = {
1227
1231
  instanceId: `instance-${crypto.randomUUID()}`,
1228
- parentId: y,
1229
- position: E,
1232
+ parentId: v,
1233
+ position: p,
1230
1234
  effect: e._effect
1231
1235
  };
1232
- o.getState().addSignalElement(m, I);
1236
+ o.getState().addSignalElement(m, T);
1233
1237
  const n = o.getState().getNestedState(e._stateKey, e._path);
1234
1238
  let S;
1235
1239
  if (e._effect)
@@ -1238,21 +1242,21 @@ function ot({
1238
1242
  "state",
1239
1243
  `return (${e._effect})(state)`
1240
1244
  )(n);
1241
- } catch (L) {
1242
- console.error("Error evaluating effect function during mount:", L), S = n;
1245
+ } catch (U) {
1246
+ console.error("Error evaluating effect function during mount:", U), S = n;
1243
1247
  }
1244
1248
  else
1245
1249
  S = n;
1246
1250
  S !== null && typeof S == "object" && (S = JSON.stringify(S));
1247
- const B = document.createTextNode(String(S));
1248
- g.replaceWith(B);
1249
- }, [e._stateKey, e._path.join("."), e._effect]), de("span", {
1251
+ const W = document.createTextNode(String(S));
1252
+ g.replaceWith(W);
1253
+ }, [e._stateKey, e._path.join("."), e._effect]), le("span", {
1250
1254
  ref: i,
1251
1255
  style: { display: "none" },
1252
1256
  "data-signal-id": m
1253
1257
  });
1254
1258
  }
1255
- function yt(e) {
1259
+ function pt(e) {
1256
1260
  const i = je(
1257
1261
  (m) => {
1258
1262
  const g = o.getState().stateComponents.get(e._stateKey) || {
@@ -1265,7 +1269,7 @@ function yt(e) {
1265
1269
  },
1266
1270
  () => o.getState().getNestedState(e._stateKey, e._path)
1267
1271
  );
1268
- return de("text", {}, String(i));
1272
+ return le("text", {}, String(i));
1269
1273
  }
1270
1274
  function at({
1271
1275
  stateKey: e,
@@ -1273,32 +1277,32 @@ function at({
1273
1277
  itemPath: m,
1274
1278
  children: g
1275
1279
  }) {
1276
- const [, T] = K({}), [A, E] = Ye(), y = J(null);
1280
+ const [, I] = K({}), [N, p] = Ye(), v = q(null);
1277
1281
  return oe(() => {
1278
- E.height > 0 && E.height !== y.current && (y.current = E.height, o.getState().setShadowMetadata(e, m, {
1282
+ p.height > 0 && p.height !== v.current && (v.current = p.height, o.getState().setShadowMetadata(e, m, {
1279
1283
  virtualizer: {
1280
- itemHeight: E.height
1284
+ itemHeight: p.height
1281
1285
  }
1282
1286
  }));
1283
- }, [E.height, e, m]), le(() => {
1284
- const s = `${e}////${i}`, I = o.getState().stateComponents.get(e) || {
1287
+ }, [p.height, e, m]), ce(() => {
1288
+ const s = `${e}////${i}`, T = o.getState().stateComponents.get(e) || {
1285
1289
  components: /* @__PURE__ */ new Map()
1286
1290
  };
1287
- return I.components.set(s, {
1288
- forceUpdate: () => T({}),
1291
+ return T.components.set(s, {
1292
+ forceUpdate: () => I({}),
1289
1293
  paths: /* @__PURE__ */ new Set([m.join(".")])
1290
- }), o.getState().stateComponents.set(e, I), () => {
1294
+ }), o.getState().stateComponents.set(e, T), () => {
1291
1295
  const n = o.getState().stateComponents.get(e);
1292
1296
  n && n.components.delete(s);
1293
1297
  };
1294
- }, [e, i, m.join(".")]), /* @__PURE__ */ we("div", { ref: A, children: g });
1298
+ }, [e, i, m.join(".")]), /* @__PURE__ */ Oe("div", { ref: N, children: g });
1295
1299
  }
1296
1300
  export {
1297
- Ge as $cogsSignal,
1298
- yt as $cogsSignalStore,
1301
+ Ve as $cogsSignal,
1302
+ pt as $cogsSignalStore,
1299
1303
  It as addStateOptions,
1300
- Tt as createCogsState,
1301
- vt as notifyComponent,
1304
+ vt as createCogsState,
1305
+ yt as notifyComponent,
1302
1306
  nt as useCogsStateFn
1303
1307
  };
1304
1308
  //# sourceMappingURL=CogsState.jsx.map