cogsbox-state 0.5.288 → 0.5.290

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