cogsbox-state 0.5.270 → 0.5.272

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