cogsbox-state 0.5.124 → 0.5.126

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,12 +1,12 @@
1
1
  "use client";
2
2
  import { jsx as le } from "react/jsx-runtime";
3
- import { useState as ne, useRef as Z, useEffect as re, useLayoutEffect as ve, useMemo as Ie, createElement as X, useSyncExternalStore as _e, startTransition as Ee } from "react";
4
- import { transformStateFunc as pe, isFunction as L, getNestedValue as U, isDeepEqual as M, debounce as we } from "./utility.js";
5
- import { pushFunc as ee, updateFn as B, cutFunc as J, ValidationWrapper as $e, FormControlComponent as Ae } from "./Functions.jsx";
3
+ import { useState as ne, useRef as H, useEffect as re, useLayoutEffect as _e, useMemo as Ee, createElement as Q, useSyncExternalStore as we, startTransition as $e } from "react";
4
+ import { transformStateFunc as he, isFunction as L, getDifferences as Ae, getNestedValue as D, isDeepEqual as U, debounce as Ne } from "./utility.js";
5
+ import { pushFunc as ee, updateFn as J, cutFunc as Z, ValidationWrapper as Te, FormControlComponent as Ve } from "./Functions.jsx";
6
6
  import "zod";
7
7
  import { getGlobalStore as r, formRefStore as de } from "./store.js";
