cogsbox-state 0.5.184 → 0.5.185

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,57 +1,57 @@
1
1
  "use client";
2
2
  import { jsx as Se } from "react/jsx-runtime";
3
- import { useState as le, useRef as X, useEffect as de, useLayoutEffect as $e, useMemo as Ne, createElement as ee, useSyncExternalStore as he, startTransition as ke } from "react";
4
- import { transformStateFunc as Ve, isDeepEqual as G, isFunction as q, getNestedValue as D, getDifferences as we, debounce as Ae } from "./utility.js";
5
- import { pushFunc as se, updateFn as Z, cutFunc as Y, ValidationWrapper as xe, FormControlComponent as Te } from "./Functions.jsx";
3
+ import { useState as le, useRef as X, useEffect as de, useLayoutEffect as he, useMemo as $e, createElement as ee, useSyncExternalStore as Te, startTransition as ke } from "react";
4
+ import { transformStateFunc as Ne, isDeepEqual as M, isFunction as q, getNestedValue as U, getDifferences as we, debounce as Ve } from "./utility.js";
5
+ import { pushFunc as se, updateFn as Z, cutFunc as Y, ValidationWrapper as xe, FormControlComponent as Ae } from "./Functions.jsx";
6
6
  import Ce from "superjson";
7
7
  import { v4 as ue } from "uuid";
8
8
  import "zod";
9
9
  import { getGlobalStore as r, formRefStore as me } from "./store.js";
10
10
  import { useCogsConfig as Ee } from "./CogsStateClient.jsx";
