cogsbox-state 0.5.120 → 0.5.122

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