cogsbox-state 0.5.125 → 0.5.127

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()), b = 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,189 +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
- b.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}////${b.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}////${b.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, F = `${g}-${o.join(".")}`;
220
- if (F) {
221
- let k = !1, I = r.getState().signalDomElements.get(F);
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
+ a,
264
265
  u,
265
- Q ?? "NONE",
266
+ ve ?? "NONE",
266
267
  K
267
268
  ), K)
268
- for (const [k, I] of K.components.entries()) {
269
- let N = !1;
269
+ for (const [
270
+ F,
271
+ I
272
+ ] of K.components.entries()) {
273
+ let A = !1;
270
274
  const V = Array.isArray(I.reactiveType) ? I.reactiveType : [I.reactiveType || "component"];
271
- if (console.log("component.............................", k, I), !V.includes("none")) {
275
+ if (!V.includes("none")) {
272
276
  if (V.includes("all")) {
273
277
  I.forceUpdate();
274
278
  continue;
275
279
  }
276
- if (V.includes("component") && (console.log(
277
- "component.............................includes(component1111",
278
- k,
279
- I.paths,
280
- Q
281
- ), I.paths && (I.paths.has(Q) || I.paths.has("")) && (console.log(
282
- "component.............................includes(component22222",
283
- k,
284
- I
285
- ), N = !0)), !N && V.includes("deps") && (console.log(
286
- "component.............................includes(deps",
287
- k,
288
- I
289
- ), I.depsFunction)) {
290
- const $ = I.depsFunction(w);
291
- console.log(
292
- "depsResult.............................includes(deps",
293
- I.deps,
294
- $
295
- ), 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();
296
306
  }
297
- N && I.forceUpdate();
298
307
  }
299
308
  }
300
309
  const se = {
301
310
  timeStamp: Date.now(),
302
311
  stateKey: g,
303
- path: o,
312
+ path: a,
304
313
  updateType: u.updateType,
305
314
  status: "new",
306
- oldValue: z,
307
- newValue: me
315
+ oldValue: me,
316
+ newValue: ye
308
317
  };
309
- if (Ve(g, (k) => {
310
- const N = [...k ?? [], se].reduce((V, $) => {
311
- const W = `${$.stateKey}:${JSON.stringify($.path)}`, j = V.get(W);
312
- 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;
313
322
  }, /* @__PURE__ */ new Map());
314
- return Array.from(N.values());
315
- }), ke(
316
- w,
323
+ return Array.from(A.values());
324
+ }), Oe(
325
+ h,
317
326
  g,
318
- h.current,
319
- O
327
+ p.current,
328
+ x
320
329
  ), E && E({
321
330
  updateLog: s,
322
331
  update: se
323
- }), h.current?.serverSync) {
324
- const k = r.getState().serverState[g], I = h.current?.serverSync;
325
- Ce(g, {
326
- syncKey: typeof I.syncKey == "string" ? I.syncKey : I.syncKey({ state: w }),
327
- 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,
328
337
  actionTimeStamp: Date.now() + (I.debounce ?? 3e3),
329
338
  status: "waiting"
330
339
  });
331
340
  }
332
- return w;
341
+ return h;
333
342
  });
334
343
  };