11
- function ye(e, s) {
12
- const m = r.getState().getInitialOptions, u = r.getState().setInitialStateOptions, g = m(e) || {};
11
+ function ye(e, c) {
12
+ const m = r.getState().getInitialOptions, u = r.getState().setInitialStateOptions, f = m(e) || {};
13
13
  u(e, {
14
- ...g,
15
- ...s
14
+ ...f,
15
+ ...c
16
16
  });
17
17
  }
18
18
  function ve({
19
19
  stateKey: e,
20
- options: s,
20
+ options: c,
21
21
  initialOptionsPart: m
22
22
  }) {
23
- const u = B(e) || {}, g = m[e] || {}, w = r.getState().setInitialStateOptions, I = { ...g, ...u };
23
+ const u = B(e) || {}, f = m[e] || {}, h = r.getState().setInitialStateOptions, v = { ...f, ...u };
24
24
  let y = !1;
25
- if (s)
26
- for (const i in s)
27
- I.hasOwnProperty(i) ? (i == "localStorage" && s[i] && I[i].key !== s[i]?.key && (y = !0, I[i] = s[i]), i == "initialState" && s[i] && I[i] !== s[i] && // Different references
28
- !G(I[i], s[i]) && (y = !0, I[i] = s[i])) : (y = !0, I[i] = s[i]);
29
- y && w(e, I);
25
+ if (c)
26
+ for (const i in c)
27
+ v.hasOwnProperty(i) ? (i == "localStorage" && c[i] && v[i].key !== c[i]?.key && (y = !0, v[i] = c[i]), i == "initialState" && c[i] && v[i] !== c[i] && // Different references
28
+ !M(v[i], c[i]) && (y = !0, v[i] = c[i])) : (y = !0, v[i] = c[i]);
29
+ y && h(e, v);
30
30
  }
31
- function Ye(e, { formElements: s, validation: m }) {
32
- return { initialState: e, formElements: s, validation: m };
31
+ function Ye(e, { formElements: c, validation: m }) {
32
+ return { initialState: e, formElements: c, validation: m };
33
33
  }
34
- const Xe = (e, s) => {
34
+ const Xe = (e, c) => {
35
35
  let m = e;
36
- const [u, g] = Ve(m);
37
- (Object.keys(g).length > 0 || s && Object.keys(s).length > 0) && Object.keys(g).forEach((y) => {
38
- g[y] = g[y] || {}, g[y].formElements = {
39
- ...s?.formElements,
36
+ const [u, f] = Ne(m);
37
+ (Object.keys(f).length > 0 || c && Object.keys(c).length > 0) && Object.keys(f).forEach((y) => {
38
+ f[y] = f[y] || {}, f[y].formElements = {
39
+ ...c?.formElements,
40
40
  // Global defaults first
41
- ...s?.validation,
42
- ...g[y].formElements || {}
41
+ ...c?.validation,
42
+ ...f[y].formElements || {}
43
43
  // State-specific overrides
44
- }, B(y) || r.getState().setInitialStateOptions(y, g[y]);
44
+ }, B(y) || r.getState().setInitialStateOptions(y, f[y]);
45
45
  }), r.getState().setInitialStates(u), r.getState().setCreatedState(u);
46
- const w = (y, i) => {
46
+ const h = (y, i) => {
47
47
  const [S] = le(i?.componentId ?? ue());
48
48
  ve({
49
49
  stateKey: y,
50
50
  options: i,
51
- initialOptionsPart: g
51
+ initialOptionsPart: f
52
52
  });
53
- const t = r.getState().cogsStateStore[y] || u[y], v = i?.modifyState ? i.modifyState(t) : t, [F, V] = De(
54
- v,
53
+ const t = r.getState().cogsStateStore[y] || u[y], I = i?.modifyState ? i.modifyState(t) : t, [O, k] = Ue(
54
+ I,
55
55
  {
56
56
  stateKey: y,
57
57
  syncUpdate: i?.syncUpdate,
@@ -66,12 +66,12 @@ const Xe = (e, s) => {
66
66
  serverState: i?.serverState
67
67
  }
68
68
  );
69
- return V;
69
+ return k;
70
70
  };
71
- function I(y, i) {
72
- ve({ stateKey: y, options: i, initialOptionsPart: g }), i.localStorage && Oe(y, i), ne(y);
71
+ function v(y, i) {
72
+ ve({ stateKey: y, options: i, initialOptionsPart: f }), i.localStorage && je(y, i), ne(y);
73
73
  }
74
- return { useCogsState: w, setCogsOptions: I };
74
+ return { useCogsState: h, setCogsOptions: v };
75
75
  }, {
76
76
  setUpdaterState: Q,
77
77
  setState: z,
@@ -80,97 +80,97 @@ const Xe = (e, s) => {
80
80
  getValidationErrors: be,
81
81
  setStateLog: Pe,
82
82
  updateInitialStateGlobal: ge,
83
- addValidationError: je,
83
+ addValidationError: Fe,
84
84
  removeValidationError: W,
85
- setServerSyncActions: Fe
86
- } = r.getState(), Ie = (e, s, m, u, g) => {
85
+ setServerSyncActions: Oe
86
+ } = r.getState(), Ie = (e, c, m, u, f) => {
87
87
  m?.log && console.log(
88
88
  "saving to localstorage",
89
- s,
89
+ c,
90
90
  m.localStorage?.key,
91
91
  u
92
92
  );
93
- const w = q(m?.localStorage?.key) ? m.localStorage?.key(e) : m?.localStorage?.key;
94
- if (w && u) {
95
- const I = `${u}-${s}-${w}`;
93
+ const h = q(m?.localStorage?.key) ? m.localStorage?.key(e) : m?.localStorage?.key;
94
+ if (h && u) {
95
+ const v = `${u}-${c}-${h}`;
96
96
  let y;
97
97
  try {
98
- y = te(I)?.lastSyncedWithServer;
98
+ y = te(v)?.lastSyncedWithServer;
99
99
  } catch {
100
100
  }
101
101
  const i = {
102
102
  state: e,
103
103
  lastUpdated: Date.now(),
104
- lastSyncedWithServer: g ?? y
104
+ lastSyncedWithServer: f ?? y
105
105
  }, S = Ce.serialize(i);
106
106
  window.localStorage.setItem(
107
- I,
107
+ v,
108
108
  JSON.stringify(S.json)
109
109
  );
110
110
  }
111
111
  }, te = (e) => {
112
112
  if (!e) return null;
113
113
  try {
114
- const s = window.localStorage.getItem(e);
115
- return s ? JSON.parse(s) : null;
116
- } catch (s) {
117
- return console.error("Error loading from localStorage:", s), null;
114
+ const c = window.localStorage.getItem(e);
115
+ return c ? JSON.parse(c) : null;
116
+ } catch (c) {
117
+ return console.error("Error loading from localStorage:", c), null;
118
118
  }
119
- }, Oe = (e, s) => {
120
- const m = r.getState().cogsStateStore[e], { sessionId: u } = Ee(), g = q(s?.localStorage?.key) ? s.localStorage.key(m) : s?.localStorage?.key;
121
- if (g && u) {
122
- const w = te(
123
- `${u}-${e}-${g}`
119
+ }, je = (e, c) => {
120
+ const m = r.getState().cogsStateStore[e], { sessionId: u } = Ee(), f = q(c?.localStorage?.key) ? c.localStorage.key(m) : c?.localStorage?.key;
121
+ if (f && u) {
122
+ const h = te(
123
+ `${u}-${e}-${f}`
124
124
  );
125
- if (w && w.lastUpdated > (w.lastSyncedWithServer || 0))
126
- return z(e, w.state), ne(e), !0;
125
+ if (h && h.lastUpdated > (h.lastSyncedWithServer || 0))
126
+ return z(e, h.state), ne(e), !0;
127
127
  }
128
128
  return !1;
129
- }, Re = (e, s, m, u, g, w) => {
130
- const I = {
131
- initialState: s,
129
+ }, pe = (e, c, m, u, f, h) => {
130
+ const v = {
131
+ initialState: c,
132
132
  updaterState: K(
133
133
  e,
134
134
  u,
135
- g,
136
- w
135
+ f,
136
+ h
137
137
  ),
138
138
  state: m
139
139
  };
140
- ge(e, I.initialState), Q(e, I.updaterState), z(e, I.state);
140
+ ge(e, v.initialState), Q(e, v.updaterState), z(e, v.state);
141
141
  }, ne = (e) => {
142
- const s = r.getState().stateComponents.get(e);
143
- if (!s) return;
142
+ const c = r.getState().stateComponents.get(e);
143
+ if (!c) return;
144
144
  const m = /* @__PURE__ */ new Set();
145
- s.components.forEach((u) => {
145
+ c.components.forEach((u) => {
146
146
  (u ? Array.isArray(u.reactiveType) ? u.reactiveType : [u.reactiveType || "component"] : null)?.includes("none") || m.add(() => u.forceUpdate());
147
147
  }), queueMicrotask(() => {
148
148
  m.forEach((u) => u());
149
149
  });
150
- }, Qe = (e, s) => {
150
+ }, Qe = (e, c) => {
151
151
  const m = r.getState().stateComponents.get(e);
152
152
  if (m) {
153
- const u = `${e}////${s}`, g = m.components.get(u);
154
- if ((g ? Array.isArray(g.reactiveType) ? g.reactiveType : [g.reactiveType || "component"] : null)?.includes("none"))
153
+ const u = `${e}////${c}`, f = m.components.get(u);
154
+ if ((f ? Array.isArray(f.reactiveType) ? f.reactiveType : [f.reactiveType || "component"] : null)?.includes("none"))
155
155
  return;
156
- g && g.forceUpdate();
156
+ f && f.forceUpdate();
157
157
  }
158
- }, Ue = (e, s, m, u) => {
158
+ }, Re = (e, c, m, u) => {
159
159
  switch (e) {
160
160
  case "update":
161
161
  return {
162
- oldValue: D(s, u),
163
- newValue: D(m, u)
162
+ oldValue: U(c, u),
163
+ newValue: U(m, u)
164
164
  };
165
165
  case "insert":
166
166
  return {
167
167
  oldValue: null,
168
168
  // or undefined
169
- newValue: D(m, u)
169
+ newValue: U(m, u)
170
170
  };
171
171
  case "cut":
172
172
  return {
173
- oldValue: D(s, u),
173
+ oldValue: U(c, u),
174
174
  newValue: null
175
175
  // or undefined
176
176
  };
@@ -178,260 +178,260 @@ const Xe = (e, s) => {
178
178
  return { oldValue: null, newValue: null };
179
179
  }
180
180
  };
181
- function De(e, {
182
- stateKey: s,
181
+ function Ue(e, {
182
+ stateKey: c,
183
183
  serverSync: m,
184
184
  localStorage: u,
185
- formElements: g,
186
- reactiveDeps: w,
187
- reactiveType: I,
185
+ formElements: f,
186
+ reactiveDeps: h,
187
+ reactiveType: v,
188
188
  componentId: y,
189
189
  initialState: i,
190
190
  syncUpdate: S,
191
191
  dependencies: t,
192
- serverState: v
192
+ serverState: I
193
193
  } = {}) {
194
- const [F, V] = le({}), { sessionId: A } = Ee();
195
- let O = !s;
196
- const [f] = le(s ?? ue()), l = r.getState().stateLog[f], J = X(/* @__PURE__ */ new Set()), p = X(y ?? ue()), x = X(
194
+ const [O, k] = le({}), { sessionId: N } = Ee();
195
+ let j = !c;
196
+ const [g] = le(c ?? ue()), l = r.getState().stateLog[g], J = X(/* @__PURE__ */ new Set()), G = X(y ?? ue()), V = X(
197
197
  null
198
198
  );
199
- x.current = B(f) ?? null, de(() => {
200
- if (S && S.stateKey === f && S.path?.[0]) {
201
- z(f, (a) => ({
199
+ V.current = B(g) ?? null, de(() => {
200
+ if (S && S.stateKey === g && S.path?.[0]) {
201
+ z(g, (a) => ({
202
202
  ...a,
203
203
  [S.path[0]]: S.newValue
204
204
  }));
205
- const c = `${S.stateKey}:${S.path.join(".")}`;
206
- r.getState().setSyncInfo(c, {
205
+ const s = `${S.stateKey}:${S.path.join(".")}`;
206
+ r.getState().setSyncInfo(s, {
207
207
  timeStamp: S.timeStamp,
208
208
  userId: S.userId
209
209
  });
210
210
  }
211
211
  }, [S]), de(() => {
212
212
  if (i) {
213
- ye(f, {
213
+ ye(g, {
214
214
  initialState: i
215
215
  });
216
- const c = x.current, d = c?.serverState?.id !== void 0 && c?.serverState?.status === "success" && c?.serverState?.data, E = r.getState().initialStateGlobal[f];
217
- if (!(E && !G(E, i) || !E) && !d)
216
+ const s = V.current, d = s?.serverState?.id !== void 0 && s?.serverState?.status === "success" && s?.serverState?.data, w = r.getState().initialStateGlobal[g];
217
+ if (!(w && !M(w, i) || !w) && !d)
218
218
  return;
219
- let _ = null;
220
- const b = q(c?.localStorage?.key) ? c?.localStorage?.key(i) : c?.localStorage?.key;
221
- b && A && (_ = te(`${A}-${f}-${b}`));
222
- let h = i, L = !1;
223
- const re = d ? Date.now() : 0, H = _?.lastUpdated || 0, ae = _?.lastSyncedWithServer || 0;
224
- d && re > H ? (h = c.serverState.data, L = !0) : _ && H > ae && (h = _.state, c?.localStorage?.onChange && c?.localStorage?.onChange(h)), Re(
225
- f,
219
+ let E = null;
220
+ const b = q(s?.localStorage?.key) ? s?.localStorage?.key(i) : s?.localStorage?.key;
221
+ b && N && (E = te(`${N}-${g}-${b}`));
222
+ let T = i, L = !1;
223
+ const re = d ? Date.now() : 0, H = E?.lastUpdated || 0, ae = E?.lastSyncedWithServer || 0;
224
+ d && re > H ? (T = s.serverState.data, L = !0) : E && H > ae && (T = E.state, s?.localStorage?.onChange && s?.localStorage?.onChange(T)), pe(
225
+ g,
226
226
  i,
227
- h,
227
+ T,
228
228
  n,
229
- p.current,
230
- A
231
- ), L && b && A && Ie(h, f, c, A, Date.now()), ne(f), (Array.isArray(I) ? I : [I || "component"]).includes("none") || V({});
229
+ G.current,
230
+ N
231
+ ), L && b && N && Ie(T, g, s, N, Date.now()), ne(g), (Array.isArray(v) ? v : [v || "component"]).includes("none") || k({});
232
232
  }
233
233
  }, [
234
234
  i,
235
- v?.status,
236
- v?.data,
235
+ I?.status,
236
+ I?.data,
237
237
  ...t || []
238
- ]), $e(() => {
239
- O && ye(f, {
238
+ ]), he(() => {
239
+ j && ye(g, {
240
240
  serverSync: m,
241
- formElements: g,
241
+ formElements: f,
242
242
  initialState: i,
243
243
  localStorage: u,
244
- middleware: x.current?.middleware
244
+ middleware: V.current?.middleware
245
245
  });
246
- const c = `${f}////${p.current}`, a = r.getState().stateComponents.get(f) || {
246
+ const s = `${g}////${G.current}`, a = r.getState().stateComponents.get(g) || {
247
247
  components: /* @__PURE__ */ new Map()
248
248
  };
249
- return a.components.set(c, {
250
- forceUpdate: () => V({}),
249
+ return a.components.set(s, {
250
+ forceUpdate: () => k({}),
251
251
  paths: /* @__PURE__ */ new Set(),
252
252
  deps: [],
253
- depsFunction: w || void 0,
254
- reactiveType: I ?? ["component", "deps"]
255
- }), r.getState().stateComponents.set(f, a), V({}), () => {
256
- const d = `${f}////${p.current}`;
257
- a && (a.components.delete(d), a.components.size === 0 && r.getState().stateComponents.delete(f));
253
+ depsFunction: h || void 0,
254
+ reactiveType: v ?? ["component", "deps"]
255
+ }), r.getState().stateComponents.set(g, a), k({}), () => {
256
+ const d = `${g}////${G.current}`;
257
+ a && (a.components.delete(d), a.components.size === 0 && r.getState().stateComponents.delete(g));
258
258
  };
259
259
  }, []);
260
- const n = (c, a, d, E) => {
260
+ const n = (s, a, d, w) => {
261
261
  if (Array.isArray(a)) {
262
- const k = `${f}-${a.join(".")}`;
263
- J.current.add(k);
262
+ const x = `${g}-${a.join(".")}`;
263
+ J.current.add(x);
264
264
  }
265
- z(f, (k) => {
266
- const _ = q(c) ? c(k) : c, b = `${f}-${a.join(".")}`;
265
+ z(g, (x) => {
266
+ const E = q(s) ? s(x) : s, b = `${g}-${a.join(".")}`;
267
267
  if (b) {
268
- let U = !1, N = r.getState().signalDomElements.get(b);
269
- if ((!N || N.size === 0) && (d.updateType === "insert" || d.updateType === "cut")) {
270
- const P = a.slice(0, -1), R = D(_, P);
271
- if (Array.isArray(R)) {
272
- U = !0;
273
- const $ = `${f}-${P.join(".")}`;
274
- N = r.getState().signalDomElements.get($);
268
+ let R = !1, $ = r.getState().signalDomElements.get(b);
269
+ if ((!$ || $.size === 0) && (d.updateType === "insert" || d.updateType === "cut")) {
270
+ const P = a.slice(0, -1), p = U(E, P);
271
+ if (Array.isArray(p)) {
272
+ R = !0;
273
+ const _ = `${g}-${P.join(".")}`;
274
+ $ = r.getState().signalDomElements.get(_);
275
275
  }
276
276
  }
277
- if (N) {
278
- const P = U ? D(_, a.slice(0, -1)) : D(_, a);
279
- N.forEach(({ parentId: R, position: $, effect: C }) => {
280
- const T = document.querySelector(
281
- `[data-parent-id="${R}"]`
277
+ if ($) {
278
+ const P = R ? U(E, a.slice(0, -1)) : U(E, a);
279
+ $.forEach(({ parentId: p, position: _, effect: C }) => {
280
+ const A = document.querySelector(
281
+ `[data-parent-id="${p}"]`
282
282
  );
283
- if (T) {
284
- const M = Array.from(T.childNodes);
285
- if (M[$]) {
286
- const j = C ? new Function("state", `return (${C})(state)`)(P) : P;
287
- M[$].textContent = String(j);
283
+ if (A) {
284
+ const D = Array.from(A.childNodes);
285
+ if (D[_]) {
286
+ const F = C ? new Function("state", `return (${C})(state)`)(P) : P;
287
+ D[_].textContent = String(F);
288
288
  }
289
289
  }
290
290
  });
291
291
  }
292
292
  }
293
- d.updateType === "update" && (E || x.current?.validation?.key) && a && W(
294
- (E || x.current?.validation?.key) + "." + a.join(".")
293
+ d.updateType === "update" && (w || V.current?.validation?.key) && a && W(
294
+ (w || V.current?.validation?.key) + "." + a.join(".")
295
295
  );
296
- const h = a.slice(0, a.length - 1);
297
- d.updateType === "cut" && x.current?.validation?.key && W(
298
- x.current?.validation?.key + "." + h.join(".")
299
- ), d.updateType === "insert" && x.current?.validation?.key && be(
300
- x.current?.validation?.key + "." + h.join(".")
301
- ).filter(([N, P]) => {
302
- let R = N?.split(".").length;
303
- if (N == h.join(".") && R == h.length - 1) {
304
- let $ = N + "." + h;
305
- W(N), je($, P);
296
+ const T = a.slice(0, a.length - 1);
297
+ d.updateType === "cut" && V.current?.validation?.key && W(
298
+ V.current?.validation?.key + "." + T.join(".")
299
+ ), d.updateType === "insert" && V.current?.validation?.key && be(
300
+ V.current?.validation?.key + "." + T.join(".")
301
+ ).filter(([$, P]) => {
302
+ let p = $?.split(".").length;
303
+ if ($ == T.join(".") && p == T.length - 1) {
304
+ let _ = $ + "." + T;
305
+ W($), Fe(_, P);
306
306
  }
307
307
  });
308
- const L = r.getState().stateComponents.get(f);
308
+ const L = r.getState().stateComponents.get(g);
309
309
  if (L) {
310
- const U = we(k, _), N = new Set(U), P = d.updateType === "update" ? a.join(".") : a.slice(0, -1).join(".") || "";
310
+ const R = we(x, E), $ = new Set(R), P = d.updateType === "update" ? a.join(".") : a.slice(0, -1).join(".") || "";
311
311
  for (const [
312
- R,
313
- $
312
+ p,
313
+ _
314
314
  ] of L.components.entries()) {
315
315
  let C = !1;
316
- const T = Array.isArray($.reactiveType) ? $.reactiveType : [$.reactiveType || "component"];
317
- if (!T.includes("none")) {
318
- if (T.includes("all")) {
319
- $.forceUpdate();
316
+ const A = Array.isArray(_.reactiveType) ? _.reactiveType : [_.reactiveType || "component"];
317
+ if (!A.includes("none")) {
318
+ if (A.includes("all")) {
319
+ _.forceUpdate();
320
320
  continue;
321
321
  }
322
- if (T.includes("component") && (($.paths.has(P) || $.paths.has("")) && (C = !0), !C))
323
- for (const M of N) {
324
- let j = M;
322
+ if (A.includes("component") && ((_.paths.has(P) || _.paths.has("")) && (C = !0), !C))
323
+ for (const D of $) {
324
+ let F = D;
325
325
  for (; ; ) {
326
- if ($.paths.has(j)) {
326
+ if (_.paths.has(F)) {
327
327
  C = !0;
328
328
  break;
329
329
  }
330
- const ie = j.lastIndexOf(".");
330
+ const ie = F.lastIndexOf(".");
331
331
  if (ie !== -1) {
332
- const fe = j.substring(
332
+ const fe = F.substring(
333
333
  0,
334
334
  ie
335
335
  );
336
336
  if (!isNaN(
337
- Number(j.substring(ie + 1))
338
- ) && $.paths.has(fe)) {
337
+ Number(F.substring(ie + 1))
338
+ ) && _.paths.has(fe)) {
339
339
  C = !0;
340
340
  break;
341
341
  }
342
- j = fe;
342
+ F = fe;
343
343
  } else
344
- j = "";
345
- if (j === "")
344
+ F = "";
345
+ if (F === "")
346
346
  break;
347
347
  }
348
348
  if (C) break;
349
349
  }
350
- if (!C && T.includes("deps") && $.depsFunction) {
351
- const M = $.depsFunction(_);
352
- let j = !1;
353
- typeof M == "boolean" ? M && (j = !0) : G($.deps, M) || ($.deps = M, j = !0), j && (C = !0);
350
+ if (!C && A.includes("deps") && _.depsFunction) {
351
+ const D = _.depsFunction(E);
352
+ let F = !1;
353
+ typeof D == "boolean" ? D && (F = !0) : M(_.deps, D) || (_.deps = D, F = !0), F && (C = !0);
354
354
  }
355
- C && $.forceUpdate();
355
+ C && _.forceUpdate();
356
356
  }
357
357
  }
358
358
  }
359
359
  const re = Date.now();
360
- a = a.map((U, N) => {
361
- const P = a.slice(0, -1), R = D(_, P);
362
- return N === a.length - 1 && ["insert", "cut"].includes(d.updateType) ? (R.length - 1).toString() : U;
360
+ a = a.map((R, $) => {
361
+ const P = a.slice(0, -1), p = U(E, P);
362
+ return $ === a.length - 1 && ["insert", "cut"].includes(d.updateType) ? (p.length - 1).toString() : R;
363
363
  }), console.log(
364
364
  "mmmmmmmmmmmmmmmmm22222222222222",
365
365
  d.updateType,
366
- k,
367
- _,
366
+ x,
367
+ E,
368
368
  a
369
369
  );
370
- const { oldValue: H, newValue: ae } = Ue(
370
+ const { oldValue: H, newValue: ae } = Re(
371
371
  d.updateType,
372
- k,
373
- _,
372
+ x,
373
+ E,
374
374
  a
375
375
  ), oe = {
376
376
  timeStamp: re,
377
- stateKey: f,
377
+ stateKey: g,
378
378
  path: a,
379
379
  updateType: d.updateType,
380
380
  status: "new",
381
381
  oldValue: H,
382
382
  newValue: ae
383
383
  };
384
- if (Pe(f, (U) => {
385
- const P = [...U ?? [], oe].reduce((R, $) => {
386
- const C = `${$.stateKey}:${JSON.stringify($.path)}`, T = R.get(C);
387
- return T ? (T.timeStamp = Math.max(T.timeStamp, $.timeStamp), T.newValue = $.newValue, T.oldValue = T.oldValue ?? $.oldValue, T.updateType = $.updateType) : R.set(C, { ...$ }), R;
384
+ if (Pe(g, (R) => {
385
+ const P = [...R ?? [], oe].reduce((p, _) => {
386
+ const C = `${_.stateKey}:${JSON.stringify(_.path)}`, A = p.get(C);
387
+ return A ? (A.timeStamp = Math.max(A.timeStamp, _.timeStamp), A.newValue = _.newValue, A.oldValue = A.oldValue ?? _.oldValue, A.updateType = _.updateType) : p.set(C, { ..._ }), p;
388
388
  }, /* @__PURE__ */ new Map());
389
389
  return Array.from(P.values());
390
390
  }), Ie(
391
- _,
392
- f,
393
- x.current,
394
- A
395
- ), x.current?.middleware && x.current.middleware({
391
+ E,
392
+ g,
393
+ V.current,
394
+ N
395
+ ), V.current?.middleware && V.current.middleware({
396
396
  updateLog: l,
397
397
  update: oe
398
- }), x.current?.serverSync) {
399
- const U = r.getState().serverState[f], N = x.current?.serverSync;
400
- Fe(f, {
401
- syncKey: typeof N.syncKey == "string" ? N.syncKey : N.syncKey({ state: _ }),
402
- rollBackState: U,
403
- actionTimeStamp: Date.now() + (N.debounce ?? 3e3),
398
+ }), V.current?.serverSync) {
399
+ const R = r.getState().serverState[g], $ = V.current?.serverSync;
400
+ Oe(g, {
401
+ syncKey: typeof $.syncKey == "string" ? $.syncKey : $.syncKey({ state: E }),
402
+ rollBackState: R,
403
+ actionTimeStamp: Date.now() + ($.debounce ?? 3e3),
404
404
  status: "waiting"
405
405
  });
406
406
  }
407
- return _;
407
+ return E;
408
408
  });
409
409
  };
410
- r.getState().updaterState[f] || (Q(
411
- f,
410
+ r.getState().updaterState[g] || (Q(
411
+ g,
412
412
  K(
413
- f,
413
+ g,
414
414
  n,
415
- p.current,
416
- A
415
+ G.current,
416
+ N
417
417
  )
418
- ), r.getState().cogsStateStore[f] || z(f, e), r.getState().initialStateGlobal[f] || ge(f, e));
419
- const o = Ne(() => K(
420
- f,
418
+ ), r.getState().cogsStateStore[g] || z(g, e), r.getState().initialStateGlobal[g] || ge(g, e));
419
+ const o = $e(() => K(
420
+ g,
421
421
  n,
422
- p.current,
423
- A
424
- ), [f]);
425
- return [_e(f), o];
422
+ G.current,
423
+ N
424
+ ), [g]);
425
+ return [_e(g), o];
426
426
  }
427
- function K(e, s, m, u) {
428
- const g = /* @__PURE__ */ new Map();
429
- let w = 0;
430
- const I = (S) => {
427
+ function K(e, c, m, u) {
428
+ const f = /* @__PURE__ */ new Map();
429
+ let h = 0;
430
+ const v = (S) => {
431
431
  const t = S.join(".");
432
- for (const [v] of g)
433
- (v === t || v.startsWith(t + ".")) && g.delete(v);
434
- w++;
432
+ for (const [I] of f)
433
+ (I === t || I.startsWith(t + ".")) && f.delete(I);
434
+ h++;
435
435
  }, y = {
436
436
  removeValidation: (S) => {
437
437
  S?.validationKey && W(S.validationKey);
@@ -439,31 +439,31 @@ function K(e, s, m, u) {
439
439
  revertToInitialState: (S) => {
440
440
  const t = r.getState().getInitialOptions(e)?.validation;
441
441
  t?.key && W(t?.key), S?.validationKey && W(S.validationKey);
442
- const v = r.getState().initialStateGlobal[e];
443
- r.getState().clearSelectedIndexesForState(e), g.clear(), w++;
444
- const F = i(v, []), V = B(e), A = q(V?.localStorage?.key) ? V?.localStorage?.key(v) : V?.localStorage?.key, O = `${u}-${e}-${A}`;
445
- O && localStorage.removeItem(O), Q(e, F), z(e, v);
446
- const f = r.getState().stateComponents.get(e);
447
- return f && f.components.forEach((l) => {
442
+ const I = r.getState().initialStateGlobal[e];
443
+ r.getState().clearSelectedIndexesForState(e), f.clear(), h++;
444
+ const O = i(I, []), k = B(e), N = q(k?.localStorage?.key) ? k?.localStorage?.key(I) : k?.localStorage?.key, j = `${u}-${e}-${N}`;
445
+ j && localStorage.removeItem(j), Q(e, O), z(e, I);
446
+ const g = r.getState().stateComponents.get(e);
447
+ return g && g.components.forEach((l) => {
448
448
  l.forceUpdate();
449
- }), v;
449
+ }), I;
450
450
  },
451
451
  updateInitialState: (S) => {
452
- g.clear(), w++;
452
+ f.clear(), h++;
453
453
  const t = K(
454
454
  e,
455
- s,
455
+ c,
456
456
  m,
457
457
  u
458
- ), v = r.getState().initialStateGlobal[e], F = B(e), V = q(F?.localStorage?.key) ? F?.localStorage?.key(v) : F?.localStorage?.key, A = `${u}-${e}-${V}`;
459
- return console.log("removing storage", A), localStorage.getItem(A) && localStorage.removeItem(A), ke(() => {
458
+ ), I = r.getState().initialStateGlobal[e], O = B(e), k = q(O?.localStorage?.key) ? O?.localStorage?.key(I) : O?.localStorage?.key, N = `${u}-${e}-${k}`;
459
+ return console.log("removing storage", N), localStorage.getItem(N) && localStorage.removeItem(N), ke(() => {
460
460
  ge(e, S), Q(e, t), z(e, S);
461
- const O = r.getState().stateComponents.get(e);
462
- O && O.components.forEach((f) => {
463
- f.forceUpdate();
461
+ const j = r.getState().stateComponents.get(e);
462
+ j && j.components.forEach((g) => {
463
+ g.forceUpdate();
464
464
  });
465
465
  }), {
466
- fetchId: (O) => t.get()[O]
466
+ fetchId: (j) => t.get()[j]
467
467
  };
468
468
  },
469
469
  _initialState: r.getState().initialStateGlobal[e],
@@ -471,29 +471,29 @@ function K(e, s, m, u) {
471
471
  _isLoading: r.getState().isLoadingGlobal[e],
472
472
  _isServerSynced: () => {
473
473
  const S = r.getState().serverState[e];
474
- return !!(S && G(S, _e(e)));
474
+ return !!(S && M(S, _e(e)));
475
475
  }
476
476
  };
477
- function i(S, t = [], v) {
478
- const F = t.map(String).join(".");
479
- g.get(F);
480
- const V = function() {
477
+ function i(S, t = [], I) {
478
+ const O = t.map(String).join(".");
479
+ f.get(O);
480
+ const k = function() {
481
481
  return r().getNestedState(e, t);
482
482
  };
483
- Object.keys(y).forEach((f) => {
484
- V[f] = y[f];
483
+ Object.keys(y).forEach((g) => {
484
+ k[g] = y[g];
485
485
  });
486
- const A = {
487
- apply(f, l, J) {
486
+ const N = {
487
+ apply(g, l, J) {
488
488
  return console.log(
489
489
  `PROXY APPLY TRAP HIT: stateKey=${e}, path=${t.join(".")}`
490
490
  ), console.trace("Apply trap stack trace"), r().getNestedState(e, t);
491
491
  },
492
- get(f, l) {
492
+ get(g, l) {
493
493
  if (l !== "then" && !l.startsWith("$") && l !== "stateMapNoRender") {
494
- const n = t.join("."), o = `${e}////${m}`, c = r.getState().stateComponents.get(e);
495
- if (c) {
496
- const a = c.components.get(o);
494
+ const n = t.join("."), o = `${e}////${m}`, s = r.getState().stateComponents.get(e);
495
+ if (s) {
496
+ const a = s.components.get(o);
497
497
  a && (t.length > 0 || l === "get") && a.paths.add(n);
498
498
  }
499
499
  }
@@ -507,17 +507,17 @@ function K(e, s, m, u) {
507
507
  const n = r.getState().getInitialOptions(e), o = n?.sync;
508
508
  if (!o)
509
509
  return console.error(`No mutation defined for state key "${e}"`), { success: !1, error: "No mutation defined" };
510
- const c = r.getState().getNestedState(e, []), a = n?.validation?.key;
510
+ const s = r.getState().getNestedState(e, []), a = n?.validation?.key;
511
511
  try {
512
- const d = await o.action(c);
512
+ const d = await o.action(s);
513
513
  if (d && !d.success && d.errors && a) {
514
- r.getState().removeValidationError(a), d.errors.forEach((k) => {
515
- const _ = [a, ...k.path].join(".");
516
- r.getState().addValidationError(_, k.message);
514
+ r.getState().removeValidationError(a), d.errors.forEach((x) => {
515
+ const E = [a, ...x.path].join(".");
516
+ r.getState().addValidationError(E, x.message);
517
517
  });
518
- const E = r.getState().stateComponents.get(e);
519
- E && E.components.forEach((k) => {
520
- k.forceUpdate();
518
+ const w = r.getState().stateComponents.get(e);
519
+ w && w.components.forEach((x) => {
520
+ x.forceUpdate();
521
521
  });
522
522
  }
523
523
  return d?.success && o.onSuccess ? o.onSuccess(d.data) : !d?.success && o.onError && o.onError(d.error), d;
@@ -526,20 +526,20 @@ function K(e, s, m, u) {
526
526
  }
527
527
  };
528
528
  if (l === "_status") {
529
- const n = r.getState().getNestedState(e, t), o = r.getState().initialStateGlobal[e], c = D(o, t);
530
- return G(n, c) ? "fresh" : "stale";
529
+ const n = r.getState().getNestedState(e, t), o = r.getState().initialStateGlobal[e], s = U(o, t);
530
+ return M(n, s) ? "fresh" : "stale";
531
531
  }
532
532
  if (l === "getStatus")
533
533
  return function() {
534
534
  const n = r().getNestedState(
535
535
  e,
536
536
  t
537
- ), o = r.getState().initialStateGlobal[e], c = D(o, t);
538
- return G(n, c) ? "fresh" : "stale";
537
+ ), o = r.getState().initialStateGlobal[e], s = U(o, t);
538
+ return M(n, s) ? "fresh" : "stale";
539
539
  };
540
540
  if (l === "removeStorage")
541
541
  return () => {
542
- const n = r.getState().initialStateGlobal[e], o = B(e), c = q(o?.localStorage?.key) ? o?.localStorage?.key(n) : o?.localStorage?.key, a = `${u}-${e}-${c}`;
542
+ const n = r.getState().initialStateGlobal[e], o = B(e), s = q(o?.localStorage?.key) ? o?.localStorage?.key(n) : o?.localStorage?.key, a = `${u}-${e}-${s}`;
543
543
  console.log("removing storage", a), a && localStorage.removeItem(a);
544
544
  };
545
545
  if (l === "showValidationErrors")
@@ -557,7 +557,7 @@ function K(e, s, m, u) {
557
557
  return i(
558
558
  S[n],
559
559
  [...t, n.toString()],
560
- v
560
+ I
561
561
  );
562
562
  };
563
563
  if (l === "clearSelected")
@@ -568,39 +568,54 @@ function K(e, s, m, u) {
568
568
  return () => r.getState().getSelectedIndex(e, t.join(".")) ?? -1;
569
569
  if (l === "stateSort")
570
570
  return (n) => {
571
- const a = [...r.getState().getNestedState(e, t).map((d, E) => ({
571
+ const a = [...S.map((d, w) => ({
572
572
  ...d,
573
- __origIndex: E.toString()
573
+ // If it's already filtered, preserve its original index. Otherwise, use its current position.
574
+ __origIndex: d.__origIndex ?? w.toString()
574
575
  }))].sort(n);
575
- return g.clear(), w++, i(a, t, {
576
- filtered: [...v?.filtered || [], t],
576
+ return i(a, t, {
577
+ filtered: [...I?.filtered || [], t],
577
578
  validIndices: a.map(
578
579
  (d) => parseInt(d.__origIndex)
579
580
  )
580
581
  });
581
582
  };
582
- if (l === "stateMap" || l === "stateMapNoRender")
583
+ if (l === "stateFilter")
583
584
  return (n) => {
584
- const o = v?.filtered?.some(
585
- (a) => a.join(".") === t.join(".")
586
- ), c = o ? S : r.getState().getNestedState(e, t);
587
- return l !== "stateMapNoRender" && (g.clear(), w++), c.map((a, d) => {
588
- const E = o && a.__origIndex ? a.__origIndex : d, k = i(
589
- a,
590
- [...t, E.toString()],
591
- v
592
- );
593
- return n(
594
- a,
595
- k,
596
- d,
597
- S,
598
- i(S, t, v)
599
- );
585
+ const s = S.filter(
586
+ (a, d) => {
587
+ const w = {
588
+ ...a,
589
+ __origIndex: a.__origIndex ?? d.toString()
590
+ };
591
+ return n(w, d);
592
+ }
593
+ );
594
+ return i(s, t, {
595
+ filtered: [...I?.filtered || [], t],
596
+ validIndices: s.map(
597
+ (a) => parseInt(a.__origIndex)
598
+ )
600
599
  });
601
600
  };
601
+ if (l === "stateMap" || l === "stateMapNoRender")
602
+ return (n) => S.map((s, a) => {
603
+ const d = s.__origIndex ?? a, w = i(
604
+ s,
605
+ [...t, d.toString()],
606
+ I
607
+ // Pass meta through
608
+ );
609
+ return n(
610
+ s,
611
+ w,
612
+ a,
613
+ S,
614
+ i(S, t, I)
615
+ );
616
+ });
602
617
  if (l === "$stateMap")
603
- return (n) => ee(Me, {
618
+ return (n) => ee(De, {
604
619
  proxy: {
605
620
  _stateKey: e,
606
621
  _path: t,
@@ -611,25 +626,25 @@ function K(e, s, m, u) {
611
626
  });
612
627
  if (l === "stateFlattenOn")
613
628
  return (n) => {
614
- const c = v?.filtered?.some(
629
+ const s = I?.filtered?.some(
615
630
  (d) => d.join(".") === t.join(".")
616
631
  ) ? S : r.getState().getNestedState(e, t);
617
- g.clear(), w++;
618
- const a = c.flatMap(
619
- (d, E) => d[n] ?? []
632
+ f.clear(), h++;
633
+ const a = s.flatMap(
634
+ (d, w) => d[n] ?? []
620
635
  );
621
636
  return i(
622
637
  a,
623
638
  [...t, "[*]", n],
624
- v
639
+ I
625
640
  );
626
641
  };
627
642
  if (l === "findWith")
628
643
  return (n, o) => {
629
- const c = S.findIndex((E) => E[n] === o);
630
- if (c === -1) return;
631
- const a = S[c], d = [...t, c.toString()];
632
- return g.clear(), w++, i(a, d);
644
+ const s = S.findIndex((w) => w[n] === o);
645
+ if (s === -1) return;
646
+ const a = S[s], d = [...t, s.toString()];
647
+ return f.clear(), h++, i(a, d);
633
648
  };
634
649
  if (l === "index")
635
650
  return (n) => {
@@ -640,40 +655,40 @@ function K(e, s, m, u) {
640
655
  return () => {
641
656
  const n = r.getState().getNestedState(e, t);
642
657
  if (n.length === 0) return;
643
- const o = n.length - 1, c = n[o], a = [...t, o.toString()];
644
- return i(c, a);
658
+ const o = n.length - 1, s = n[o], a = [...t, o.toString()];
659
+ return i(s, a);
645
660
  };
646
661
  if (l === "insert")
647
- return (n) => (I(t), se(s, n, t, e), i(
662
+ return (n) => (v(t), se(c, n, t, e), i(
648
663
  r.getState().getNestedState(e, t),
649
664
  t
650
665
  ));
651
666
  if (l === "uniqueInsert")
652
- return (n, o, c) => {
667
+ return (n, o, s) => {
653
668
  const a = r.getState().getNestedState(e, t), d = q(n) ? n(a) : n;
654
- let E = null;
655
- if (!a.some((_) => {
669
+ let w = null;
670
+ if (!a.some((E) => {
656
671
  if (o) {
657
- const h = o.every(
658
- (L) => G(_[L], d[L])
672
+ const T = o.every(
673
+ (L) => M(E[L], d[L])
659
674
  );
660
- return h && (E = _), h;
675
+ return T && (w = E), T;
661
676
  }
662
- const b = G(_, d);
663
- return b && (E = _), b;
677
+ const b = M(E, d);
678
+ return b && (w = E), b;
664
679
  }))
665
- I(t), se(s, d, t, e);
666
- else if (c && E) {
667
- const _ = c(E), b = a.map(
668
- (h) => G(h, E) ? _ : h
680
+ v(t), se(c, d, t, e);
681
+ else if (s && w) {
682
+ const E = s(w), b = a.map(
683
+ (T) => M(T, w) ? E : T
669
684
  );
670
- I(t), Z(s, b, t);
685
+ v(t), Z(c, b, t);
671
686
  }
672
687
  };
673
688
  if (l === "cut")
674
689
  return (n, o) => {
675
690
  if (!o?.waitForSync)
676
- return I(t), Y(s, t, e, n), i(
691
+ return v(t), Y(c, t, e, n), i(
677
692
  r.getState().getNestedState(e, t),
678
693
  t
679
694
  );
@@ -681,41 +696,28 @@ function K(e, s, m, u) {
681
696
  if (l === "cutByValue")
682
697
  return (n) => {
683
698
  for (let o = 0; o < S.length; o++)
684
- S[o] === n && Y(s, t, e, o);
699
+ S[o] === n && Y(c, t, e, o);
685
700
  };
686
701
  if (l === "toggleByValue")
687
702
  return (n) => {
688
- const o = S.findIndex((c) => c === n);
689
- o > -1 ? Y(s, t, e, o) : se(s, n, t, e);
703
+ const o = S.findIndex((s) => s === n);
704
+ o > -1 ? Y(c, t, e, o) : se(c, n, t, e);
690
705
  };
691
706
  if (l === "stateFind")
692
707
  return (n) => {
693
- const o = r.getState().getNestedState(e, t), c = o.findIndex(n);
694
- if (c === -1)
708
+ const o = r.getState().getNestedState(e, t), s = o.findIndex(n);
709
+ if (s === -1)
695
710
  return;
696
- const a = o[c], d = [...t, c.toString()];
711
+ const a = o[s], d = [...t, s.toString()];
697
712
  return i(a, d);
698
713
  };
699
- if (l === "stateFilter")
700
- return (n) => {
701
- const c = r.getState().getNestedState(e, t).filter((a, d) => {
702
- const E = { ...a, __origIndex: d.toString() };
703
- return n(E, d);
704
- });
705
- return g.clear(), w++, i(c, t, {
706
- filtered: [...v?.filtered || [], t],
707
- validIndices: c.map(
708
- (a) => parseInt(a.__origIndex)
709
- )
710
- });
711
- };
712
714
  }
713
715
  const J = t[t.length - 1];
714
716
  if (!isNaN(Number(J))) {
715
717
  const n = t.slice(0, -1), o = r.getState().getNestedState(e, n);
716
718
  if (Array.isArray(o) && l === "cut")
717
719
  return () => Y(
718
- s,
720
+ c,
719
721
  n,
720
722
  e,
721
723
  Number(J)
@@ -747,26 +749,26 @@ function K(e, s, m, u) {
747
749
  if (l == "getLocalStorage")
748
750
  return (n) => te(u + "-" + e + "-" + n);
749
751
  if (l === "_selected") {
750
- const n = t.slice(0, -1), o = n.join("."), c = r.getState().getNestedState(e, n);
751
- return Array.isArray(c) ? Number(t[t.length - 1]) === r.getState().getSelectedIndex(e, o) : void 0;
752
+ const n = t.slice(0, -1), o = n.join("."), s = r.getState().getNestedState(e, n);
753
+ return Array.isArray(s) ? Number(t[t.length - 1]) === r.getState().getSelectedIndex(e, o) : void 0;
752
754
  }
753
755
  if (l === "setSelected")
754
756
  return (n) => {
755
- const o = t.slice(0, -1), c = Number(t[t.length - 1]), a = o.join(".");
756
- n ? r.getState().setSelectedIndex(e, a, c) : r.getState().setSelectedIndex(e, a, void 0);
757
+ const o = t.slice(0, -1), s = Number(t[t.length - 1]), a = o.join(".");
758
+ n ? r.getState().setSelectedIndex(e, a, s) : r.getState().setSelectedIndex(e, a, void 0);
757
759
  const d = r.getState().getNestedState(e, [...o]);
758
- Z(s, d, o), I(o);
760
+ Z(c, d, o), v(o);
759
761
  };
760
762
  if (l === "toggleSelected")
761
763
  return () => {
762
- const n = t.slice(0, -1), o = Number(t[t.length - 1]), c = n.join("."), a = r.getState().getSelectedIndex(e, c);
764
+ const n = t.slice(0, -1), o = Number(t[t.length - 1]), s = n.join("."), a = r.getState().getSelectedIndex(e, s);
763
765
  r.getState().setSelectedIndex(
764
766
  e,
765
- c,
767
+ s,
766
768
  a === o ? void 0 : o
767
769
  );
768
770
  const d = r.getState().getNestedState(e, [...n]);
769
- Z(s, d, n), I(n);
771
+ Z(c, d, n), v(n);
770
772
  };
771
773
  if (t.length == 0) {
772
774
  if (l === "validateZodSchema")
@@ -777,16 +779,16 @@ function K(e, s, m, u) {
777
779
  if (!n?.key)
778
780
  throw new Error("Validation key not found");
779
781
  W(n.key);
780
- const c = r.getState().cogsStateStore[e];
782
+ const s = r.getState().cogsStateStore[e];
781
783
  try {
782
784
  const a = r.getState().getValidationErrors(n.key);
783
- a && a.length > 0 && a.forEach(([E]) => {
784
- E && E.startsWith(n.key) && W(E);
785
+ a && a.length > 0 && a.forEach(([w]) => {
786
+ w && w.startsWith(n.key) && W(w);
785
787
  });
786
- const d = n.zodSchema.safeParse(c);
787
- return d.success ? !0 : (d.error.errors.forEach((k) => {
788
- const _ = k.path, b = k.message, h = [n.key, ..._].join(".");
789
- o(h, b);
788
+ const d = n.zodSchema.safeParse(s);
789
+ return d.success ? !0 : (d.error.errors.forEach((x) => {
790
+ const E = x.path, b = x.message, T = [n.key, ...E].join(".");
791
+ o(T, b);
790
792
  }), ne(e), !1);
791
793
  } catch (a) {
792
794
  return console.error("Zod schema validation failed", a), !1;
@@ -821,7 +823,7 @@ function K(e, s, m, u) {
821
823
  path: t,
822
824
  validationKey: r.getState().getInitialOptions(e)?.validation?.key || "",
823
825
  stateKey: e,
824
- validIndices: v?.validIndices,
826
+ validIndices: I?.validIndices,
825
827
  children: n
826
828
  }
827
829
  );
@@ -831,98 +833,98 @@ function K(e, s, m, u) {
831
833
  if (l === "update")
832
834
  return (n, o) => {
833
835
  if (o?.debounce)
834
- Ae(() => {
835
- Z(s, n, t, "");
836
- const c = r.getState().getNestedState(e, t);
837
- o?.afterUpdate && o.afterUpdate(c);
836
+ Ve(() => {
837
+ Z(c, n, t, "");
838
+ const s = r.getState().getNestedState(e, t);
839
+ o?.afterUpdate && o.afterUpdate(s);
838
840
  }, o.debounce);
839
841
  else {
840
- Z(s, n, t, "");
841
- const c = r.getState().getNestedState(e, t);
842
- o?.afterUpdate && o.afterUpdate(c);
842
+ Z(c, n, t, "");
843
+ const s = r.getState().getNestedState(e, t);
844
+ o?.afterUpdate && o.afterUpdate(s);
843
845
  }
844
- I(t);
846
+ v(t);
845
847
  };
846
848
  if (l === "formElement")
847
849
  return (n, o) => /* @__PURE__ */ Se(
848
- Te,
850
+ Ae,
849
851
  {
850
- setState: s,
852
+ setState: c,
851
853
  stateKey: e,
852
854
  path: t,
853
855
  child: n,
854
856
  formOpts: o
855
857
  }
856
858
  );
857
- const p = [...t, l], x = r.getState().getNestedState(e, p);
858
- return i(x, p, v);
859
+ const G = [...t, l], V = r.getState().getNestedState(e, G);
860
+ return i(V, G, I);
859
861
  }
860
- }, O = new Proxy(V, A);
861
- return g.set(F, {
862
- proxy: O,
863
- stateVersion: w
864
- }), O;
862
+ }, j = new Proxy(k, N);
863
+ return f.set(O, {
864
+ proxy: j,
865
+ stateVersion: h
866
+ }), j;
865
867
  }
866
868
  return i(
867
869
  r.getState().getNestedState(e, [])
868
870
  );
869
871
  }
870
872
  function ce(e) {
871
- return ee(Ge, { proxy: e });
873
+ return ee(Me, { proxy: e });
872
874
  }
873
- function Me({
875
+ function De({
874
876
  proxy: e,
875
- rebuildStateShape: s
877
+ rebuildStateShape: c
876
878
  }) {
877
879
  const m = r().getNestedState(e._stateKey, e._path);
878
- return Array.isArray(m) ? s(
880
+ return Array.isArray(m) ? c(
879
881
  m,
880
882
  e._path
881
883
  ).stateMapNoRender(
882
- (g, w, I, y, i) => e._mapFn(g, w, I, y, i)
884
+ (f, h, v, y, i) => e._mapFn(f, h, v, y, i)
883
885
  ) : null;
884
886
  }
885
- function Ge({
887
+ function Me({
886
888
  proxy: e
887
889
  }) {
888
- const s = X(null), m = `${e._stateKey}-${e._path.join(".")}`;
890
+ const c = X(null), m = `${e._stateKey}-${e._path.join(".")}`;
889
891
  return de(() => {
890
- const u = s.current;
892
+ const u = c.current;
891
893
  if (!u || !u.parentElement) return;
892
- const g = u.parentElement, I = Array.from(g.childNodes).indexOf(u);
893
- let y = g.getAttribute("data-parent-id");
894
- y || (y = `parent-${crypto.randomUUID()}`, g.setAttribute("data-parent-id", y));
894
+ const f = u.parentElement, v = Array.from(f.childNodes).indexOf(u);
895
+ let y = f.getAttribute("data-parent-id");
896
+ y || (y = `parent-${crypto.randomUUID()}`, f.setAttribute("data-parent-id", y));
895
897
  const S = {
896
898
  instanceId: `instance-${crypto.randomUUID()}`,
897
899
  parentId: y,
898
- position: I,
900
+ position: v,
899
901
  effect: e._effect
900
902
  };
901
903
  r.getState().addSignalElement(m, S);
902
904
  const t = r.getState().getNestedState(e._stateKey, e._path);
903
- let v;
905
+ let I;
904
906
  if (e._effect)
905
907
  try {
906
- v = new Function(
908
+ I = new Function(
907
909
  "state",
908
910
  `return (${e._effect})(state)`
909
911
  )(t);
910
- } catch (V) {
911
- console.error("Error evaluating effect function during mount:", V), v = t;
912
+ } catch (k) {
913
+ console.error("Error evaluating effect function during mount:", k), I = t;
912
914
  }
913
915
  else
914
- v = t;
915
- v !== null && typeof v == "object" && (v = JSON.stringify(v));
916
- const F = document.createTextNode(String(v));
917
- u.replaceWith(F);
916
+ I = t;
917
+ I !== null && typeof I == "object" && (I = JSON.stringify(I));
918
+ const O = document.createTextNode(String(I));
919
+ u.replaceWith(O);
918
920
  }, [e._stateKey, e._path.join("."), e._effect]), ee("span", {
919
- ref: s,
921
+ ref: c,
920
922
  style: { display: "none" },
921
923
  "data-signal-id": m
922
924
  });
923
925
  }
924
926
  function Ke(e) {
925
- const s = he(
927
+ const c = Te(
926
928
  (m) => {
927
929
  const u = r.getState().stateComponents.get(e._stateKey) || {
928
930
  components: /* @__PURE__ */ new Map()
@@ -934,7 +936,7 @@ function Ke(e) {
934
936
  },
935
937
  () => r.getState().getNestedState(e._stateKey, e._path)
936
938
  );
937
- return ee("text", {}, String(s));
939
+ return ee("text", {}, String(c));
938
940
  }
939
941
  export {
940
942
  ce as $cogsSignal,
@@ -942,6 +944,6 @@ export {
942
944
  Ye as addStateOptions,
943
945
  Xe as createCogsState,
944
946
  Qe as notifyComponent,
945
- De as useCogsStateFn
947
+ Ue as useCogsStateFn
946
948
  };
947
949
  //# sourceMappingURL=CogsState.jsx.map