cogsbox-state 0.5.237 → 0.5.239

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,62 +1,62 @@
1
1
  "use client";
2
- import { jsx as ge } from "react/jsx-runtime";
3
- import { useState as te, useRef as ee, useEffect as ce, useLayoutEffect as pe, useMemo as $e, createElement as ae, useSyncExternalStore as Ae, startTransition as Ve } from "react";
4
- import { transformStateFunc as ke, isDeepEqual as G, isFunction as q, getNestedValue as W, getDifferences as le, debounce as Ne } from "./utility.js";
2
+ import { jsx as fe } from "react/jsx-runtime";
3
+ import { useState as te, useRef as ee, useEffect as ce, useLayoutEffect as Ie, useMemo as $e, createElement as oe, useSyncExternalStore as Ae, startTransition as Ne } from "react";
4
+ import { transformStateFunc as Ve, isDeepEqual as G, isFunction as z, getNestedValue as W, getDifferences as le, debounce as ke } from "./utility.js";
5
5
  import { pushFunc as se, updateFn as X, cutFunc as K, ValidationWrapper as Ce, FormControlComponent as _e } from "./Functions.jsx";
6
6
  import Pe from "superjson";
7
7
  import { v4 as de } from "uuid";
8
8
  import "zod";
9
- import { getGlobalStore as n, formRefStore as fe } from "./store.js";
10
- import { useCogsConfig as Ie } from "./CogsStateClient.jsx";
9
+ import { getGlobalStore as n, formRefStore as Se } from "./store.js";
10
+ import { useCogsConfig as Te } from "./CogsStateClient.jsx";
11
11
  import { applyPatch as xe } from "fast-json-patch";