335
- r.getState().updaterState[g] || (H(
344
+ r.getState().updaterState[g] || (Y(
336
345
  g,
337
- Y(
346
+ X(
338
347
  g,
339
348
  n,
340
- b.current,
341
- O
349
+ P.current,
350
+ x
342
351
  )
343
- ), r.getState().cogsStateStore[g] || D(g, e), r.getState().initialStateGlobal[g] || ae(g, e));
344
- const a = Ie(() => Y(
352
+ ), r.getState().cogsStateStore[g] || G(g, e), r.getState().initialStateGlobal[g] || oe(g, e));
353
+ const o = Ee(() => X(
345
354
  g,
346
355
  n,
347
- b.current,
348
- O
356
+ P.current,
357
+ x
349
358
  ), [g]);
350
- return [fe(g), a];
359
+ return [fe(g), o];
351
360
  }
352
- function Y(e, i, v, S) {
361
+ function X(e, i, v, S) {
353
362
  const d = /* @__PURE__ */ new Map();
354
363
  let E = 0;
355
364
  const _ = (f) => {
@@ -359,15 +368,15 @@ function Y(e, i, v, S) {
359
368
  E++;
360
369
  }, m = {
361
370
  removeValidation: (f) => {
362
- f?.validationKey && P(f.validationKey);
371
+ f?.validationKey && R(f.validationKey);
363
372
  },
364
373
  revertToInitialState: (f) => {
365
374
  const t = r.getState().getInitialOptions(e)?.validation;
366
- t?.key && P(t?.key), f?.validationKey && P(f.validationKey);
375
+ t?.key && R(t?.key), f?.validationKey && R(f.validationKey);
367
376
  const y = r.getState().initialStateGlobal[e];
368
377
  r.getState().clearSelectedIndexesForState(e), d.clear(), E++;
369
- const x = c(y, []), T = q(e), O = L(T?.localStorage?.key) ? T?.localStorage?.key(y) : T?.localStorage?.key, R = `${S}-${e}-${O}`;
370
- 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);
371
380
  const g = r.getState().stateComponents.get(e);
372
381
  return g && g.components.forEach((s) => {
373
382
  s.forceUpdate();
@@ -375,17 +384,17 @@ function Y(e, i, v, S) {
375
384
  },
376
385
  updateInitialState: (f) => {
377
386
  d.clear(), E++;
378
- const t = Y(
387
+ const t = X(
379
388
  e,
380
389
  i,
381
390
  v,
382
391
  S
383
392
  );
384
- return Ee(() => {
385
- ae(e, f), H(e, t), D(e, f);
393
+ return $e(() => {
394
+ oe(e, f), Y(e, t), G(e, f);
386
395
  const y = r.getState().stateComponents.get(e);
387
- y && y.components.forEach((x) => {
388
- x.forceUpdate();
396
+ y && y.components.forEach((C) => {
397
+ C.forceUpdate();
389
398
  });
390
399
  }), {
391
400
  fetchId: (y) => t.get()[y]
@@ -396,48 +405,48 @@ function Y(e, i, v, S) {
396
405
  _isLoading: r.getState().isLoadingGlobal[e],
397
406
  _isServerSynced: () => {
398
407
  const f = r.getState().serverState[e];
399
- return !!(f && M(f, fe(e)));
408
+ return !!(f && U(f, fe(e)));
400
409
  }
401
410
  };
402
411
  function c(f, t = [], y) {
403
- const x = t.map(String).join(".");
404
- d.get(x);
412
+ const C = t.map(String).join(".");
413
+ d.get(C);
405
414
  const T = function() {
406
415
  return r().getNestedState(e, t);
407
416
  };
408
417
  Object.keys(m).forEach((g) => {
409
418
  T[g] = m[g];
410
419
  });
411
- const O = {
412
- apply(g, s, G) {
420
+ const x = {
421
+ apply(g, s, W) {
413
422
  return console.log(
414
423
  `PROXY APPLY TRAP HIT: stateKey=${e}, path=${t.join(".")}`
415
424
  ), console.trace("Apply trap stack trace"), r().getNestedState(e, t);
416
425
  },
417
426
  get(g, s) {
418
427
  if (s !== "then" && !s.startsWith("$") && s !== "stateMapNoRender") {
419
- 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);
420
429
  if (l) {
421
- const o = l.components.get(a);
422
- 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);
423
432
  }
424
433
  }
425
434
  if (s === "_status") {
426
- const n = r.getState().getNestedState(e, t), a = r.getState().initialStateGlobal[e], l = U(a, t);
427
- 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";
428
437
  }
429
438
  if (s === "getStatus")
430
439
  return function() {
431
440
  const n = r().getNestedState(
432
441
  e,
433
442
  t
434
- ), a = r.getState().initialStateGlobal[e], l = U(a, t);
435
- return M(n, l) ? "fresh" : "stale";
443
+ ), o = r.getState().initialStateGlobal[e], l = D(o, t);
444
+ return U(n, l) ? "fresh" : "stale";
436
445
  };
437
446
  if (s === "removeStorage")
438
447
  return () => {
439
- 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}`;
440
- 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);
441
450
  };
442
451
  if (s === "showValidationErrors")
443
452
  return () => {
@@ -461,31 +470,31 @@ function Y(e, i, v, S) {
461
470
  return () => r.getState().getSelectedIndex(e, t.join(".")) ?? -1;
462
471
  if (s === "stateSort")
463
472
  return (n) => {
464
- const o = [...r.getState().getNestedState(e, t).map((u, p) => ({
473
+ const a = [...r.getState().getNestedState(e, t).map((u, w) => ({
465
474
  ...u,
466
- __origIndex: p.toString()
475
+ __origIndex: w.toString()
467
476
  }))].sort(n);
468
- return d.clear(), E++, c(o, t, {
477
+ return d.clear(), E++, c(a, t, {
469
478
  filtered: [...y?.filtered || [], t],
470
- validIndices: o.map(
479
+ validIndices: a.map(
471
480
  (u) => parseInt(u.__origIndex)
472
481
  )
473
482
  });
474
483
  };
475
484
  if (s === "stateMap" || s === "stateMapNoRender")
476
485
  return (n) => {
477
- const a = y?.filtered?.some(
478
- (o) => o.join(".") === t.join(".")
479
- ), l = a ? f : r.getState().getNestedState(e, t);
480
- return s !== "stateMapNoRender" && (d.clear(), E++), l.map((o, u) => {
481
- const p = a && o.__origIndex ? o.__origIndex : u, A = c(
482
- o,
483
- [...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()],
484
493
  y
485
494
  );
486
495
  return n(
487
- o,
488
- A,
496
+ a,
497
+ N,
489
498
  u,
490
499
  f,
491
500
  c(f, t, y)
@@ -493,7 +502,7 @@ function Y(e, i, v, S) {
493
502
  });
494
503
  };
495
504
  if (s === "$stateMap")
496
- return (n) => X(je, {
505
+ return (n) => Q(Fe, {
497
506
  proxy: {
498
507
  _stateKey: e,
499
508
  _path: t,
@@ -508,28 +517,28 @@ function Y(e, i, v, S) {
508
517
  (u) => u.join(".") === t.join(".")
509
518
  ) ? f : r.getState().getNestedState(e, t);
510
519
  d.clear(), E++;
511
- const o = l.flatMap(
512
- (u, p) => u[n] ?? []
520
+ const a = l.flatMap(
521
+ (u, w) => u[n] ?? []
513
522
  );
514
523
  return c(
515
- o,
524
+ a,
516
525
  [...t, "[*]", n],
517
526
  y
518
527
  );
519
528
  };
520
529
  if (s === "findWith")
521
- return (n, a) => {
530
+ return (n, o) => {
522
531
  const l = f.findIndex(
523
- (p) => p[n] === a
532
+ (w) => w[n] === o
524
533
  );
525
534
  if (l === -1) return;
526
- const o = f[l], u = [...t, l.toString()];
527
- 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);
528
537
  };
529
538
  if (s === "index")
530
539
  return (n) => {
531
- const a = f[n];
532
- return c(a, [...t, n.toString()]);
540
+ const o = f[n];
541
+ return c(o, [...t, n.toString()]);
533
542
  };
534
543
  if (s === "insert")
535
544
  return (n) => (_(t), ee(i, n, t, e), c(
@@ -537,65 +546,65 @@ function Y(e, i, v, S) {
537
546
  []
538
547
  ));
539
548
  if (s === "uniqueInsert")
540
- return (n, a, l) => {
541
- const o = r.getState().getNestedState(e, t), u = L(n) ? n(o) : n;
542
- let p = null;
543
- if (!o.some((w) => {
544
- if (a) {
545
- const C = a.every(
546
- (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])
547
556
  );
548
- return C && (p = w), C;
557
+ return O && (w = h), O;
549
558
  }
550
- const F = M(w, u);
551
- return F && (p = w), F;
559
+ const j = U(h, u);
560
+ return j && (w = h), j;
552
561
  }))
553
562
  _(t), ee(i, u, t, e);
554
- else if (l && p) {
555
- const w = l(p), F = o.map(
556
- (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
557
566
  );
558
- _(t), B(i, F, t);
567
+ _(t), J(i, j, t);
559
568
  }
560
569
  };
561
570
  if (s === "cut")
562
- return (n, a) => {
563
- a?.waitForSync || (_(t), J(i, t, e, n));
571
+ return (n, o) => {
572
+ o?.waitForSync || (_(t), Z(i, t, e, n));
564
573
  };
565
574
  if (s === "cutByValue")
566
575
  return (n) => {
567
- for (let a = 0; a < f.length; a++)
568
- 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);
569
578
  };
570
579
  if (s === "toggleByValue")
571
580
  return (n) => {
572
- const a = f.findIndex((l) => l === n);
573
- 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);
574
583
  };
575
584
  if (s === "stateFilter")
576
585
  return (n) => {
577
- const a = f.map((u, p) => ({
586
+ const o = f.map((u, w) => ({
578
587
  ...u,
579
- __origIndex: p.toString()
580
- })), l = [], o = [];
581
- for (let u = 0; u < a.length; u++)
582
- n(a[u], u) && (l.push(u), o.push(a[u]));
583
- 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, {
584
593
  filtered: [...y?.filtered || [], t],
585
594
  validIndices: l
586
595
  // Always pass validIndices, even if empty
587
596
  });
588
597
  };
589
598
  }
590
- const G = t[t.length - 1];
591
- if (!isNaN(Number(G))) {
592
- const n = t.slice(0, -1), a = r.getState().getNestedState(e, n);
593
- if (Array.isArray(a) && s === "cut")
594
- 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(
595
604
  i,
596
605
  n,
597
606
  e,
598
- Number(G)
607
+ Number(W)
599
608
  );
600
609
  }
601
610
  if (s === "get")
@@ -624,38 +633,38 @@ function Y(e, i, v, S) {
624
633
  if (s == "getLocalStorage")
625
634
  return (n) => Se(S + "-" + e + "-" + n);
626
635
  if (s === "_selected") {
627
- const n = t.slice(0, -1), a = n.join("."), l = r.getState().getNestedState(e, n);
628
- 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;
629
638
  }
630
639
  if (s === "setSelected")
631
640
  return (n) => {
632
- const a = t.slice(0, -1), l = Number(t[t.length - 1]), o = a.join(".");
633
- n ? r.getState().setSelectedIndex(e, o, l) : r.getState().setSelectedIndex(e, o, void 0);
634
- const u = r.getState().getNestedState(e, [...a]);
635
- 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);
636
645
  };
637
646
  if (t.length == 0) {
638
647
  if (s === "validateZodSchema")
639
648
  return () => {
640
- const n = r.getState().getInitialOptions(e)?.validation, a = r.getState().addValidationError;
649
+ const n = r.getState().getInitialOptions(e)?.validation, o = r.getState().addValidationError;
641
650
  if (!n?.zodSchema)
642
651
  throw new Error("Zod schema not found");
643
652
  if (!n?.key)
644
653
  throw new Error("Validation key not found");
645
- P(n.key);
654
+ R(n.key);
646
655
  const l = r.getState().cogsStateStore[e];
647
656
  try {
648
- const o = r.getState().getValidationErrors(n.key);
649
- o && o.length > 0 && o.forEach(([p]) => {
650
- 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);
651
660
  });
652
661
  const u = n.zodSchema.safeParse(l);
653
- return u.success ? !0 : (u.error.errors.forEach((A) => {
654
- const w = A.path, F = A.message, C = [n.key, ...w].join(".");
655
- a(C, F);
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);
656
665
  }), ie(e), !1);
657
- } catch (o) {
658
- return console.error("Zod schema validation failed", o), !1;
666
+ } catch (a) {
667
+ return console.error("Zod schema validation failed", a), !1;
659
668
  }
660
669
  };
661
670
  if (s === "_componentId") return v;
@@ -679,11 +688,11 @@ function Y(e, i, v, S) {
679
688
  if (s === "validationWrapper")
680
689
  return ({
681
690
  children: n,
682
- hideMessage: a
691
+ hideMessage: o
683
692
  }) => /* @__PURE__ */ le(
684
- $e,
693
+ Te,
685
694
  {
686
- formOpts: a ? { validation: { message: "" } } : void 0,
695
+ formOpts: o ? { validation: { message: "" } } : void 0,
687
696
  path: t,
688
697
  validationKey: r.getState().getInitialOptions(e)?.validation?.key || "",
689
698
  stateKey: e,
@@ -695,48 +704,48 @@ function Y(e, i, v, S) {
695
704
  if (s === "_path") return t;
696
705
  if (s === "_isServerSynced") return m._isServerSynced;
697
706
  if (s === "update")
698
- return (n, a) => {
699
- if (a?.debounce)
700
- we(() => {
701
- B(i, n, t, "");
707
+ return (n, o) => {
708
+ if (o?.debounce)
709
+ Ne(() => {
710
+ J(i, n, t, "");
702
711
  const l = r.getState().getNestedState(e, t);
703
- a?.afterUpdate && a.afterUpdate(l);
704
- }, a.debounce);
712
+ o?.afterUpdate && o.afterUpdate(l);
713
+ }, o.debounce);
705
714
  else {
706
- B(i, n, t, "");
715
+ J(i, n, t, "");
707
716
  const l = r.getState().getNestedState(e, t);
708
- a?.afterUpdate && a.afterUpdate(l);
717
+ o?.afterUpdate && o.afterUpdate(l);
709
718
  }
710
719
  _(t);
711
720
  };
712
721
  if (s === "formElement")
713
- return (n, a) => /* @__PURE__ */ le(
714
- Ae,
722
+ return (n, o) => /* @__PURE__ */ le(
723
+ Ve,
715
724
  {
716
725
  setState: i,
717
726
  stateKey: e,
718
727
  path: t,
719
728
  child: n,
720
- formOpts: a
729
+ formOpts: o
721
730
  }
722
731
  );
723
- const b = [...t, s], h = r.getState().getNestedState(e, b);
724
- return c(h, b, y);
732
+ const P = [...t, s], p = r.getState().getNestedState(e, P);
733
+ return c(p, P, y);
725
734
  }
726
- }, R = new Proxy(T, O);
727
- return d.set(x, {
728
- proxy: R,
735
+ }, M = new Proxy(T, x);
736
+ return d.set(C, {
737
+ proxy: M,
729
738
  stateVersion: E
730
- }), R;
739
+ }), M;
731
740
  }
732
741
  return c(
733
742
  r.getState().getNestedState(e, [])
734
743
  );
735
744
  }
736
745
  function te(e) {
737
- return X(Fe, { proxy: e });
746
+ return Q(Re, { proxy: e });
738
747
  }
739
- function je({
748
+ function Fe({
740
749
  proxy: e,
741
750
  rebuildStateShape: i
742
751
  }) {
@@ -748,10 +757,10 @@ function je({
748
757
  (d, E, _, m, c) => e._mapFn(d, E, _, m, c)
749
758
  ) : null;
750
759
  }
751
- function Fe({
760
+ function Re({
752
761
  proxy: e
753
762
  }) {
754
- const i = Z(null), v = `${e._stateKey}-${e._path.join(".")}`;
763
+ const i = H(null), v = `${e._stateKey}-${e._path.join(".")}`;
755
764
  return re(() => {
756
765
  const S = i.current;
757
766
  if (!S || !S.parentElement) return;
@@ -779,16 +788,16 @@ function Fe({
779
788
  else
780
789
  y = t;
781
790
  y !== null && typeof y == "object" && (y = JSON.stringify(y));
782
- const x = document.createTextNode(String(y));
783
- S.replaceWith(x);
784
- }, [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", {
785
794
  ref: i,
786
795
  style: { display: "none" },
787
796
  "data-signal-id": v
788
797
  });
789
798
  }
790
- function Je(e) {
791
- const i = _e(
799
+ function Ye(e) {
800
+ const i = we(
792
801
  (v) => {
793
802
  const S = r.getState().stateComponents.get(e._stateKey) || {
794
803
  components: /* @__PURE__ */ new Map()
@@ -800,14 +809,14 @@ function Je(e) {
800
809
  },
801
810
  () => r.getState().getNestedState(e._stateKey, e._path)
802
811
  );
803
- return X("text", {}, String(i));
812
+ return Q("text", {}, String(i));
804
813
  }
805
814
  export {
806
815
  te as $cogsSignal,
807
- Je as $cogsSignalStore,
808
- qe as addStateOptions,
809
- ze as createCogsState,
810
- Be as notifyComponent,
811
- Oe as useCogsStateFn
816
+ Ye as $cogsSignalStore,
817
+ Je as addStateOptions,
818
+ Ze as createCogsState,
819
+ He as notifyComponent,
820
+ je as useCogsStateFn
812
821
  };
813
822
  //# sourceMappingURL=CogsState.jsx.map