cogsbox-state 0.5.431 → 0.5.432

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,179 +1,179 @@
1
1
  "use client";
2
2
  import { jsx as $t } from "react/jsx-runtime";
3
- import { useState as lt, useRef as B, useEffect as rt, useLayoutEffect as _t, useMemo as At, createElement as gt, useSyncExternalStore as Ft, startTransition as Ut, useCallback as Et } from "react";
4
- import { transformStateFunc as Dt, isDeepEqual as Q, isFunction as tt, getNestedValue as U, getDifferences as Pt, debounce as Gt } from "./utility.js";
5
- import { pushFunc as wt, updateFn as ut, cutFunc as ft, ValidationWrapper as Lt, FormControlComponent as Wt } from "./Functions.jsx";
6
- import Bt from "superjson";
3
+ import { useState as dt, useRef as q, useEffect as at, useLayoutEffect as pt, useMemo as Et, createElement as gt, useSyncExternalStore as Ft, startTransition as Ut, useCallback as St } from "react";
4
+ import { transformStateFunc as Dt, isDeepEqual as X, isFunction as et, getNestedValue as L, getDifferences as Pt, debounce as Lt } from "./utility.js";
5
+ import { pushFunc as At, updateFn as ut, cutFunc as ft, ValidationWrapper as Gt, FormControlComponent as Ht } from "./Functions.jsx";
6
+ import zt from "superjson";
7
7
  import { v4 as kt } from "uuid";
8
8
  import "zod";
9
- import { getGlobalStore as o, formRefStore as Vt } from "./store.js";
10
- import { useCogsConfig as Mt } from "./CogsStateClient.jsx";
11
- import { applyPatch as zt } from "fast-json-patch";
12
- import Ht from "react-use-measure";
9
+ import { getGlobalStore as o, formRefStore as bt } from "./store.js";
10
+ import { useCogsConfig as Rt } from "./CogsStateClient.jsx";
11
+ import { applyPatch as Wt } from "fast-json-patch";
12
+ import Bt from "react-use-measure";
13
13
  import "ulid";