12
- function Se(e, o) {
12
+ function me(e, i) {
13
13
  const S = n.getState().getInitialOptions, l = n.getState().setInitialStateOptions, c = S(e) || {};
14
14
  l(e, {
15
15
  ...c,
16
- ...o
16
+ ...i
17
17
  });
18
18
  }
19
- function me({
19
+ function ye({
20
20
  stateKey: e,
21
- options: o,
21
+ options: i,
22
22
  initialOptionsPart: S
23
23
  }) {
24
- const l = Z(e) || {}, c = S[e] || {}, w = n.getState().setInitialStateOptions, I = { ...c, ...l };
25
- let p = !1;
26
- if (o)
27
- for (const s in o)
28
- I.hasOwnProperty(s) ? (s == "localStorage" && o[s] && I[s].key !== o[s]?.key && (p = !0, I[s] = o[s]), s == "initialState" && o[s] && I[s] !== o[s] && // Different references
29
- !G(I[s], o[s]) && (p = !0, I[s] = o[s])) : (p = !0, I[s] = o[s]);
30
- p && w(e, I);
24
+ const l = Z(e) || {}, c = S[e] || {}, b = n.getState().setInitialStateOptions, I = { ...c, ...l };
25
+ let h = !1;
26
+ if (i)
27
+ for (const s in i)
28
+ I.hasOwnProperty(s) ? (s == "localStorage" && i[s] && I[s].key !== i[s]?.key && (h = !0, I[s] = i[s]), s == "initialState" && i[s] && I[s] !== i[s] && // Different references
29
+ !G(I[s], i[s]) && (h = !0, I[s] = i[s])) : (h = !0, I[s] = i[s]);
30
+ h && b(e, I);
31
31
  }
32
- function Ke(e, { formElements: o, validation: S }) {
33
- return { initialState: e, formElements: o, validation: S };
32
+ function Ke(e, { formElements: i, validation: S }) {
33
+ return { initialState: e, formElements: i, validation: S };
34
34
  }
35
- const et = (e, o) => {
35
+ const et = (e, i) => {
36
36
  let S = e;
37
- const [l, c] = ke(S);
38
- (Object.keys(c).length > 0 || o && Object.keys(o).length > 0) && Object.keys(c).forEach((p) => {
39
- c[p] = c[p] || {}, c[p].formElements = {
40
- ...o?.formElements,
37
+ const [l, c] = Ve(S);
38
+ (Object.keys(c).length > 0 || i && Object.keys(i).length > 0) && Object.keys(c).forEach((h) => {
39
+ c[h] = c[h] || {}, c[h].formElements = {
40
+ ...i?.formElements,
41
41
  // Global defaults first
42
- ...o?.validation,
43
- ...c[p].formElements || {}
42
+ ...i?.validation,
43
+ ...c[h].formElements || {}
44
44
  // State-specific overrides
45
- }, Z(p) || n.getState().setInitialStateOptions(p, c[p]);
45
+ }, Z(h) || n.getState().setInitialStateOptions(h, c[h]);
46
46
  }), n.getState().setInitialStates(l), n.getState().setCreatedState(l);
47
- const w = (p, s) => {
48
- const [v] = te(s?.componentId ?? de());
49
- me({
50
- stateKey: p,
47
+ const b = (h, s) => {
48
+ const [p] = te(s?.componentId ?? de());
49
+ ye({
50
+ stateKey: h,
51
51
  options: s,
52
52
  initialOptionsPart: c
53
53
  });
54
- const r = n.getState().cogsStateStore[p] || l[p], m = s?.modifyState ? s.modifyState(r) : r, [U, x] = De(
54
+ const r = n.getState().cogsStateStore[h] || l[h], m = s?.modifyState ? s.modifyState(r) : r, [D, x] = Ue(
55
55
  m,
56
56
  {
57
- stateKey: p,
57
+ stateKey: h,
58
58
  syncUpdate: s?.syncUpdate,
59
- componentId: v,
59
+ componentId: p,
60
60
  localStorage: s?.localStorage,
61
61
  middleware: s?.middleware,
62
62
  enabledSync: s?.enabledSync,
@@ -69,98 +69,98 @@ const et = (e, o) => {
69
69
  );
70
70
  return x;
71
71
  };
72
- function I(p, s) {
73
- me({ stateKey: p, options: s, initialOptionsPart: c }), s.localStorage && Ue(p, s), ie(p);
72
+ function I(h, s) {
73
+ ye({ stateKey: h, options: s, initialOptionsPart: c }), s.localStorage && De(h, s), ie(h);
74
74
  }
75
- return { useCogsState: w, setCogsOptions: I };
75
+ return { useCogsState: b, setCogsOptions: I };
76
76
  }, {
77
77
  setUpdaterState: ne,
78
78
  setState: J,
79
79
  getInitialOptions: Z,
80
80
  getKeyState: be,
81
- getValidationErrors: je,
82
- setStateLog: Oe,
83
- updateInitialStateGlobal: ue,
81
+ getValidationErrors: Oe,
82
+ setStateLog: je,
83
+ updateInitialStateGlobal: ge,
84
84
  addValidationError: Fe,
85
- removeValidationError: z,
86
- setServerSyncActions: Me
87
- } = n.getState(), ye = (e, o, S, l, c) => {
85
+ removeValidationError: q,
86
+ setServerSyncActions: Re
87
+ } = n.getState(), ve = (e, i, S, l, c) => {
88
88
  S?.log && console.log(
89
89
  "saving to localstorage",
90
- o,
90
+ i,
91
91
  S.localStorage?.key,
92
92
  l
93
93
  );
94
- const w = q(S?.localStorage?.key) ? S.localStorage?.key(e) : S?.localStorage?.key;
95
- if (w && l) {
96
- const I = `${l}-${o}-${w}`;
97
- let p;
94
+ const b = z(S?.localStorage?.key) ? S.localStorage?.key(e) : S?.localStorage?.key;
95
+ if (b && l) {
96
+ const I = `${l}-${i}-${b}`;
97
+ let h;
98
98
  try {
99
- p = oe(I)?.lastSyncedWithServer;
99
+ h = ae(I)?.lastSyncedWithServer;
100
100
  } catch {
101
101
  }
102
102
  const s = {
103
103
  state: e,
104
104
  lastUpdated: Date.now(),
105
- lastSyncedWithServer: c ?? p
106
- }, v = Pe.serialize(s);
105
+ lastSyncedWithServer: c ?? h
106
+ }, p = Pe.serialize(s);
107
107
  window.localStorage.setItem(
108
108
  I,
109
- JSON.stringify(v.json)
109
+ JSON.stringify(p.json)
110
110
  );
111
111
  }
112
- }, oe = (e) => {
112
+ }, ae = (e) => {
113
113
  if (!e) return null;
114
114
  try {
115
- const o = window.localStorage.getItem(e);
116
- return o ? JSON.parse(o) : null;
117
- } catch (o) {
118
- return console.error("Error loading from localStorage:", o), null;
115
+ const i = window.localStorage.getItem(e);
116
+ return i ? JSON.parse(i) : null;
117
+ } catch (i) {
118
+ return console.error("Error loading from localStorage:", i), null;
119
119
  }
120
- }, Ue = (e, o) => {
121
- const S = n.getState().cogsStateStore[e], { sessionId: l } = Ie(), c = q(o?.localStorage?.key) ? o.localStorage.key(S) : o?.localStorage?.key;
120
+ }, De = (e, i) => {
121
+ const S = n.getState().cogsStateStore[e], { sessionId: l } = Te(), c = z(i?.localStorage?.key) ? i.localStorage.key(S) : i?.localStorage?.key;
122
122
  if (c && l) {
123
- const w = oe(
123
+ const b = ae(
124
124
  `${l}-${e}-${c}`
125
125
  );
126
- if (w && w.lastUpdated > (w.lastSyncedWithServer || 0))
127
- return J(e, w.state), ie(e), !0;
126
+ if (b && b.lastUpdated > (b.lastSyncedWithServer || 0))
127
+ return J(e, b.state), ie(e), !0;
128
128
  }
129
129
  return !1;
130
- }, we = (e, o, S, l, c, w) => {
130
+ }, we = (e, i, S, l, c, b) => {
131
131
  const I = {
132
- initialState: o,
132
+ initialState: i,
133
133
  updaterState: re(
134
134
  e,
135
135
  l,
136
136
  c,
137
- w
137
+ b
138
138
  ),
139
139
  state: S
140
140
  };
141
- ue(e, I.initialState), ne(e, I.updaterState), J(e, I.state);
141
+ ge(e, I.initialState), ne(e, I.updaterState), J(e, I.state);
142
142
  }, ie = (e) => {
143
- const o = n.getState().stateComponents.get(e);
144
- if (!o) return;
143
+ const i = n.getState().stateComponents.get(e);
144
+ if (!i) return;
145
145
  const S = /* @__PURE__ */ new Set();
146
- o.components.forEach((l) => {
146
+ i.components.forEach((l) => {
147
147
  (l ? Array.isArray(l.reactiveType) ? l.reactiveType : [l.reactiveType || "component"] : null)?.includes("none") || S.add(() => l.forceUpdate());
148
148
  }), queueMicrotask(() => {
149
149
  S.forEach((l) => l());
150
150
  });
151
- }, tt = (e, o) => {
151
+ }, tt = (e, i) => {
152
152
  const S = n.getState().stateComponents.get(e);
153
153
  if (S) {
154
- const l = `${e}////${o}`, c = S.components.get(l);
154
+ const l = `${e}////${i}`, c = S.components.get(l);
155
155
  if ((c ? Array.isArray(c.reactiveType) ? c.reactiveType : [c.reactiveType || "component"] : null)?.includes("none"))
156
156
  return;
157
157
  c && c.forceUpdate();
158
158
  }
159
- }, Re = (e, o, S, l) => {
159
+ }, Me = (e, i, S, l) => {
160
160
  switch (e) {
161
161
  case "update":
162
162
  return {
163
- oldValue: W(o, l),
163
+ oldValue: W(i, l),
164
164
  newValue: W(S, l)
165
165
  };
166
166
  case "insert":
@@ -171,7 +171,7 @@ const et = (e, o) => {
171
171
  };
172
172
  case "cut":
173
173
  return {
174
- oldValue: W(o, l),
174
+ oldValue: W(i, l),
175
175
  newValue: null
176
176
  // or undefined
177
177
  };
@@ -179,237 +179,237 @@ const et = (e, o) => {
179
179
  return { oldValue: null, newValue: null };
180
180
  }
181
181
  };
182
- function Te(e, o, S) {
183
- const l = o.split(".");
182
+ function ue(e, i, S) {
183
+ const l = i.split(".");
184
184
  let c = e;
185
- for (const w of l)
186
- c.children.has(w) || c.children.set(w, {
185
+ for (const b of l)
186
+ c.children.has(b) || c.children.set(b, {
187
187
  subscribers: /* @__PURE__ */ new Set(),
188
188
  children: /* @__PURE__ */ new Map()
189
- }), c = c.children.get(w);
189
+ }), c = c.children.get(b);
190
190
  c.subscribers.add(S);
191
191
  }
192
- function Ee(e, o, S) {
193
- const l = o.split(".");
192
+ function Ee(e, i, S) {
193
+ const l = i.split(".");
194
194
  let c = e;
195
- for (const w of l)
196
- if (c = c.children.get(w), !c) return;
195
+ for (const b of l)
196
+ if (c = c.children.get(b), !c) return;
197
197
  c.subscribers.delete(S);
198
198
  }
199
- function ve(e, o) {
200
- console.log("Getting subscribers for path:", o);
201
- const S = o.split("."), l = new Set(e.subscribers);
199
+ function pe(e, i) {
200
+ console.log("Getting subscribers for path:", i);
201
+ const S = i.split("."), l = new Set(e.subscribers);
202
202
  console.log("Root subscribers:", e.subscribers);
203
203
  let c = e;
204
- for (const w of S) {
205
- if (c = c.children?.get(w), console.log(`Segment ${w} subscribers:`, c?.subscribers), !c) break;
204
+ for (const b of S) {
205
+ if (c = c.children?.get(b), console.log(`Segment ${b} subscribers:`, c?.subscribers), !c) break;
206
206
  c.subscribers.forEach((I) => l.add(I));
207
207
  }
208
208
  return console.log("Total subscribers found:", l), l;
209
209
  }
210
- function De(e, {
211
- stateKey: o,
210
+ function Ue(e, {
211
+ stateKey: i,
212
212
  serverSync: S,
213
213
  localStorage: l,
214
214
  formElements: c,
215
- reactiveDeps: w,
215
+ reactiveDeps: b,
216
216
  reactiveType: I,
217
- componentId: p,
217
+ componentId: h,
218
218
  initialState: s,
219
- syncUpdate: v,
219
+ syncUpdate: p,
220
220
  dependencies: r,
221
221
  serverState: m
222
222
  } = {}) {
223
- const [U, x] = te({}), { sessionId: j } = Ie();
224
- let R = !o;
225
- const [y] = te(o ?? de()), d = n.getState().stateLog[y], Q = ee(/* @__PURE__ */ new Set()), L = ee(p ?? de()), N = ee(
223
+ const [D, x] = te({}), { sessionId: O } = Te();
224
+ let M = !i;
225
+ const [v] = te(i ?? de()), d = n.getState().stateLog[v], Q = ee(/* @__PURE__ */ new Set()), L = ee(h ?? de()), k = ee(
226
226
  null
227
227
  );
228
- N.current = Z(y) ?? null, ce(() => {
229
- if (v && v.stateKey === y && v.path?.[0]) {
230
- J(y, (a) => ({
231
- ...a,
232
- [v.path[0]]: v.newValue
228
+ k.current = Z(v) ?? null, ce(() => {
229
+ if (p && p.stateKey === v && p.path?.[0]) {
230
+ J(v, (o) => ({
231
+ ...o,
232
+ [p.path[0]]: p.newValue
233
233
  }));
234
- const t = `${v.stateKey}:${v.path.join(".")}`;
234
+ const t = `${p.stateKey}:${p.path.join(".")}`;
235
235
  n.getState().setSyncInfo(t, {
236
- timeStamp: v.timeStamp,
237
- userId: v.userId
236
+ timeStamp: p.timeStamp,
237
+ userId: p.userId
238
238
  });
239
239
  }
240
- }, [v]), ce(() => {
240
+ }, [p]), ce(() => {
241
241
  if (s) {
242
- Se(y, {
242
+ me(v, {
243
243
  initialState: s
244
244
  });
245
- const t = N.current, i = t?.serverState?.id !== void 0 && t?.serverState?.status === "success" && t?.serverState?.data, u = n.getState().initialStateGlobal[y];
246
- if (!(u && !G(u, s) || !u) && !i)
245
+ const t = k.current, a = t?.serverState?.id !== void 0 && t?.serverState?.status === "success" && t?.serverState?.data, g = n.getState().initialStateGlobal[v];
246
+ if (!(g && !G(g, s) || !g) && !a)
247
247
  return;
248
248
  let f = null;
249
- const A = q(t?.localStorage?.key) ? t?.localStorage?.key(s) : t?.localStorage?.key;
250
- A && j && (f = oe(`${j}-${y}-${A}`));
251
- let b = s, T = !1;
252
- const C = i ? Date.now() : 0, $ = f?.lastUpdated || 0, _ = f?.lastSyncedWithServer || 0;
253
- i && C > $ ? (b = t.serverState.data, T = !0) : f && $ > _ && (b = f.state, t?.localStorage?.onChange && t?.localStorage?.onChange(b)), we(
254
- y,
249
+ const A = z(t?.localStorage?.key) ? t?.localStorage?.key(s) : t?.localStorage?.key;
250
+ A && O && (f = ae(`${O}-${v}-${A}`));
251
+ let T = s, w = !1;
252
+ const C = a ? Date.now() : 0, $ = f?.lastUpdated || 0, _ = f?.lastSyncedWithServer || 0;
253
+ a && C > $ ? (T = t.serverState.data, w = !0) : f && $ > _ && (T = f.state, t?.localStorage?.onChange && t?.localStorage?.onChange(T)), we(
254
+ v,
255
255
  s,
256
- b,
256
+ T,
257
257
  H,
258
258
  L.current,
259
- j
260
- ), T && A && j && ye(b, y, t, j, Date.now()), ie(y), (Array.isArray(I) ? I : [I || "component"]).includes("none") || x({});
259
+ O
260
+ ), w && A && O && ve(T, v, t, O, Date.now()), ie(v), (Array.isArray(I) ? I : [I || "component"]).includes("none") || x({});
261
261
  }
262
262
  }, [
263
263
  s,
264
264
  m?.status,
265
265
  m?.data,
266
266
  ...r || []
267
- ]), pe(() => {
268
- R && Se(y, {
267
+ ]), Ie(() => {
268
+ M && me(v, {
269
269
  serverSync: S,
270
270
  formElements: c,
271
271
  initialState: s,
272
272
  localStorage: l,
273
- middleware: N.current?.middleware
273
+ middleware: k.current?.middleware
274
274
  });
275
- const t = `${y}////${L.current}`, a = n.getState().stateComponents.get(y) || {
275
+ const t = `${v}////${L.current}`, o = n.getState().stateComponents.get(v) || {
276
276
  components: /* @__PURE__ */ new Map(),
277
277
  pathTrie: {
278
278
  subscribers: /* @__PURE__ */ new Set(),
279
279
  children: /* @__PURE__ */ new Map()
280
280
  }
281
281
  };
282
- return a.components.set(t, {
282
+ return o.components.set(t, {
283
283
  forceUpdate: () => x({}),
284
284
  paths: /* @__PURE__ */ new Set(),
285
285
  deps: [],
286
- depsFunction: w || void 0,
286
+ depsFunction: b || void 0,
287
287
  reactiveType: I ?? ["component", "deps"]
288
- }), n.getState().stateComponents.set(y, a), x({}), () => {
289
- const i = `${y}////${L.current}`;
290
- a && (a.components.delete(i), a.components.size === 0 && n.getState().stateComponents.delete(y));
288
+ }), n.getState().stateComponents.set(v, o), x({}), () => {
289
+ const a = `${v}////${L.current}`;
290
+ o && (o.components.delete(a), o.components.size === 0 && n.getState().stateComponents.delete(v));
291
291
  };
292
292
  }, []);
293
- const H = (t, a, i, u) => {
294
- if (Array.isArray(a)) {
295
- const h = `${y}-${a.join(".")}`;
296
- Q.current.add(h);
293
+ const H = (t, o, a, g) => {
294
+ if (Array.isArray(o)) {
295
+ const y = `${v}-${o.join(".")}`;
296
+ Q.current.add(y);
297
297
  }
298
- J(y, (h) => {
299
- const f = q(t) ? t(h) : t, A = `${y}-${a.join(".")}`;
298
+ J(v, (y) => {
299
+ const f = z(t) ? t(y) : t, A = `${v}-${o.join(".")}`;
300
300
  if (A) {
301
- let M = !1, E = n.getState().signalDomElements.get(A);
302
- if ((!E || E.size === 0) && (i.updateType === "insert" || i.updateType === "cut")) {
303
- const O = a.slice(0, -1), D = W(f, O);
304
- if (Array.isArray(D)) {
305
- M = !0;
306
- const V = `${y}-${O.join(".")}`;
307
- E = n.getState().signalDomElements.get(V);
301
+ let R = !1, E = n.getState().signalDomElements.get(A);
302
+ if ((!E || E.size === 0) && (a.updateType === "insert" || a.updateType === "cut")) {
303
+ const j = o.slice(0, -1), U = W(f, j);
304
+ if (Array.isArray(U)) {
305
+ R = !0;
306
+ const N = `${v}-${j.join(".")}`;
307
+ E = n.getState().signalDomElements.get(N);
308
308
  }
309
309
  }
310
310
  if (E) {
311
- const O = M ? W(f, a.slice(0, -1)) : W(f, a);
312
- E.forEach(({ parentId: D, position: V, effect: k }) => {
311
+ const j = R ? W(f, o.slice(0, -1)) : W(f, o);
312
+ E.forEach(({ parentId: U, position: N, effect: V }) => {
313
313
  const P = document.querySelector(
314
- `[data-parent-id="${D}"]`
314
+ `[data-parent-id="${U}"]`
315
315
  );
316
316
  if (P) {
317
317
  const B = Array.from(P.childNodes);
318
- if (B[V]) {
319
- const Y = k ? new Function("state", `return (${k})(state)`)(O) : O;
320
- B[V].textContent = String(Y);
318
+ if (B[N]) {
319
+ const Y = V ? new Function("state", `return (${V})(state)`)(j) : j;
320
+ B[N].textContent = String(Y);
321
321
  }
322
322
  }
323
323
  });
324
324
  }
325
325
  }
326
- i.updateType === "update" && (u || N.current?.validation?.key) && a && z(
327
- (u || N.current?.validation?.key) + "." + a.join(".")
326
+ a.updateType === "update" && (g || k.current?.validation?.key) && o && q(
327
+ (g || k.current?.validation?.key) + "." + o.join(".")
328
328
  );
329
- const b = a.slice(0, a.length - 1);
330
- i.updateType === "cut" && N.current?.validation?.key && z(
331
- N.current?.validation?.key + "." + b.join(".")
332
- ), i.updateType === "insert" && N.current?.validation?.key && je(
333
- N.current?.validation?.key + "." + b.join(".")
334
- ).filter(([E, O]) => {
335
- let D = E?.split(".").length;
336
- if (E == b.join(".") && D == b.length - 1) {
337
- let V = E + "." + b;
338
- z(E), Fe(V, O);
329
+ const T = o.slice(0, o.length - 1);
330
+ a.updateType === "cut" && k.current?.validation?.key && q(
331
+ k.current?.validation?.key + "." + T.join(".")
332
+ ), a.updateType === "insert" && k.current?.validation?.key && Oe(
333
+ k.current?.validation?.key + "." + T.join(".")
334
+ ).filter(([E, j]) => {
335
+ let U = E?.split(".").length;
336
+ if (E == T.join(".") && U == T.length - 1) {
337
+ let N = E + "." + T;
338
+ q(E), Fe(N, j);
339
339
  }
340
340
  });
341
- const T = n.getState().stateComponents.get(y);
342
- if (T && T.pathTrie) {
343
- const M = le(h, f), E = /* @__PURE__ */ new Set();
344
- M.forEach((V) => {
345
- ve(
346
- T.pathTrie,
347
- V
341
+ const w = n.getState().stateComponents.get(v);
342
+ if (w && w.pathTrie) {
343
+ const R = le(y, f), E = /* @__PURE__ */ new Set();
344
+ R.forEach((N) => {
345
+ pe(
346
+ w.pathTrie,
347
+ N
348
348
  ).forEach((P) => E.add(P));
349
349
  });
350
- const O = i.updateType === "update" ? a.join(".") : a.slice(0, -1).join(".") || "";
351
- ve(
352
- T.pathTrie,
353
- O
354
- ).forEach((V) => E.add(V)), console.log("componentsToUpdate", E), E.forEach((V) => {
355
- const k = T.components.get(V);
356
- if (k) {
357
- const P = Array.isArray(k.reactiveType) ? k.reactiveType : [k.reactiveType || "component"];
350
+ const j = a.updateType === "update" ? o.join(".") : o.slice(0, -1).join(".") || "";
351
+ pe(
352
+ w.pathTrie,
353
+ j
354
+ ).forEach((N) => E.add(N)), console.log("componentsToUpdate", E), E.forEach((N) => {
355
+ const V = w.components.get(N);
356
+ if (V) {
357
+ const P = Array.isArray(V.reactiveType) ? V.reactiveType : [V.reactiveType || "component"];
358
358
  if (P.includes("none")) return;
359
359
  if (P.includes("all")) {
360
- k.forceUpdate();
360
+ V.forceUpdate();
361
361
  return;
362
362
  }
363
- if (P.includes("deps") && k.depsFunction) {
364
- const B = k.depsFunction(f);
363
+ if (P.includes("deps") && V.depsFunction) {
364
+ const B = V.depsFunction(f);
365
365
  let Y = !1;
366
- if (typeof B == "boolean" ? B && (Y = !0) : G(k.deps, B) || (k.deps = B, Y = !0), Y) {
367
- k.forceUpdate();
366
+ if (typeof B == "boolean" ? B && (Y = !0) : G(V.deps, B) || (V.deps = B, Y = !0), Y) {
367
+ V.forceUpdate();
368
368
  return;
369
369
  }
370
370
  }
371
- P.includes("component") && k.forceUpdate();
371
+ P.includes("component") && V.forceUpdate();
372
372
  }
373
373
  });
374
374
  }
375
375
  const C = Date.now();
376
- a = a.map((M, E) => {
377
- const O = a.slice(0, -1), D = W(f, O);
378
- return E === a.length - 1 && ["insert", "cut"].includes(i.updateType) ? (D.length - 1).toString() : M;
376
+ o = o.map((R, E) => {
377
+ const j = o.slice(0, -1), U = W(f, j);
378
+ return E === o.length - 1 && ["insert", "cut"].includes(a.updateType) ? (U.length - 1).toString() : R;
379
379
  });
380
- const { oldValue: $, newValue: _ } = Re(
381
- i.updateType,
382
- h,
380
+ const { oldValue: $, newValue: _ } = Me(
381
+ a.updateType,
382
+ y,
383
383
  f,
384
- a
384
+ o
385
385
  ), F = {
386
386
  timeStamp: C,
387
- stateKey: y,
388
- path: a,
389
- updateType: i.updateType,
387
+ stateKey: v,
388
+ path: o,
389
+ updateType: a.updateType,
390
390
  status: "new",
391
391
  oldValue: $,
392
392
  newValue: _
393
393
  };
394
- if (Oe(y, (M) => {
395
- const O = [...M ?? [], F].reduce((D, V) => {
396
- const k = `${V.stateKey}:${JSON.stringify(V.path)}`, P = D.get(k);
397
- return P ? (P.timeStamp = Math.max(P.timeStamp, V.timeStamp), P.newValue = V.newValue, P.oldValue = P.oldValue ?? V.oldValue, P.updateType = V.updateType) : D.set(k, { ...V }), D;
394
+ if (je(v, (R) => {
395
+ const j = [...R ?? [], F].reduce((U, N) => {
396
+ const V = `${N.stateKey}:${JSON.stringify(N.path)}`, P = U.get(V);
397
+ return P ? (P.timeStamp = Math.max(P.timeStamp, N.timeStamp), P.newValue = N.newValue, P.oldValue = P.oldValue ?? N.oldValue, P.updateType = N.updateType) : U.set(V, { ...N }), U;
398
398
  }, /* @__PURE__ */ new Map());
399
- return Array.from(O.values());
400
- }), ye(
399
+ return Array.from(j.values());
400
+ }), ve(
401
401
  f,
402
- y,
403
- N.current,
404
- j
405
- ), N.current?.middleware && N.current.middleware({
402
+ v,
403
+ k.current,
404
+ O
405
+ ), k.current?.middleware && k.current.middleware({
406
406
  updateLog: d,
407
407
  update: F
408
- }), N.current?.serverSync) {
409
- const M = n.getState().serverState[y], E = N.current?.serverSync;
410
- Me(y, {
408
+ }), k.current?.serverSync) {
409
+ const R = n.getState().serverState[v], E = k.current?.serverSync;
410
+ Re(v, {
411
411
  syncKey: typeof E.syncKey == "string" ? E.syncKey : E.syncKey({ state: f }),
412
- rollBackState: M,
412
+ rollBackState: R,
413
413
  actionTimeStamp: Date.now() + (E.debounce ?? 3e3),
414
414
  status: "waiting"
415
415
  });
@@ -417,90 +417,90 @@ function De(e, {
417
417
  return f;
418
418
  });
419
419
  };
420
- n.getState().updaterState[y] || (ne(
421
- y,
420
+ n.getState().updaterState[v] || (ne(
421
+ v,
422
422
  re(
423
- y,
423
+ v,
424
424
  H,
425
425
  L.current,
426
- j
426
+ O
427
427
  )
428
- ), n.getState().cogsStateStore[y] || J(y, e), n.getState().initialStateGlobal[y] || ue(y, e));
429
- const g = $e(() => re(
430
- y,
428
+ ), n.getState().cogsStateStore[v] || J(v, e), n.getState().initialStateGlobal[v] || ge(v, e));
429
+ const u = $e(() => re(
430
+ v,
431
431
  H,
432
432
  L.current,
433
- j
434
- ), [y, j]);
435
- return [be(y), g];
433
+ O
434
+ ), [v, O]);
435
+ return [be(v), u];
436
436
  }
437
- function re(e, o, S, l) {
437
+ function re(e, i, S, l) {
438
438
  const c = /* @__PURE__ */ new Map();
439
- let w = 0;
440
- const I = (v) => {
441
- const r = v.join(".");
439
+ let b = 0;
440
+ const I = (p) => {
441
+ const r = p.join(".");
442
442
  for (const [m] of c)
443
443
  (m === r || m.startsWith(r + ".")) && c.delete(m);
444
- w++;
445
- }, p = {
446
- removeValidation: (v) => {
447
- v?.validationKey && z(v.validationKey);
444
+ b++;
445
+ }, h = {
446
+ removeValidation: (p) => {
447
+ p?.validationKey && q(p.validationKey);
448
448
  },
449
- revertToInitialState: (v) => {
449
+ revertToInitialState: (p) => {
450
450
  const r = n.getState().getInitialOptions(e)?.validation;
451
- r?.key && z(r?.key), v?.validationKey && z(v.validationKey);
451
+ r?.key && q(r?.key), p?.validationKey && q(p.validationKey);
452
452
  const m = n.getState().initialStateGlobal[e];
453
- n.getState().clearSelectedIndexesForState(e), c.clear(), w++;
454
- const U = s(m, []), x = Z(e), j = q(x?.localStorage?.key) ? x?.localStorage?.key(m) : x?.localStorage?.key, R = `${l}-${e}-${j}`;
455
- R && localStorage.removeItem(R), ne(e, U), J(e, m);
456
- const y = n.getState().stateComponents.get(e);
457
- return y && y.components.forEach((d) => {
453
+ n.getState().clearSelectedIndexesForState(e), c.clear(), b++;
454
+ const D = s(m, []), x = Z(e), O = z(x?.localStorage?.key) ? x?.localStorage?.key(m) : x?.localStorage?.key, M = `${l}-${e}-${O}`;
455
+ M && localStorage.removeItem(M), ne(e, D), J(e, m);
456
+ const v = n.getState().stateComponents.get(e);
457
+ return v && v.components.forEach((d) => {
458
458
  d.forceUpdate();
459
459
  }), m;
460
460
  },
461
- updateInitialState: (v) => {
462
- c.clear(), w++;
461
+ updateInitialState: (p) => {
462
+ c.clear(), b++;
463
463
  const r = re(
464
464
  e,
465
- o,
465
+ i,
466
466
  S,
467
467
  l
468
- ), m = n.getState().initialStateGlobal[e], U = Z(e), x = q(U?.localStorage?.key) ? U?.localStorage?.key(m) : U?.localStorage?.key, j = `${l}-${e}-${x}`;
469
- return localStorage.getItem(j) && localStorage.removeItem(j), Ve(() => {
470
- ue(e, v), ne(e, r), J(e, v);
471
- const R = n.getState().stateComponents.get(e);
472
- R && R.components.forEach((y) => {
473
- y.forceUpdate();
468
+ ), m = n.getState().initialStateGlobal[e], D = Z(e), x = z(D?.localStorage?.key) ? D?.localStorage?.key(m) : D?.localStorage?.key, O = `${l}-${e}-${x}`;
469
+ return localStorage.getItem(O) && localStorage.removeItem(O), Ne(() => {
470
+ ge(e, p), ne(e, r), J(e, p);
471
+ const M = n.getState().stateComponents.get(e);
472
+ M && M.components.forEach((v) => {
473
+ v.forceUpdate();
474
474
  });
475
475
  }), {
476
- fetchId: (R) => r.get()[R]
476
+ fetchId: (M) => r.get()[M]
477
477
  };
478
478
  },
479
479
  _initialState: n.getState().initialStateGlobal[e],
480
480
  _serverState: n.getState().serverState[e],
481
481
  _isLoading: n.getState().isLoadingGlobal[e],
482
482
  _isServerSynced: () => {
483
- const v = n.getState().serverState[e];
484
- return !!(v && G(v, be(e)));
483
+ const p = n.getState().serverState[e];
484
+ return !!(p && G(p, be(e)));
485
485
  }
486
486
  };
487
- function s(v, r = [], m) {
488
- const U = r.map(String).join(".");
489
- c.get(U);
487
+ function s(p, r = [], m) {
488
+ const D = r.map(String).join(".");
489
+ c.get(D);
490
490
  const x = function() {
491
491
  return n().getNestedState(e, r);
492
492
  };
493
- Object.keys(p).forEach((y) => {
494
- x[y] = p[y];
493
+ Object.keys(h).forEach((v) => {
494
+ x[v] = h[v];
495
495
  });
496
- const j = {
497
- apply(y, d, Q) {
496
+ const O = {
497
+ apply(v, d, Q) {
498
498
  return console.log(
499
499
  `PROXY APPLY TRAP HIT: stateKey=${e}, path=${r.join(".")}`
500
500
  ), console.trace("Apply trap stack trace"), n().getNestedState(e, r);
501
501
  },
502
- get(y, d) {
503
- m?.validIndices && !Array.isArray(v) && (m = { ...m, validIndices: void 0 });
502
+ get(v, d) {
503
+ m?.validIndices && !Array.isArray(p) && (m = { ...m, validIndices: void 0 });
504
504
  const Q = /* @__PURE__ */ new Set([
505
505
  "insert",
506
506
  "cut",
@@ -525,18 +525,35 @@ function re(e, o, S, l) {
525
525
  "getComponents"
526
526
  ]);
527
527
  if (d !== "then" && !d.startsWith("$") && d !== "stateMapNoRender" && !Q.has(d)) {
528
- const g = `${e}////${S}`, t = n.getState().stateComponents.get(e);
528
+ const u = `${e}////${S}`, t = n.getState().stateComponents.get(e);
529
529
  if (t) {
530
- const a = t.components.get(g);
531
- if (a && !a.pathsInitialized && (a.pathsInitialized = !0, !a.paths.has(""))) {
532
- const i = r.join(".");
533
- let u = !0;
534
- for (const h of a.paths)
535
- if (i.startsWith(h) && (i === h || i[h.length] === ".")) {
536
- u = !1;
530
+ const o = t.components.get(u);
531
+ if (o && !o.paths.has("")) {
532
+ const a = r.join(".");
533
+ let g = !0;
534
+ for (const y of o.paths)
535
+ if (a.startsWith(y) && (a === y || a[y.length] === ".")) {
536
+ g = !1;
537
537
  break;
538
538
  }
539
- u && a.paths.add(i);
539
+ if (g) {
540
+ console.log(
541
+ "ADDING PATH:",
542
+ a,
543
+ "for component:",
544
+ u
545
+ ), o.paths.add(a);
546
+ const y = n.getState().stateComponents.get(e);
547
+ y && y.pathTrie ? (console.log(
548
+ "ADDING TO TRIE:",
549
+ a,
550
+ u
551
+ ), ue(
552
+ y.pathTrie,
553
+ a,
554
+ u
555
+ ), console.log("TRIE AFTER ADD:", y.pathTrie)) : console.log("NO TRIE FOUND for state:", e);
556
+ }
540
557
  }
541
558
  }
542
559
  }
@@ -547,65 +564,65 @@ function re(e, o, S, l) {
547
564
  );
548
565
  if (d === "sync" && r.length === 0)
549
566
  return async function() {
550
- const g = n.getState().getInitialOptions(e), t = g?.sync;
567
+ const u = n.getState().getInitialOptions(e), t = u?.sync;
551
568
  if (!t)
552
569
  return console.error(`No mutation defined for state key "${e}"`), { success: !1, error: "No mutation defined" };
553
- const a = n.getState().getNestedState(e, []), i = g?.validation?.key;
570
+ const o = n.getState().getNestedState(e, []), a = u?.validation?.key;
554
571
  try {
555
- const u = await t.action(a);
556
- if (u && !u.success && u.errors && i) {
557
- n.getState().removeValidationError(i), u.errors.forEach((f) => {
558
- const A = [i, ...f.path].join(".");
572
+ const g = await t.action(o);
573
+ if (g && !g.success && g.errors && a) {
574
+ n.getState().removeValidationError(a), g.errors.forEach((f) => {
575
+ const A = [a, ...f.path].join(".");
559
576
  n.getState().addValidationError(A, f.message);
560
577
  });
561
- const h = n.getState().stateComponents.get(e);
562
- h && h.components.forEach((f) => {
578
+ const y = n.getState().stateComponents.get(e);
579
+ y && y.components.forEach((f) => {
563
580
  f.forceUpdate();
564
581
  });
565
582
  }
566
- return u?.success && t.onSuccess ? t.onSuccess(u.data) : !u?.success && t.onError && t.onError(u.error), u;
567
- } catch (u) {
568
- return t.onError && t.onError(u), { success: !1, error: u };
583
+ return g?.success && t.onSuccess ? t.onSuccess(g.data) : !g?.success && t.onError && t.onError(g.error), g;
584
+ } catch (g) {
585
+ return t.onError && t.onError(g), { success: !1, error: g };
569
586
  }
570
587
  };
571
588
  if (d === "_status") {
572
- const g = n.getState().getNestedState(e, r), t = n.getState().initialStateGlobal[e], a = W(t, r);
573
- return G(g, a) ? "fresh" : "stale";
589
+ const u = n.getState().getNestedState(e, r), t = n.getState().initialStateGlobal[e], o = W(t, r);
590
+ return G(u, o) ? "fresh" : "stale";
574
591
  }
575
592
  if (d === "getStatus")
576
593
  return function() {
577
- const g = n().getNestedState(
594
+ const u = n().getNestedState(
578
595
  e,
579
596
  r
580
- ), t = n.getState().initialStateGlobal[e], a = W(t, r);
581
- return G(g, a) ? "fresh" : "stale";
597
+ ), t = n.getState().initialStateGlobal[e], o = W(t, r);
598
+ return G(u, o) ? "fresh" : "stale";
582
599
  };
583
600
  if (d === "removeStorage")
584
601
  return () => {
585
- const g = n.getState().initialStateGlobal[e], t = Z(e), a = q(t?.localStorage?.key) ? t?.localStorage?.key(g) : t?.localStorage?.key, i = `${l}-${e}-${a}`;
586
- i && localStorage.removeItem(i);
602
+ const u = n.getState().initialStateGlobal[e], t = Z(e), o = z(t?.localStorage?.key) ? t?.localStorage?.key(u) : t?.localStorage?.key, a = `${l}-${e}-${o}`;
603
+ a && localStorage.removeItem(a);
587
604
  };
588
605
  if (d === "showValidationErrors")
589
606
  return () => {
590
- const g = n.getState().getInitialOptions(e)?.validation;
591
- if (!g?.key)
607
+ const u = n.getState().getInitialOptions(e)?.validation;
608
+ if (!u?.key)
592
609
  throw new Error("Validation key not found");
593
- return n.getState().getValidationErrors(g.key + "." + r.join("."));
610
+ return n.getState().getValidationErrors(u.key + "." + r.join("."));
594
611
  };
595
- if (Array.isArray(v)) {
596
- const g = () => m?.validIndices ? v.map((a, i) => ({
597
- item: a,
598
- originalIndex: m.validIndices[i]
599
- })) : n.getState().getNestedState(e, r).map((a, i) => ({
600
- item: a,
601
- originalIndex: i
612
+ if (Array.isArray(p)) {
613
+ const u = () => m?.validIndices ? p.map((o, a) => ({
614
+ item: o,
615
+ originalIndex: m.validIndices[a]
616
+ })) : n.getState().getNestedState(e, r).map((o, a) => ({
617
+ item: o,
618
+ originalIndex: a
602
619
  }));
603
620
  if (d === "getSelected")
604
621
  return () => {
605
622
  const t = n.getState().getSelectedIndex(e, r.join("."));
606
623
  if (t !== void 0)
607
624
  return s(
608
- v[t],
625
+ p[t],
609
626
  [...r, t.toString()],
610
627
  m
611
628
  );
@@ -618,39 +635,39 @@ function re(e, o, S, l) {
618
635
  return () => n.getState().getSelectedIndex(e, r.join(".")) ?? -1;
619
636
  if (d === "stateSort")
620
637
  return (t) => {
621
- const i = [...g()].sort(
638
+ const a = [...u()].sort(
622
639
  (f, A) => t(f.item, A.item)
623
- ), u = i.map(({ item: f }) => f), h = {
640
+ ), g = a.map(({ item: f }) => f), y = {
624
641
  ...m,
625
- validIndices: i.map(
642
+ validIndices: a.map(
626
643
  ({ originalIndex: f }) => f
627
644
  )
628
645
  };
629
- return s(u, r, h);
646
+ return s(g, r, y);
630
647
  };
631
648
  if (d === "stateFilter")
632
649
  return (t) => {
633
- const i = g().filter(
650
+ const a = u().filter(
634
651
  ({ item: f }, A) => t(f, A)
635
- ), u = i.map(({ item: f }) => f), h = {
652
+ ), g = a.map(({ item: f }) => f), y = {
636
653
  ...m,
637
- validIndices: i.map(
654
+ validIndices: a.map(
638
655
  ({ originalIndex: f }) => f
639
656
  )
640
657
  };
641
- return s(u, r, h);
658
+ return s(g, r, y);
642
659
  };
643
660
  if (d === "stateMap")
644
661
  return (t) => {
645
- const a = n.getState().getNestedState(e, r);
646
- return Array.isArray(a) ? a.map((i, u) => {
647
- let h;
648
- m?.validIndices && m.validIndices[u] !== void 0 ? h = m.validIndices[u] : h = u;
649
- const f = [...r, h.toString()], A = s(i, f, m);
650
- return t(i, A, {
662
+ const o = n.getState().getNestedState(e, r);
663
+ return Array.isArray(o) ? o.map((a, g) => {
664
+ let y;
665
+ m?.validIndices && m.validIndices[g] !== void 0 ? y = m.validIndices[g] : y = g;
666
+ const f = [...r, y.toString()], A = s(a, f, m);
667
+ return t(a, A, {
651
668
  register: () => {
652
- const [, T] = te({}), C = `${S}-${r.join(".")}-${h}`;
653
- pe(() => {
669
+ const [, w] = te({}), C = `${S}-${r.join(".")}-${y}`;
670
+ Ie(() => {
654
671
  const $ = `${e}////${C}`, _ = n.getState().stateComponents.get(e) || {
655
672
  components: /* @__PURE__ */ new Map(),
656
673
  pathTrie: {
@@ -659,13 +676,12 @@ function re(e, o, S, l) {
659
676
  }
660
677
  };
661
678
  return _.components.set($, {
662
- forceUpdate: () => T({}),
679
+ forceUpdate: () => w({}),
663
680
  paths: /* @__PURE__ */ new Set([f.join(".")]),
664
681
  deps: [],
665
682
  depsFunction: void 0,
666
- reactiveType: ["component"],
667
- pathsInitialized: !0
668
- }), _.pathTrie && Te(
683
+ reactiveType: ["component"]
684
+ }), _.pathTrie && ue(
669
685
  _.pathTrie,
670
686
  f.join("."),
671
687
  $
@@ -679,29 +695,29 @@ function re(e, o, S, l) {
679
695
  };
680
696
  }, [e, C]);
681
697
  },
682
- index: u,
683
- originalIndex: h
698
+ index: g,
699
+ originalIndex: y
684
700
  });
685
701
  }) : (console.warn(
686
702
  `stateMap called on a non-array value at path: ${r.join(".")}. The current value is:`,
687
- a
703
+ o
688
704
  ), null);
689
705
  };
690
706
  if (d === "stateMapNoRender")
691
- return (t) => v.map((i, u) => {
692
- let h;
693
- m?.validIndices && m.validIndices[u] !== void 0 ? h = m.validIndices[u] : h = u;
694
- const f = [...r, h.toString()], A = s(i, f, m);
707
+ return (t) => p.map((a, g) => {
708
+ let y;
709
+ m?.validIndices && m.validIndices[g] !== void 0 ? y = m.validIndices[g] : y = g;
710
+ const f = [...r, y.toString()], A = s(a, f, m);
695
711
  return t(
696
- i,
712
+ a,
697
713
  A,
698
- u,
699
- v,
700
- s(v, r, m)
714
+ g,
715
+ p,
716
+ s(p, r, m)
701
717
  );
702
718
  });
703
719
  if (d === "$stateMap")
704
- return (t) => ae(Ge, {
720
+ return (t) => oe(Ge, {
705
721
  proxy: {
706
722
  _stateKey: e,
707
723
  _path: r,
@@ -712,100 +728,100 @@ function re(e, o, S, l) {
712
728
  });
713
729
  if (d === "stateFlattenOn")
714
730
  return (t) => {
715
- const a = v;
716
- c.clear(), w++;
717
- const i = a.flatMap(
718
- (u) => u[t] ?? []
731
+ const o = p;
732
+ c.clear(), b++;
733
+ const a = o.flatMap(
734
+ (g) => g[t] ?? []
719
735
  );
720
736
  return s(
721
- i,
737
+ a,
722
738
  [...r, "[*]", t],
723
739
  m
724
740
  );
725
741
  };
726
742
  if (d === "index")
727
743
  return (t) => {
728
- const a = v[t];
729
- return s(a, [...r, t.toString()]);
744
+ const o = p[t];
745
+ return s(o, [...r, t.toString()]);
730
746
  };
731
747
  if (d === "last")
732
748
  return () => {
733
749
  const t = n.getState().getNestedState(e, r);
734
750
  if (t.length === 0) return;
735
- const a = t.length - 1, i = t[a], u = [...r, a.toString()];
736
- return s(i, u);
751
+ const o = t.length - 1, a = t[o], g = [...r, o.toString()];
752
+ return s(a, g);
737
753
  };
738
754
  if (d === "insert")
739
- return (t) => (I(r), se(o, t, r, e), s(
755
+ return (t) => (I(r), se(i, t, r, e), s(
740
756
  n.getState().getNestedState(e, r),
741
757
  r
742
758
  ));
743
759
  if (d === "uniqueInsert")
744
- return (t, a, i) => {
745
- const u = n.getState().getNestedState(e, r), h = q(t) ? t(u) : t;
760
+ return (t, o, a) => {
761
+ const g = n.getState().getNestedState(e, r), y = z(t) ? t(g) : t;
746
762
  let f = null;
747
- if (!u.some((b) => {
748
- if (a) {
749
- const C = a.every(
750
- ($) => G(b[$], h[$])
763
+ if (!g.some((T) => {
764
+ if (o) {
765
+ const C = o.every(
766
+ ($) => G(T[$], y[$])
751
767
  );
752
- return C && (f = b), C;
768
+ return C && (f = T), C;
753
769
  }
754
- const T = G(b, h);
755
- return T && (f = b), T;
770
+ const w = G(T, y);
771
+ return w && (f = T), w;
756
772
  }))
757
- I(r), se(o, h, r, e);
758
- else if (i && f) {
759
- const b = i(f), T = u.map(
760
- (C) => G(C, f) ? b : C
773
+ I(r), se(i, y, r, e);
774
+ else if (a && f) {
775
+ const T = a(f), w = g.map(
776
+ (C) => G(C, f) ? T : C
761
777
  );
762
- I(r), X(o, T, r);
778
+ I(r), X(i, w, r);
763
779
  }
764
780
  };
765
781
  if (d === "cut")
766
- return (t, a) => {
767
- if (!a?.waitForSync)
768
- return I(r), K(o, r, e, t), s(
782
+ return (t, o) => {
783
+ if (!o?.waitForSync)
784
+ return I(r), K(i, r, e, t), s(
769
785
  n.getState().getNestedState(e, r),
770
786
  r
771
787
  );
772
788
  };
773
789
  if (d === "cutByValue")
774
790
  return (t) => {
775
- for (let a = 0; a < v.length; a++)
776
- v[a] === t && K(o, r, e, a);
791
+ for (let o = 0; o < p.length; o++)
792
+ p[o] === t && K(i, r, e, o);
777
793
  };
778
794
  if (d === "toggleByValue")
779
795
  return (t) => {
780
- const a = v.findIndex((i) => i === t);
781
- a > -1 ? K(o, r, e, a) : se(o, t, r, e);
796
+ const o = p.findIndex((a) => a === t);
797
+ o > -1 ? K(i, r, e, o) : se(i, t, r, e);
782
798
  };
783
799
  if (d === "stateFind")
784
800
  return (t) => {
785
- const i = g().find(
786
- ({ item: h }, f) => t(h, f)
801
+ const a = u().find(
802
+ ({ item: y }, f) => t(y, f)
787
803
  );
788
- if (!i) return;
789
- const u = [...r, i.originalIndex.toString()];
790
- return s(i.item, u, m);
804
+ if (!a) return;
805
+ const g = [...r, a.originalIndex.toString()];
806
+ return s(a.item, g, m);
791
807
  };
792
808
  if (d === "findWith")
793
- return (t, a) => {
794
- const u = g().find(
795
- ({ item: f }) => f[t] === a
809
+ return (t, o) => {
810
+ const g = u().find(
811
+ ({ item: f }) => f[t] === o
796
812
  );
797
- if (!u) return;
798
- const h = [...r, u.originalIndex.toString()];
799
- return s(u.item, h, m);
813
+ if (!g) return;
814
+ const y = [...r, g.originalIndex.toString()];
815
+ return s(g.item, y, m);
800
816
  };
801
817
  }
802
818
  const L = r[r.length - 1];
803
819
  if (!isNaN(Number(L))) {
804
- const g = r.slice(0, -1), t = n.getState().getNestedState(e, g);
820
+ const u = r.slice(0, -1), t = n.getState().getNestedState(e, u);
805
821
  if (Array.isArray(t) && d === "cut")
806
822
  return () => K(
807
- o,
808
- g,
823
+ i,
824
+ u,
809
825
  e,
810
826
  Number(L)
811
827
  );
@@ -813,10 +829,10 @@ function re(e, o, S, l) {
813
829
  if (d === "get")
814
830
  return () => n.getState().getNestedState(e, r);
815
831
  if (d === "$derive")
816
- return (g) => he({
832
+ return (u) => he({
817
833
  _stateKey: e,
818
834
  _path: r,
819
- _effect: g.toString()
835
+ _effect: u.toString()
820
836
  });
821
837
  if (d === "$get")
822
838
  return () => he({
@@ -824,130 +840,130 @@ function re(e, o, S, l) {
824
840
  _path: r
825
841
  });
826
842
  if (d === "lastSynced") {
827
- const g = `${e}:${r.join(".")}`;
828
- return n.getState().getSyncInfo(g);
843
+ const u = `${e}:${r.join(".")}`;
844
+ return n.getState().getSyncInfo(u);
829
845
  }
830
846
  if (d == "getLocalStorage")
831
- return (g) => oe(l + "-" + e + "-" + g);
847
+ return (u) => ae(l + "-" + e + "-" + u);
832
848
  if (d === "_selected") {
833
- const g = r.slice(0, -1), t = g.join("."), a = n.getState().getNestedState(e, g);
834
- return Array.isArray(a) ? Number(r[r.length - 1]) === n.getState().getSelectedIndex(e, t) : void 0;
849
+ const u = r.slice(0, -1), t = u.join("."), o = n.getState().getNestedState(e, u);
850
+ return Array.isArray(o) ? Number(r[r.length - 1]) === n.getState().getSelectedIndex(e, t) : void 0;
835
851
  }
836
852
  if (d === "setSelected")
837
- return (g) => {
838
- const t = r.slice(0, -1), a = Number(r[r.length - 1]), i = t.join(".");
839
- g ? n.getState().setSelectedIndex(e, i, a) : n.getState().setSelectedIndex(e, i, void 0);
840
- const u = n.getState().getNestedState(e, [...t]);
841
- X(o, u, t), I(t);
853
+ return (u) => {
854
+ const t = r.slice(0, -1), o = Number(r[r.length - 1]), a = t.join(".");
855
+ u ? n.getState().setSelectedIndex(e, a, o) : n.getState().setSelectedIndex(e, a, void 0);
856
+ const g = n.getState().getNestedState(e, [...t]);
857
+ X(i, g, t), I(t);
842
858
  };
843
859
  if (d === "toggleSelected")
844
860
  return () => {
845
- const g = r.slice(0, -1), t = Number(r[r.length - 1]), a = g.join("."), i = n.getState().getSelectedIndex(e, a);
861
+ const u = r.slice(0, -1), t = Number(r[r.length - 1]), o = u.join("."), a = n.getState().getSelectedIndex(e, o);
846
862
  n.getState().setSelectedIndex(
847
863
  e,
848
- a,
849
- i === t ? void 0 : t
864
+ o,
865
+ a === t ? void 0 : t
850
866
  );
851
- const u = n.getState().getNestedState(e, [...g]);
852
- X(o, u, g), I(g);
867
+ const g = n.getState().getNestedState(e, [...u]);
868
+ X(i, g, u), I(u);
853
869
  };
854
870
  if (r.length == 0) {
855
871
  if (d === "applyJsonPatch")
856
- return (g) => {
857
- const t = n.getState().cogsStateStore[e], i = xe(t, g).newDocument;
872
+ return (u) => {
873
+ const t = n.getState().cogsStateStore[e], a = xe(t, u).newDocument;
858
874
  we(
859
875
  e,
860
876
  n.getState().initialStateGlobal[e],
877
+ a,
861
878
  i,
862
- o,
863
879
  S,
864
880
  l
865
881
  );
866
- const u = n.getState().stateComponents.get(e);
867
- if (u) {
868
- const h = le(t, i), f = new Set(h);
882
+ const g = n.getState().stateComponents.get(e);
883
+ if (g) {
884
+ const y = le(t, a), f = new Set(y);
869
885
  for (const [
870
886
  A,
871
- b
872
- ] of u.components.entries()) {
873
- let T = !1;
874
- const C = Array.isArray(b.reactiveType) ? b.reactiveType : [b.reactiveType || "component"];
887
+ T
888
+ ] of g.components.entries()) {
889
+ let w = !1;
890
+ const C = Array.isArray(T.reactiveType) ? T.reactiveType : [T.reactiveType || "component"];
875
891
  if (!C.includes("none")) {
876
892
  if (C.includes("all")) {
877
- b.forceUpdate();
893
+ T.forceUpdate();
878
894
  continue;
879
895
  }
880
- if (C.includes("component") && (b.paths.has("") && (T = !0), !T))
896
+ if (C.includes("component") && (T.paths.has("") && (w = !0), !w))
881
897
  for (const $ of f) {
882
- if (b.paths.has($)) {
883
- T = !0;
898
+ if (T.paths.has($)) {
899
+ w = !0;
884
900
  break;
885
901
  }
886
902
  let _ = $.lastIndexOf(".");
887
903
  for (; _ !== -1; ) {
888
904
  const F = $.substring(0, _);
889
- if (b.paths.has(F)) {
890
- T = !0;
905
+ if (T.paths.has(F)) {
906
+ w = !0;
891
907
  break;
892
908
  }
893
- const M = $.substring(
909
+ const R = $.substring(
894
910
  _ + 1
895
911
  );
896
- if (!isNaN(Number(M))) {
912
+ if (!isNaN(Number(R))) {
897
913
  const E = F.lastIndexOf(".");
898
914
  if (E !== -1) {
899
- const O = F.substring(
915
+ const j = F.substring(
900
916
  0,
901
917
  E
902
918
  );
903
- if (b.paths.has(O)) {
904
- T = !0;
919
+ if (T.paths.has(j)) {
920
+ w = !0;
905
921
  break;
906
922
  }
907
923
  }
908
924
  }
909
925
  _ = F.lastIndexOf(".");
910
926
  }
911
- if (T) break;
927
+ if (w) break;
912
928
  }
913
- if (!T && C.includes("deps") && b.depsFunction) {
914
- const $ = b.depsFunction(i);
929
+ if (!w && C.includes("deps") && T.depsFunction) {
930
+ const $ = T.depsFunction(a);
915
931
  let _ = !1;
916
- typeof $ == "boolean" ? $ && (_ = !0) : G(b.deps, $) || (b.deps = $, _ = !0), _ && (T = !0);
932
+ typeof $ == "boolean" ? $ && (_ = !0) : G(T.deps, $) || (T.deps = $, _ = !0), _ && (w = !0);
917
933
  }
918
- T && b.forceUpdate();
934
+ w && T.forceUpdate();
919
935
  }
920
936
  }
921
937
  }
922
938
  };
923
939
  if (d === "validateZodSchema")
924
940
  return () => {
925
- const g = n.getState().getInitialOptions(e)?.validation, t = n.getState().addValidationError;
926
- if (!g?.zodSchema)
941
+ const u = n.getState().getInitialOptions(e)?.validation, t = n.getState().addValidationError;
942
+ if (!u?.zodSchema)
927
943
  throw new Error("Zod schema not found");
928
- if (!g?.key)
944
+ if (!u?.key)
929
945
  throw new Error("Validation key not found");
930
- z(g.key);
931
- const a = n.getState().cogsStateStore[e];
946
+ q(u.key);
947
+ const o = n.getState().cogsStateStore[e];
932
948
  try {
933
- const i = n.getState().getValidationErrors(g.key);
934
- i && i.length > 0 && i.forEach(([h]) => {
935
- h && h.startsWith(g.key) && z(h);
949
+ const a = n.getState().getValidationErrors(u.key);
950
+ a && a.length > 0 && a.forEach(([y]) => {
951
+ y && y.startsWith(u.key) && q(y);
936
952
  });
937
- const u = g.zodSchema.safeParse(a);
938
- return u.success ? !0 : (u.error.errors.forEach((f) => {
939
- const A = f.path, b = f.message, T = [g.key, ...A].join(".");
940
- t(T, b);
953
+ const g = u.zodSchema.safeParse(o);
954
+ return g.success ? !0 : (g.error.errors.forEach((f) => {
955
+ const A = f.path, T = f.message, w = [u.key, ...A].join(".");
956
+ t(w, T);
941
957
  }), ie(e), !1);
942
- } catch (i) {
943
- return console.error("Zod schema validation failed", i), !1;
958
+ } catch (a) {
959
+ return console.error("Zod schema validation failed", a), !1;
944
960
  }
945
961
  };
946
962
  if (d === "_componentId") return S;
947
963
  if (d === "getComponents")
948
964
  return () => n().stateComponents.get(e);
949
965
  if (d === "getAllFormRefs")
950
- return () => fe.getState().getFormRefsByStateKey(e);
966
+ return () => Se.getState().getFormRefsByStateKey(e);
951
967
  if (d === "_initialState")
952
968
  return n.getState().initialStateGlobal[e];
953
969
  if (d === "_serverState")
@@ -955,17 +971,17 @@ function re(e, o, S, l) {
955
971
  if (d === "_isLoading")
956
972
  return n.getState().isLoadingGlobal[e];
957
973
  if (d === "revertToInitialState")
958
- return p.revertToInitialState;
959
- if (d === "updateInitialState") return p.updateInitialState;
960
- if (d === "removeValidation") return p.removeValidation;
974
+ return h.revertToInitialState;
975
+ if (d === "updateInitialState") return h.updateInitialState;
976
+ if (d === "removeValidation") return h.removeValidation;
961
977
  }
962
978
  if (d === "getFormRef")
963
- return () => fe.getState().getFormRef(e + "." + r.join("."));
979
+ return () => Se.getState().getFormRef(e + "." + r.join("."));
964
980
  if (d === "validationWrapper")
965
981
  return ({
966
- children: g,
982
+ children: u,
967
983
  hideMessage: t
968
- }) => /* @__PURE__ */ ge(
984
+ }) => /* @__PURE__ */ fe(
969
985
  Ce,
970
986
  {
971
987
  formOpts: t ? { validation: { message: "" } } : void 0,
@@ -973,83 +989,83 @@ function re(e, o, S, l) {
973
989
  validationKey: n.getState().getInitialOptions(e)?.validation?.key || "",
974
990
  stateKey: e,
975
991
  validIndices: m?.validIndices,
976
- children: g
992
+ children: u
977
993
  }
978
994
  );
979
995
  if (d === "_stateKey") return e;
980
996
  if (d === "_path") return r;
981
- if (d === "_isServerSynced") return p._isServerSynced;
997
+ if (d === "_isServerSynced") return h._isServerSynced;
982
998
  if (d === "update")
983
- return (g, t) => {
999
+ return (u, t) => {
984
1000
  if (t?.debounce)
985
- Ne(() => {
986
- X(o, g, r, "");
987
- const a = n.getState().getNestedState(e, r);
988
- t?.afterUpdate && t.afterUpdate(a);
1001
+ ke(() => {
1002
+ X(i, u, r, "");
1003
+ const o = n.getState().getNestedState(e, r);
1004
+ t?.afterUpdate && t.afterUpdate(o);
989
1005
  }, t.debounce);
990
1006
  else {
991
- X(o, g, r, "");
992
- const a = n.getState().getNestedState(e, r);
993
- t?.afterUpdate && t.afterUpdate(a);
1007
+ X(i, u, r, "");
1008
+ const o = n.getState().getNestedState(e, r);
1009
+ t?.afterUpdate && t.afterUpdate(o);
994
1010
  }
995
1011
  I(r);
996
1012
  };
997
1013
  if (d === "formElement")
998
- return (g, t) => /* @__PURE__ */ ge(
1014
+ return (u, t) => /* @__PURE__ */ fe(
999
1015
  _e,
1000
1016
  {
1001
- setState: o,
1017
+ setState: i,
1002
1018
  stateKey: e,
1003
1019
  path: r,
1004
- child: g,
1020
+ child: u,
1005
1021
  formOpts: t
1006
1022
  }
1007
1023
  );
1008
- const N = [...r, d], H = n.getState().getNestedState(e, N);
1009
- return s(H, N, m);
1024
+ const k = [...r, d], H = n.getState().getNestedState(e, k);
1025
+ return s(H, k, m);
1010
1026
  }
1011
- }, R = new Proxy(x, j);
1012
- return c.set(U, {
1013
- proxy: R,
1014
- stateVersion: w
1015
- }), R;
1027
+ }, M = new Proxy(x, O);
1028
+ return c.set(D, {
1029
+ proxy: M,
1030
+ stateVersion: b
1031
+ }), M;
1016
1032
  }
1017
1033
  return s(
1018
1034
  n.getState().getNestedState(e, [])
1019
1035
  );
1020
1036
  }
1021
1037
  function he(e) {
1022
- return ae(We, { proxy: e });
1038
+ return oe(We, { proxy: e });
1023
1039
  }
1024
1040
  function Ge({
1025
1041
  proxy: e,
1026
- rebuildStateShape: o
1042
+ rebuildStateShape: i
1027
1043
  }) {
1028
1044
  const S = n().getNestedState(e._stateKey, e._path);
1029
- return Array.isArray(S) ? o(
1045
+ return Array.isArray(S) ? i(
1030
1046
  S,
1031
1047
  e._path
1032
1048
  ).stateMapNoRender(
1033
- (c, w, I, p, s) => e._mapFn(c, w, I, p, s)
1049
+ (c, b, I, h, s) => e._mapFn(c, b, I, h, s)
1034
1050
  ) : null;
1035
1051
  }
1036
1052
  function We({
1037
1053
  proxy: e
1038
1054
  }) {
1039
- const o = ee(null), S = `${e._stateKey}-${e._path.join(".")}`;
1055
+ const i = ee(null), S = `${e._stateKey}-${e._path.join(".")}`;
1040
1056
  return ce(() => {
1041
- const l = o.current;
1057
+ const l = i.current;
1042
1058
  if (!l || !l.parentElement) return;
1043
1059
  const c = l.parentElement, I = Array.from(c.childNodes).indexOf(l);
1044
- let p = c.getAttribute("data-parent-id");
1045
- p || (p = `parent-${crypto.randomUUID()}`, c.setAttribute("data-parent-id", p));
1046
- const v = {
1060
+ let h = c.getAttribute("data-parent-id");
1061
+ h || (h = `parent-${crypto.randomUUID()}`, c.setAttribute("data-parent-id", h));
1062
+ const p = {
1047
1063
  instanceId: `instance-${crypto.randomUUID()}`,
1048
- parentId: p,
1064
+ parentId: h,
1049
1065
  position: I,
1050
1066
  effect: e._effect
1051
1067
  };
1052
- n.getState().addSignalElement(S, v);
1068
+ n.getState().addSignalElement(S, p);
1053
1069
  const r = n.getState().getNestedState(e._stateKey, e._path);
1054
1070
  let m;
1055
1071
  if (e._effect)
@@ -1064,16 +1080,16 @@ function We({
1064
1080
  else
1065
1081
  m = r;
1066
1082
  m !== null && typeof m == "object" && (m = JSON.stringify(m));
1067
- const U = document.createTextNode(String(m));
1068
- l.replaceWith(U);
1069
- }, [e._stateKey, e._path.join("."), e._effect]), ae("span", {
1070
- ref: o,
1083
+ const D = document.createTextNode(String(m));
1084
+ l.replaceWith(D);
1085
+ }, [e._stateKey, e._path.join("."), e._effect]), oe("span", {
1086
+ ref: i,
1071
1087
  style: { display: "none" },
1072
1088
  "data-signal-id": S
1073
1089
  });
1074
1090
  }
1075
1091
  function nt(e) {
1076
- const o = Ae(
1092
+ const i = Ae(
1077
1093
  (S) => {
1078
1094
  const l = n.getState().stateComponents.get(e._stateKey) || {
1079
1095
  components: /* @__PURE__ */ new Map(),
@@ -1087,9 +1103,8 @@ function nt(e) {
1087
1103
  paths: /* @__PURE__ */ new Set([e._path.join(".")]),
1088
1104
  deps: [],
1089
1105
  depsFunction: void 0,
1090
- reactiveType: ["component"],
1091
- pathsInitialized: !0
1092
- }), l.pathTrie && Te(l.pathTrie, e._path.join("."), c), n.getState().stateComponents.set(e._stateKey, l), () => {
1106
+ reactiveType: ["component"]
1107
+ }), l.pathTrie && ue(l.pathTrie, e._path.join("."), c), n.getState().stateComponents.set(e._stateKey, l), () => {
1093
1108
  l.components.delete(c), l.pathTrie && Ee(
1094
1109
  l.pathTrie,
1095
1110
  e._path.join("."),
@@ -1099,7 +1114,7 @@ function nt(e) {
1099
1114
  },
1100
1115
  () => n.getState().getNestedState(e._stateKey, e._path)
1101
1116
  );
1102
- return ae("text", {}, String(o));
1117
+ return oe("text", {}, String(i));
1103
1118
  }
1104
1119
  export {
1105
1120
  he as $cogsSignal,
@@ -1107,6 +1122,6 @@ export {
1107
1122
  Ke as addStateOptions,
1108
1123
  et as createCogsState,
1109
1124
  tt as notifyComponent,
1110
- De as useCogsStateFn
1125
+ Ue as useCogsStateFn
1111
1126
  };
1112
1127
  //# sourceMappingURL=CogsState.jsx.map