8
- import { useCogsConfig as Ne } from "./CogsStateClient.jsx";
9
- import oe from "./node_modules/uuid/dist/esm-browser/v4.js";
8
+ import { useCogsConfig as pe } from "./CogsStateClient.jsx";
9
+ import ae from "./node_modules/uuid/dist/esm-browser/v4.js";
10
10
  function ue(e, i) {
11
11
  const v = r.getState().getInitialOptions, S = r.getState().setInitialStateOptions, d = v(e) || {};
12
12
  S(e, {
@@ -19,19 +19,19 @@ function ge({
19
19
  options: i,
20
20
  initialOptionsPart: v
21
21
  }) {
22
- const S = q(e) || {}, d = v[e] || {}, E = r.getState().setInitialStateOptions, _ = { ...d, ...S };
22
+ const S = B(e) || {}, d = v[e] || {}, E = r.getState().setInitialStateOptions, _ = { ...d, ...S };
23
23
  let m = !1;
24
24
  if (i)
25
25
  for (const c in i)
26
26
  _.hasOwnProperty(c) ? (c == "localStorage" && i[c] && _[c].key !== i[c]?.key && (m = !0, _[c] = i[c]), c == "initialState" && i[c] && _[c] !== i[c] && (m = !0, _[c] = i[c])) : (m = !0, _[c] = i[c]);
27
27
  m && E(e, _);
28
28
  }
29
- function qe(e, { formElements: i, validation: v }) {
29
+ function Je(e, { formElements: i, validation: v }) {
30
30
  return { initialState: e, formElements: i, validation: v };
31
31
  }
32
- const ze = (e, i) => {
32
+ const Ze = (e, i) => {
33
33
  let v = e;
34
- const [S, d] = pe(v);
34
+ const [S, d] = he(v);
35
35
  (Object.keys(d).length > 0 || i && Object.keys(i).length > 0) && Object.keys(d).forEach((m) => {
36
36
  d[m] = d[m] || {}, d[m].formElements = {
37
37
  ...i?.formElements,
@@ -39,16 +39,16 @@ const ze = (e, i) => {
39
39
  ...i?.validation,
40
40
  ...d[m].formElements || {}
41
41
  // State-specific overrides
42
- }, q(m) || r.getState().setInitialStateOptions(m, d[m]);
42
+ }, B(m) || r.getState().setInitialStateOptions(m, d[m]);
43
43
  }), r.getState().setInitialStates(S), r.getState().setCreatedState(S);
44
44
  const E = (m, c) => {
45
- const [f] = ne(c?.componentId ?? oe());
45
+ const [f] = ne(c?.componentId ?? ae());
46
46
  ge({
47
47
  stateKey: m,
48
48
  options: c,
49
49
  initialOptionsPart: d
50
50
  });
51
- const t = r.getState().cogsStateStore[m] || S[m], y = c?.modifyState ? c.modifyState(t) : t, [x, T] = Oe(
51
+ const t = r.getState().cogsStateStore[m] || S[m], y = c?.modifyState ? c.modifyState(t) : t, [C, T] = je(
52
52
  y,
53
53
  {
54
54
  stateKey: m,
@@ -70,16 +70,16 @@ const ze = (e, i) => {
70
70
  }
71
71
  return { useCogsState: E, setCogsOptions: _ };
72
72
  }, {
73
- setUpdaterState: H,
74
- setState: D,
75
- getInitialOptions: q,
73
+ setUpdaterState: Y,
74
+ setState: G,
75
+ getInitialOptions: B,
76
76
  getKeyState: fe,
77
- getValidationErrors: Te,
78
- setStateLog: Ve,
79
- updateInitialStateGlobal: ae,
80
- addValidationError: he,
81
- removeValidationError: P,
82
- setServerSyncActions: Ce
77
+ getValidationErrors: ke,
78
+ setStateLog: Ce,
79
+ updateInitialStateGlobal: oe,
80
+ addValidationError: xe,
81
+ removeValidationError: R,
82
+ setServerSyncActions: be
83
83
  } = r.getState(), Se = (e) => {
84
84
  if (!e) return null;
85
85
  try {
@@ -88,7 +88,7 @@ const ze = (e, i) => {
88
88
  } catch (i) {
89
89
  return console.error("Error loading from localStorage:", i), null;
90
90
  }
91
- }, ke = (e, i, v, S) => {
91
+ }, Oe = (e, i, v, S) => {
92
92
  v?.log && console.log(
93
93
  "saving to localstorage",
94
94
  i,
@@ -105,10 +105,10 @@ const ze = (e, i) => {
105
105
  }, _ = `${S}-${i}-${d}`;
106
106
  window.localStorage.setItem(_, JSON.stringify(E));
107
107
  }
108
- }, xe = (e, i, v, S, d, E) => {
108
+ }, Pe = (e, i, v, S, d, E) => {
109
109
  const _ = {
110
110
  initialState: i,
111
- updaterState: Y(
111
+ updaterState: X(
112
112
  e,
113
113
  S,
114
114
  d,
@@ -116,7 +116,7 @@ const ze = (e, i) => {
116
116
  ),
117
117
  state: v
118
118
  };
119
- ae(e, _.initialState), H(e, _.updaterState), D(e, _.state);
119
+ oe(e, _.initialState), Y(e, _.updaterState), G(e, _.state);
120
120
  }, ie = (e) => {
121
121
  const i = r.getState().stateComponents.get(e);
122
122
  if (!i) return;
@@ -126,7 +126,7 @@ const ze = (e, i) => {
126
126
  }), queueMicrotask(() => {
127
127
  v.forEach((S) => S());
128
128
  });
129
- }, Be = (e, i) => {
129
+ }, He = (e, i) => {
130
130
  const v = r.getState().stateComponents.get(e);
131
131
  if (v) {
132
132
  const S = `${e}////${i}`, d = v.components.get(S);
@@ -135,7 +135,7 @@ const ze = (e, i) => {
135
135
  d && d.forceUpdate();
136
136
  }
137
137
  };
138
- function Oe(e, {
138
+ function je(e, {
139
139
  stateKey: i,
140
140
  serverSync: v,
141
141
  localStorage: S,
@@ -148,13 +148,13 @@ function Oe(e, {
148
148
  syncUpdate: t,
149
149
  dependencies: y
150
150
  } = {}) {
151
- const [x, T] = ne({}), { sessionId: O } = Ne();
152
- let R = !i;
153
- const [g] = ne(i ?? oe()), s = r.getState().stateLog[g], G = Z(/* @__PURE__ */ new Set()), F = Z(c ?? oe()), h = Z(null);
154
- h.current = q(g), re(() => {
151
+ const [C, T] = ne({}), { sessionId: x } = pe();
152
+ let M = !i;
153
+ const [g] = ne(i ?? ae()), s = r.getState().stateLog[g], W = H(/* @__PURE__ */ new Set()), P = H(c ?? ae()), p = H(null);
154
+ p.current = B(g), re(() => {
155
155
  if (t && t.stateKey === g && t.path?.[0]) {
156
- D(g, (o) => ({
157
- ...o,
156
+ G(g, (a) => ({
157
+ ...a,
158
158
  [t.path[0]]: t.newValue
159
159
  }));
160
160
  const l = `${t.stateKey}:${t.path.join(".")}`;
@@ -167,188 +167,198 @@ function Oe(e, {
167
167
  f && ue(g, {
168
168
  initialState: f
169
169
  });
170
- const l = h.current;
171
- let o = null;
170
+ const l = p.current;
171
+ let a = null;
172
172
  const u = L(l?.localStorage?.key) ? l?.localStorage?.key(f) : l?.localStorage?.key;
173
- console.log("newoptions", l), console.log("localkey", u), console.log("initialState", f), u && O && (o = Se(
174
- O + "-" + g + "-" + u
173
+ console.log("newoptions", l), console.log("localkey", u), console.log("initialState", f), u && x && (a = Se(
174
+ x + "-" + g + "-" + u
175
175
  ));
176
- const p = r.getState().iniitialCreatedState[g];
177
- console.log("createdState - intiual", p, f);
178
- let A = null;
176
+ const w = r.getState().iniitialCreatedState[g];
177
+ console.log("createdState - intiual", w, f);
178
+ let N = null;
179
179
  if (f) {
180
- A = f, o && o.lastUpdated > (o.lastSyncedWithServer || 0) && (A = o.state, l?.localStorage?.onChange && l?.localStorage?.onChange(A)), console.log("newState thius is newstate", A), xe(
180
+ N = f, a && a.lastUpdated > (a.lastSyncedWithServer || 0) && (N = a.state, l?.localStorage?.onChange && l?.localStorage?.onChange(N)), console.log("newState thius is newstate", N), Pe(
181
181
  g,
182
182
  f,
183
- A,
183
+ N,
184
184
  n,
185
- F.current,
186
- O
185
+ P.current,
186
+ x
187
187
  ), ie(g);
188
- const w = Array.isArray(m) ? m : [m || "component"];
189
- console.log("reactiveTypes.............................", w), w.includes("none") || T({});
188
+ const h = Array.isArray(m) ? m : [m || "component"];
189
+ console.log("reactiveTypes.............................", h), h.includes("none") || T({});
190
190
  }
191
- }, [f, ...y || []]), ve(() => {
192
- R && ue(g, {
191
+ }, [f, ...y || []]), _e(() => {
192
+ M && ue(g, {
193
193
  serverSync: v,
194
194
  formElements: d,
195
195
  initialState: f,
196
196
  localStorage: S,
197
197
  middleware: E
198
198
  });
199
- const l = `${g}////${F.current}`, o = r.getState().stateComponents.get(g) || {
199
+ const l = `${g}////${P.current}`, a = r.getState().stateComponents.get(g) || {
200
200
  components: /* @__PURE__ */ new Map()
201
201
  };
202
- return o.components.set(l, {
202
+ return a.components.set(l, {
203
203
  forceUpdate: () => T({}),
204
204
  paths: /* @__PURE__ */ new Set(),
205
205
  deps: [],
206
206
  depsFunction: _ || void 0,
207
207
  reactiveType: m ?? ["component", "deps"]
208
- }), r.getState().stateComponents.set(g, o), T({}), () => {
209
- const u = `${g}////${F.current}`;
210
- o && (o.components.delete(u), o.components.size === 0 && r.getState().stateComponents.delete(g));
208
+ }), r.getState().stateComponents.set(g, a), T({}), () => {
209
+ const u = `${g}////${P.current}`;
210
+ a && (a.components.delete(u), a.components.size === 0 && r.getState().stateComponents.delete(g));
211
211
  };
212
212
  }, []);
213
- const n = (l, o, u, p) => {
214
- if (Array.isArray(o)) {
215
- const A = `${g}-${o.join(".")}`;
216
- G.current.add(A);
213
+ const n = (l, a, u, w) => {
214
+ if (Array.isArray(a)) {
215
+ const N = `${g}-${a.join(".")}`;
216
+ W.current.add(N);
217
217
  }
218
- D(g, (A) => {
219
- const w = L(l) ? l(A) : l, b = `${g}-${o.join(".")}`;
220
- if (b) {
221
- let k = !1, I = r.getState().signalDomElements.get(b);
218
+ G(g, (N) => {
219
+ const h = L(l) ? l(N) : l, j = Ae(N, h), O = new Set(j), q = `${g}-${a.join(".")}`;
220
+ if (q) {
221
+ let F = !1, I = r.getState().signalDomElements.get(q);
222
222
  if ((!I || I.size === 0) && (u.updateType === "insert" || u.updateType === "cut")) {
223
- const N = o.slice(0, -1), V = U(w, N);
223
+ const A = a.slice(0, -1), V = D(h, A);
224
224
  if (Array.isArray(V)) {
225
- k = !0;
226
- const $ = `${g}-${N.join(".")}`;
225
+ F = !0;
226
+ const $ = `${g}-${A.join(".")}`;
227
227
  I = r.getState().signalDomElements.get($);
228
228
  }
229
229
  }
230
230
  if (I) {
231
- const N = k ? U(w, o.slice(0, -1)) : U(w, o);
232
- I.forEach(({ parentId: V, position: $, effect: W }) => {
233
- const j = document.querySelector(
231
+ const A = F ? D(h, a.slice(0, -1)) : D(h, a);
232
+ I.forEach(({ parentId: V, position: $, effect: k }) => {
233
+ const b = document.querySelector(
234
234
  `[data-parent-id="${V}"]`
235
235
  );
236
- if (j) {
237
- const ce = Array.from(j.childNodes);
236
+ if (b) {
237
+ const ce = Array.from(b.childNodes);
238
238
  if (ce[$]) {
239
- const ye = W ? new Function("state", `return (${W})(state)`)(N) : N;
240
- ce[$].textContent = String(ye);
239
+ const Ie = k ? new Function("state", `return (${k})(state)`)(A) : A;
240
+ ce[$].textContent = String(Ie);
241
241
  }
242
242
  }
243
243
  });
244
244
  }
245
245
  }
246
- u.updateType === "update" && (p || h.current?.validationKey) && o && P(
247
- (p || h.current?.validationKey) + "." + o.join(".")
246
+ u.updateType === "update" && (w || p.current?.validationKey) && a && R(
247
+ (w || p.current?.validationKey) + "." + a.join(".")
248
248
  );
249
- const C = o.slice(0, o.length - 1);
250
- u.updateType === "cut" && h.current?.validationKey && P(
251
- h.current?.validationKey + "." + C.join(".")
252
- ), u.updateType === "insert" && h.current?.validationKey && Te(
253
- h.current?.validationKey + "." + C.join(".")
254
- ).filter(([I, N]) => {
249
+ const z = a.slice(0, a.length - 1);
250
+ u.updateType === "cut" && p.current?.validationKey && R(
251
+ p.current?.validationKey + "." + z.join(".")
252
+ ), u.updateType === "insert" && p.current?.validationKey && ke(
253
+ p.current?.validationKey + "." + z.join(".")
254
+ ).filter(([I, A]) => {
255
255
  let V = I?.split(".").length;
256
- if (I == C.join(".") && V == C.length - 1) {
257
- let $ = I + "." + C;
258
- P(I), he($, N);
256
+ if (I == z.join(".") && V == z.length - 1) {
257
+ let $ = I + "." + z;
258
+ R(I), xe($, A);
259
259
  }
260
260
  });
261
- const z = U(A, o), me = U(w, o), Q = u.updateType === "update" ? o.join(".") : [...o].slice(0, -1).join("."), K = r.getState().stateComponents.get(g);
261
+ const me = D(N, a), ye = D(h, a), ve = u.updateType === "update" ? a.join(".") : [...a].slice(0, -1).join("."), K = r.getState().stateComponents.get(g);
262
262
  if (console.log(
263
263
  "pathetocaheck.............................",
264
- Q,
264
+ a,
265
+ u,
266
+ ve ?? "NONE",
265
267
  K
266
268
  ), K)
267
- for (const [k, I] of K.components.entries()) {
268
- let N = !1;
269
+ for (const [
270
+ F,
271
+ I
272
+ ] of K.components.entries()) {
273
+ let A = !1;
269
274
  const V = Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"];
270
- if (console.log("component.............................", k, I), !V.includes("none")) {
275
+ if (!V.includes("none")) {
271
276
  if (V.includes("all")) {
272
277
  I.forceUpdate();
273
278
  continue;
274
279
  }
275
- if (V.includes("component") && (console.log(
276
- "component.............................includes(component1111",
277
- k,
278
- I.paths,
279
- Q
280
- ), I.paths && (I.paths.has(Q) || I.paths.has("")) && (console.log(
281
- "component.............................includes(component22222",
282
- k,
283
- I
284
- ), N = !0)), !N && V.includes("deps") && (console.log(
285
- "component.............................includes(deps",
286
- k,
287
- I
288
- ), I.depsFunction)) {
289
- const $ = I.depsFunction(w);
290
- console.log(
291
- "depsResult.............................includes(deps",
292
- I.deps,
293
- $
294
- ), typeof $ == "boolean" ? $ && (N = !0) : M(I.deps, $) || (I.deps = $, N = !0);
280
+ if (V.includes("component")) {
281
+ for (const $ of O) {
282
+ if (I.paths.has($)) {
283
+ A = !0;
284
+ break;
285
+ }
286
+ let k = $;
287
+ for (; k.includes("."); )
288
+ if (k = k.substring(
289
+ 0,
290
+ k.lastIndexOf(".")
291
+ ), I.paths.has(k)) {
292
+ A = !0;
293
+ break;
294
+ }
295
+ if (A) break;
296
+ if (I.paths.has("")) {
297
+ A = !0;
298
+ break;
299
+ }
300
+ }
301
+ if (!A && V.includes("deps") && I.depsFunction) {
302
+ const $ = I.depsFunction(h);
303
+ typeof $ == "boolean" ? $ && (A = !0) : U(I.deps, $) || (I.deps = $, A = !0);
304
+ }
305
+ A && I.forceUpdate();
295
306
  }
296
- N && I.forceUpdate();
297
307
  }
298
308
  }
299
309
  const se = {
300
310
  timeStamp: Date.now(),
301
311
  stateKey: g,
302
- path: o,
312
+ path: a,
303
313
  updateType: u.updateType,
304
314
  status: "new",
305
- oldValue: z,
306
- newValue: me
315
+ oldValue: me,
316
+ newValue: ye
307
317
  };
308
- if (Ve(g, (k) => {
309
- const N = [...k ?? [], se].reduce((V, $) => {
310
- const W = `${$.stateKey}:${JSON.stringify($.path)}`, j = V.get(W);
311
- return j ? (j.timeStamp = Math.max(j.timeStamp, $.timeStamp), j.newValue = $.newValue, j.oldValue = j.oldValue ?? $.oldValue, j.updateType = $.updateType) : V.set(W, { ...$ }), V;
318
+ if (Ce(g, (F) => {
319
+ const A = [...F ?? [], se].reduce((V, $) => {
320
+ const k = `${$.stateKey}:${JSON.stringify($.path)}`, b = V.get(k);
321
+ return b ? (b.timeStamp = Math.max(b.timeStamp, $.timeStamp), b.newValue = $.newValue, b.oldValue = b.oldValue ?? $.oldValue, b.updateType = $.updateType) : V.set(k, { ...$ }), V;
312
322
  }, /* @__PURE__ */ new Map());
313
- return Array.from(N.values());
314
- }), ke(
315
- w,
323
+ return Array.from(A.values());
324
+ }), Oe(
325
+ h,
316
326
  g,
317
- h.current,
318
- O
327
+ p.current,
328
+ x
319
329
  ), E && E({
320
330
  updateLog: s,
321
331
  update: se
322
- }), h.current?.serverSync) {
323
- const k = r.getState().serverState[g], I = h.current?.serverSync;
324
- Ce(g, {
325
- syncKey: typeof I.syncKey == "string" ? I.syncKey : I.syncKey({ state: w }),
326
- rollBackState: k,
332
+ }), p.current?.serverSync) {
333
+ const F = r.getState().serverState[g], I = p.current?.serverSync;
334
+ be(g, {
335
+ syncKey: typeof I.syncKey == "string" ? I.syncKey : I.syncKey({ state: h }),
336
+ rollBackState: F,
327
337
  actionTimeStamp: Date.now() + (I.debounce ?? 3e3),
328
338
  status: "waiting"
329
339
  });
330
340
  }
331
- return w;
341
+ return h;
332
342
  });
333
343
  };
334
- r.getState().updaterState[g] || (H(
344
+ r.getState().updaterState[g] || (Y(
335
345
  g,
336
- Y(
346
+ X(
337
347
  g,
338
348
  n,
339
- F.current,
340
- O
349
+ P.current,
350
+ x
341
351
  )
342
- ), r.getState().cogsStateStore[g] || D(g, e), r.getState().initialStateGlobal[g] || ae(g, e));
343
- const a = Ie(() => Y(
352
+ ), r.getState().cogsStateStore[g] || G(g, e), r.getState().initialStateGlobal[g] || oe(g, e));
353
+ const o = Ee(() => X(
344
354
  g,
345
355
  n,
346
- F.current,
347
- O
356
+ P.current,
357
+ x
348
358
  ), [g]);
349
- return [fe(g), a];
359
+ return [fe(g), o];
350
360
  }
351
- function Y(e, i, v, S) {
361
+ function X(e, i, v, S) {
352
362
  const d = /* @__PURE__ */ new Map();
353
363
  let E = 0;
354
364
  const _ = (f) => {
@@ -358,15 +368,15 @@ function Y(e, i, v, S) {
358
368
  E++;
359
369
  }, m = {
360
370
  removeValidation: (f) => {
361
- f?.validationKey && P(f.validationKey);
371
+ f?.validationKey && R(f.validationKey);
362
372
  },
363
373
  revertToInitialState: (f) => {
364
374
  const t = r.getState().getInitialOptions(e)?.validation;
365
- t?.key && P(t?.key), f?.validationKey && P(f.validationKey);
375
+ t?.key && R(t?.key), f?.validationKey && R(f.validationKey);
366
376
  const y = r.getState().initialStateGlobal[e];
367
377
  r.getState().clearSelectedIndexesForState(e), d.clear(), E++;
368
- const x = c(y, []), T = q(e), O = L(T?.localStorage?.key) ? T?.localStorage?.key(y) : T?.localStorage?.key, R = `${S}-${e}-${O}`;
369
- R && localStorage.removeItem(R), H(e, x), D(e, y);
378
+ const C = c(y, []), T = B(e), x = L(T?.localStorage?.key) ? T?.localStorage?.key(y) : T?.localStorage?.key, M = `${S}-${e}-${x}`;
379
+ M && localStorage.removeItem(M), Y(e, C), G(e, y);
370
380
  const g = r.getState().stateComponents.get(e);
371
381
  return g && g.components.forEach((s) => {
372
382
  s.forceUpdate();
@@ -374,17 +384,17 @@ function Y(e, i, v, S) {
374
384
  },
375
385
  updateInitialState: (f) => {
376
386
  d.clear(), E++;
377
- const t = Y(
387
+ const t = X(
378
388
  e,
379
389
  i,
380
390
  v,
381
391
  S
382
392
  );
383
- return Ee(() => {
384
- ae(e, f), H(e, t), D(e, f);
393
+ return $e(() => {
394
+ oe(e, f), Y(e, t), G(e, f);
385
395
  const y = r.getState().stateComponents.get(e);
386
- y && y.components.forEach((x) => {
387
- x.forceUpdate();
396
+ y && y.components.forEach((C) => {
397
+ C.forceUpdate();
388
398
  });
389
399
  }), {
390
400
  fetchId: (y) => t.get()[y]
@@ -395,48 +405,48 @@ function Y(e, i, v, S) {
395
405
  _isLoading: r.getState().isLoadingGlobal[e],
396
406
  _isServerSynced: () => {
397
407
  const f = r.getState().serverState[e];
398
- return !!(f && M(f, fe(e)));
408
+ return !!(f && U(f, fe(e)));
399
409
  }
400
410
  };
401
411
  function c(f, t = [], y) {
402
- const x = t.map(String).join(".");
403
- d.get(x);
412
+ const C = t.map(String).join(".");
413
+ d.get(C);
404
414
  const T = function() {
405
415
  return r().getNestedState(e, t);
406
416
  };
407
417
  Object.keys(m).forEach((g) => {
408
418
  T[g] = m[g];
409
419
  });
410
- const O = {
411
- apply(g, s, G) {
420
+ const x = {
421
+ apply(g, s, W) {
412
422
  return console.log(
413
423
  `PROXY APPLY TRAP HIT: stateKey=${e}, path=${t.join(".")}`
414
424
  ), console.trace("Apply trap stack trace"), r().getNestedState(e, t);
415
425
  },
416
426
  get(g, s) {
417
427
  if (s !== "then" && !s.startsWith("$") && s !== "stateMapNoRender") {
418
- const n = t.join("."), a = `${e}////${v}`, l = r.getState().stateComponents.get(e);
428
+ const n = t.join("."), o = `${e}////${v}`, l = r.getState().stateComponents.get(e);
419
429
  if (l) {
420
- const o = l.components.get(a);
421
- o && (t.length > 0 || s === "get") && o.paths.add(n);
430
+ const a = l.components.get(o);
431
+ a && (t.length > 0 || s === "get") && a.paths.add(n);
422
432
  }
423
433
  }
424
434
  if (s === "_status") {
425
- const n = r.getState().getNestedState(e, t), a = r.getState().initialStateGlobal[e], l = U(a, t);
426
- return M(n, l) ? "fresh" : "stale";
435
+ const n = r.getState().getNestedState(e, t), o = r.getState().initialStateGlobal[e], l = D(o, t);
436
+ return U(n, l) ? "fresh" : "stale";
427
437
  }
428
438
  if (s === "getStatus")
429
439
  return function() {
430
440
  const n = r().getNestedState(
431
441
  e,
432
442
  t
433
- ), a = r.getState().initialStateGlobal[e], l = U(a, t);
434
- return M(n, l) ? "fresh" : "stale";
443
+ ), o = r.getState().initialStateGlobal[e], l = D(o, t);
444
+ return U(n, l) ? "fresh" : "stale";
435
445
  };
436
446
  if (s === "removeStorage")
437
447
  return () => {
438
- const n = r.getState().initialStateGlobal[e], a = q(e), l = L(a?.localStorage?.key) ? a?.localStorage?.key(n) : a?.localStorage?.key, o = `${S}-${e}-${l}`;
439
- console.log("removing storage", o), o && localStorage.removeItem(o);
448
+ const n = r.getState().initialStateGlobal[e], o = B(e), l = L(o?.localStorage?.key) ? o?.localStorage?.key(n) : o?.localStorage?.key, a = `${S}-${e}-${l}`;
449
+ console.log("removing storage", a), a && localStorage.removeItem(a);
440
450
  };
441
451
  if (s === "showValidationErrors")
442
452
  return () => {
@@ -460,31 +470,31 @@ function Y(e, i, v, S) {
460
470
  return () => r.getState().getSelectedIndex(e, t.join(".")) ?? -1;
461
471
  if (s === "stateSort")
462
472
  return (n) => {
463
- const o = [...r.getState().getNestedState(e, t).map((u, p) => ({
473
+ const a = [...r.getState().getNestedState(e, t).map((u, w) => ({
464
474
  ...u,
465
- __origIndex: p.toString()
475
+ __origIndex: w.toString()
466
476
  }))].sort(n);
467
- return d.clear(), E++, c(o, t, {
477
+ return d.clear(), E++, c(a, t, {
468
478
  filtered: [...y?.filtered || [], t],
469
- validIndices: o.map(
479
+ validIndices: a.map(
470
480
  (u) => parseInt(u.__origIndex)
471
481
  )
472
482
  });
473
483
  };
474
484
  if (s === "stateMap" || s === "stateMapNoRender")
475
485
  return (n) => {
476
- const a = y?.filtered?.some(
477
- (o) => o.join(".") === t.join(".")
478
- ), l = a ? f : r.getState().getNestedState(e, t);
479
- return s !== "stateMapNoRender" && (d.clear(), E++), l.map((o, u) => {
480
- const p = a && o.__origIndex ? o.__origIndex : u, A = c(
481
- o,
482
- [...t, p.toString()],
486
+ const o = y?.filtered?.some(
487
+ (a) => a.join(".") === t.join(".")
488
+ ), l = o ? f : r.getState().getNestedState(e, t);
489
+ return s !== "stateMapNoRender" && (d.clear(), E++), l.map((a, u) => {
490
+ const w = o && a.__origIndex ? a.__origIndex : u, N = c(
491
+ a,
492
+ [...t, w.toString()],
483
493
  y
484
494
  );
485
495
  return n(
486
- o,
487
- A,
496
+ a,
497
+ N,
488
498
  u,
489
499
  f,
490
500
  c(f, t, y)
@@ -492,7 +502,7 @@ function Y(e, i, v, S) {
492
502
  });
493
503
  };
494
504
  if (s === "$stateMap")
495
- return (n) => X(je, {
505
+ return (n) => Q(Fe, {
496
506
  proxy: {
497
507
  _stateKey: e,
498
508
  _path: t,
@@ -507,28 +517,28 @@ function Y(e, i, v, S) {
507
517
  (u) => u.join(".") === t.join(".")
508
518
  ) ? f : r.getState().getNestedState(e, t);
509
519
  d.clear(), E++;
510
- const o = l.flatMap(
511
- (u, p) => u[n] ?? []
520
+ const a = l.flatMap(
521
+ (u, w) => u[n] ?? []
512
522
  );
513
523
  return c(
514
- o,
524
+ a,
515
525
  [...t, "[*]", n],
516
526
  y
517
527
  );
518
528
  };
519
529
  if (s === "findWith")
520
- return (n, a) => {
530
+ return (n, o) => {
521
531
  const l = f.findIndex(
522
- (p) => p[n] === a
532
+ (w) => w[n] === o
523
533
  );
524
534
  if (l === -1) return;
525
- const o = f[l], u = [...t, l.toString()];
526
- return d.clear(), E++, d.clear(), E++, c(o, u);
535
+ const a = f[l], u = [...t, l.toString()];
536
+ return d.clear(), E++, d.clear(), E++, c(a, u);
527
537
  };
528
538
  if (s === "index")
529
539
  return (n) => {
530
- const a = f[n];
531
- return c(a, [...t, n.toString()]);
540
+ const o = f[n];
541
+ return c(o, [...t, n.toString()]);
532
542
  };
533
543
  if (s === "insert")
534
544
  return (n) => (_(t), ee(i, n, t, e), c(
@@ -536,65 +546,65 @@ function Y(e, i, v, S) {
536
546
  []
537
547
  ));
538
548
  if (s === "uniqueInsert")
539
- return (n, a, l) => {
540
- const o = r.getState().getNestedState(e, t), u = L(n) ? n(o) : n;
541
- let p = null;
542
- if (!o.some((w) => {
543
- if (a) {
544
- const C = a.every(
545
- (z) => M(w[z], u[z])
549
+ return (n, o, l) => {
550
+ const a = r.getState().getNestedState(e, t), u = L(n) ? n(a) : n;
551
+ let w = null;
552
+ if (!a.some((h) => {
553
+ if (o) {
554
+ const O = o.every(
555
+ (q) => U(h[q], u[q])
546
556
  );
547
- return C && (p = w), C;
557
+ return O && (w = h), O;
548
558
  }
549
- const b = M(w, u);
550
- return b && (p = w), b;
559
+ const j = U(h, u);
560
+ return j && (w = h), j;
551
561
  }))
552
562
  _(t), ee(i, u, t, e);
553
- else if (l && p) {
554
- const w = l(p), b = o.map(
555
- (C) => M(C, p) ? w : C
563
+ else if (l && w) {
564
+ const h = l(w), j = a.map(
565
+ (O) => U(O, w) ? h : O
556
566
  );
557
- _(t), B(i, b, t);
567
+ _(t), J(i, j, t);
558
568
  }
559
569
  };
560
570
  if (s === "cut")
561
- return (n, a) => {
562
- a?.waitForSync || (_(t), J(i, t, e, n));
571
+ return (n, o) => {
572
+ o?.waitForSync || (_(t), Z(i, t, e, n));
563
573
  };
564
574
  if (s === "cutByValue")
565
575
  return (n) => {
566
- for (let a = 0; a < f.length; a++)
567
- f[a] === n && J(i, t, e, a);
576
+ for (let o = 0; o < f.length; o++)
577
+ f[o] === n && Z(i, t, e, o);
568
578
  };
569
579
  if (s === "toggleByValue")
570
580
  return (n) => {
571
- const a = f.findIndex((l) => l === n);
572
- a > -1 ? J(i, t, e, a) : ee(i, n, t, e);
581
+ const o = f.findIndex((l) => l === n);
582
+ o > -1 ? Z(i, t, e, o) : ee(i, n, t, e);
573
583
  };
574
584
  if (s === "stateFilter")
575
585
  return (n) => {
576
- const a = f.map((u, p) => ({
586
+ const o = f.map((u, w) => ({
577
587
  ...u,
578
- __origIndex: p.toString()
579
- })), l = [], o = [];
580
- for (let u = 0; u < a.length; u++)
581
- n(a[u], u) && (l.push(u), o.push(a[u]));
582
- return d.clear(), E++, c(o, t, {
588
+ __origIndex: w.toString()
589
+ })), l = [], a = [];
590
+ for (let u = 0; u < o.length; u++)
591
+ n(o[u], u) && (l.push(u), a.push(o[u]));
592
+ return d.clear(), E++, c(a, t, {
583
593
  filtered: [...y?.filtered || [], t],
584
594
  validIndices: l
585
595
  // Always pass validIndices, even if empty
586
596
  });
587
597
  };
588
598
  }
589
- const G = t[t.length - 1];
590
- if (!isNaN(Number(G))) {
591
- const n = t.slice(0, -1), a = r.getState().getNestedState(e, n);
592
- if (Array.isArray(a) && s === "cut")
593
- return () => J(
599
+ const W = t[t.length - 1];
600
+ if (!isNaN(Number(W))) {
601
+ const n = t.slice(0, -1), o = r.getState().getNestedState(e, n);
602
+ if (Array.isArray(o) && s === "cut")
603
+ return () => Z(
594
604
  i,
595
605
  n,
596
606
  e,
597
- Number(G)
607
+ Number(W)
598
608
  );
599
609
  }
600
610
  if (s === "get")
@@ -623,38 +633,38 @@ function Y(e, i, v, S) {
623
633
  if (s == "getLocalStorage")
624
634
  return (n) => Se(S + "-" + e + "-" + n);
625
635
  if (s === "_selected") {
626
- const n = t.slice(0, -1), a = n.join("."), l = r.getState().getNestedState(e, n);
627
- return Array.isArray(l) ? Number(t[t.length - 1]) === r.getState().getSelectedIndex(e, a) : void 0;
636
+ const n = t.slice(0, -1), o = n.join("."), l = r.getState().getNestedState(e, n);
637
+ return Array.isArray(l) ? Number(t[t.length - 1]) === r.getState().getSelectedIndex(e, o) : void 0;
628
638
  }
629
639
  if (s === "setSelected")
630
640
  return (n) => {
631
- const a = t.slice(0, -1), l = Number(t[t.length - 1]), o = a.join(".");
632
- n ? r.getState().setSelectedIndex(e, o, l) : r.getState().setSelectedIndex(e, o, void 0);
633
- const u = r.getState().getNestedState(e, [...a]);
634
- B(i, u, a), _(a);
641
+ const o = t.slice(0, -1), l = Number(t[t.length - 1]), a = o.join(".");
642
+ n ? r.getState().setSelectedIndex(e, a, l) : r.getState().setSelectedIndex(e, a, void 0);
643
+ const u = r.getState().getNestedState(e, [...o]);
644
+ J(i, u, o), _(o);
635
645
  };
636
646
  if (t.length == 0) {
637
647
  if (s === "validateZodSchema")
638
648
  return () => {
639
- const n = r.getState().getInitialOptions(e)?.validation, a = r.getState().addValidationError;
649
+ const n = r.getState().getInitialOptions(e)?.validation, o = r.getState().addValidationError;
640
650
  if (!n?.zodSchema)
641
651
  throw new Error("Zod schema not found");
642
652
  if (!n?.key)
643
653
  throw new Error("Validation key not found");
644
- P(n.key);
654
+ R(n.key);
645
655
  const l = r.getState().cogsStateStore[e];
646
656
  try {
647
- const o = r.getState().getValidationErrors(n.key);
648
- o && o.length > 0 && o.forEach(([p]) => {
649
- p && p.startsWith(n.key) && P(p);
657
+ const a = r.getState().getValidationErrors(n.key);
658
+ a && a.length > 0 && a.forEach(([w]) => {
659
+ w && w.startsWith(n.key) && R(w);
650
660
  });
651
661
  const u = n.zodSchema.safeParse(l);
652
- return u.success ? !0 : (u.error.errors.forEach((A) => {
653
- const w = A.path, b = A.message, C = [n.key, ...w].join(".");
654
- a(C, b);
662
+ return u.success ? !0 : (u.error.errors.forEach((N) => {
663
+ const h = N.path, j = N.message, O = [n.key, ...h].join(".");
664
+ o(O, j);
655
665
  }), ie(e), !1);
656
- } catch (o) {
657
- return console.error("Zod schema validation failed", o), !1;
666
+ } catch (a) {
667
+ return console.error("Zod schema validation failed", a), !1;
658
668
  }
659
669
  };
660
670
  if (s === "_componentId") return v;
@@ -678,11 +688,11 @@ function Y(e, i, v, S) {
678
688
  if (s === "validationWrapper")
679
689
  return ({
680
690
  children: n,
681
- hideMessage: a
691
+ hideMessage: o
682
692
  }) => /* @__PURE__ */ le(
683
- $e,
693
+ Te,
684
694
  {
685
- formOpts: a ? { validation: { message: "" } } : void 0,
695
+ formOpts: o ? { validation: { message: "" } } : void 0,
686
696
  path: t,
687
697
  validationKey: r.getState().getInitialOptions(e)?.validation?.key || "",
688
698
  stateKey: e,
@@ -694,48 +704,48 @@ function Y(e, i, v, S) {
694
704
  if (s === "_path") return t;
695
705
  if (s === "_isServerSynced") return m._isServerSynced;
696
706
  if (s === "update")
697
- return (n, a) => {
698
- if (a?.debounce)
699
- we(() => {
700
- B(i, n, t, "");
707
+ return (n, o) => {
708
+ if (o?.debounce)
709
+ Ne(() => {
710
+ J(i, n, t, "");
701
711
  const l = r.getState().getNestedState(e, t);
702
- a?.afterUpdate && a.afterUpdate(l);
703
- }, a.debounce);
712
+ o?.afterUpdate && o.afterUpdate(l);
713
+ }, o.debounce);
704
714
  else {
705
- B(i, n, t, "");
715
+ J(i, n, t, "");
706
716
  const l = r.getState().getNestedState(e, t);
707
- a?.afterUpdate && a.afterUpdate(l);
717
+ o?.afterUpdate && o.afterUpdate(l);
708
718
  }
709
719
  _(t);
710
720
  };
711
721
  if (s === "formElement")
712
- return (n, a) => /* @__PURE__ */ le(
713
- Ae,
722
+ return (n, o) => /* @__PURE__ */ le(
723
+ Ve,
714
724
  {
715
725
  setState: i,
716
726
  stateKey: e,
717
727
  path: t,
718
728
  child: n,
719
- formOpts: a
729
+ formOpts: o
720
730
  }
721
731
  );
722
- const F = [...t, s], h = r.getState().getNestedState(e, F);
723
- return c(h, F, y);
732
+ const P = [...t, s], p = r.getState().getNestedState(e, P);
733
+ return c(p, P, y);
724
734
  }
725
- }, R = new Proxy(T, O);
726
- return d.set(x, {
727
- proxy: R,
735
+ }, M = new Proxy(T, x);
736
+ return d.set(C, {
737
+ proxy: M,
728
738
  stateVersion: E
729
- }), R;
739
+ }), M;
730
740
  }
731
741
  return c(
732
742
  r.getState().getNestedState(e, [])
733
743
  );
734
744
  }
735
745
  function te(e) {
736
- return X(be, { proxy: e });
746
+ return Q(Re, { proxy: e });
737
747
  }
738
- function je({
748
+ function Fe({
739
749
  proxy: e,
740
750
  rebuildStateShape: i
741
751
  }) {
@@ -747,10 +757,10 @@ function je({
747
757
  (d, E, _, m, c) => e._mapFn(d, E, _, m, c)
748
758
  ) : null;
749
759
  }
750
- function be({
760
+ function Re({
751
761
  proxy: e
752
762
  }) {
753
- const i = Z(null), v = `${e._stateKey}-${e._path.join(".")}`;
763
+ const i = H(null), v = `${e._stateKey}-${e._path.join(".")}`;
754
764
  return re(() => {
755
765
  const S = i.current;
756
766
  if (!S || !S.parentElement) return;
@@ -778,16 +788,16 @@ function be({
778
788
  else
779
789
  y = t;
780
790
  y !== null && typeof y == "object" && (y = JSON.stringify(y));
781
- const x = document.createTextNode(String(y));
782
- S.replaceWith(x);
783
- }, [e._stateKey, e._path.join("."), e._effect]), X("span", {
791
+ const C = document.createTextNode(String(y));
792
+ S.replaceWith(C);
793
+ }, [e._stateKey, e._path.join("."), e._effect]), Q("span", {
784
794
  ref: i,
785
795
  style: { display: "none" },
786
796
  "data-signal-id": v
787
797
  });
788
798
  }
789
- function Je(e) {
790
- const i = _e(
799
+ function Ye(e) {
800
+ const i = we(
791
801
  (v) => {
792
802
  const S = r.getState().stateComponents.get(e._stateKey) || {
793
803
  components: /* @__PURE__ */ new Map()
@@ -799,14 +809,14 @@ function Je(e) {
799
809
  },
800
810
  () => r.getState().getNestedState(e._stateKey, e._path)
801
811
  );
802
- return X("text", {}, String(i));
812
+ return Q("text", {}, String(i));
803
813
  }
804
814
  export {
805
815
  te as $cogsSignal,
806
- Je as $cogsSignalStore,
807
- qe as addStateOptions,
808
- ze as createCogsState,
809
- Be as notifyComponent,
810
- Oe as useCogsStateFn
816
+ Ye as $cogsSignalStore,
817
+ Je as addStateOptions,
818
+ Ze as createCogsState,
819
+ He as notifyComponent,
820
+ je as useCogsStateFn
811
821
  };
812
822
  //# sourceMappingURL=CogsState.jsx.map