cogsbox-state 0.5.262 → 0.5.264

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