cogsbox-state 0.5.128 → 0.5.129

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