cogsbox-state 0.5.285 → 0.5.287

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