cogsbox-state 0.5.279 → 0.5.281

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