14
- function bt(t, i) {
14
+ function Mt(t, s) {
15
15
  const S = o.getState().getInitialOptions, g = o.getState().setInitialStateOptions, I = S(t) || {};
16
16
  g(t, {
17
17
  ...I,
18
- ...i
18
+ ...s
19
19
  });
20
20
  }
21
21
  function Nt({
22
22
  stateKey: t,
23
- options: i,
23
+ options: s,
24
24
  initialOptionsPart: S
25
25
  }) {
26
- const g = ct(t) || {}, I = S[t] || {}, k = o.getState().setInitialStateOptions, w = { ...I, ...g };
26
+ const g = lt(t) || {}, I = S[t] || {}, T = o.getState().setInitialStateOptions, A = { ...I, ...g };
27
27
  let v = !1;
28
- if (i)
29
- for (const s in i)
30
- w.hasOwnProperty(s) ? (s == "localStorage" && i[s] && w[s].key !== i[s]?.key && (v = !0, w[s] = i[s]), s == "initialState" && i[s] && w[s] !== i[s] && // Different references
31
- !Q(w[s], i[s]) && (v = !0, w[s] = i[s])) : (v = !0, w[s] = i[s]);
32
- v && k(t, w);
28
+ if (s)
29
+ for (const i in s)
30
+ A.hasOwnProperty(i) ? (i == "localStorage" && s[i] && A[i].key !== s[i]?.key && (v = !0, A[i] = s[i]), i == "initialState" && s[i] && A[i] !== s[i] && // Different references
31
+ !X(A[i], s[i]) && (v = !0, A[i] = s[i])) : (v = !0, A[i] = s[i]);
32
+ v && T(t, A);
33
33
  }
34
- function Se(t, { formElements: i, validation: S }) {
35
- return { initialState: t, formElements: i, validation: S };
34
+ function Se(t, { formElements: s, validation: S }) {
35
+ return { initialState: t, formElements: s, validation: S };
36
36
  }
37
- const me = (t, i) => {
37
+ const me = (t, s) => {
38
38
  let S = t;
39
39
  const [g, I] = Dt(S);
40
- (Object.keys(I).length > 0 || i && Object.keys(i).length > 0) && Object.keys(I).forEach((v) => {
40
+ (Object.keys(I).length > 0 || s && Object.keys(s).length > 0) && Object.keys(I).forEach((v) => {
41
41
  I[v] = I[v] || {}, I[v].formElements = {
42
- ...i?.formElements,
42
+ ...s?.formElements,
43
43
  // Global defaults first
44
- ...i?.validation,
44
+ ...s?.validation,
45
45
  ...I[v].formElements || {}
46
46
  // State-specific overrides
47
- }, ct(v) || o.getState().setInitialStateOptions(v, I[v]);
47
+ }, lt(v) || o.getState().setInitialStateOptions(v, I[v]);
48
48
  }), o.getState().setInitialStates(g), o.getState().setCreatedState(g);
49
- const k = (v, s) => {
50
- const [$] = lt(s?.componentId ?? kt());
49
+ const T = (v, i) => {
50
+ const [$] = dt(i?.componentId ?? kt());
51
51
  Nt({
52
52
  stateKey: v,
53
- options: s,
53
+ options: i,
54
54
  initialOptionsPart: I
55
55
  });
56
- const y = o.getState().cogsStateStore[v] || g[v], r = s?.modifyState ? s.modifyState(y) : y, [h, M] = Xt(
57
- r,
56
+ const y = o.getState().cogsStateStore[v] || g[v], n = i?.modifyState ? i.modifyState(y) : y, [w, O] = Xt(
57
+ n,
58
58
  {
59
59
  stateKey: v,
60
- syncUpdate: s?.syncUpdate,
60
+ syncUpdate: i?.syncUpdate,
61
61
  componentId: $,
62
- localStorage: s?.localStorage,
63
- middleware: s?.middleware,
64
- enabledSync: s?.enabledSync,
65
- reactiveType: s?.reactiveType,
66
- reactiveDeps: s?.reactiveDeps,
67
- initialState: s?.initialState,
68
- dependencies: s?.dependencies,
69
- serverState: s?.serverState
62
+ localStorage: i?.localStorage,
63
+ middleware: i?.middleware,
64
+ enabledSync: i?.enabledSync,
65
+ reactiveType: i?.reactiveType,
66
+ reactiveDeps: i?.reactiveDeps,
67
+ initialState: i?.initialState,
68
+ dependencies: i?.dependencies,
69
+ serverState: i?.serverState
70
70
  }
71
71
  );
72
- return M;
72
+ return O;
73
73
  };
74
- function w(v, s) {
75
- Nt({ stateKey: v, options: s, initialOptionsPart: I }), s.localStorage && Yt(v, s), nt(v);
74
+ function A(v, i) {
75
+ Nt({ stateKey: v, options: i, initialOptionsPart: I }), i.localStorage && Yt(v, i), ot(v);
76
76
  }
77
- return { useCogsState: k, setCogsOptions: w };
77
+ return { useCogsState: T, setCogsOptions: A };
78
78
  }, {
79
- setUpdaterState: St,
80
- setState: ot,
81
- getInitialOptions: ct,
79
+ setUpdaterState: mt,
80
+ setState: st,
81
+ getInitialOptions: lt,
82
82
  getKeyState: Ot,
83
83
  getValidationErrors: qt,
84
84
  setStateLog: Jt,
85
- updateInitialStateGlobal: Tt,
86
- addValidationError: xt,
87
- removeValidationError: K,
85
+ updateInitialStateGlobal: xt,
86
+ addValidationError: Tt,
87
+ removeValidationError: tt,
88
88
  setServerSyncActions: Zt
89
- } = o.getState(), pt = (t, i, S, g, I) => {
89
+ } = o.getState(), Ct = (t, s, S, g, I) => {
90
90
  S?.log && console.log(
91
91
  "saving to localstorage",
92
- i,
92
+ s,
93
93
  S.localStorage?.key,
94
94
  g
95
95
  );
96
- const k = tt(S?.localStorage?.key) ? S.localStorage?.key(t) : S?.localStorage?.key;
97
- if (k && g) {
98
- const w = `${g}-${i}-${k}`;
96
+ const T = et(S?.localStorage?.key) ? S.localStorage?.key(t) : S?.localStorage?.key;
97
+ if (T && g) {
98
+ const A = `${g}-${s}-${T}`;
99
99
  let v;
100
100
  try {
101
- v = It(w)?.lastSyncedWithServer;
101
+ v = yt(A)?.lastSyncedWithServer;
102
102
  } catch {
103
103
  }
104
- const s = {
104
+ const i = {
105
105
  state: t,
106
106
  lastUpdated: Date.now(),
107
107
  lastSyncedWithServer: I ?? v
108
- }, $ = Bt.serialize(s);
108
+ }, $ = zt.serialize(i);
109
109
  window.localStorage.setItem(
110
- w,
110
+ A,
111
111
  JSON.stringify($.json)
112
112
  );
113
113
  }
114
- }, It = (t) => {
114
+ }, yt = (t) => {
115
115
  if (!t) return null;
116
116
  try {
117
- const i = window.localStorage.getItem(t);
118
- return i ? JSON.parse(i) : null;
119
- } catch (i) {
120
- return console.error("Error loading from localStorage:", i), null;
117
+ const s = window.localStorage.getItem(t);
118
+ return s ? JSON.parse(s) : null;
119
+ } catch (s) {
120
+ return console.error("Error loading from localStorage:", s), null;
121
121
  }
122
- }, Yt = (t, i) => {
123
- const S = o.getState().cogsStateStore[t], { sessionId: g } = Mt(), I = tt(i?.localStorage?.key) ? i.localStorage.key(S) : i?.localStorage?.key;
122
+ }, Yt = (t, s) => {
123
+ const S = o.getState().cogsStateStore[t], { sessionId: g } = Rt(), I = et(s?.localStorage?.key) ? s.localStorage.key(S) : s?.localStorage?.key;
124
124
  if (I && g) {
125
- const k = It(
125
+ const T = yt(
126
126
  `${g}-${t}-${I}`
127
127
  );
128
- if (k && k.lastUpdated > (k.lastSyncedWithServer || 0))
129
- return ot(t, k.state), nt(t), !0;
128
+ if (T && T.lastUpdated > (T.lastSyncedWithServer || 0))
129
+ return st(t, T.state), ot(t), !0;
130
130
  }
131
131
  return !1;
132
- }, Rt = (t, i, S, g, I, k) => {
133
- const w = {
134
- initialState: i,
135
- updaterState: mt(
132
+ }, jt = (t, s, S, g, I, T) => {
133
+ const A = {
134
+ initialState: s,
135
+ updaterState: It(
136
136
  t,
137
137
  g,
138
138
  I,
139
- k
139
+ T
140
140
  ),
141
141
  state: S
142
142
  };
143
- Tt(t, w.initialState), St(t, w.updaterState), ot(t, w.state);
144
- }, nt = (t) => {
145
- const i = o.getState().stateComponents.get(t);
146
- if (!i) return;
143
+ xt(t, A.initialState), mt(t, A.updaterState), st(t, A.state);
144
+ }, ot = (t) => {
145
+ const s = o.getState().stateComponents.get(t);
146
+ if (!s) return;
147
147
  const S = /* @__PURE__ */ new Set();
148
- i.components.forEach((g) => {
148
+ s.components.forEach((g) => {
149
149
  (g ? Array.isArray(g.reactiveType) ? g.reactiveType : [g.reactiveType || "component"] : null)?.includes("none") || S.add(() => g.forceUpdate());
150
150
  }), queueMicrotask(() => {
151
151
  S.forEach((g) => g());
152
152
  });
153
- }, Ie = (t, i) => {
153
+ }, Ie = (t, s) => {
154
154
  const S = o.getState().stateComponents.get(t);
155
155
  if (S) {
156
- const g = `${t}////${i}`, I = S.components.get(g);
156
+ const g = `${t}////${s}`, I = S.components.get(g);
157
157
  if ((I ? Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"] : null)?.includes("none"))
158
158
  return;
159
159
  I && I.forceUpdate();
160
160
  }
161
- }, Qt = (t, i, S, g) => {
161
+ }, Qt = (t, s, S, g) => {
162
162
  switch (t) {
163
163
  case "update":
164
164
  return {
165
- oldValue: U(i, g),
166
- newValue: U(S, g)
165
+ oldValue: L(s, g),
166
+ newValue: L(S, g)
167
167
  };
168
168
  case "insert":
169
169
  return {
170
170
  oldValue: null,
171
171
  // or undefined
172
- newValue: U(S, g)
172
+ newValue: L(S, g)
173
173
  };
174
174
  case "cut":
175
175
  return {
176
- oldValue: U(i, g),
176
+ oldValue: L(s, g),
177
177
  newValue: null
178
178
  // or undefined
179
179
  };
@@ -182,303 +182,303 @@ const me = (t, i) => {
182
182
  }
183
183
  };
184
184
  function Xt(t, {
185
- stateKey: i,
185
+ stateKey: s,
186
186
  serverSync: S,
187
187
  localStorage: g,
188
188
  formElements: I,
189
- reactiveDeps: k,
190
- reactiveType: w,
189
+ reactiveDeps: T,
190
+ reactiveType: A,
191
191
  componentId: v,
192
- initialState: s,
192
+ initialState: i,
193
193
  syncUpdate: $,
194
194
  dependencies: y,
195
- serverState: r
195
+ serverState: n
196
196
  } = {}) {
197
- const [h, M] = lt({}), { sessionId: C } = Mt();
198
- let X = !i;
199
- const [f] = lt(i ?? kt()), W = o.getState().stateLog[f], c = B(/* @__PURE__ */ new Set()), et = B(v ?? kt()), P = B(
197
+ const [w, O] = dt({}), { sessionId: p } = Rt();
198
+ let K = !s;
199
+ const [f] = dt(s ?? kt()), z = o.getState().stateLog[f], c = q(/* @__PURE__ */ new Set()), nt = q(v ?? kt()), R = q(
200
200
  null
201
201
  );
202
- P.current = ct(f) ?? null, rt(() => {
202
+ R.current = lt(f) ?? null, at(() => {
203
203
  if ($ && $.stateKey === f && $.path?.[0]) {
204
- ot(f, (e) => ({
204
+ st(f, (e) => ({
205
205
  ...e,
206
206
  [$.path[0]]: $.newValue
207
207
  }));
208
- const n = `${$.stateKey}:${$.path.join(".")}`;
209
- o.getState().setSyncInfo(n, {
208
+ const r = `${$.stateKey}:${$.path.join(".")}`;
209
+ o.getState().setSyncInfo(r, {
210
210
  timeStamp: $.timeStamp,
211
211
  userId: $.userId
212
212
  });
213
213
  }
214
- }, [$]), rt(() => {
215
- if (s) {
216
- bt(f, {
217
- initialState: s
214
+ }, [$]), at(() => {
215
+ if (i) {
216
+ Mt(f, {
217
+ initialState: i
218
218
  });
219
- const n = P.current, a = n?.serverState?.id !== void 0 && n?.serverState?.status === "success" && n?.serverState?.data, u = o.getState().initialStateGlobal[f];
220
- if (!(u && !Q(u, s) || !u) && !a)
219
+ const r = R.current, a = r?.serverState?.id !== void 0 && r?.serverState?.status === "success" && r?.serverState?.data, d = o.getState().initialStateGlobal[f];
220
+ if (!(d && !X(d, i) || !d) && !a)
221
221
  return;
222
222
  let l = null;
223
- const m = tt(n?.localStorage?.key) ? n?.localStorage?.key(s) : n?.localStorage?.key;
224
- m && C && (l = It(`${C}-${f}-${m}`));
225
- let x = s, T = !1;
226
- const R = a ? Date.now() : 0, z = l?.lastUpdated || 0, D = l?.lastSyncedWithServer || 0;
227
- a && R > z ? (x = n.serverState.data, T = !0) : l && z > D && (x = l.state, n?.localStorage?.onChange && n?.localStorage?.onChange(x)), o.getState().initializeShadowState(f, s), Rt(
223
+ const m = et(r?.localStorage?.key) ? r?.localStorage?.key(i) : r?.localStorage?.key;
224
+ m && p && (l = yt(`${p}-${f}-${m}`));
225
+ let x = i, b = !1;
226
+ const F = a ? Date.now() : 0, W = l?.lastUpdated || 0, D = l?.lastSyncedWithServer || 0;
227
+ a && F > W ? (x = r.serverState.data, b = !0) : l && W > D && (x = l.state, r?.localStorage?.onChange && r?.localStorage?.onChange(x)), o.getState().initializeShadowState(f, i), jt(
228
228
  f,
229
- s,
229
+ i,
230
230
  x,
231
- at,
232
- et.current,
233
- C
234
- ), T && m && C && pt(x, f, n, C, Date.now()), nt(f), (Array.isArray(w) ? w : [w || "component"]).includes("none") || M({});
231
+ it,
232
+ nt.current,
233
+ p
234
+ ), b && m && p && Ct(x, f, r, p, Date.now()), ot(f), (Array.isArray(A) ? A : [A || "component"]).includes("none") || O({});
235
235
  }
236
236
  }, [
237
- s,
238
- r?.status,
239
- r?.data,
237
+ i,
238
+ n?.status,
239
+ n?.data,
240
240
  ...y || []
241
- ]), _t(() => {
242
- X && bt(f, {
241
+ ]), pt(() => {
242
+ K && Mt(f, {
243
243
  serverSync: S,
244
244
  formElements: I,
245
- initialState: s,
245
+ initialState: i,
246
246
  localStorage: g,
247
- middleware: P.current?.middleware
247
+ middleware: R.current?.middleware
248
248
  });
249
- const n = `${f}////${et.current}`, e = o.getState().stateComponents.get(f) || {
249
+ const r = `${f}////${nt.current}`, e = o.getState().stateComponents.get(f) || {
250
250
  components: /* @__PURE__ */ new Map()
251
251
  };
252
- return e.components.set(n, {
253
- forceUpdate: () => M({}),
252
+ return e.components.set(r, {
253
+ forceUpdate: () => O({}),
254
254
  paths: /* @__PURE__ */ new Set(),
255
255
  deps: [],
256
- depsFunction: k || void 0,
257
- reactiveType: w ?? ["component", "deps"]
258
- }), o.getState().stateComponents.set(f, e), M({}), () => {
259
- e && (e.components.delete(n), e.components.size === 0 && o.getState().stateComponents.delete(f));
256
+ depsFunction: T || void 0,
257
+ reactiveType: A ?? ["component", "deps"]
258
+ }), o.getState().stateComponents.set(f, e), O({}), () => {
259
+ e && (e.components.delete(r), e.components.size === 0 && o.getState().stateComponents.delete(f));
260
260
  };
261
261
  }, []);
262
- const at = (n, e, a, u) => {
262
+ const it = (r, e, a, d) => {
263
263
  if (Array.isArray(e)) {
264
264
  const l = `${f}-${e.join(".")}`;
265
265
  c.current.add(l);
266
266
  }
267
- const d = o.getState();
268
- ot(f, (l) => {
269
- const m = tt(n) ? n(l) : n, x = `${f}-${e.join(".")}`;
267
+ const u = o.getState();
268
+ st(f, (l) => {
269
+ const m = et(r) ? r(l) : r, x = `${f}-${e.join(".")}`;
270
270
  if (x) {
271
- let _ = !1, E = d.signalDomElements.get(x);
272
- if ((!E || E.size === 0) && (a.updateType === "insert" || a.updateType === "cut")) {
273
- const V = e.slice(0, -1), N = U(m, V);
274
- if (Array.isArray(N)) {
275
- _ = !0;
276
- const A = `${f}-${V.join(".")}`;
277
- E = d.signalDomElements.get(A);
271
+ let P = !1, h = u.signalDomElements.get(x);
272
+ if ((!h || h.size === 0) && (a.updateType === "insert" || a.updateType === "cut")) {
273
+ const C = e.slice(0, -1), V = L(m, C);
274
+ if (Array.isArray(V)) {
275
+ P = !0;
276
+ const E = `${f}-${C.join(".")}`;
277
+ h = u.signalDomElements.get(E);
278
278
  }
279
279
  }
280
- if (E) {
281
- const V = _ ? U(m, e.slice(0, -1)) : U(m, e);
282
- E.forEach(({ parentId: N, position: A, effect: b }) => {
283
- const J = document.querySelector(
284
- `[data-parent-id="${N}"]`
280
+ if (h) {
281
+ const C = P ? L(m, e.slice(0, -1)) : L(m, e);
282
+ h.forEach(({ parentId: V, position: E, effect: M }) => {
283
+ const Z = document.querySelector(
284
+ `[data-parent-id="${V}"]`
285
285
  );
286
- if (J) {
287
- const G = Array.from(J.childNodes);
288
- if (G[A]) {
289
- const j = b ? new Function("state", `return (${b})(state)`)(V) : V;
290
- G[A].textContent = String(j);
286
+ if (Z) {
287
+ const G = Array.from(Z.childNodes);
288
+ if (G[E]) {
289
+ const U = M ? new Function("state", `return (${M})(state)`)(C) : C;
290
+ G[E].textContent = String(U);
291
291
  }
292
292
  }
293
293
  });
294
294
  }
295
295
  }
296
- a.updateType === "update" && (u || P.current?.validation?.key) && e && K(
297
- (u || P.current?.validation?.key) + "." + e.join(".")
296
+ a.updateType === "update" && (d || R.current?.validation?.key) && e && tt(
297
+ (d || R.current?.validation?.key) + "." + e.join(".")
298
298
  );
299
- const T = e.slice(0, e.length - 1);
300
- a.updateType === "cut" && P.current?.validation?.key && K(
301
- P.current?.validation?.key + "." + T.join(".")
302
- ), a.updateType === "insert" && P.current?.validation?.key && qt(
303
- P.current?.validation?.key + "." + T.join(".")
304
- ).filter((E) => {
305
- let V = E?.split(".").length;
306
- const N = "";
307
- if (E == T.join(".") && V == T.length - 1) {
308
- let A = E + "." + T;
309
- K(E), xt(A, N);
299
+ const b = e.slice(0, e.length - 1);
300
+ a.updateType === "cut" && R.current?.validation?.key && tt(
301
+ R.current?.validation?.key + "." + b.join(".")
302
+ ), a.updateType === "insert" && R.current?.validation?.key && qt(
303
+ R.current?.validation?.key + "." + b.join(".")
304
+ ).filter((h) => {
305
+ let C = h?.split(".").length;
306
+ const V = "";
307
+ if (h == b.join(".") && C == b.length - 1) {
308
+ let E = h + "." + b;
309
+ tt(h), Tt(E, V);
310
310
  }
311
311
  });
312
- const R = d.stateComponents.get(f);
313
- if (R) {
314
- const _ = Pt(l, m), E = new Set(_), V = a.updateType === "update" ? e.join(".") : e.slice(0, -1).join(".") || "";
312
+ const F = u.stateComponents.get(f);
313
+ if (F) {
314
+ const P = Pt(l, m), h = new Set(P), C = a.updateType === "update" ? e.join(".") : e.slice(0, -1).join(".") || "";
315
315
  for (const [
316
- N,
317
- A
318
- ] of R.components.entries()) {
319
- let b = !1;
320
- const J = Array.isArray(A.reactiveType) ? A.reactiveType : [A.reactiveType || "component"];
321
- if (!J.includes("none")) {
322
- if (J.includes("all")) {
323
- A.forceUpdate();
316
+ V,
317
+ E
318
+ ] of F.components.entries()) {
319
+ let M = !1;
320
+ const Z = Array.isArray(E.reactiveType) ? E.reactiveType : [E.reactiveType || "component"];
321
+ if (!Z.includes("none")) {
322
+ if (Z.includes("all")) {
323
+ E.forceUpdate();
324
324
  continue;
325
325
  }
326
- if (J.includes("component") && ((A.paths.has(V) || A.paths.has("")) && (b = !0), !b))
327
- for (const G of E) {
328
- let j = G;
326
+ if (Z.includes("component") && ((E.paths.has(C) || E.paths.has("")) && (M = !0), !M))
327
+ for (const G of h) {
328
+ let U = G;
329
329
  for (; ; ) {
330
- if (A.paths.has(j)) {
331
- b = !0;
330
+ if (E.paths.has(U)) {
331
+ M = !0;
332
332
  break;
333
333
  }
334
- const p = j.lastIndexOf(".");
335
- if (p !== -1) {
336
- const O = j.substring(
334
+ const k = U.lastIndexOf(".");
335
+ if (k !== -1) {
336
+ const N = U.substring(
337
337
  0,
338
- p
338
+ k
339
339
  );
340
340
  if (!isNaN(
341
- Number(j.substring(p + 1))
342
- ) && A.paths.has(O)) {
343
- b = !0;
341
+ Number(U.substring(k + 1))
342
+ ) && E.paths.has(N)) {
343
+ M = !0;
344
344
  break;
345
345
  }
346
- j = O;
346
+ U = N;
347
347
  } else
348
- j = "";
349
- if (j === "")
348
+ U = "";
349
+ if (U === "")
350
350
  break;
351
351
  }
352
- if (b) break;
352
+ if (M) break;
353
353
  }
354
- if (!b && J.includes("deps") && A.depsFunction) {
355
- const G = A.depsFunction(m);
356
- let j = !1;
357
- typeof G == "boolean" ? G && (j = !0) : Q(A.deps, G) || (A.deps = G, j = !0), j && (b = !0);
354
+ if (!M && Z.includes("deps") && E.depsFunction) {
355
+ const G = E.depsFunction(m);
356
+ let U = !1;
357
+ typeof G == "boolean" ? G && (U = !0) : X(E.deps, G) || (E.deps = G, U = !0), U && (M = !0);
358
358
  }
359
- b && A.forceUpdate();
359
+ M && E.forceUpdate();
360
360
  }
361
361
  }
362
362
  }
363
- const z = Date.now();
364
- let { oldValue: D, newValue: H } = Qt(
363
+ const W = Date.now();
364
+ let { oldValue: D, newValue: B } = Qt(
365
365
  a.updateType,
366
366
  l,
367
367
  m,
368
368
  e
369
369
  );
370
- const it = {
371
- timeStamp: z,
370
+ const ct = {
371
+ timeStamp: W,
372
372
  stateKey: f,
373
373
  path: e,
374
374
  updateType: a.updateType,
375
375
  status: "new",
376
376
  oldValue: D,
377
- newValue: H
377
+ newValue: B
378
378
  };
379
379
  switch (a.updateType) {
380
380
  case "insert": {
381
- const _ = e.slice(0, -1), V = e[e.length - 1].split(":")[1];
382
- H = U(m, _).find((A) => A.id == V), D = null, d.insertShadowArrayElement(f, _, H);
381
+ const P = e.slice(0, -1), C = e[e.length - 1].split(":")[1];
382
+ B = L(m, P).find((E) => E.id == C), D = null, u.insertShadowArrayElement(f, P, B);
383
383
  break;
384
384
  }
385
385
  case "cut": {
386
- D = U(l, e), H = null, d.removeShadowArrayElement(f, e);
386
+ D = L(l, e), B = null, u.removeShadowArrayElement(f, e);
387
387
  break;
388
388
  }
389
389
  case "update": {
390
- D = U(l, e), H = U(m, e);
391
- const _ = e.map((E, V) => {
392
- const N = e.slice(0, V + 1), A = U(m, N);
393
- return A?.id ? `id:${A.id}` : E;
390
+ D = L(l, e), B = L(m, e);
391
+ const P = e.map((h, C) => {
392
+ const V = e.slice(0, C + 1), E = L(m, V);
393
+ return E?.id ? `id:${E.id}` : h;
394
394
  });
395
- d.updateShadowAtPath(f, _, H);
395
+ u.updateShadowAtPath(f, P, B);
396
396
  break;
397
397
  }
398
398
  }
399
- if (Jt(f, (_) => {
400
- const E = [..._ ?? [], it], V = /* @__PURE__ */ new Map();
401
- return E.forEach((N) => {
402
- const A = `${N.stateKey}:${JSON.stringify(N.path)}`, b = V.get(A);
403
- b ? (b.timeStamp = Math.max(b.timeStamp, N.timeStamp), b.newValue = N.newValue, b.oldValue = b.oldValue ?? N.oldValue, b.updateType = N.updateType) : V.set(A, { ...N });
404
- }), Array.from(V.values());
405
- }), pt(
399
+ if (Jt(f, (P) => {
400
+ const h = [...P ?? [], ct], C = /* @__PURE__ */ new Map();
401
+ return h.forEach((V) => {
402
+ const E = `${V.stateKey}:${JSON.stringify(V.path)}`, M = C.get(E);
403
+ M ? (M.timeStamp = Math.max(M.timeStamp, V.timeStamp), M.newValue = V.newValue, M.oldValue = M.oldValue ?? V.oldValue, M.updateType = V.updateType) : C.set(E, { ...V });
404
+ }), Array.from(C.values());
405
+ }), Ct(
406
406
  m,
407
407
  f,
408
- P.current,
409
- C
410
- ), P.current?.middleware && P.current.middleware({
411
- updateLog: W,
412
- update: it
413
- }), P.current?.serverSync) {
414
- const _ = d.serverState[f], E = P.current?.serverSync;
408
+ R.current,
409
+ p
410
+ ), R.current?.middleware && R.current.middleware({
411
+ updateLog: z,
412
+ update: ct
413
+ }), R.current?.serverSync) {
414
+ const P = u.serverState[f], h = R.current?.serverSync;
415
415
  Zt(f, {
416
- syncKey: typeof E.syncKey == "string" ? E.syncKey : E.syncKey({ state: m }),
417
- rollBackState: _,
418
- actionTimeStamp: Date.now() + (E.debounce ?? 3e3),
416
+ syncKey: typeof h.syncKey == "string" ? h.syncKey : h.syncKey({ state: m }),
417
+ rollBackState: P,
418
+ actionTimeStamp: Date.now() + (h.debounce ?? 3e3),
419
419
  status: "waiting"
420
420
  });
421
421
  }
422
422
  return m;
423
423
  });
424
424
  };
425
- o.getState().updaterState[f] || (St(
425
+ o.getState().updaterState[f] || (mt(
426
426
  f,
427
- mt(
427
+ It(
428
428
  f,
429
- at,
430
- et.current,
431
- C
429
+ it,
430
+ nt.current,
431
+ p
432
432
  )
433
- ), o.getState().cogsStateStore[f] || ot(f, t), o.getState().initialStateGlobal[f] || Tt(f, t));
434
- const yt = At(() => mt(
433
+ ), o.getState().cogsStateStore[f] || st(f, t), o.getState().initialStateGlobal[f] || xt(f, t));
434
+ const vt = Et(() => It(
435
435
  f,
436
- at,
437
- et.current,
438
- C
439
- ), [f, C]);
440
- return [Ot(f), yt];
436
+ it,
437
+ nt.current,
438
+ p
439
+ ), [f, p]);
440
+ return [Ot(f), vt];
441
441
  }
442
- function mt(t, i, S, g) {
442
+ function It(t, s, S, g) {
443
443
  const I = /* @__PURE__ */ new Map();
444
- let k = 0;
445
- const w = (y) => {
446
- const r = y.join(".");
447
- for (const [h] of I)
448
- (h === r || h.startsWith(r + ".")) && I.delete(h);
449
- k++;
444
+ let T = 0;
445
+ const A = (y) => {
446
+ const n = y.join(".");
447
+ for (const [w] of I)
448
+ (w === n || w.startsWith(n + ".")) && I.delete(w);
449
+ T++;
450
450
  }, v = {
451
451
  removeValidation: (y) => {
452
- y?.validationKey && K(y.validationKey);
452
+ y?.validationKey && tt(y.validationKey);
453
453
  },
454
454
  revertToInitialState: (y) => {
455
- const r = o.getState().getInitialOptions(t)?.validation;
456
- r?.key && K(r?.key), y?.validationKey && K(y.validationKey);
457
- const h = o.getState().initialStateGlobal[t];
458
- o.getState().initializeShadowState(t, h), o.getState().clearSelectedIndexesForState(t), I.clear(), k++;
459
- const M = $(h, []), C = ct(t), X = tt(C?.localStorage?.key) ? C?.localStorage?.key(h) : C?.localStorage?.key, f = `${g}-${t}-${X}`;
460
- f && localStorage.removeItem(f), St(t, M), ot(t, h);
461
- const W = o.getState().stateComponents.get(t);
462
- return W && W.components.forEach((c) => {
455
+ const n = o.getState().getInitialOptions(t)?.validation;
456
+ n?.key && tt(n?.key), y?.validationKey && tt(y.validationKey);
457
+ const w = o.getState().initialStateGlobal[t];
458
+ o.getState().initializeShadowState(t, w), o.getState().clearSelectedIndexesForState(t), I.clear(), T++;
459
+ const O = $(w, []), p = lt(t), K = et(p?.localStorage?.key) ? p?.localStorage?.key(w) : p?.localStorage?.key, f = `${g}-${t}-${K}`;
460
+ f && localStorage.removeItem(f), mt(t, O), st(t, w);
461
+ const z = o.getState().stateComponents.get(t);
462
+ return z && z.components.forEach((c) => {
463
463
  c.forceUpdate();
464
- }), h;
464
+ }), w;
465
465
  },
466
466
  updateInitialState: (y) => {
467
- I.clear(), k++;
468
- const r = mt(
467
+ I.clear(), T++;
468
+ const n = It(
469
469
  t,
470
- i,
470
+ s,
471
471
  S,
472
472
  g
473
- ), h = o.getState().initialStateGlobal[t], M = ct(t), C = tt(M?.localStorage?.key) ? M?.localStorage?.key(h) : M?.localStorage?.key, X = `${g}-${t}-${C}`;
474
- return localStorage.getItem(X) && localStorage.removeItem(X), Ut(() => {
475
- Tt(t, y), o.getState().initializeShadowState(t, y), St(t, r), ot(t, y);
473
+ ), w = o.getState().initialStateGlobal[t], O = lt(t), p = et(O?.localStorage?.key) ? O?.localStorage?.key(w) : O?.localStorage?.key, K = `${g}-${t}-${p}`;
474
+ return localStorage.getItem(K) && localStorage.removeItem(K), Ut(() => {
475
+ xt(t, y), o.getState().initializeShadowState(t, y), mt(t, n), st(t, y);
476
476
  const f = o.getState().stateComponents.get(t);
477
- f && f.components.forEach((W) => {
478
- W.forceUpdate();
477
+ f && f.components.forEach((z) => {
478
+ z.forceUpdate();
479
479
  });
480
480
  }), {
481
- fetchId: (f) => r.get()[f]
481
+ fetchId: (f) => n.get()[f]
482
482
  };
483
483
  },
484
484
  _initialState: o.getState().initialStateGlobal[t],
@@ -486,28 +486,28 @@ function mt(t, i, S, g) {
486
486
  _isLoading: o.getState().isLoadingGlobal[t],
487
487
  _isServerSynced: () => {
488
488
  const y = o.getState().serverState[t];
489
- return !!(y && Q(y, Ot(t)));
489
+ return !!(y && X(y, Ot(t)));
490
490
  }
491
491
  };
492
- function s(y) {
493
- const r = [t, ...y].join(".");
494
- return o.getState().shadowStateStore.get(r)?.arrayKeys || null;
492
+ function i(y) {
493
+ const n = [t, ...y].join(".");
494
+ return o.getState().shadowStateStore.get(n)?.arrayKeys || null;
495
495
  }
496
- function $(y, r = [], h) {
497
- const M = r.map(String).join(".");
498
- I.get(M);
499
- const C = function() {
500
- return o().getNestedState(t, r);
496
+ function $(y, n = [], w) {
497
+ const O = n.map(String).join(".");
498
+ I.get(O);
499
+ const p = function() {
500
+ return o().getNestedState(t, n);
501
501
  };
502
- Object.keys(v).forEach((W) => {
503
- C[W] = v[W];
502
+ Object.keys(v).forEach((z) => {
503
+ p[z] = v[z];
504
504
  });
505
- const X = {
506
- apply(W, c, et) {
507
- return o().getNestedState(t, r);
505
+ const K = {
506
+ apply(z, c, nt) {
507
+ return o().getNestedState(t, n);
508
508
  },
509
- get(W, c) {
510
- const et = /* @__PURE__ */ new Set([
509
+ get(z, c) {
510
+ const nt = /* @__PURE__ */ new Set([
511
511
  "insert",
512
512
  "cut",
513
513
  "cutByValue",
@@ -530,19 +530,19 @@ function mt(t, i, S, g) {
530
530
  "_stateKey",
531
531
  "getComponents"
532
532
  ]);
533
- if (c !== "then" && !c.startsWith("$") && c !== "stateMapNoRender" && !et.has(c)) {
534
- const n = `${t}////${S}`, e = o.getState().stateComponents.get(t);
533
+ if (c !== "then" && !c.startsWith("$") && c !== "stateMapNoRender" && !nt.has(c)) {
534
+ const r = `${t}////${S}`, e = o.getState().stateComponents.get(t);
535
535
  if (e) {
536
- const a = e.components.get(n);
536
+ const a = e.components.get(r);
537
537
  if (a && !a.paths.has("")) {
538
- const u = r.join(".");
539
- let d = !0;
538
+ const d = n.join(".");
539
+ let u = !0;
540
540
  for (const l of a.paths)
541
- if (u.startsWith(l) && (u === l || u[l.length] === ".")) {
542
- d = !1;
541
+ if (d.startsWith(l) && (d === l || d[l.length] === ".")) {
542
+ u = !1;
543
543
  break;
544
544
  }
545
- d && a.paths.add(u);
545
+ u && a.paths.add(d);
546
546
  }
547
547
  }
548
548
  }
@@ -551,479 +551,515 @@ function mt(t, i, S, g) {
551
551
  o.getState().cogsStateStore[t],
552
552
  o.getState().initialStateGlobal[t]
553
553
  );
554
- if (c === "sync" && r.length === 0)
554
+ if (c === "sync" && n.length === 0)
555
555
  return async function() {
556
- const n = o.getState().getInitialOptions(t), e = n?.sync;
556
+ const r = o.getState().getInitialOptions(t), e = r?.sync;
557
557
  if (!e)
558
558
  return console.error(`No mutation defined for state key "${t}"`), { success: !1, error: "No mutation defined" };
559
- const a = o.getState().getNestedState(t, []), u = n?.validation?.key;
559
+ const a = o.getState().getNestedState(t, []), d = r?.validation?.key;
560
560
  try {
561
- const d = await e.action(a);
562
- return d && !d.success && d.errors && u && (o.getState().removeValidationError(u), d.errors.forEach((l) => {
563
- const m = [u, ...l.path].join(".");
561
+ const u = await e.action(a);
562
+ return u && !u.success && u.errors && d && (o.getState().removeValidationError(d), u.errors.forEach((l) => {
563
+ const m = [d, ...l.path].join(".");
564
564
  o.getState().addValidationError(m, l.message);
565
- }), nt(t)), d?.success && e.onSuccess ? e.onSuccess(d.data) : !d?.success && e.onError && e.onError(d.error), d;
566
- } catch (d) {
567
- return e.onError && e.onError(d), { success: !1, error: d };
565
+ }), ot(t)), u?.success && e.onSuccess ? e.onSuccess(u.data) : !u?.success && e.onError && e.onError(u.error), u;
566
+ } catch (u) {
567
+ return e.onError && e.onError(u), { success: !1, error: u };
568
568
  }
569
569
  };
570
570
  if (c === "_status") {
571
- const n = o.getState().getNestedState(t, r), e = o.getState().initialStateGlobal[t], a = U(e, r);
572
- return Q(n, a) ? "fresh" : "stale";
571
+ const r = o.getState().getNestedState(t, n), e = o.getState().initialStateGlobal[t], a = L(e, n);
572
+ return X(r, a) ? "fresh" : "stale";
573
573
  }
574
574
  if (c === "getStatus")
575
575
  return function() {
576
- const n = o().getNestedState(
576
+ const r = o().getNestedState(
577
577
  t,
578
- r
579
- ), e = o.getState().initialStateGlobal[t], a = U(e, r);
580
- return Q(n, a) ? "fresh" : "stale";
578
+ n
579
+ ), e = o.getState().initialStateGlobal[t], a = L(e, n);
580
+ return X(r, a) ? "fresh" : "stale";
581
581
  };
582
582
  if (c === "removeStorage")
583
583
  return () => {
584
- const n = o.getState().initialStateGlobal[t], e = ct(t), a = tt(e?.localStorage?.key) ? e.localStorage.key(n) : e?.localStorage?.key, u = `${g}-${t}-${a}`;
585
- u && localStorage.removeItem(u);
584
+ const r = o.getState().initialStateGlobal[t], e = lt(t), a = et(e?.localStorage?.key) ? e.localStorage.key(r) : e?.localStorage?.key, d = `${g}-${t}-${a}`;
585
+ d && localStorage.removeItem(d);
586
586
  };
587
587
  if (c === "showValidationErrors")
588
588
  return () => {
589
- const n = o.getState().getInitialOptions(t)?.validation;
590
- if (!n?.key) throw new Error("Validation key not found");
591
- return o.getState().getValidationErrors(n.key + "." + r.join("."));
589
+ const r = o.getState().getInitialOptions(t)?.validation;
590
+ if (!r?.key) throw new Error("Validation key not found");
591
+ return o.getState().getValidationErrors(r.key + "." + n.join("."));
592
592
  };
593
593
  if (Array.isArray(y)) {
594
594
  if (c === "getSelected")
595
595
  return () => {
596
- const n = o.getState().getSelectedIndex(t, r.join("."));
597
- if (n === void 0) return;
598
- const e = o.getState().getNestedState(t, r);
599
- if (!e || n >= e.length)
596
+ const r = o.getState().getSelectedIndex(t, n.join("."));
597
+ if (r === void 0) return;
598
+ const e = o.getState().getNestedState(t, n);
599
+ if (!e || r >= e.length)
600
600
  return;
601
- const a = e[n], u = `id:${a.id}`;
602
- return $(a, [...r, u], h);
601
+ const a = e[r], d = `id:${a.id}`;
602
+ return $(a, [...n, d], w);
603
603
  };
604
604
  if (c === "clearSelected")
605
605
  return () => {
606
- o.getState().clearSelectedIndex({ stateKey: t, path: r });
606
+ o.getState().clearSelectedIndex({ stateKey: t, path: n });
607
607
  };
608
608
  if (c === "getSelectedIndex")
609
609
  return () => {
610
- const n = o.getState().getSelectedIndex(t, r.join("."));
611
- if (n === void 0) return -1;
612
- if (h?.validIds) {
613
- const a = (s(r) || [])[n];
614
- return a ? h.validIds.indexOf(a) : -1;
610
+ const r = o.getState().getSelectedIndex(t, n.join("."));
611
+ if (r === void 0) return -1;
612
+ if (w?.validIds) {
613
+ const a = (i(n) || [])[r];
614
+ return a ? w.validIds.indexOf(a) : -1;
615
615
  }
616
- return n;
616
+ return r;
617
617
  };
618
618
  if (c === "useVirtualView")
619
- return (n) => {
619
+ return (r) => {
620
620
  const {
621
621
  itemHeight: e = 50,
622
622
  overscan: a = 6,
623
- stickToBottom: u = !1,
624
- dependencies: d = []
625
- } = n, l = B(null), [m, x] = lt({
623
+ stickToBottom: d = !1,
624
+ dependencies: u = []
625
+ } = r, l = q(null), [m, x] = dt({
626
626
  startIndex: 0,
627
627
  endIndex: 10
628
- }), [T, R] = lt(0), z = B(!0), D = B(!1), H = B(0), it = B(m);
629
- rt(() => o.getState().subscribeToShadowState(t, () => {
630
- R((O) => O + 1);
628
+ }), [b, F] = dt(0), W = q(!0), D = q(!1), B = q(0), ct = q(m);
629
+ i(n), at(() => o.getState().subscribeToShadowState(t, () => {
630
+ F((N) => N + 1);
631
631
  }), [t]);
632
- const _ = o().getNestedState(
632
+ const P = o().getNestedState(
633
633
  t,
634
- r
635
- ), E = _.length, V = s(r), { totalHeight: N, positions: A } = At(() => {
636
- let p = 0;
637
- const O = [];
638
- for (let F = 0; F < E; F++) {
639
- O[F] = p;
640
- const Z = V?.[F];
641
- let q = e;
642
- if (Z) {
643
- const L = [...r, Z];
644
- q = o.getState().getShadowMetadata(t, L)?.virtualizer?.itemHeight || e;
645
- }
646
- p += q;
634
+ n
635
+ ), h = P.length, { totalHeight: C, positions: V } = Et(() => {
636
+ const N = o.getState().getShadowMetadata(t, n)?.arrayKeys || [];
637
+ let _ = 0;
638
+ const H = [];
639
+ for (let j = 0; j < h; j++) {
640
+ H[j] = _;
641
+ const Y = N[j];
642
+ let J = e;
643
+ Y && (J = o.getState().getShadowMetadata(t, [...n, Y])?.virtualizer?.itemHeight || e), _ += J;
647
644
  }
648
- return { totalHeight: p, positions: O };
645
+ return { totalHeight: _, positions: H };
649
646
  }, [
650
- E,
647
+ h,
651
648
  t,
652
- r.join("."),
649
+ n.join("."),
653
650
  e,
654
- T,
655
- V
656
- // Add `orderedIds` to the dependency array
657
- ]), b = At(() => {
658
- const p = Math.max(0, m.startIndex), O = Math.min(E, m.endIndex), F = V?.slice(p, O), Z = new Map(
659
- _.map((L) => [`id:${L.id}`, L])
660
- ), q = F?.map((L) => Z.get(L)).filter(Boolean) || [];
661
- return $(q, r, {
662
- ...h,
663
- validIds: F
664
- // Pass the sliced IDs as the new `validIds`
651
+ b
652
+ ]), E = Et(() => {
653
+ const k = Math.max(0, m.startIndex), N = Math.min(h, m.endIndex), H = o.getState().getShadowMetadata(t, n)?.arrayKeys || [], j = P.slice(k, N), Y = H.slice(k, N);
654
+ return $(j, n, {
655
+ ...w,
656
+ validIds: Y
665
657
  });
666
- }, [
667
- m.startIndex,
668
- m.endIndex,
669
- _,
670
- E,
671
- V
672
- ]);
673
- rt(() => {
674
- if (!u || E === 0) return;
675
- E > H.current && z.current && !D.current && setTimeout(() => G(E - 1, "smooth"), 50), H.current = E;
676
- }, [E, u]), rt(() => {
677
- const p = l.current;
678
- if (!p) return;
679
- const O = () => {
680
- const { scrollTop: F, scrollHeight: Z, clientHeight: q } = p, L = Z - F - q;
681
- z.current = L < 5, L > 100 && (D.current = !0), L < 5 && (D.current = !1);
682
- let st = 0;
683
- for (let Y = 0; Y < A.length; Y++)
684
- if (A[Y] > F - e * a) {
685
- st = Math.max(0, Y - 1);
658
+ }, [m.startIndex, m.endIndex, P, h]), M = St(() => {
659
+ const k = o.getState().getShadowMetadata(t, n);
660
+ if (!k || k && k?.arrayKeys?.length === 0)
661
+ return !1;
662
+ const N = h - 1, _ = [...n, k.arrayKeys[N]];
663
+ if (N >= 0) {
664
+ const H = o.getState().getShadowMetadata(t, _);
665
+ if (H?.virtualizer?.domRef) {
666
+ const j = H.virtualizer.domRef;
667
+ if (j && j.scrollIntoView)
668
+ return j.scrollIntoView({
669
+ behavior: "auto",
670
+ block: "end",
671
+ inline: "nearest"
672
+ }), !0;
673
+ }
674
+ }
675
+ return !1;
676
+ }, [t, n, h]);
677
+ at(() => {
678
+ if (!d || h === 0) return;
679
+ const k = h > B.current, N = B.current === 0 && h > 0;
680
+ if ((k || N) && W.current && !D.current) {
681
+ const _ = Math.ceil(
682
+ (l.current?.clientHeight || 0) / e
683
+ ), H = {
684
+ startIndex: Math.max(
685
+ 0,
686
+ h - _ - a
687
+ ),
688
+ endIndex: h
689
+ };
690
+ x(H);
691
+ const j = setTimeout(() => {
692
+ G(h - 1, "smooth");
693
+ }, 50);
694
+ return () => clearTimeout(j);
695
+ }
696
+ B.current = h;
697
+ }, [h, e, a]), at(() => {
698
+ const k = l.current;
699
+ if (!k) return;
700
+ const N = () => {
701
+ const { scrollTop: _, scrollHeight: H, clientHeight: j } = k, Y = H - _ - j;
702
+ W.current = Y < 5, Y > 100 && (D.current = !0), Y < 5 && (D.current = !1);
703
+ let J = 0;
704
+ for (let Q = 0; Q < V.length; Q++)
705
+ if (V[Q] > _ - e * a) {
706
+ J = Math.max(0, Q - 1);
686
707
  break;
687
708
  }
688
- let dt = st;
689
- const jt = F + q;
690
- for (let Y = st; Y < A.length && !(A[Y] > jt + e * a); Y++)
691
- dt = Y;
692
- const vt = Math.max(0, st), ht = Math.min(
693
- E,
694
- dt + 1 + a
709
+ let rt = J;
710
+ const Vt = _ + j;
711
+ for (let Q = J; Q < V.length && !(V[Q] > Vt + e * a); Q++)
712
+ rt = Q;
713
+ const ht = Math.max(0, J), wt = Math.min(
714
+ h,
715
+ rt + 1 + a
695
716
  );
696
- (vt !== it.current.startIndex || ht !== it.current.endIndex) && (it.current = {
697
- startIndex: vt,
698
- endIndex: ht
717
+ (ht !== ct.current.startIndex || wt !== ct.current.endIndex) && (ct.current = {
718
+ startIndex: ht,
719
+ endIndex: wt
699
720
  }, x({
700
- startIndex: vt,
701
- endIndex: ht
721
+ startIndex: ht,
722
+ endIndex: wt
702
723
  }));
703
724
  };
704
- return p.addEventListener("scroll", O, {
725
+ if (k.addEventListener("scroll", N, {
705
726
  passive: !0
706
- }), O(), () => p.removeEventListener("scroll", O);
707
- }, [A, E, e, a]);
708
- const J = Et(() => {
709
- z.current = !0, D.current = !1, l.current && (l.current.scrollTop = l.current.scrollHeight);
710
- }, []), G = Et(
711
- (p, O = "smooth") => {
712
- const F = l.current;
713
- if (!F) return;
714
- const Z = V?.[p];
715
- if (Z) {
716
- const L = [...r, Z], dt = o.getState().getShadowMetadata(t, L)?.virtualizer?.domRef;
717
- if (dt?.scrollIntoView) {
718
- dt.scrollIntoView({ behavior: O, block: "nearest" });
719
- return;
720
- }
727
+ }), d && h > 0 && !D.current) {
728
+ const { scrollTop: _ } = k;
729
+ _ === 0 && (k.scrollTop = k.scrollHeight, W.current = !0);
730
+ }
731
+ return N(), () => {
732
+ k.removeEventListener("scroll", N);
733
+ };
734
+ }, [V, h, e, a, d]);
735
+ const Z = St(() => {
736
+ W.current = !0, D.current = !1, !M() && l.current && (l.current.scrollTop = l.current.scrollHeight);
737
+ }, [M]), G = St(
738
+ (k, N = "smooth") => {
739
+ const _ = l.current;
740
+ if (!_) return;
741
+ if (k === h - 1) {
742
+ _.scrollTo({
743
+ top: _.scrollHeight,
744
+ behavior: N
745
+ });
746
+ return;
721
747
  }
722
- const q = A[p];
723
- q !== void 0 && F.scrollTo({ top: q, behavior: O });
748
+ const J = (o.getState().getShadowMetadata(t, n)?.arrayKeys || [])[k];
749
+ let rt;
750
+ J && (rt = o.getState().getShadowMetadata(t, [...n, J])?.virtualizer?.domRef), rt ? rt.scrollIntoView({
751
+ behavior: N,
752
+ block: "center"
753
+ }) : V[k] !== void 0 && _.scrollTo({
754
+ top: V[k],
755
+ behavior: N
756
+ });
724
757
  },
725
- [A, t, r, V]
726
- // Add `orderedIds` to dependency array
727
- ), j = {
758
+ [V, t, n, h]
759
+ // Add totalCount to the dependencies
760
+ ), U = {
728
761
  outer: {
729
762
  ref: l,
730
763
  style: { overflowY: "auto", height: "100%" }
731
764
  },
732
765
  inner: {
733
- style: { height: `${N}px`, position: "relative" }
766
+ style: {
767
+ height: `${C}px`,
768
+ position: "relative"
769
+ }
734
770
  },
735
771
  list: {
736
772
  style: {
737
- transform: `translateY(${A[m.startIndex] || 0}px)`
773
+ transform: `translateY(${V[m.startIndex] || 0}px)`
738
774
  }
739
775
  }
740
776
  };
741
777
  return {
742
- virtualState: b,
743
- virtualizerProps: j,
744
- scrollToBottom: J,
778
+ virtualState: E,
779
+ virtualizerProps: U,
780
+ scrollToBottom: Z,
745
781
  scrollToIndex: G
746
782
  };
747
783
  };
748
784
  if (c === "stateMap")
749
- return (n) => {
750
- const e = y, a = h?.validIds || s(r) || [], u = $(y, r, h);
751
- return e.map((d, l) => {
752
- const m = a[l] || `id:${d.id}`, x = [...r, m], T = $(d, x, h);
753
- return n(
754
- d,
755
- T,
785
+ return (r) => {
786
+ const e = y, a = w?.validIds || i(n) || [], d = $(y, n, w);
787
+ return e.map((u, l) => {
788
+ const m = a[l] || `id:${u.id}`, x = [...n, m], b = $(u, x, w);
789
+ return r(
790
+ u,
791
+ b,
756
792
  l,
757
793
  y,
758
- u
794
+ d
759
795
  );
760
796
  });
761
797
  };
762
798
  if (c === "stateMapNoRender")
763
- return (n) => {
764
- const e = y, a = h?.validIds || s(r) || [], u = $(y, r, h);
765
- return e.map((d, l) => {
766
- const m = a[l] || `id:${d.id}`, x = [...r, m], T = $(d, x, h);
767
- return n(
768
- d,
769
- T,
799
+ return (r) => {
800
+ const e = y, a = w?.validIds || i(n) || [], d = $(y, n, w);
801
+ return e.map((u, l) => {
802
+ const m = a[l] || `id:${u.id}`, x = [...n, m], b = $(u, x, w);
803
+ return r(
804
+ u,
805
+ b,
770
806
  l,
771
807
  y,
772
- u
808
+ d
773
809
  );
774
810
  });
775
811
  };
776
812
  if (c === "$stateMap")
777
- return (n) => gt(Kt, {
778
- proxy: { _stateKey: t, _path: r, _mapFn: n },
813
+ return (r) => gt(Kt, {
814
+ proxy: { _stateKey: t, _path: n, _mapFn: r },
779
815
  rebuildStateShape: $
780
816
  });
781
817
  if (c === "stateList")
782
- return (n) => {
818
+ return (r) => {
783
819
  const e = y;
784
820
  if (!Array.isArray(e)) return null;
785
- const a = h?.validIds || s(r) || [], u = s(r) || [], d = $(
821
+ const a = w?.validIds || i(n) || [], d = i(n) || [], u = $(
786
822
  e,
787
- r,
788
- h
823
+ n,
824
+ w
789
825
  );
790
826
  return e.map((l, m) => {
791
- const x = a[m] || `id:${l.id}`, T = u.indexOf(x), R = [...r, x], z = $(l, R, h), D = `${S}-${r.join(".")}-${x}`;
827
+ const x = a[m] || `id:${l.id}`, b = d.indexOf(x), F = [...n, x], W = $(l, F, w), D = `${S}-${n.join(".")}-${x}`;
792
828
  return gt(ee, {
793
829
  key: x,
794
830
  stateKey: t,
795
831
  itemComponentId: D,
796
- itemPath: R,
797
- children: n(
832
+ itemPath: F,
833
+ children: r(
798
834
  l,
799
- z,
800
- { localIndex: m, originalIndex: T },
835
+ W,
836
+ { localIndex: m, originalIndex: b },
801
837
  e,
802
- d
838
+ u
803
839
  )
804
840
  });
805
841
  });
806
842
  };
807
843
  if (c === "stateFlattenOn")
808
- return (n) => {
844
+ return (r) => {
809
845
  const e = y;
810
- I.clear(), k++;
846
+ I.clear(), T++;
811
847
  const a = e.flatMap(
812
- (u) => u[n] ?? []
848
+ (d) => d[r] ?? []
813
849
  );
814
850
  return $(
815
851
  a,
816
- [...r, "[*]", n],
817
- h
852
+ [...n, "[*]", r],
853
+ w
818
854
  );
819
855
  };
820
856
  if (c === "index")
821
- return (n) => {
822
- const a = (h?.validIds || s(r))?.[n];
857
+ return (r) => {
858
+ const a = (w?.validIds || i(n))?.[r];
823
859
  if (!a)
824
860
  return $(void 0, [
825
- ...r,
826
- n.toString()
861
+ ...n,
862
+ r.toString()
827
863
  ]);
828
- const d = o.getState().getNestedState(t, r).find(
864
+ const u = o.getState().getNestedState(t, n).find(
829
865
  (m) => `id:${m.id}` === a
830
- ), l = [...r, a];
831
- return $(d, l, h);
866
+ ), l = [...n, a];
867
+ return $(u, l, w);
832
868
  };
833
869
  if (c === "last")
834
870
  return () => {
835
- const n = o.getState().getNestedState(t, r);
836
- if (n.length === 0) return;
837
- const e = n.length - 1, a = n[e], u = [...r, e.toString()];
838
- return $(a, u);
871
+ const r = o.getState().getNestedState(t, n);
872
+ if (r.length === 0) return;
873
+ const e = r.length - 1, a = r[e], d = [...n, e.toString()];
874
+ return $(a, d);
839
875
  };
840
876
  if (c === "insert")
841
- return (n) => (w(r), wt(i, n, r, t), $(
842
- o.getState().getNestedState(t, r),
843
- r
877
+ return (r) => (A(n), At(s, r, n, t), $(
878
+ o.getState().getNestedState(t, n),
879
+ n
844
880
  ));
845
881
  if (c === "uniqueInsert")
846
- return (n, e, a) => {
847
- const u = o.getState().getNestedState(t, r), d = tt(n) ? n(u) : n;
882
+ return (r, e, a) => {
883
+ const d = o.getState().getNestedState(t, n), u = et(r) ? r(d) : r;
848
884
  let l = null;
849
- if (!u.some((x) => {
850
- const T = e ? e.every(
851
- (R) => Q(x[R], d[R])
852
- ) : Q(x, d);
853
- return T && (l = x), T;
885
+ if (!d.some((x) => {
886
+ const b = e ? e.every(
887
+ (F) => X(x[F], u[F])
888
+ ) : X(x, u);
889
+ return b && (l = x), b;
854
890
  }))
855
- w(r), wt(i, d, r, t);
891
+ A(n), At(s, u, n, t);
856
892
  else if (a && l) {
857
- const x = a(l), T = u.map(
858
- (R) => Q(R, l) ? x : R
893
+ const x = a(l), b = d.map(
894
+ (F) => X(F, l) ? x : F
859
895
  );
860
- w(r), ut(i, T, r);
896
+ A(n), ut(s, b, n);
861
897
  }
862
898
  };
863
899
  if (c === "cut")
864
- return (n, e) => {
900
+ return (r, e) => {
865
901
  if (!e?.waitForSync)
866
- return w(r), ft(i, r, t, n), $(
867
- o.getState().getNestedState(t, r),
868
- r
902
+ return A(n), ft(s, n, t, r), $(
903
+ o.getState().getNestedState(t, n),
904
+ n
869
905
  );
870
906
  };
871
907
  if (c === "cutByValue")
872
- return (n) => {
873
- const e = y.findIndex((a) => a === n);
874
- e > -1 && ft(i, r, t, e);
908
+ return (r) => {
909
+ const e = y.findIndex((a) => a === r);
910
+ e > -1 && ft(s, n, t, e);
875
911
  };
876
912
  if (c === "toggleByValue")
877
- return (n) => {
878
- const e = y.findIndex((a) => a === n);
879
- e > -1 ? ft(i, r, t, e) : wt(i, n, r, t);
913
+ return (r) => {
914
+ const e = y.findIndex((a) => a === r);
915
+ e > -1 ? ft(s, n, t, e) : At(s, r, n, t);
880
916
  };
881
917
  if (c === "stateFilter")
882
- return (n) => {
883
- const e = h?.validIds || s(r) || [], a = o.getState().getNestedState(t, r), u = new Map(
918
+ return (r) => {
919
+ const e = w?.validIds || i(n) || [], a = o.getState().getNestedState(t, n), d = new Map(
884
920
  a.map((m) => [`id:${m.id}`, m])
885
- ), d = [], l = [];
921
+ ), u = [], l = [];
886
922
  return e.forEach((m, x) => {
887
- const T = u.get(m);
888
- T && n(T, x) && (d.push(m), l.push(T));
889
- }), $(l, r, {
890
- validIds: d
923
+ const b = d.get(m);
924
+ b && r(b, x) && (u.push(m), l.push(b));
925
+ }), $(l, n, {
926
+ validIds: u
891
927
  });
892
928
  };
893
929
  if (c === "stateSort")
894
- return (n) => {
930
+ return (r) => {
895
931
  const a = y.map((l) => ({
896
932
  item: l,
897
933
  id: `id:${l.id}`
898
934
  }));
899
- a.sort((l, m) => n(l.item, m.item));
900
- const u = a.map((l) => l.item), d = a.map((l) => l.id);
901
- return $(u, r, {
902
- validIds: d
935
+ a.sort((l, m) => r(l.item, m.item));
936
+ const d = a.map((l) => l.item), u = a.map((l) => l.id);
937
+ return $(d, n, {
938
+ validIds: u
903
939
  });
904
940
  };
905
941
  if (c === "findWith")
906
- return (n, e) => {
942
+ return (r, e) => {
907
943
  const a = y.find(
908
- (l) => l[n] === e
944
+ (l) => l[r] === e
909
945
  );
910
946
  if (!a) return;
911
- const u = `id:${a.id}`, d = [...r, u];
912
- return $(a, d, h);
947
+ const d = `id:${a.id}`, u = [...n, d];
948
+ return $(a, u, w);
913
949
  };
914
950
  }
915
- const P = r[r.length - 1];
916
- if (!isNaN(Number(P))) {
917
- const n = r.slice(0, -1), e = o.getState().getNestedState(t, n);
951
+ const R = n[n.length - 1];
952
+ if (!isNaN(Number(R))) {
953
+ const r = n.slice(0, -1), e = o.getState().getNestedState(t, r);
918
954
  if (Array.isArray(e) && c === "cut")
919
955
  return () => ft(
920
- i,
921
- n,
956
+ s,
957
+ r,
922
958
  t,
923
- Number(P)
959
+ Number(R)
924
960
  );
925
961
  }
926
962
  if (c === "get")
927
963
  return () => {
928
- if (h?.validIds && Array.isArray(y)) {
929
- const n = o.getState().getNestedState(t, r);
930
- if (!Array.isArray(n)) return [];
964
+ if (w?.validIds && Array.isArray(y)) {
965
+ const r = o.getState().getNestedState(t, n);
966
+ if (!Array.isArray(r)) return [];
931
967
  const e = new Map(
932
- n.map((a) => [`id:${a.id}`, a])
968
+ r.map((a) => [`id:${a.id}`, a])
933
969
  );
934
- return h.validIds.map((a) => e.get(a)).filter(Boolean);
970
+ return w.validIds.map((a) => e.get(a)).filter(Boolean);
935
971
  }
936
- return o.getState().getNestedState(t, r);
972
+ return o.getState().getNestedState(t, n);
937
973
  };
938
974
  if (c === "$derive")
939
- return (n) => Ct({
975
+ return (r) => _t({
940
976
  _stateKey: t,
941
- _path: r,
942
- _effect: n.toString()
977
+ _path: n,
978
+ _effect: r.toString()
943
979
  });
944
980
  if (c === "$get")
945
- return () => Ct({ _stateKey: t, _path: r });
981
+ return () => _t({ _stateKey: t, _path: n });
946
982
  if (c === "lastSynced") {
947
- const n = `${t}:${r.join(".")}`;
948
- return o.getState().getSyncInfo(n);
983
+ const r = `${t}:${n.join(".")}`;
984
+ return o.getState().getSyncInfo(r);
949
985
  }
950
986
  if (c == "getLocalStorage")
951
- return (n) => It(g + "-" + t + "-" + n);
987
+ return (r) => yt(g + "-" + t + "-" + r);
952
988
  if (c === "_selected") {
953
- const n = r.slice(0, -1), e = n.join(".");
989
+ const r = n.slice(0, -1), e = r.join(".");
954
990
  if (Array.isArray(
955
- o.getState().getNestedState(t, n)
991
+ o.getState().getNestedState(t, r)
956
992
  )) {
957
- const a = r[r.length - 1];
958
- return s(n)?.indexOf(a) === o.getState().getSelectedIndex(t, e);
993
+ const a = n[n.length - 1];
994
+ return i(r)?.indexOf(a) === o.getState().getSelectedIndex(t, e);
959
995
  }
960
996
  return;
961
997
  }
962
998
  if (c === "setSelected")
963
- return (n) => {
964
- const e = r.slice(0, -1), a = r[r.length - 1], d = s(e)?.indexOf(a);
965
- if (d === void 0 || d === -1) return;
999
+ return (r) => {
1000
+ const e = n.slice(0, -1), a = n[n.length - 1], u = i(e)?.indexOf(a);
1001
+ if (u === void 0 || u === -1) return;
966
1002
  const l = e.join(".");
967
1003
  o.getState().setSelectedIndex(
968
1004
  t,
969
1005
  l,
970
- n ? d : void 0
1006
+ r ? u : void 0
971
1007
  );
972
1008
  const m = o.getState().getNestedState(t, [...e]);
973
- ut(i, m, e), w(e);
1009
+ ut(s, m, e), A(e);
974
1010
  };
975
1011
  if (c === "toggleSelected")
976
1012
  return () => {
977
- const n = r.slice(0, -1), e = r[r.length - 1], u = s(n)?.indexOf(e);
978
- if (u === void 0 || u === -1) return;
979
- const d = n.join("."), l = o.getState().getSelectedIndex(t, d);
1013
+ const r = n.slice(0, -1), e = n[n.length - 1], d = i(r)?.indexOf(e);
1014
+ if (d === void 0 || d === -1) return;
1015
+ const u = r.join("."), l = o.getState().getSelectedIndex(t, u);
980
1016
  o.getState().setSelectedIndex(
981
1017
  t,
982
- d,
983
- l === u ? void 0 : u
1018
+ u,
1019
+ l === d ? void 0 : d
984
1020
  );
985
- const m = o.getState().getNestedState(t, [...n]);
986
- ut(i, m, n), w(n);
1021
+ const m = o.getState().getNestedState(t, [...r]);
1022
+ ut(s, m, r), A(r);
987
1023
  };
988
- if (r.length == 0) {
1024
+ if (n.length == 0) {
989
1025
  if (c === "addValidation")
990
- return (n) => {
1026
+ return (r) => {
991
1027
  const e = o.getState().getInitialOptions(t)?.validation;
992
1028
  if (!e?.key) throw new Error("Validation key not found");
993
- K(e.key), n.forEach((a) => {
994
- const u = [e.key, ...a.path].join(".");
995
- xt(u, a.message);
996
- }), nt(t);
1029
+ tt(e.key), r.forEach((a) => {
1030
+ const d = [e.key, ...a.path].join(".");
1031
+ Tt(d, a.message);
1032
+ }), ot(t);
997
1033
  };
998
1034
  if (c === "applyJsonPatch")
999
- return (n) => {
1000
- const e = o.getState().cogsStateStore[t], a = zt(e, n).newDocument;
1001
- Rt(
1035
+ return (r) => {
1036
+ const e = o.getState().cogsStateStore[t], a = Wt(e, r).newDocument;
1037
+ jt(
1002
1038
  t,
1003
1039
  o.getState().initialStateGlobal[t],
1004
1040
  a,
1005
- i,
1041
+ s,
1006
1042
  S,
1007
1043
  g
1008
- ), nt(t);
1044
+ ), ot(t);
1009
1045
  };
1010
1046
  if (c === "validateZodSchema")
1011
1047
  return () => {
1012
- const n = o.getState().getInitialOptions(t)?.validation;
1013
- if (!n?.zodSchema || !n?.key)
1048
+ const r = o.getState().getInitialOptions(t)?.validation;
1049
+ if (!r?.zodSchema || !r?.key)
1014
1050
  throw new Error("Zod schema or validation key not found");
1015
- K(n.key);
1016
- const e = o.getState().cogsStateStore[t], a = n.zodSchema.safeParse(e);
1017
- return a.success ? !0 : (a.error.errors.forEach((u) => {
1018
- const d = [n.key, ...u.path].join(".");
1019
- xt(d, u.message);
1020
- }), nt(t), !1);
1051
+ tt(r.key);
1052
+ const e = o.getState().cogsStateStore[t], a = r.zodSchema.safeParse(e);
1053
+ return a.success ? !0 : (a.error.errors.forEach((d) => {
1054
+ const u = [r.key, ...d.path].join(".");
1055
+ Tt(u, d.message);
1056
+ }), ot(t), !1);
1021
1057
  };
1022
1058
  if (c === "_componentId") return S;
1023
1059
  if (c === "getComponents")
1024
1060
  return () => o().stateComponents.get(t);
1025
1061
  if (c === "getAllFormRefs")
1026
- return () => Vt.getState().getFormRefsByStateKey(t);
1062
+ return () => bt.getState().getFormRefsByStateKey(t);
1027
1063
  if (c === "_initialState")
1028
1064
  return o.getState().initialStateGlobal[t];
1029
1065
  if (c === "_serverState")
@@ -1036,116 +1072,116 @@ function mt(t, i, S, g) {
1036
1072
  if (c === "removeValidation") return v.removeValidation;
1037
1073
  }
1038
1074
  if (c === "getFormRef")
1039
- return () => Vt.getState().getFormRef(t + "." + r.join("."));
1075
+ return () => bt.getState().getFormRef(t + "." + n.join("."));
1040
1076
  if (c === "validationWrapper")
1041
1077
  return ({
1042
- children: n,
1078
+ children: r,
1043
1079
  hideMessage: e
1044
1080
  }) => /* @__PURE__ */ $t(
1045
- Lt,
1081
+ Gt,
1046
1082
  {
1047
1083
  formOpts: e ? { validation: { message: "" } } : void 0,
1048
- path: r,
1084
+ path: n,
1049
1085
  stateKey: t,
1050
- children: n
1086
+ children: r
1051
1087
  }
1052
1088
  );
1053
1089
  if (c === "_stateKey") return t;
1054
- if (c === "_path") return r;
1090
+ if (c === "_path") return n;
1055
1091
  if (c === "_isServerSynced") return v._isServerSynced;
1056
1092
  if (c === "update")
1057
- return (n, e) => {
1093
+ return (r, e) => {
1058
1094
  if (e?.debounce)
1059
- Gt(() => {
1060
- ut(i, n, r, "");
1061
- const a = o.getState().getNestedState(t, r);
1095
+ Lt(() => {
1096
+ ut(s, r, n, "");
1097
+ const a = o.getState().getNestedState(t, n);
1062
1098
  e?.afterUpdate && e.afterUpdate(a);
1063
1099
  }, e.debounce);
1064
1100
  else {
1065
- ut(i, n, r, "");
1066
- const a = o.getState().getNestedState(t, r);
1101
+ ut(s, r, n, "");
1102
+ const a = o.getState().getNestedState(t, n);
1067
1103
  e?.afterUpdate && e.afterUpdate(a);
1068
1104
  }
1069
- w(r);
1105
+ A(n);
1070
1106
  };
1071
1107
  if (c === "formElement")
1072
- return (n, e) => /* @__PURE__ */ $t(
1073
- Wt,
1108
+ return (r, e) => /* @__PURE__ */ $t(
1109
+ Ht,
1074
1110
  {
1075
- setState: i,
1111
+ setState: s,
1076
1112
  stateKey: t,
1077
- path: r,
1078
- child: n,
1113
+ path: n,
1114
+ child: r,
1079
1115
  formOpts: e
1080
1116
  }
1081
1117
  );
1082
- const at = [...r, c], yt = o.getState().getNestedState(t, at);
1083
- return $(yt, at, h);
1118
+ const it = [...n, c], vt = o.getState().getNestedState(t, it);
1119
+ return $(vt, it, w);
1084
1120
  }
1085
- }, f = new Proxy(C, X);
1086
- return I.set(M, {
1121
+ }, f = new Proxy(p, K);
1122
+ return I.set(O, {
1087
1123
  proxy: f,
1088
- stateVersion: k
1124
+ stateVersion: T
1089
1125
  }), f;
1090
1126
  }
1091
1127
  return $(
1092
1128
  o.getState().getNestedState(t, [])
1093
1129
  );
1094
1130
  }
1095
- function Ct(t) {
1131
+ function _t(t) {
1096
1132
  return gt(te, { proxy: t });
1097
1133
  }
1098
1134
  function Kt({
1099
1135
  proxy: t,
1100
- rebuildStateShape: i
1136
+ rebuildStateShape: s
1101
1137
  }) {
1102
1138
  const S = o().getNestedState(t._stateKey, t._path);
1103
- return Array.isArray(S) ? i(
1139
+ return Array.isArray(S) ? s(
1104
1140
  S,
1105
1141
  t._path
1106
1142
  ).stateMapNoRender(
1107
- (I, k, w, v, s) => t._mapFn(I, k, w, v, s)
1143
+ (I, T, A, v, i) => t._mapFn(I, T, A, v, i)
1108
1144
  ) : null;
1109
1145
  }
1110
1146
  function te({
1111
1147
  proxy: t
1112
1148
  }) {
1113
- const i = B(null), S = `${t._stateKey}-${t._path.join(".")}`;
1114
- return rt(() => {
1115
- const g = i.current;
1149
+ const s = q(null), S = `${t._stateKey}-${t._path.join(".")}`;
1150
+ return at(() => {
1151
+ const g = s.current;
1116
1152
  if (!g || !g.parentElement) return;
1117
- const I = g.parentElement, w = Array.from(I.childNodes).indexOf(g);
1153
+ const I = g.parentElement, A = Array.from(I.childNodes).indexOf(g);
1118
1154
  let v = I.getAttribute("data-parent-id");
1119
1155
  v || (v = `parent-${crypto.randomUUID()}`, I.setAttribute("data-parent-id", v));
1120
1156
  const $ = {
1121
1157
  instanceId: `instance-${crypto.randomUUID()}`,
1122
1158
  parentId: v,
1123
- position: w,
1159
+ position: A,
1124
1160
  effect: t._effect
1125
1161
  };
1126
1162
  o.getState().addSignalElement(S, $);
1127
1163
  const y = o.getState().getNestedState(t._stateKey, t._path);
1128
- let r = y;
1164
+ let n = y;
1129
1165
  if (t._effect)
1130
1166
  try {
1131
- r = new Function(
1167
+ n = new Function(
1132
1168
  "state",
1133
1169
  `return (${t._effect})(state)`
1134
1170
  )(y);
1135
- } catch (M) {
1136
- console.error("Error evaluating effect function:", M);
1171
+ } catch (O) {
1172
+ console.error("Error evaluating effect function:", O);
1137
1173
  }
1138
- r !== null && typeof r == "object" && (r = JSON.stringify(r));
1139
- const h = document.createTextNode(String(r));
1140
- g.replaceWith(h);
1174
+ n !== null && typeof n == "object" && (n = JSON.stringify(n));
1175
+ const w = document.createTextNode(String(n));
1176
+ g.replaceWith(w);
1141
1177
  }, [t._stateKey, t._path.join("."), t._effect]), gt("span", {
1142
- ref: i,
1178
+ ref: s,
1143
1179
  style: { display: "none" },
1144
1180
  "data-signal-id": S
1145
1181
  });
1146
1182
  }
1147
1183
  function ye(t) {
1148
- const i = Ft(
1184
+ const s = Ft(
1149
1185
  (S) => {
1150
1186
  const g = o.getState().stateComponents.get(t._stateKey) || { components: /* @__PURE__ */ new Map() };
1151
1187
  return g.components.set(t._stateKey, {
@@ -1155,37 +1191,37 @@ function ye(t) {
1155
1191
  },
1156
1192
  () => o.getState().getNestedState(t._stateKey, t._path)
1157
1193
  );
1158
- return gt("text", {}, String(i));
1194
+ return gt("text", {}, String(s));
1159
1195
  }
1160
1196
  function ee({
1161
1197
  stateKey: t,
1162
- itemComponentId: i,
1198
+ itemComponentId: s,
1163
1199
  itemPath: S,
1164
1200
  children: g
1165
1201
  }) {
1166
- const [, I] = lt({}), [k, w] = Ht(), v = B(null), s = B(null), $ = Et(
1202
+ const [, I] = dt({}), [T, A] = Bt(), v = q(null), i = q(null), $ = St(
1167
1203
  (y) => {
1168
- k(y), v.current = y;
1204
+ T(y), v.current = y;
1169
1205
  },
1170
- [k]
1206
+ [T]
1171
1207
  );
1172
- return rt(() => {
1173
- w.height > 0 && w.height !== s.current && (s.current = w.height, o.getState().setShadowMetadata(t, S, {
1174
- virtualizer: { itemHeight: w.height, domRef: v.current }
1208
+ return at(() => {
1209
+ A.height > 0 && A.height !== i.current && (i.current = A.height, o.getState().setShadowMetadata(t, S, {
1210
+ virtualizer: { itemHeight: A.height, domRef: v.current }
1175
1211
  }));
1176
- }, [w.height, t, S]), _t(() => {
1177
- const y = `${t}////${i}`, r = o.getState().stateComponents.get(t) || { components: /* @__PURE__ */ new Map() };
1178
- return r.components.set(y, {
1212
+ }, [A.height, t, S]), pt(() => {
1213
+ const y = `${t}////${s}`, n = o.getState().stateComponents.get(t) || { components: /* @__PURE__ */ new Map() };
1214
+ return n.components.set(y, {
1179
1215
  forceUpdate: () => I({}),
1180
1216
  paths: /* @__PURE__ */ new Set([S.join(".")])
1181
- }), o.getState().stateComponents.set(t, r), () => {
1182
- const h = o.getState().stateComponents.get(t);
1183
- h && h.components.delete(y);
1217
+ }), o.getState().stateComponents.set(t, n), () => {
1218
+ const w = o.getState().stateComponents.get(t);
1219
+ w && w.components.delete(y);
1184
1220
  };
1185
- }, [t, i, S.join(".")]), /* @__PURE__ */ $t("div", { ref: $, children: g });
1221
+ }, [t, s, S.join(".")]), /* @__PURE__ */ $t("div", { ref: $, children: g });
1186
1222
  }
1187
1223
  export {
1188
- Ct as $cogsSignal,
1224
+ _t as $cogsSignal,
1189
1225
  ye as $cogsSignalStore,
1190
1226
  Se as addStateOptions,
1191
1227
  me as createCogsState,