cogsbox-state 0.5.353 → 0.5.355

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