cogsbox-state 0.5.69 → 0.5.71

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,42 +1,42 @@
1
1
  "use client";
2
2
  import { jsx as ct } from "react/jsx-runtime";
3
- import { useState as et, useRef as J, useEffect as nt, useLayoutEffect as It, useMemo as _t, createElement as Y, useSyncExternalStore as Et, startTransition as Z } from "react";
4
- import { transformStateFunc as wt, isFunction as D, getNestedValue as U, isDeepEqual as R, debounce as Nt } from "./utility.js";
5
- import { pushFunc as K, updateFn as z, cutFunc as B, ValidationWrapper as Vt, FormControlComponent as $t } from "./Functions.jsx";
3
+ import { useState as et, useRef as Z, useEffect as nt, useLayoutEffect as It, useMemo as _t, createElement as Q, useSyncExternalStore as Et, startTransition as H } from "react";
4
+ import { transformStateFunc as wt, isFunction as G, getNestedValue as U, isDeepEqual as M, debounce as Nt } from "./utility.js";
5
+ import { pushFunc as K, updateFn as B, cutFunc as J, ValidationWrapper as Vt, FormControlComponent as $t } from "./Functions.jsx";
6
6
  import "zod";
7
7
  import { getGlobalStore as r, formRefStore as lt } from "./store.js";
8
8
  import { useCogsConfig as At } from "./CogsStateClient.jsx";
9
9
  import rt from "./node_modules/uuid/dist/esm-browser/v4.js";
