cogsbox-state 0.5.284 → 0.5.286

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,120 +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().getNestedState(
633
- t,
634
- n
635
- );
636
- let b = 0;
637
- const G = [];
638
- for (let z = 0; z < $.length; z++)
639
- G[z] = b, b += w(z);
640
- return { totalHeight: b, positions: G };
641
- }, [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(
642
629
  t,
643
630
  n
644
- ), D = U.length, P = xt(() => {
645
- const $ = Math.max(0, u.startIndex), b = Math.min(D, u.endIndex), G = Array.from(
646
- { length: b - $ },
647
- (K, st) => $ + st
648
- ), z = G.map((K) => U[K]);
649
- 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, {
650
646
  ...S,
651
- validIndices: G
647
+ validIndices: _
652
648
  });
653
- }, [u.startIndex, u.endIndex, U, D]);
654
- ft(() => {
655
- const $ = g.current;
656
- if (!$) return;
657
- const b = V.current, G = D > C.current;
658
- C.current = D;
659
- const { totalHeight: z, positions: K } = A(), st = () => {
660
- const { scrollTop: vt, clientHeight: $t, scrollHeight: jt } = $;
661
- V.current = jt - vt - $t < 10;
662
- let J = 0, It = K.length - 1;
663
- for (; J < It; ) {
664
- const et = Math.floor((J + It) / 2);
665
- K[et] < vt ? J = et + 1 : It = et;
666
- }
667
- J = Math.max(0, J - s);
668
- const Ot = vt + $t;
669
- let tt = J;
670
- for (; tt < K.length && K[tt] < Ot; )
671
- tt++;
672
- 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);
673
668
  };
674
- return $.addEventListener("scroll", st, {
669
+ return E.addEventListener("scroll", M, {
675
670
  passive: !0
676
- }), i && (x.current ? $.scrollTo({
677
- top: $.scrollHeight,
671
+ }), i && (P.current ? E.scrollTo({
672
+ top: E.scrollHeight,
678
673
  behavior: "auto"
679
- }) : b && G && requestAnimationFrame(() => {
680
- $.scrollTo({
681
- top: $.scrollHeight,
674
+ }) : V && _ && requestAnimationFrame(() => {
675
+ E.scrollTo({
676
+ top: E.scrollHeight,
682
677
  behavior: "smooth"
683
678
  });
684
- })), x.current = !1, st(), () => $.removeEventListener("scroll", st);
685
- }, [D, A, s, i]);
686
- const N = dt(
687
- ($ = "smooth") => {
679
+ })), P.current = !1, M(), () => E.removeEventListener("scroll", M);
680
+ }, [b, s, i, C]);
681
+ const O = mt(
682
+ (E = "smooth") => {
688
683
  g.current && g.current.scrollTo({
689
684
  top: g.current.scrollHeight,
690
- behavior: $
685
+ behavior: E
691
686
  });
692
687
  },
693
688
  []
694
- ), M = dt(
695
- ($, b = "smooth") => {
696
- if (g.current) {
697
- const { positions: G } = A();
698
- g.current.scrollTo({
699
- top: G[$] || 0,
700
- behavior: b
701
- });
702
- }
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
+ });
703
695
  },
704
- [A]
705
- ), {
706
- totalHeight: W,
707
- positions: T
708
- } = A(), F = T[u.startIndex] || 0, j = {
696
+ [C]
697
+ // Depends on the calculated positions
698
+ ), $ = {
709
699
  outer: {
710
700
  ref: g,
711
701
  style: { overflowY: "auto", height: "100%" }
712
702
  },
713
703
  inner: {
714
704
  style: {
715
- height: `${W}px`,
705
+ height: `${B}px`,
716
706
  position: "relative"
717
707
  }
718
708
  },
719
709
  list: {
720
710
  style: {
721
- transform: `translateY(${F}px)`
711
+ transform: `translateY(${C[u.startIndex] || 0}px)`
722
712
  }
723
713
  }
724
714
  };
