cogsbox-state 0.5.256 → 0.5.258

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