cogsbox-state 0.5.428 → 0.5.430

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