cogsbox-state 0.5.384 → 0.5.385

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