cogsbox-state 0.5.350 → 0.5.352

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