725
715
  return {
726
- virtualState: P,
727
- virtualizerProps: j,
728
- scrollToBottom: N,
729
- scrollToIndex: M
716
+ virtualState: N,
717
+ virtualizerProps: $,
718
+ scrollToBottom: O,
719
+ scrollToIndex: D
730
720
  };
731
721
  };
732
722
  if (l === "stateSort")
733
723
  return (e) => {
734
724
  const s = [...d()].sort(
735
- (u, E) => e(u.item, E.item)
725
+ (u, A) => e(u.item, A.item)
736
726
  ), i = s.map(({ item: u }) => u), g = {
737
727
  ...S,
738
728
  validIndices: s.map(
@@ -744,7 +734,7 @@ function mt(t, c, h, f) {
744
734
  if (l === "stateFilter")
745
735
  return (e) => {
746
736
  const s = d().filter(
747
- ({ item: u }, E) => e(u, E)
737
+ ({ item: u }, A) => e(u, A)
748
738
  ), i = s.map(({ item: u }) => u), g = {
749
739
  ...S,
750
740
  validIndices: s.map(
@@ -757,22 +747,22 @@ function mt(t, c, h, f) {
757
747
  return (e) => {
758
748
  const r = o.getState().getNestedState(t, n);
759
749
  return Array.isArray(r) ? (S?.validIndices || Array.from({ length: r.length }, (i, g) => g)).map((i, g) => {
760
- 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);
761
751
  return e(u, w, {
762
752
  register: () => {
763
- const [, V] = ot({}), C = `${h}-${n.join(".")}-${i}`;
764
- ft(() => {
765
- 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) || {
766
756
  components: /* @__PURE__ */ new Map()
767
757
  };
768
- return U.components.set(x, {
769
- forceUpdate: () => V({}),
770
- paths: /* @__PURE__ */ new Set([E.join(".")])
758
+ return U.components.set(b, {
759
+ forceUpdate: () => P({}),
760
+ paths: /* @__PURE__ */ new Set([A.join(".")])
771
761
  }), o.getState().stateComponents.set(t, U), () => {
772
- const D = o.getState().stateComponents.get(t);
773
- D && D.components.delete(x);
762
+ const B = o.getState().stateComponents.get(t);
763
+ B && B.components.delete(b);
774
764
  };
775
- }, [t, C]);
765
+ }, [t, x]);
776
766
  },
777
767
  index: g,
778
768
  originalIndex: i
@@ -783,20 +773,20 @@ function mt(t, c, h, f) {
783
773
  ), null);
784
774
  };
785
775
  if (l === "stateMapNoRender")
786
- return (e) => y.map((s, i) => {
776
+ return (e) => m.map((s, i) => {
787
777
  let g;
788
778
  S?.validIndices && S.validIndices[i] !== void 0 ? g = S.validIndices[i] : g = i;
789
- const u = [...n, g.toString()], E = a(s, u, S);
779
+ const u = [...n, g.toString()], A = a(s, u, S);
790
780
  return e(
791
781
  s,
792
- E,
782
+ A,
793
783
  i,
794
- y,
795
- a(y, n, S)
784
+ m,
785
+ a(m, n, S)
796
786
  );
797
787
  });
798
788
  if (l === "$stateMap")
799
- return (e) => ct(Kt, {
789
+ return (e) => st(qt, {
800
790
  proxy: {
801
791
  _stateKey: t,
802
792
  _path: n,
@@ -809,12 +799,12 @@ function mt(t, c, h, f) {
809
799
  return (e) => {
810
800
  const r = o.getState().getNestedState(t, n);
811
801
  return Array.isArray(r) ? (S?.validIndices || Array.from({ length: r.length }, (i, g) => g)).map((i, g) => {
812
- const u = r[i], E = [...n, i.toString()], w = a(u, E, S), A = `${h}-${n.join(".")}-${i}`;
813
- 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, {
814
804
  key: i,
815
805
  stateKey: t,
816
- itemComponentId: A,
817
- itemPath: E,
806
+ itemComponentId: T,
807
+ itemPath: A,
818
808
  children: e(
819
809
  u,
820
810
  w,
@@ -829,8 +819,8 @@ function mt(t, c, h, f) {
829
819
  };
830
820
  if (l === "stateFlattenOn")
831
821
  return (e) => {
832
- const r = y;
833
- v.clear(), k++;
822
+ const r = m;
823
+ y.clear(), k++;
834
824
  const s = r.flatMap(
835
825
  (i) => i[e] ?? []
836
826
  );
@@ -842,7 +832,7 @@ function mt(t, c, h, f) {
842
832
  };
843
833
  if (l === "index")
844
834
  return (e) => {
845
- const r = y[e];
835
+ const r = m[e];
846
836
  return a(r, [...n, e.toString()]);
847
837
  };
848
838
  if (l === "last")
@@ -853,49 +843,49 @@ function mt(t, c, h, f) {
853
843
  return a(s, i);
854
844
  };
855
845
  if (l === "insert")
856
- return (e) => (p(n), pt(c, e, n, t), a(
846
+ return (e) => (p(n), ht(c, e, n, t), a(
857
847
  o.getState().getNestedState(t, n),
858
848
  n
859
849
  ));
860
850
  if (l === "uniqueInsert")
861
851
  return (e, r, s) => {
862
- 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;
863
853
  let u = null;
864
854
  if (!i.some((w) => {
865
855
  if (r) {
866
- const V = r.every(
867
- (C) => B(w[C], g[C])
856
+ const P = r.every(
857
+ (x) => H(w[x], g[x])
868
858
  );
869
- return V && (u = w), V;
859
+ return P && (u = w), P;
870
860
  }
871
- const A = B(w, g);
872
- return A && (u = w), A;
861
+ const T = H(w, g);
862
+ return T && (u = w), T;
873
863
  }))
874
- p(n), pt(c, g, n, t);
864
+ p(n), ht(c, g, n, t);
875
865
  else if (s && u) {
876
- const w = s(u), A = i.map(
877
- (V) => B(V, u) ? w : V
866
+ const w = s(u), T = i.map(
867
+ (P) => H(P, u) ? w : P
878
868
  );
879
- p(n), it(c, A, n);
869
+ p(n), at(c, T, n);
880
870
  }
881
871
  };
882
872
  if (l === "cut")
883
873
  return (e, r) => {
884
874
  if (!r?.waitForSync)
885
- return p(n), ut(c, n, t, e), a(
875
+ return p(n), ct(c, n, t, e), a(
886
876
  o.getState().getNestedState(t, n),
887
877
  n
888
878
  );
889
879
  };
890
880
  if (l === "cutByValue")
891
881
  return (e) => {
892
- for (let r = 0; r < y.length; r++)
893
- 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);
894
884
  };
895
885
  if (l === "toggleByValue")
896
886
  return (e) => {
897
- const r = y.findIndex((s) => s === e);
898
- 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);
899
889
  };
900
890
  if (l === "stateFind")
901
891
  return (e) => {
@@ -916,33 +906,33 @@ function mt(t, c, h, f) {
916
906
  return a(i.item, g, S);
917
907
  };
918
908
  }
919
- const Q = n[n.length - 1];
920
- if (!isNaN(Number(Q))) {
909
+ const X = n[n.length - 1];
910
+ if (!isNaN(Number(X))) {
921
911
  const d = n.slice(0, -1), e = o.getState().getNestedState(t, d);
922
912
  if (Array.isArray(e) && l === "cut")
923
- return () => ut(
913
+ return () => ct(
924
914
  c,
925
915
  d,
926
916
  t,
927
- Number(Q)
917
+ Number(X)
928
918
  );
929
919
  }
930
920
  if (l === "get")
931
921
  return () => {
932
- if (S?.validIndices && Array.isArray(y)) {
922
+ if (S?.validIndices && Array.isArray(m)) {
933
923
  const d = o.getState().getNestedState(t, n);
934
924
  return S.validIndices.map((e) => d[e]);
935
925
  }
936
926
  return o.getState().getNestedState(t, n);
937
927
  };
938
928
  if (l === "$derive")
939
- return (d) => Vt({
929
+ return (d) => kt({
940
930
  _stateKey: t,
941
931
  _path: n,
942
932
  _effect: d.toString()
943
933
  });
944
934
  if (l === "$get")
945
- return () => Vt({
935
+ return () => kt({
946
936
  _stateKey: t,
947
937
  _path: n
948
938
  });
@@ -951,7 +941,7 @@ function mt(t, c, h, f) {
951
941
  return o.getState().getSyncInfo(d);
952
942
  }
953
943
  if (l == "getLocalStorage")
954
- return (d) => ht(f + "-" + t + "-" + d);
944
+ return (d) => ft(f + "-" + t + "-" + d);
955
945
  if (l === "_selected") {
956
946
  const d = n.slice(0, -1), e = d.join("."), r = o.getState().getNestedState(t, d);
957
947
  return Array.isArray(r) ? Number(n[n.length - 1]) === o.getState().getSelectedIndex(t, e) : void 0;
@@ -961,7 +951,7 @@ function mt(t, c, h, f) {
961
951
  const e = n.slice(0, -1), r = Number(n[n.length - 1]), s = e.join(".");
962
952
  d ? o.getState().setSelectedIndex(t, s, r) : o.getState().setSelectedIndex(t, s, void 0);
963
953
  const i = o.getState().getNestedState(t, [...e]);
964
- it(c, i, e), p(e);
954
+ at(c, i, e), p(e);
965
955
  };
966
956
  if (l === "toggleSelected")
967
957
  return () => {
@@ -972,73 +962,73 @@ function mt(t, c, h, f) {
972
962
  s === e ? void 0 : e
973
963
  );
974
964
  const i = o.getState().getNestedState(t, [...d]);
975
- it(c, i, d), p(d);
965
+ at(c, i, d), p(d);
976
966
  };
977
967
  if (n.length == 0) {
978
968
  if (l === "applyJsonPatch")
979
969
  return (d) => {
980
- const e = o.getState().cogsStateStore[t], s = Ht(e, d).newDocument;
981
- Mt(
970
+ const e = o.getState().cogsStateStore[t], s = Ft(e, d).newDocument;
971
+ Ct(
982
972
  t,
983
973
  o.getState().initialStateGlobal[t],
984
974
  s,
985
975
  c,
986
- h,
976
+ v,
987
977
  f
988
978
  );
989
979
  const i = o.getState().stateComponents.get(t);
990
980
  if (i) {
991
- const g = Et(e, s), u = new Set(g);
981
+ const g = It(e, s), u = new Set(g);
992
982
  for (const [
993
- E,
983
+ A,
994
984
  w
995
985
  ] of i.components.entries()) {
996
- let A = !1;
997
- const V = Array.isArray(w.reactiveType) ? w.reactiveType : [w.reactiveType || "component"];
998
- if (!V.includes("none")) {
999
- 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")) {
1000
990
  w.forceUpdate();
1001
991
  continue;
1002
992
  }
1003
- if (V.includes("component") && (w.paths.has("") && (A = !0), !A))
1004
- for (const C of u) {
1005
- if (w.paths.has(C)) {
1006
- 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;
1007
997
  break;
1008
998
  }
1009
- let x = C.lastIndexOf(".");
1010
- for (; x !== -1; ) {
1011
- const U = C.substring(0, x);
999
+ let b = x.lastIndexOf(".");
1000
+ for (; b !== -1; ) {
1001
+ const U = x.substring(0, b);
1012
1002
  if (w.paths.has(U)) {
1013
- A = !0;
1003
+ T = !0;
1014
1004
  break;
1015
1005
  }
1016
- const D = C.substring(
1017
- x + 1
1006
+ const B = x.substring(
1007
+ b + 1
1018
1008
  );
1019
- if (!isNaN(Number(D))) {
1020
- const P = U.lastIndexOf(".");
1021
- if (P !== -1) {
1009
+ if (!isNaN(Number(B))) {
1010
+ const C = U.lastIndexOf(".");
1011
+ if (C !== -1) {
1022
1012
  const N = U.substring(
1023
1013
  0,
1024
- P
1014
+ C
1025
1015
  );
1026
1016
  if (w.paths.has(N)) {
1027
- A = !0;
1017
+ T = !0;
1028
1018
  break;
1029
1019
  }
1030
1020
  }
1031
1021
  }
1032
- x = U.lastIndexOf(".");
1022
+ b = U.lastIndexOf(".");
1033
1023
  }
1034
- if (A) break;
1024
+ if (T) break;
1035
1025
  }
1036
- if (!A && V.includes("deps") && w.depsFunction) {
1037
- const C = w.depsFunction(s);
1038
- let x = !1;
1039
- 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);
1040
1030
  }
1041
- A && w.forceUpdate();
1031
+ T && w.forceUpdate();
1042
1032
  }
1043
1033
  }
1044
1034
  }
@@ -1050,27 +1040,27 @@ function mt(t, c, h, f) {
1050
1040
  throw new Error("Zod schema not found");
1051
1041
  if (!d?.key)
1052
1042
  throw new Error("Validation key not found");
1053
- Y(d.key);
1043
+ J(d.key);
1054
1044
  const r = o.getState().cogsStateStore[t];
1055
1045
  try {
1056
1046
  const s = o.getState().getValidationErrors(d.key);
1057
1047
  s && s.length > 0 && s.forEach(([g]) => {
1058
- g && g.startsWith(d.key) && Y(g);
1048
+ g && g.startsWith(d.key) && J(g);
1059
1049
  });
1060
1050
  const i = d.zodSchema.safeParse(r);
1061
1051
  return i.success ? !0 : (i.error.errors.forEach((u) => {
1062
- const E = u.path, w = u.message, A = [d.key, ...E].join(".");
1063
- e(A, w);
1064
- }), yt(t), !1);
1052
+ const A = u.path, w = u.message, T = [d.key, ...A].join(".");
1053
+ e(T, w);
1054
+ }), St(t), !1);
1065
1055
  } catch (s) {
1066
1056
  return console.error("Zod schema validation failed", s), !1;
1067
1057
  }
1068
1058
  };
1069
- if (l === "_componentId") return h;
1059
+ if (l === "_componentId") return v;
1070
1060
  if (l === "getComponents")
1071
1061
  return () => o().stateComponents.get(t);
1072
1062
  if (l === "getAllFormRefs")
1073
- return () => kt.getState().getFormRefsByStateKey(t);
1063
+ return () => Et.getState().getFormRefsByStateKey(t);
1074
1064
  if (l === "_initialState")
1075
1065
  return o.getState().initialStateGlobal[t];
1076
1066
  if (l === "_serverState")
@@ -1083,13 +1073,13 @@ function mt(t, c, h, f) {
1083
1073
  if (l === "removeValidation") return I.removeValidation;
1084
1074
  }
1085
1075
  if (l === "getFormRef")
1086
- return () => kt.getState().getFormRef(t + "." + n.join("."));
1076
+ return () => Et.getState().getFormRef(t + "." + n.join("."));
1087
1077
  if (l === "validationWrapper")
1088
1078
  return ({
1089
1079
  children: d,
1090
1080
  hideMessage: e
1091
- }) => /* @__PURE__ */ wt(
1092
- Wt,
1081
+ }) => /* @__PURE__ */ vt(
1082
+ jt,
1093
1083
  {
1094
1084
  formOpts: e ? { validation: { message: "" } } : void 0,
1095
1085
  path: n,
@@ -1105,21 +1095,21 @@ function mt(t, c, h, f) {
1105
1095
  if (l === "update")
1106
1096
  return (d, e) => {
1107
1097
  if (e?.debounce)
1108
- Dt(() => {
1109
- it(c, d, n, "");
1098
+ Mt(() => {
1099
+ at(c, d, n, "");
1110
1100
  const r = o.getState().getNestedState(t, n);
1111
1101
  e?.afterUpdate && e.afterUpdate(r);
1112
1102
  }, e.debounce);
1113
1103
  else {
1114
- it(c, d, n, "");
1104
+ at(c, d, n, "");
1115
1105
  const r = o.getState().getNestedState(t, n);
1116
1106
  e?.afterUpdate && e.afterUpdate(r);
1117
1107
  }
1118
1108
  p(n);
1119
1109
  };
1120
1110
  if (l === "formElement")
1121
- return (d, e) => /* @__PURE__ */ wt(
1122
- Gt,
1111
+ return (d, e) => /* @__PURE__ */ vt(
1112
+ Ot,
1123
1113
  {
1124
1114
  setState: c,
1125
1115
  stateKey: t,
@@ -1128,51 +1118,51 @@ function mt(t, c, h, f) {
1128
1118
  formOpts: e
1129
1119
  }
1130
1120
  );
1131
- const _ = [...n, l], at = o.getState().getNestedState(t, _);
1132
- return a(at, _, S);
1121
+ const j = [...n, l], ot = o.getState().getNestedState(t, j);
1122
+ return a(ot, j, S);
1133
1123
  }
1134
- }, H = new Proxy(O, R);
1135
- return v.set(L, {
1136
- proxy: H,
1124
+ }, G = new Proxy(R, F);
1125
+ return y.set(W, {
1126
+ proxy: G,
1137
1127
  stateVersion: k
1138
- }), H;
1128
+ }), G;
1139
1129
  }
1140
1130
  return a(
1141
1131
  o.getState().getNestedState(t, [])
1142
1132
  );
1143
1133
  }
1144
- function Vt(t) {
1145
- return ct(te, { proxy: t });
1134
+ function kt(t) {
1135
+ return st(Jt, { proxy: t });
1146
1136
  }
1147
- function Kt({
1137
+ function qt({
1148
1138
  proxy: t,
1149
1139
  rebuildStateShape: c
1150
1140
  }) {
1151
- const h = o().getNestedState(t._stateKey, t._path);
1152
- return Array.isArray(h) ? c(
1153
- h,
1141
+ const v = o().getNestedState(t._stateKey, t._path);
1142
+ return Array.isArray(v) ? c(
1143
+ v,
1154
1144
  t._path
1155
1145
  ).stateMapNoRender(
1156
- (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)
1157
1147
  ) : null;
1158
1148
  }
1159
- function te({
1149
+ function Jt({
1160
1150
  proxy: t
1161
1151
  }) {
1162
- const c = Z(null), h = `${t._stateKey}-${t._path.join(".")}`;
1163
- return gt(() => {
1152
+ const c = Y(null), v = `${t._stateKey}-${t._path.join(".")}`;
1153
+ return lt(() => {
1164
1154
  const f = c.current;
1165
1155
  if (!f || !f.parentElement) return;
1166
- const v = f.parentElement, p = Array.from(v.childNodes).indexOf(f);
1167
- let I = v.getAttribute("data-parent-id");
1168
- I || (I = `parent-${crypto.randomUUID()}`, v.setAttribute("data-parent-id", I));
1169
- 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 = {
1170
1160
  instanceId: `instance-${crypto.randomUUID()}`,
1171
1161
  parentId: I,
1172
1162
  position: p,
1173
1163
  effect: t._effect
1174
1164
  };
1175
- o.getState().addSignalElement(h, y);
1165
+ o.getState().addSignalElement(v, m);
1176
1166
  const n = o.getState().getNestedState(t._stateKey, t._path);
1177
1167
  let S;
1178
1168
  if (t._effect)
@@ -1181,67 +1171,67 @@ function te({
1181
1171
  "state",
1182
1172
  `return (${t._effect})(state)`
1183
1173
  )(n);
1184
- } catch (O) {
1185
- 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;
1186
1176
  }
1187
1177
  else
1188
1178
  S = n;
1189
1179
  S !== null && typeof S == "object" && (S = JSON.stringify(S));
1190
- const L = document.createTextNode(String(S));
1191
- f.replaceWith(L);
1192
- }, [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", {
1193
1183
  ref: c,
1194
1184
  style: { display: "none" },
1195
- "data-signal-id": h
1185
+ "data-signal-id": v
1196
1186
  });
1197
1187
  }
1198
- function he(t) {
1199
- const c = Rt(
1200
- (h) => {
1188
+ function de(t) {
1189
+ const c = xt(
1190
+ (v) => {
1201
1191
  const f = o.getState().stateComponents.get(t._stateKey) || {
1202
1192
  components: /* @__PURE__ */ new Map()
1203
1193
  };
1204
1194
  return f.components.set(t._stateKey, {
1205
- forceUpdate: h,
1195
+ forceUpdate: v,
1206
1196
  paths: /* @__PURE__ */ new Set([t._path.join(".")])
1207
1197
  }), () => f.components.delete(t._stateKey);
1208
1198
  },
1209
1199
  () => o.getState().getNestedState(t._stateKey, t._path)
1210
1200
  );
1211
- return ct("text", {}, String(c));
1201
+ return st("text", {}, String(c));
1212
1202
  }
1213
- function ee({
1203
+ function Yt({
1214
1204
  stateKey: t,
1215
1205
  itemComponentId: c,
1216
- itemPath: h,
1206
+ itemPath: v,
1217
1207
  children: f
1218
1208
  }) {
1219
- const [, v] = ot({}), [k, p] = zt();
1220
- return gt(() => {
1221
- 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, {
1222
1212
  virtualizer: {
1223
1213
  itemHeight: p.height
1224
1214
  }
1225
1215
  });
1226
- }, [p.height]), ft(() => {
1216
+ }, [p.height]), dt(() => {
1227
1217
  const I = `${t}////${c}`, a = o.getState().stateComponents.get(t) || {
1228
1218
  components: /* @__PURE__ */ new Map()
1229
1219
  };
1230
1220
  return a.components.set(I, {
1231
- forceUpdate: () => v({}),
1232
- paths: /* @__PURE__ */ new Set([h.join(".")])
1221
+ forceUpdate: () => y({}),
1222
+ paths: /* @__PURE__ */ new Set([v.join(".")])
1233
1223
  }), o.getState().stateComponents.set(t, a), () => {
1234
- const y = o.getState().stateComponents.get(t);
1235
- y && y.components.delete(I);
1224
+ const m = o.getState().stateComponents.get(t);
1225
+ m && m.components.delete(I);
1236
1226
  };
1237
- }, [t, c, h.join(".")]), /* @__PURE__ */ wt("div", { ref: k, children: f });
1227
+ }, [t, c, v.join(".")]), /* @__PURE__ */ vt("div", { ref: k, children: f });
1238
1228
  }
1239
1229
  export {
1240
- Vt as $cogsSignal,
1241
- he as $cogsSignalStore,
1242
- fe as addStateOptions,
1243
- Se as createCogsState,
1244
- me as notifyComponent,
1245
- 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
1246
1236
  };
1247
1237
  //# sourceMappingURL=CogsState.jsx.map