10
- function dt(t, a) {
10
+ function dt(t, i) {
11
11
  const S = r.getState().getInitialOptions, y = r.getState().setInitialStateOptions, g = S(t) || {};
12
12
  y(t, {
13
13
  ...g,
14
- ...a
14
+ ...i
15
15
  });
16
16
  }
17
17
  function ut({
18
18
  stateKey: t,
19
- options: a,
19
+ options: i,
20
20
  initialOptionsPart: S
21
21
  }) {
22
- const y = Q(t) || {}, g = S[t] || {}, w = r.getState().setInitialStateOptions, _ = { ...g, ...y };
22
+ const y = q(t) || {}, g = S[t] || {}, w = r.getState().setInitialStateOptions, _ = { ...g, ...y };
23
23
  let v = !1;
24
- if (a)
25
- for (const l in a)
26
- _.hasOwnProperty(l) ? l == "localStorage" && a[l] && _[l].key !== a[l]?.key && (v = !0, _[l] = a[l]) : (v = !0, _[l] = a[l]);
24
+ if (i)
25
+ for (const l in i)
26
+ _.hasOwnProperty(l) ? l == "localStorage" && i[l] && _[l].key !== i[l]?.key && (v = !0, _[l] = i[l]) : (v = !0, _[l] = i[l]);
27
27
  v && w(t, _);
28
28
  }
29
- function qt(t, { formElements: a, validation: S }) {
30
- return { initialState: t, formElements: a, validation: S };
29
+ function qt(t, { formElements: i, validation: S }) {
30
+ return { initialState: t, formElements: i, validation: S };
31
31
  }
32
- const zt = (t, a) => {
32
+ const zt = (t, i) => {
33
33
  let S = t;
34
34
  const [y, g] = wt(S);
35
- (a?.formElements || a?.validation) && Object.keys(g).forEach((v) => {
35
+ (i?.formElements || i?.validation) && Object.keys(g).forEach((v) => {
36
36
  g[v] = g[v] || {}, g[v].formElements = {
37
- ...a.formElements,
37
+ ...i.formElements,
38
38
  // Global defaults first
39
- ...a?.validation,
39
+ ...i?.validation,
40
40
  ...g[v].formElements || {}
41
41
  // State-specific overrides
42
42
  };
@@ -48,7 +48,7 @@ const zt = (t, a) => {
48
48
  options: l,
49
49
  initialOptionsPart: g
50
50
  });
51
- const e = r.getState().cogsStateStore[v] || y[v], m = l?.modifyState ? l.modifyState(e) : e, [T, V] = bt(
51
+ const e = r.getState().cogsStateStore[v] || y[v], m = l?.modifyState ? l.modifyState(e) : e, [h, V] = Ft(
52
52
  m,
53
53
  {
54
54
  stateKey: v,
@@ -70,44 +70,44 @@ const zt = (t, a) => {
70
70
  }
71
71
  return { useCogsState: w, setCogsOptions: _ };
72
72
  }, {
73
- setUpdaterState: H,
74
- setState: G,
75
- getInitialOptions: Q,
73
+ setUpdaterState: Y,
74
+ setState: L,
75
+ getInitialOptions: q,
76
76
  getKeyState: gt,
77
77
  getValidationErrors: kt,
78
78
  setStateLog: pt,
79
79
  updateInitialStateGlobal: ot,
80
80
  addValidationError: Ct,
81
- removeValidationError: P,
82
- setServerSyncActions: ht
81
+ removeValidationError: R,
82
+ setServerSyncActions: Tt
83
83
  } = r.getState(), ft = (t) => {
84
84
  if (!t) return null;
85
85
  try {
86
- const a = window.localStorage.getItem(t);
87
- return a ? JSON.parse(a) : null;
88
- } catch (a) {
89
- return console.error("Error loading from localStorage:", a), null;
86
+ const i = window.localStorage.getItem(t);
87
+ return i ? JSON.parse(i) : null;
88
+ } catch (i) {
89
+ return console.error("Error loading from localStorage:", i), null;
90
90
  }
91
- }, Tt = (t, a, S, y) => {
91
+ }, ht = (t, i, S, y) => {
92
92
  S.log && console.log(
93
93
  "saving to localstorage",
94
- a,
94
+ i,
95
95
  S.localStorage?.key,
96
96
  y
97
97
  );
98
- const g = D(S.localStorage?.key) ? S.localStorage?.key(t) : S.localStorage?.key;
98
+ const g = G(S.localStorage?.key) ? S.localStorage?.key(t) : S.localStorage?.key;
99
99
  if (g && y) {
100
100
  const w = {
101
101
  state: t,
102
102
  lastUpdated: Date.now(),
103
- lastSyncedWithServer: r.getState().serverSyncLog[a]?.[0]?.timeStamp,
104
- baseServerState: r.getState().serverState[a]
105
- }, _ = `${y}-${a}-${g}`;
103
+ lastSyncedWithServer: r.getState().serverSyncLog[i]?.[0]?.timeStamp,
104
+ baseServerState: r.getState().serverState[i]
105
+ }, _ = `${y}-${i}-${g}`;
106
106
  window.localStorage.setItem(_, JSON.stringify(w));
107
107
  }
108
- }, Ft = (t, a, S, y, g, w) => {
108
+ }, bt = (t, i, S, y, g, w) => {
109
109
  const _ = {
110
- initialState: a,
110
+ initialState: i,
111
111
  updaterState: X(
112
112
  t,
113
113
  y,
@@ -116,29 +116,29 @@ const zt = (t, a) => {
116
116
  ),
117
117
  state: S
118
118
  };
119
- Z(() => {
120
- ot(t, _.initialState), H(t, _.updaterState), G(t, _.state);
119
+ H(() => {
120
+ ot(t, _.initialState), Y(t, _.updaterState), L(t, _.state);
121
121
  });
122
122
  }, St = (t) => {
123
- const a = r.getState().stateComponents.get(t);
124
- if (!a) return;
123
+ const i = r.getState().stateComponents.get(t);
124
+ if (!i) return;
125
125
  const S = /* @__PURE__ */ new Set();
126
- a.components.forEach((y) => {
126
+ i.components.forEach((y) => {
127
127
  S.add(() => y.forceUpdate());
128
128
  }), queueMicrotask(() => {
129
- Z(() => {
129
+ H(() => {
130
130
  S.forEach((y) => y());
131
131
  });
132
132
  });
133
- }, Bt = (t, a) => {
133
+ }, Bt = (t, i) => {
134
134
  const S = r.getState().stateComponents.get(t);
135
135
  if (S) {
136
- const y = `${t}////${a}`, g = S.components.get(y);
136
+ const y = `${t}////${i}`, g = S.components.get(y);
137
137
  g && g.forceUpdate();
138
138
  }
139
139
  };
140
- function bt(t, {
141
- stateKey: a,
140
+ function Ft(t, {
141
+ stateKey: i,
142
142
  serverSync: S,
143
143
  localStorage: y,
144
144
  formElements: g,
@@ -150,12 +150,12 @@ function bt(t, {
150
150
  syncUpdate: e,
151
151
  dependencies: m
152
152
  } = {}) {
153
- const [T, V] = et({}), { sessionId: A } = At();
154
- let j = !a;
155
- const [u] = et(a ?? rt()), s = r.getState().stateLog[u], L = J(/* @__PURE__ */ new Set()), O = J(l ?? rt()), C = J(null);
156
- C.current = Q(u), nt(() => {
153
+ const [h, V] = et({}), { sessionId: A } = At();
154
+ let j = !i;
155
+ const [u] = et(i ?? rt()), s = r.getState().stateLog[u], D = Z(/* @__PURE__ */ new Set()), x = Z(l ?? rt()), C = Z(null);
156
+ C.current = q(u), nt(() => {
157
157
  if (e && e.stateKey === u && e.path?.[0]) {
158
- G(u, (o) => ({
158
+ L(u, (o) => ({
159
159
  ...o,
160
160
  [e.path[0]]: e.newValue
161
161
  }));
@@ -172,17 +172,17 @@ function bt(t, {
172
172
  const c = C.current;
173
173
  let o = null;
174
174
  c.log && console.log("newoptions", c);
175
- const d = D(c.localStorage?.key) ? c.localStorage?.key(f) : c.localStorage?.key;
175
+ const d = G(c.localStorage?.key) ? c.localStorage?.key(f) : c.localStorage?.key;
176
176
  d && A && (o = ft(
177
177
  A + "-" + u + "-" + d
178
178
  ));
179
179
  let E = null;
180
- f && (E = f, o && o.lastUpdated > (o.lastSyncedWithServer || 0) && (E = o.state), Ft(
180
+ f && (E = f, o && o.lastUpdated > (o.lastSyncedWithServer || 0) && (E = o.state), bt(
181
181
  u,
182
182
  f,
183
183
  E,
184
184
  n,
185
- O.current,
185
+ x.current,
186
186
  A
187
187
  ), St(u), V({}));
188
188
  }, [f, ...m || []]), It(() => {
@@ -193,7 +193,7 @@ function bt(t, {
193
193
  localStorage: y,
194
194
  middleware: w
195
195
  });
196
- const c = `${u}////${O.current}`, o = r.getState().stateComponents.get(u) || {
196
+ const c = `${u}////${x.current}`, o = r.getState().stateComponents.get(u) || {
197
197
  components: /* @__PURE__ */ new Map()
198
198
  };
199
199
  return o.components.set(c, {
@@ -203,35 +203,35 @@ function bt(t, {
203
203
  depsFunction: _ || void 0,
204
204
  reactiveType: v ?? ["component", "deps"]
205
205
  }), r.getState().stateComponents.set(u, o), V({}), () => {
206
- const d = `${u}////${O.current}`;
206
+ const d = `${u}////${x.current}`;
207
207
  o && (o.components.delete(d), o.components.size === 0 && r.getState().stateComponents.delete(u));
208
208
  };
209
209
  }, []);
210
210
  const n = (c, o, d, E) => {
211
211
  if (Array.isArray(o)) {
212
- const F = `${u}-${o.join(".")}`;
213
- L.current.add(F);
212
+ const b = `${u}-${o.join(".")}`;
213
+ D.current.add(b);
214
214
  }
215
- G(u, (F) => {
216
- const $ = D(c) ? c(F) : c, x = `${u}-${o.join(".")}`;
217
- if (x) {
218
- let M = !1, I = r.getState().signalDomElements.get(x);
215
+ L(u, (b) => {
216
+ const $ = G(c) ? c(b) : c, O = `${u}-${o.join(".")}`;
217
+ if (O) {
218
+ let P = !1, I = r.getState().signalDomElements.get(O);
219
219
  if ((!I || I.size === 0) && (d.updateType === "insert" || d.updateType === "cut")) {
220
220
  const k = o.slice(0, -1), p = U($, k);
221
221
  if (Array.isArray(p)) {
222
- M = !0;
222
+ P = !0;
223
223
  const N = `${u}-${k.join(".")}`;
224
224
  I = r.getState().signalDomElements.get(N);
225
225
  }
226
226
  }
227
227
  if (I) {
228
- const k = M ? U($, o.slice(0, -1)) : U($, o);
228
+ const k = P ? U($, o.slice(0, -1)) : U($, o);
229
229
  I.forEach(({ parentId: p, position: N, effect: W }) => {
230
- const b = document.querySelector(
230
+ const F = document.querySelector(
231
231
  `[data-parent-id="${p}"]`
232
232
  );
233
- if (b) {
234
- const st = Array.from(b.childNodes);
233
+ if (F) {
234
+ const st = Array.from(F.childNodes);
235
235
  if (st[N]) {
236
236
  const vt = W ? new Function("state", `return (${W})(state)`)(k) : k;
237
237
  st[N].textContent = String(vt);
@@ -240,24 +240,24 @@ function bt(t, {
240
240
  });
241
241
  }
242
242
  }
243
- d.updateType === "update" && (E || C.current?.validationKey) && o && P(
243
+ d.updateType === "update" && (E || C.current?.validationKey) && o && R(
244
244
  (E || C.current?.validationKey) + "." + o.join(".")
245
245
  );
246
- const h = o.slice(0, o.length - 1);
247
- d.updateType === "cut" && C.current?.validationKey && P(
248
- C.current?.validationKey + "." + h.join(".")
246
+ const T = o.slice(0, o.length - 1);
247
+ d.updateType === "cut" && C.current?.validationKey && R(
248
+ C.current?.validationKey + "." + T.join(".")
249
249
  ), d.updateType === "insert" && C.current?.validationKey && kt(
250
- C.current?.validationKey + "." + h.join(".")
250
+ C.current?.validationKey + "." + T.join(".")
251
251
  ).filter(([I, k]) => {
252
252
  let p = I?.split(".").length;
253
- if (I == h.join(".") && p == h.length - 1) {
254
- let N = I + "." + h;
255
- P(I), Ct(N, k);
253
+ if (I == T.join(".") && p == T.length - 1) {
254
+ let N = I + "." + T;
255
+ R(I), Ct(N, k);
256
256
  }
257
257
  });
258
- const q = U(F, o), mt = U($, o), yt = d.updateType === "update" ? o.join(".") : [...o].slice(0, -1).join("."), at = r.getState().stateComponents.get(u);
258
+ const z = U(b, o), mt = U($, o), yt = d.updateType === "update" ? o.join(".") : [...o].slice(0, -1).join("."), at = r.getState().stateComponents.get(u);
259
259
  if (at)
260
- for (const [M, I] of at.components.entries()) {
260
+ for (const [P, I] of at.components.entries()) {
261
261
  let k = !1;
262
262
  const p = Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"];
263
263
  if (!p.includes("none")) {
@@ -267,7 +267,7 @@ function bt(t, {
267
267
  }
268
268
  if (p.includes("component") && I.paths && (I.paths.has(yt) || I.paths.has("")) && (k = !0), !k && p.includes("deps") && I.depsFunction) {
269
269
  const N = I.depsFunction($);
270
- typeof N == "boolean" ? N && (k = !0) : R(I.deps, N) || (I.deps = N, k = !0);
270
+ typeof N == "boolean" ? N && (k = !0) : M(I.deps, N) || (I.deps = N, k = !0);
271
271
  }
272
272
  k && I.forceUpdate();
273
273
  }
@@ -278,16 +278,16 @@ function bt(t, {
278
278
  path: o,
279
279
  updateType: d.updateType,
280
280
  status: "new",
281
- oldValue: q,
281
+ oldValue: z,
282
282
  newValue: mt
283
283
  };
284
- if (pt(u, (M) => {
285
- const k = [...M ?? [], it].reduce((p, N) => {
286
- const W = `${N.stateKey}:${JSON.stringify(N.path)}`, b = p.get(W);
287
- return b ? (b.timeStamp = Math.max(b.timeStamp, N.timeStamp), b.newValue = N.newValue, b.oldValue = b.oldValue ?? N.oldValue, b.updateType = N.updateType) : p.set(W, { ...N }), p;
284
+ if (pt(u, (P) => {
285
+ const k = [...P ?? [], it].reduce((p, N) => {
286
+ const W = `${N.stateKey}:${JSON.stringify(N.path)}`, F = p.get(W);
287
+ return F ? (F.timeStamp = Math.max(F.timeStamp, N.timeStamp), F.newValue = N.newValue, F.oldValue = F.oldValue ?? N.oldValue, F.updateType = N.updateType) : p.set(W, { ...N }), p;
288
288
  }, /* @__PURE__ */ new Map());
289
289
  return Array.from(k.values());
290
- }), Tt(
290
+ }), ht(
291
291
  $,
292
292
  u,
293
293
  C.current,
@@ -296,10 +296,10 @@ function bt(t, {
296
296
  updateLog: s,
297
297
  update: it
298
298
  }), C.current?.serverSync) {
299
- const M = r.getState().serverState[u], I = C.current?.serverSync;
300
- ht(u, {
299
+ const P = r.getState().serverState[u], I = C.current?.serverSync;
300
+ Tt(u, {
301
301
  syncKey: typeof I.syncKey == "string" ? I.syncKey : I.syncKey({ state: $ }),
302
- rollBackState: M,
302
+ rollBackState: P,
303
303
  actionTimeStamp: Date.now() + (I.debounce ?? 3e3),
304
304
  status: "waiting"
305
305
  });
@@ -307,24 +307,24 @@ function bt(t, {
307
307
  return $;
308
308
  });
309
309
  };
310
- r.getState().updaterState[u] || (H(
310
+ r.getState().updaterState[u] || (Y(
311
311
  u,
312
312
  X(
313
313
  u,
314
314
  n,
315
- O.current,
315
+ x.current,
316
316
  A
317
317
  )
318
- ), r.getState().cogsStateStore[u] || G(u, t), r.getState().initialStateGlobal[u] || ot(u, t));
319
- const i = _t(() => X(
318
+ ), r.getState().cogsStateStore[u] || L(u, t), r.getState().initialStateGlobal[u] || ot(u, t));
319
+ const a = _t(() => X(
320
320
  u,
321
321
  n,
322
- O.current,
322
+ x.current,
323
323
  A
324
324
  ), [u]);
325
- return [gt(u), i];
325
+ return [gt(u), a];
326
326
  }
327
- function X(t, a, S, y) {
327
+ function X(t, i, S, y) {
328
328
  const g = /* @__PURE__ */ new Map();
329
329
  let w = 0;
330
330
  const _ = (f) => {
@@ -334,16 +334,16 @@ function X(t, a, S, y) {
334
334
  w++;
335
335
  }, v = {
336
336
  removeValidation: (f) => {
337
- f?.validationKey && P(f.validationKey);
337
+ f?.validationKey && R(f.validationKey);
338
338
  },
339
339
  revertToInitialState: (f) => {
340
340
  const e = r.getState().getInitialOptions(t)?.validation;
341
- e?.key && P(e?.key), f?.validationKey && P(f.validationKey);
341
+ e?.key && R(e?.key), f?.validationKey && R(f.validationKey);
342
342
  const m = r.getState().initialStateGlobal[t];
343
343
  g.clear(), w++;
344
- const T = l(m, []), V = Q(t), A = D(V?.localStorage?.key) ? V?.localStorage?.key(m) : V?.localStorage?.key;
345
- return A && localStorage.removeItem(A), Z(() => {
346
- H(t, T), G(t, m);
344
+ const h = l(m, []), V = q(t), A = G(V?.localStorage?.key) ? V?.localStorage?.key(m) : V?.localStorage?.key;
345
+ return A && localStorage.removeItem(A), H(() => {
346
+ Y(t, h), L(t, m);
347
347
  const j = r.getState().stateComponents.get(t);
348
348
  j && j.components.forEach((u) => {
349
349
  u.forceUpdate();
@@ -352,16 +352,16 @@ function X(t, a, S, y) {
352
352
  },
353
353
  updateInitialState: (f) => {
354
354
  g.clear(), w++;
355
- const e = r.getState().initialStateGlobal[t], m = Q(t), T = D(m?.localStorage?.key) ? m?.localStorage?.key(e) : m?.localStorage?.key;
356
- T && localStorage.removeItem(T);
355
+ const e = r.getState().initialStateGlobal[t], m = q(t), h = G(m?.localStorage?.key) ? m?.localStorage?.key(e) : m?.localStorage?.key;
356
+ h && localStorage.removeItem(h);
357
357
  const V = X(
358
358
  t,
359
- a,
359
+ i,
360
360
  S,
361
361
  y
362
362
  );
363
- return Z(() => {
364
- ot(t, f), H(t, V), G(t, f);
363
+ return H(() => {
364
+ ot(t, f), Y(t, V), L(t, f);
365
365
  const A = r.getState().stateComponents.get(t);
366
366
  A && A.components.forEach((j) => {
367
367
  j.forceUpdate();
@@ -375,12 +375,12 @@ function X(t, a, S, y) {
375
375
  _isLoading: r.getState().isLoadingGlobal[t],
376
376
  _isServerSynced: () => {
377
377
  const f = r.getState().serverState[t];
378
- return !!(f && R(f, gt(t)));
378
+ return !!(f && M(f, gt(t)));
379
379
  }
380
380
  };
381
381
  function l(f, e = [], m) {
382
- const T = e.map(String).join(".");
383
- g.get(T);
382
+ const h = e.map(String).join(".");
383
+ g.get(h);
384
384
  const V = function() {
385
385
  return r().getNestedState(t, e);
386
386
  };
@@ -388,28 +388,35 @@ function X(t, a, S, y) {
388
388
  V[u] = v[u];
389
389
  });
390
390
  const A = {
391
- apply(u, s, L) {
392
- return r().getNestedState(t, e);
391
+ apply(u, s, D) {
392
+ return console.log(
393
+ `PROXY APPLY TRAP HIT: stateKey=${t}, path=${e.join(".")}`
394
+ ), console.trace("Apply trap stack trace"), r().getNestedState(t, e);
393
395
  },
394
396
  get(u, s) {
395
397
  if (s !== "then" && !s.startsWith("$") && s !== "stateMapNoRender") {
396
- const n = e.join("."), i = `${t}////${S}`, c = r.getState().stateComponents.get(t);
398
+ const n = e.join("."), a = `${t}////${S}`, c = r.getState().stateComponents.get(t);
397
399
  if (c) {
398
- const o = c.components.get(i);
400
+ const o = c.components.get(a);
399
401
  o && (e.length > 0 || s === "get") && o.paths.add(n);
400
402
  }
401
403
  }
402
404
  if (s === "_status") {
403
- const n = r.getState().getNestedState(t, e), i = r.getState().initialStateGlobal[t], c = U(i, e);
404
- return R(n, c) ? "fresh" : "stale";
405
+ const n = r.getState().getNestedState(t, e), a = r.getState().initialStateGlobal[t], c = U(a, e);
406
+ return M(n, c) ? "fresh" : "stale";
405
407
  }
406
408
  if (s === "getStatus")
407
409
  return function() {
408
410
  const n = r().getNestedState(
409
411
  t,
410
412
  e
411
- ), i = r.getState().initialStateGlobal[t], c = U(i, e);
412
- return R(n, c) ? "fresh" : "stale";
413
+ ), a = r.getState().initialStateGlobal[t], c = U(a, e);
414
+ return M(n, c) ? "fresh" : "stale";
415
+ };
416
+ if (s === "removeStorage")
417
+ return () => {
418
+ const n = r.getState().initialStateGlobal[t], a = q(t), c = G(a?.localStorage?.key) ? a?.localStorage?.key(n) : a?.localStorage?.key;
419
+ c && localStorage.removeItem(c);
413
420
  };
414
421
  if (s === "showValidationErrors")
415
422
  return () => {
@@ -444,18 +451,18 @@ function X(t, a, S, y) {
444
451
  };
445
452
  if (s === "stateMap" || s === "stateMapNoRender")
446
453
  return (n) => {
447
- const i = m?.filtered?.some(
454
+ const a = m?.filtered?.some(
448
455
  (o) => o.join(".") === e.join(".")
449
- ), c = i ? f : r.getState().getNestedState(t, e);
456
+ ), c = a ? f : r.getState().getNestedState(t, e);
450
457
  return s !== "stateMapNoRender" && (g.clear(), w++), c.map((o, d) => {
451
- const E = i && o.__origIndex ? o.__origIndex : d, F = l(
458
+ const E = a && o.__origIndex ? o.__origIndex : d, b = l(
452
459
  o,
453
460
  [...e, E.toString()],
454
461
  m
455
462
  );
456
463
  return n(
457
464
  o,
458
- F,
465
+ b,
459
466
  d,
460
467
  f,
461
468
  l(f, e, m)
@@ -463,7 +470,7 @@ function X(t, a, S, y) {
463
470
  });
464
471
  };
465
472
  if (s === "$stateMap")
466
- return (n) => Y(jt, {
473
+ return (n) => Q(jt, {
467
474
  proxy: {
468
475
  _stateKey: t,
469
476
  _path: e,
@@ -488,9 +495,9 @@ function X(t, a, S, y) {
488
495
  );
489
496
  };
490
497
  if (s === "findWith")
491
- return (n, i) => {
498
+ return (n, a) => {
492
499
  const c = f.findIndex(
493
- (E) => E[n] === i
500
+ (E) => E[n] === a
494
501
  );
495
502
  if (c === -1) return;
496
503
  const o = f[c], d = [...e, c.toString()];
@@ -498,58 +505,58 @@ function X(t, a, S, y) {
498
505
  };
499
506
  if (s === "index")
500
507
  return (n) => {
501
- const i = f[n];
502
- return l(i, [...e, n.toString()]);
508
+ const a = f[n];
509
+ return l(a, [...e, n.toString()]);
503
510
  };
504
511
  if (s === "insert")
505
- return (n) => (_(e), K(a, n, e, t), l(
512
+ return (n) => (_(e), K(i, n, e, t), l(
506
513
  r.getState().cogsStateStore[t],
507
514
  []
508
515
  ));
509
516
  if (s === "uniqueInsert")
510
- return (n, i, c) => {
511
- const o = r.getState().getNestedState(t, e), d = D(n) ? n(o) : n;
517
+ return (n, a, c) => {
518
+ const o = r.getState().getNestedState(t, e), d = G(n) ? n(o) : n;
512
519
  let E = null;
513
520
  if (!o.some(($) => {
514
- if (i) {
515
- const h = i.every(
516
- (q) => R($[q], d[q])
521
+ if (a) {
522
+ const T = a.every(
523
+ (z) => M($[z], d[z])
517
524
  );
518
- return h && (E = $), h;
525
+ return T && (E = $), T;
519
526
  }
520
- const x = R($, d);
521
- return x && (E = $), x;
527
+ const O = M($, d);
528
+ return O && (E = $), O;
522
529
  }))
523
- _(e), K(a, d, e, t);
530
+ _(e), K(i, d, e, t);
524
531
  else if (c && E) {
525
- const $ = c(E), x = o.map(
526
- (h) => R(h, E) ? $ : h
532
+ const $ = c(E), O = o.map(
533
+ (T) => M(T, E) ? $ : T
527
534
  );
528
- _(e), z(a, x, e);
535
+ _(e), B(i, O, e);
529
536
  }
530
537
  };
531
538
  if (s === "cut")
532
- return (n, i) => {
533
- i?.waitForSync || (_(e), B(a, e, t, n));
539
+ return (n, a) => {
540
+ a?.waitForSync || (_(e), J(i, e, t, n));
534
541
  };
535
542
  if (s === "cutByValue")
536
543
  return (n) => {
537
- for (let i = 0; i < f.length; i++)
538
- f[i] === n && B(a, e, t, i);
544
+ for (let a = 0; a < f.length; a++)
545
+ f[a] === n && J(i, e, t, a);
539
546
  };
540
547
  if (s === "toggleByValue")
541
548
  return (n) => {
542
- const i = f.findIndex((c) => c === n);
543
- i > -1 ? B(a, e, t, i) : K(a, n, e, t);
549
+ const a = f.findIndex((c) => c === n);
550
+ a > -1 ? J(i, e, t, a) : K(i, n, e, t);
544
551
  };
545
552
  if (s === "stateFilter")
546
553
  return (n) => {
547
- const i = f.map((d, E) => ({
554
+ const a = f.map((d, E) => ({
548
555
  ...d,
549
556
  __origIndex: E.toString()
550
557
  })), c = [], o = [];
551
- for (let d = 0; d < i.length; d++)
552
- n(i[d], d) && (c.push(d), o.push(i[d]));
558
+ for (let d = 0; d < a.length; d++)
559
+ n(a[d], d) && (c.push(d), o.push(a[d]));
553
560
  return g.clear(), w++, l(o, e, {
554
561
  filtered: [...m?.filtered || [], e],
555
562
  validIndices: c
@@ -557,15 +564,15 @@ function X(t, a, S, y) {
557
564
  });
558
565
  };
559
566
  }
560
- const L = e[e.length - 1];
561
- if (!isNaN(Number(L))) {
562
- const n = e.slice(0, -1), i = r.getState().getNestedState(t, n);
563
- if (Array.isArray(i) && s === "cut")
564
- return () => B(
565
- a,
567
+ const D = e[e.length - 1];
568
+ if (!isNaN(Number(D))) {
569
+ const n = e.slice(0, -1), a = r.getState().getNestedState(t, n);
570
+ if (Array.isArray(a) && s === "cut")
571
+ return () => J(
572
+ i,
566
573
  n,
567
574
  t,
568
- Number(L)
575
+ Number(D)
569
576
  );
570
577
  }
571
578
  if (s === "get")
@@ -594,35 +601,35 @@ function X(t, a, S, y) {
594
601
  if (s == "getLocalStorage")
595
602
  return (n) => ft(y + "-" + t + "-" + n);
596
603
  if (s === "_selected") {
597
- const n = e.slice(0, -1), i = n.join("."), c = r.getState().getNestedState(t, n);
598
- return Array.isArray(c) ? Number(e[e.length - 1]) === r.getState().getSelectedIndex(t, i) : void 0;
604
+ const n = e.slice(0, -1), a = n.join("."), c = r.getState().getNestedState(t, n);
605
+ return Array.isArray(c) ? Number(e[e.length - 1]) === r.getState().getSelectedIndex(t, a) : void 0;
599
606
  }
600
607
  if (s === "setSelected")
601
608
  return (n) => {
602
- const i = e.slice(0, -1), c = Number(e[e.length - 1]), o = i.join(".");
609
+ const a = e.slice(0, -1), c = Number(e[e.length - 1]), o = a.join(".");
603
610
  n ? r.getState().setSelectedIndex(t, o, c) : r.getState().setSelectedIndex(t, o, void 0);
604
- const d = r.getState().getNestedState(t, [...i]);
605
- z(a, d, i), _(i);
611
+ const d = r.getState().getNestedState(t, [...a]);
612
+ B(i, d, a), _(a);
606
613
  };
607
614
  if (e.length == 0) {
608
615
  if (s === "validateZodSchema")
609
616
  return () => {
610
- const n = r.getState().getInitialOptions(t)?.validation, i = r.getState().addValidationError;
617
+ const n = r.getState().getInitialOptions(t)?.validation, a = r.getState().addValidationError;
611
618
  if (!n?.zodSchema)
612
619
  throw new Error("Zod schema not found");
613
620
  if (!n?.key)
614
621
  throw new Error("Validation key not found");
615
- P(n.key);
622
+ R(n.key);
616
623
  const c = r.getState().cogsStateStore[t];
617
624
  try {
618
625
  const o = r.getState().getValidationErrors(n.key);
619
626
  o && o.length > 0 && o.forEach(([E]) => {
620
- E && E.startsWith(n.key) && P(E);
627
+ E && E.startsWith(n.key) && R(E);
621
628
  });
622
629
  const d = n.zodSchema.safeParse(c);
623
- return d.success ? !0 : (d.error.errors.forEach((F) => {
624
- const $ = F.path, x = F.message, h = [n.key, ...$].join(".");
625
- i(h, x);
630
+ return d.success ? !0 : (d.error.errors.forEach((b) => {
631
+ const $ = b.path, O = b.message, T = [n.key, ...$].join(".");
632
+ a(T, O);
626
633
  }), St(t), !1);
627
634
  } catch (o) {
628
635
  return console.error("Zod schema validation failed", o), !1;
@@ -649,11 +656,11 @@ function X(t, a, S, y) {
649
656
  if (s === "validationWrapper")
650
657
  return ({
651
658
  children: n,
652
- hideMessage: i
659
+ hideMessage: a
653
660
  }) => /* @__PURE__ */ ct(
654
661
  Vt,
655
662
  {
656
- formOpts: i ? { validation: { message: "" } } : void 0,
663
+ formOpts: a ? { validation: { message: "" } } : void 0,
657
664
  path: e,
658
665
  validationKey: r.getState().getInitialOptions(t)?.validation?.key || "",
659
666
  stateKey: t,
@@ -665,36 +672,36 @@ function X(t, a, S, y) {
665
672
  if (s === "_path") return e;
666
673
  if (s === "_isServerSynced") return v._isServerSynced;
667
674
  if (s === "update")
668
- return (n, i) => {
669
- if (i?.debounce)
675
+ return (n, a) => {
676
+ if (a?.debounce)
670
677
  Nt(() => {
671
- z(a, n, e, "");
678
+ B(i, n, e, "");
672
679
  const c = r.getState().getNestedState(t, e);
673
- i?.afterUpdate && i.afterUpdate(c);
674
- }, i.debounce);
680
+ a?.afterUpdate && a.afterUpdate(c);
681
+ }, a.debounce);
675
682
  else {
676
- z(a, n, e, "");
683
+ B(i, n, e, "");
677
684
  const c = r.getState().getNestedState(t, e);
678
- i?.afterUpdate && i.afterUpdate(c);
685
+ a?.afterUpdate && a.afterUpdate(c);
679
686
  }
680
687
  _(e);
681
688
  };
682
689
  if (s === "formElement")
683
- return (n, i) => /* @__PURE__ */ ct(
690
+ return (n, a) => /* @__PURE__ */ ct(
684
691
  $t,
685
692
  {
686
- setState: a,
693
+ setState: i,
687
694
  stateKey: t,
688
695
  path: e,
689
696
  child: n,
690
- formOpts: i
697
+ formOpts: a
691
698
  }
692
699
  );
693
- const O = [...e, s], C = r.getState().getNestedState(t, O);
694
- return l(C, O, m);
700
+ const x = [...e, s], C = r.getState().getNestedState(t, x);
701
+ return l(C, x, m);
695
702
  }
696
703
  }, j = new Proxy(V, A);
697
- return g.set(T, {
704
+ return g.set(h, {
698
705
  proxy: j,
699
706
  stateVersion: w
700
707
  }), j;
@@ -704,26 +711,26 @@ function X(t, a, S, y) {
704
711
  );
705
712
  }
706
713
  function tt(t) {
707
- return Y(xt, { proxy: t });
714
+ return Q(Ot, { proxy: t });
708
715
  }
709
716
  function jt({
710
717
  proxy: t,
711
- rebuildStateShape: a
718
+ rebuildStateShape: i
712
719
  }) {
713
720
  const S = r().getNestedState(t._stateKey, t._path);
714
- return Array.isArray(S) ? a(
721
+ return Array.isArray(S) ? i(
715
722
  S,
716
723
  t._path
717
724
  ).stateMapNoRender(
718
725
  (g, w, _, v, l) => t._mapFn(g, w, _, v, l)
719
726
  ) : null;
720
727
  }
721
- function xt({
728
+ function Ot({
722
729
  proxy: t
723
730
  }) {
724
- const a = J(null), S = `${t._stateKey}-${t._path.join(".")}`;
731
+ const i = Z(null), S = `${t._stateKey}-${t._path.join(".")}`;
725
732
  return nt(() => {
726
- const y = a.current;
733
+ const y = i.current;
727
734
  if (!y || !y.parentElement) return;
728
735
  const g = y.parentElement, _ = Array.from(g.childNodes).indexOf(y);
729
736
  let v = g.getAttribute("data-parent-id");
@@ -749,16 +756,16 @@ function xt({
749
756
  else
750
757
  m = e;
751
758
  m !== null && typeof m == "object" && (m = JSON.stringify(m));
752
- const T = document.createTextNode(String(m));
753
- y.replaceWith(T);
754
- }, [t._stateKey, t._path.join("."), t._effect]), Y("span", {
755
- ref: a,
759
+ const h = document.createTextNode(String(m));
760
+ y.replaceWith(h);
761
+ }, [t._stateKey, t._path.join("."), t._effect]), Q("span", {
762
+ ref: i,
756
763
  style: { display: "none" },
757
764
  "data-signal-id": S
758
765
  });
759
766
  }
760
767
  function Jt(t) {
761
- const a = Et(
768
+ const i = Et(
762
769
  (S) => {
763
770
  const y = r.getState().stateComponents.get(t._stateKey) || {
764
771
  components: /* @__PURE__ */ new Map()
@@ -770,7 +777,7 @@ function Jt(t) {
770
777
  },
771
778
  () => r.getState().getNestedState(t._stateKey, t._path)
772
779
  );
773
- return Y("text", {}, String(a));
780
+ return Q("text", {}, String(i));
774
781
  }
775
782
  export {
776
783
  tt as $cogsSignal,
@@ -778,6 +785,6 @@ export {
778
785
  qt as addStateOptions,
779
786
  zt as createCogsState,
780
787
  Bt as notifyComponent,
781
- bt as useCogsStateFn
788
+ Ft as useCogsStateFn
782
789
  };
783
790
  //# sourceMappingURL=CogsState.jsx.map