cogsbox-state 0.5.319 → 0.5.320

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