cogsbox-state 0.5.375 → 0.5.377

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