cogsbox-state 0.5.385 → 0.5.386

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