cogsbox-state 0.5.238 → 0.5.240

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
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 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";
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
9
  import { getGlobalStore as n, formRefStore as Se } from "./store.js";
10
- import { useCogsConfig as be } from "./CogsStateClient.jsx";
10
+ import { useCogsConfig as Te } from "./CogsStateClient.jsx";
11
11
  import { applyPatch as xe } from "fast-json-patch";
12
- function me(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
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 [h] = te(s?.componentId ?? de());
47
+ const b = (h, s) => {
48
+ const [p] = te(s?.componentId ?? de());
49
49
  ye({
50
- stateKey: p,
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: h,
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
- ye({ 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
- getKeyState: we,
81
- getValidationErrors: je,
82
- setStateLog: Oe,
80
+ getKeyState: be,
81
+ getValidationErrors: Oe,
82
+ setStateLog: je,
83
83
  updateInitialStateGlobal: ge,
84
84
  addValidationError: Fe,
85
- removeValidationError: z,
86
- setServerSyncActions: Me
87
- } = n.getState(), ve = (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
- }, h = Pe.serialize(s);
105
+ lastSyncedWithServer: c ?? h
106
+ }, p = Pe.serialize(s);
107
107
  window.localStorage.setItem(
108
108
  I,
109
- JSON.stringify(h.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 } = be(), 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
- }, Te = (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
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,85 +179,85 @@ const et = (e, o) => {
179
179
  return { oldValue: null, newValue: null };
180
180
  }
181
181
  };
182
- function ue(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 he(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: h,
219
+ syncUpdate: p,
220
220
  dependencies: r,
221
221
  serverState: m
222
222
  } = {}) {
223
- const [U, x] = te({}), { sessionId: j } = be();
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 (h && h.stateKey === y && h.path?.[0]) {
230
- J(y, (a) => ({
231
- ...a,
232
- [h.path[0]]: h.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 = `${h.stateKey}:${h.path.join(".")}`;
234
+ const t = `${p.stateKey}:${p.path.join(".")}`;
235
235
  n.getState().setSyncInfo(t, {
236
- timeStamp: h.timeStamp,
237
- userId: h.userId
236
+ timeStamp: p.timeStamp,
237
+ userId: p.userId
238
238
  });
239
239
  }
240
- }, [h]), ce(() => {
240
+ }, [p]), ce(() => {
241
241
  if (s) {
242
- me(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)), Te(
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 && ve(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,
@@ -265,151 +265,151 @@ function De(e, {
265
265
  m?.data,
266
266
  ...r || []
267
267
  ]), Ie(() => {
268
- R && me(y, {
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 v = `${y}-${a.join(".")}`;
296
- Q.current.add(v);
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, (v) => {
299
- const f = q(t) ? t(v) : 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(v, f), E = /* @__PURE__ */ new Set();
344
- M.forEach((V) => {
345
- he(
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
- he(
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
- v,
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());
399
+ return Array.from(j.values());
400
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] || ge(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 [we(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 = (h) => {
441
- const r = h.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: (h) => {
447
- h?.validationKey && z(h.validationKey);
444
+ b++;
445
+ }, h = {
446
+ removeValidation: (p) => {
447
+ p?.validationKey && q(p.validationKey);
448
448
  },
449
- revertToInitialState: (h) => {
449
+ revertToInitialState: (p) => {
450
450
  const r = n.getState().getInitialOptions(e)?.validation;
451
- r?.key && z(r?.key), h?.validationKey && z(h.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: (h) => {
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
- ge(e, h), ne(e, r), J(e, h);
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 h = n.getState().serverState[e];
484
- return !!(h && G(h, we(e)));
483
+ const p = n.getState().serverState[e];
484
+ return !!(p && G(p, be(e)));
485
485
  }
486
486
  };
487
- function s(h, 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(h) && (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,25 +525,34 @@ 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);
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 v of a.paths)
535
- if (i.startsWith(v) && (i === v || i[v.length] === ".")) {
536
- u = !1;
528
+ const u = `${e}////${S}`, t = n.getState().stateComponents.get(e);
529
+ if (console.log("stateEntry", t), t) {
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
- if (u) {
540
- a.paths.add(i);
541
- const v = n.getState().stateComponents.get(e);
542
- v && v.pathTrie && ue(
543
- v.pathTrie,
544
- i,
545
- g
546
- );
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);
547
556
  }
548
557
  }
549
558
  }
@@ -555,65 +564,65 @@ function re(e, o, S, l) {
555
564
  );
556
565
  if (d === "sync" && r.length === 0)
557
566
  return async function() {
558
- const g = n.getState().getInitialOptions(e), t = g?.sync;
567
+ const u = n.getState().getInitialOptions(e), t = u?.sync;
559
568
  if (!t)
560
569
  return console.error(`No mutation defined for state key "${e}"`), { success: !1, error: "No mutation defined" };
561
- const a = n.getState().getNestedState(e, []), i = g?.validation?.key;
570
+ const o = n.getState().getNestedState(e, []), a = u?.validation?.key;
562
571
  try {
563
- const u = await t.action(a);
564
- if (u && !u.success && u.errors && i) {
565
- n.getState().removeValidationError(i), u.errors.forEach((f) => {
566
- 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(".");
567
576
  n.getState().addValidationError(A, f.message);
568
577
  });
569
- const v = n.getState().stateComponents.get(e);
570
- v && v.components.forEach((f) => {
578
+ const y = n.getState().stateComponents.get(e);
579
+ y && y.components.forEach((f) => {
571
580
  f.forceUpdate();
572
581
  });
573
582
  }
574
- return u?.success && t.onSuccess ? t.onSuccess(u.data) : !u?.success && t.onError && t.onError(u.error), u;
575
- } catch (u) {
576
- 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 };
577
586
  }
578
587
  };
579
588
  if (d === "_status") {
580
- const g = n.getState().getNestedState(e, r), t = n.getState().initialStateGlobal[e], a = W(t, r);
581
- 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";
582
591
  }
583
592
  if (d === "getStatus")
584
593
  return function() {
585
- const g = n().getNestedState(
594
+ const u = n().getNestedState(
586
595
  e,
587
596
  r
588
- ), t = n.getState().initialStateGlobal[e], a = W(t, r);
589
- return G(g, a) ? "fresh" : "stale";
597
+ ), t = n.getState().initialStateGlobal[e], o = W(t, r);
598
+ return G(u, o) ? "fresh" : "stale";
590
599
  };
591
600
  if (d === "removeStorage")
592
601
  return () => {
593
- 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}`;
594
- 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);
595
604
  };
596
605
  if (d === "showValidationErrors")
597
606
  return () => {
598
- const g = n.getState().getInitialOptions(e)?.validation;
599
- if (!g?.key)
607
+ const u = n.getState().getInitialOptions(e)?.validation;
608
+ if (!u?.key)
600
609
  throw new Error("Validation key not found");
601
- return n.getState().getValidationErrors(g.key + "." + r.join("."));
610
+ return n.getState().getValidationErrors(u.key + "." + r.join("."));
602
611
  };
603
- if (Array.isArray(h)) {
604
- const g = () => m?.validIndices ? h.map((a, i) => ({
605
- item: a,
606
- originalIndex: m.validIndices[i]
607
- })) : n.getState().getNestedState(e, r).map((a, i) => ({
608
- item: a,
609
- 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
610
619
  }));
611
620
  if (d === "getSelected")
612
621
  return () => {
613
622
  const t = n.getState().getSelectedIndex(e, r.join("."));
614
623
  if (t !== void 0)
615
624
  return s(
616
- h[t],
625
+ p[t],
617
626
  [...r, t.toString()],
618
627
  m
619
628
  );
@@ -626,38 +635,38 @@ function re(e, o, S, l) {
626
635
  return () => n.getState().getSelectedIndex(e, r.join(".")) ?? -1;
627
636
  if (d === "stateSort")
628
637
  return (t) => {
629
- const i = [...g()].sort(
638
+ const a = [...u()].sort(
630
639
  (f, A) => t(f.item, A.item)
631
- ), u = i.map(({ item: f }) => f), v = {
640
+ ), g = a.map(({ item: f }) => f), y = {
632
641
  ...m,
633
- validIndices: i.map(
642
+ validIndices: a.map(
634
643
  ({ originalIndex: f }) => f
635
644
  )
636
645
  };
637
- return s(u, r, v);
646
+ return s(g, r, y);
638
647
  };
639
648
  if (d === "stateFilter")
640
649
  return (t) => {
641
- const i = g().filter(
650
+ const a = u().filter(
642
651
  ({ item: f }, A) => t(f, A)
643
- ), u = i.map(({ item: f }) => f), v = {
652
+ ), g = a.map(({ item: f }) => f), y = {
644
653
  ...m,
645
- validIndices: i.map(
654
+ validIndices: a.map(
646
655
  ({ originalIndex: f }) => f
647
656
  )
648
657
  };
649
- return s(u, r, v);
658
+ return s(g, r, y);
650
659
  };
651
660
  if (d === "stateMap")
652
661
  return (t) => {
653
- const a = n.getState().getNestedState(e, r);
654
- return Array.isArray(a) ? a.map((i, u) => {
655
- let v;
656
- m?.validIndices && m.validIndices[u] !== void 0 ? v = m.validIndices[u] : v = u;
657
- const f = [...r, v.toString()], A = s(i, f, m);
658
- 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, {
659
668
  register: () => {
660
- const [, T] = te({}), C = `${S}-${r.join(".")}-${v}`;
669
+ const [, w] = te({}), C = `${S}-${r.join(".")}-${y}`;
661
670
  Ie(() => {
662
671
  const $ = `${e}////${C}`, _ = n.getState().stateComponents.get(e) || {
663
672
  components: /* @__PURE__ */ new Map(),
@@ -667,12 +676,11 @@ function re(e, o, S, l) {
667
676
  }
668
677
  };
669
678
  return _.components.set($, {
670
- forceUpdate: () => T({}),
679
+ forceUpdate: () => w({}),
671
680
  paths: /* @__PURE__ */ new Set([f.join(".")]),
672
681
  deps: [],
673
682
  depsFunction: void 0,
674
- reactiveType: ["component"],
675
- pathsInitialized: !0
683
+ reactiveType: ["component"]
676
684
  }), _.pathTrie && ue(
677
685
  _.pathTrie,
678
686
  f.join("."),
@@ -687,29 +695,29 @@ function re(e, o, S, l) {
687
695
  };
688
696
  }, [e, C]);
689
697
  },
690
- index: u,
691
- originalIndex: v
698
+ index: g,
699
+ originalIndex: y
692
700
  });
693
701
  }) : (console.warn(
694
702
  `stateMap called on a non-array value at path: ${r.join(".")}. The current value is:`,
695
- a
703
+ o
696
704
  ), null);
697
705
  };
698
706
  if (d === "stateMapNoRender")
699
- return (t) => h.map((i, u) => {
700
- let v;
701
- m?.validIndices && m.validIndices[u] !== void 0 ? v = m.validIndices[u] : v = u;
702
- const f = [...r, v.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);
703
711
  return t(
704
- i,
712
+ a,
705
713
  A,
706
- u,
707
- h,
708
- s(h, r, m)
714
+ g,
715
+ p,
716
+ s(p, r, m)
709
717
  );
710
718
  });
711
719
  if (d === "$stateMap")
712
- return (t) => ae(Ge, {
720
+ return (t) => oe(Ge, {
713
721
  proxy: {
714
722
  _stateKey: e,
715
723
  _path: r,
@@ -720,100 +728,100 @@ function re(e, o, S, l) {
720
728
  });
721
729
  if (d === "stateFlattenOn")
722
730
  return (t) => {
723
- const a = h;
724
- c.clear(), w++;
725
- const i = a.flatMap(
726
- (u) => u[t] ?? []
731
+ const o = p;
732
+ c.clear(), b++;
733
+ const a = o.flatMap(
734
+ (g) => g[t] ?? []
727
735
  );
728
736
  return s(
729
- i,
737
+ a,
730
738
  [...r, "[*]", t],
731
739
  m
732
740
  );
733
741
  };
734
742
  if (d === "index")
735
743
  return (t) => {
736
- const a = h[t];
737
- return s(a, [...r, t.toString()]);
744
+ const o = p[t];
745
+ return s(o, [...r, t.toString()]);
738
746
  };
739
747
  if (d === "last")
740
748
  return () => {
741
749
  const t = n.getState().getNestedState(e, r);
742
750
  if (t.length === 0) return;
743
- const a = t.length - 1, i = t[a], u = [...r, a.toString()];
744
- return s(i, u);
751
+ const o = t.length - 1, a = t[o], g = [...r, o.toString()];
752
+ return s(a, g);
745
753
  };
746
754
  if (d === "insert")
747
- return (t) => (I(r), se(o, t, r, e), s(
755
+ return (t) => (I(r), se(i, t, r, e), s(
748
756
  n.getState().getNestedState(e, r),
749
757
  r
750
758
  ));
751
759
  if (d === "uniqueInsert")
752
- return (t, a, i) => {
753
- const u = n.getState().getNestedState(e, r), v = q(t) ? t(u) : t;
760
+ return (t, o, a) => {
761
+ const g = n.getState().getNestedState(e, r), y = z(t) ? t(g) : t;
754
762
  let f = null;
755
- if (!u.some((b) => {
756
- if (a) {
757
- const C = a.every(
758
- ($) => G(b[$], v[$])
763
+ if (!g.some((T) => {
764
+ if (o) {
765
+ const C = o.every(
766
+ ($) => G(T[$], y[$])
759
767
  );
760
- return C && (f = b), C;
768
+ return C && (f = T), C;
761
769
  }
762
- const T = G(b, v);
763
- return T && (f = b), T;
770
+ const w = G(T, y);
771
+ return w && (f = T), w;
764
772
  }))
765
- I(r), se(o, v, r, e);
766
- else if (i && f) {
767
- const b = i(f), T = u.map(
768
- (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
769
777
  );
770
- I(r), X(o, T, r);
778
+ I(r), X(i, w, r);
771
779
  }
772
780
  };
773
781
  if (d === "cut")
774
- return (t, a) => {
775
- if (!a?.waitForSync)
776
- 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(
777
785
  n.getState().getNestedState(e, r),
778
786
  r
779
787
  );
780
788
  };
781
789
  if (d === "cutByValue")
782
790
  return (t) => {
783
- for (let a = 0; a < h.length; a++)
784
- h[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);
785
793
  };
786
794
  if (d === "toggleByValue")
787
795
  return (t) => {
788
- const a = h.findIndex((i) => i === t);
789
- 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);
790
798
  };
791
799
  if (d === "stateFind")
792
800
  return (t) => {
793
- const i = g().find(
794
- ({ item: v }, f) => t(v, f)
801
+ const a = u().find(
802
+ ({ item: y }, f) => t(y, f)
795
803
  );
796
- if (!i) return;
797
- const u = [...r, i.originalIndex.toString()];
798
- return s(i.item, u, m);
804
+ if (!a) return;
805
+ const g = [...r, a.originalIndex.toString()];
806
+ return s(a.item, g, m);
799
807
  };
800
808
  if (d === "findWith")
801
- return (t, a) => {
802
- const u = g().find(
803
- ({ item: f }) => f[t] === a
809
+ return (t, o) => {
810
+ const g = u().find(
811
+ ({ item: f }) => f[t] === o
804
812
  );
805
- if (!u) return;
806
- const v = [...r, u.originalIndex.toString()];
807
- return s(u.item, v, m);
813
+ if (!g) return;
814
+ const y = [...r, g.originalIndex.toString()];
815
+ return s(g.item, y, m);
808
816
  };
809
817
  }
810
818
  const L = r[r.length - 1];
811
819
  if (!isNaN(Number(L))) {
812
- 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);
813
821
  if (Array.isArray(t) && d === "cut")
814
822
  return () => K(
815
- o,
816
- g,
823
+ i,
824
+ u,
817
825
  e,
818
826
  Number(L)
819
827
  );
@@ -821,134 +829,134 @@ function re(e, o, S, l) {
821
829
  if (d === "get")
822
830
  return () => n.getState().getNestedState(e, r);
823
831
  if (d === "$derive")
824
- return (g) => pe({
832
+ return (u) => he({
825
833
  _stateKey: e,
826
834
  _path: r,
827
- _effect: g.toString()
835
+ _effect: u.toString()
828
836
  });
829
837
  if (d === "$get")
830
- return () => pe({
838
+ return () => he({
831
839
  _stateKey: e,
832
840
  _path: r
833
841
  });
834
842
  if (d === "lastSynced") {
835
- const g = `${e}:${r.join(".")}`;
836
- return n.getState().getSyncInfo(g);
843
+ const u = `${e}:${r.join(".")}`;
844
+ return n.getState().getSyncInfo(u);
837
845
  }
838
846
  if (d == "getLocalStorage")
839
- return (g) => oe(l + "-" + e + "-" + g);
847
+ return (u) => ae(l + "-" + e + "-" + u);
840
848
  if (d === "_selected") {
841
- const g = r.slice(0, -1), t = g.join("."), a = n.getState().getNestedState(e, g);
842
- 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;
843
851
  }
844
852
  if (d === "setSelected")
845
- return (g) => {
846
- const t = r.slice(0, -1), a = Number(r[r.length - 1]), i = t.join(".");
847
- g ? n.getState().setSelectedIndex(e, i, a) : n.getState().setSelectedIndex(e, i, void 0);
848
- const u = n.getState().getNestedState(e, [...t]);
849
- 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);
850
858
  };
851
859
  if (d === "toggleSelected")
852
860
  return () => {
853
- 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);
854
862
  n.getState().setSelectedIndex(
855
863
  e,
856
- a,
857
- i === t ? void 0 : t
864
+ o,
865
+ a === t ? void 0 : t
858
866
  );
859
- const u = n.getState().getNestedState(e, [...g]);
860
- X(o, u, g), I(g);
867
+ const g = n.getState().getNestedState(e, [...u]);
868
+ X(i, g, u), I(u);
861
869
  };
862
870
  if (r.length == 0) {
863
871
  if (d === "applyJsonPatch")
864
- return (g) => {
865
- const t = n.getState().cogsStateStore[e], i = xe(t, g).newDocument;
866
- Te(
872
+ return (u) => {
873
+ const t = n.getState().cogsStateStore[e], a = xe(t, u).newDocument;
874
+ we(
867
875
  e,
868
876
  n.getState().initialStateGlobal[e],
877
+ a,
869
878
  i,
870
- o,
871
879
  S,
872
880
  l
873
881
  );
874
- const u = n.getState().stateComponents.get(e);
875
- if (u) {
876
- const v = le(t, i), f = new Set(v);
882
+ const g = n.getState().stateComponents.get(e);
883
+ if (g) {
884
+ const y = le(t, a), f = new Set(y);
877
885
  for (const [
878
886
  A,
879
- b
880
- ] of u.components.entries()) {
881
- let T = !1;
882
- 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"];
883
891
  if (!C.includes("none")) {
884
892
  if (C.includes("all")) {
885
- b.forceUpdate();
893
+ T.forceUpdate();
886
894
  continue;
887
895
  }
888
- if (C.includes("component") && (b.paths.has("") && (T = !0), !T))
896
+ if (C.includes("component") && (T.paths.has("") && (w = !0), !w))
889
897
  for (const $ of f) {
890
- if (b.paths.has($)) {
891
- T = !0;
898
+ if (T.paths.has($)) {
899
+ w = !0;
892
900
  break;
893
901
  }
894
902
  let _ = $.lastIndexOf(".");
895
903
  for (; _ !== -1; ) {
896
904
  const F = $.substring(0, _);
897
- if (b.paths.has(F)) {
898
- T = !0;
905
+ if (T.paths.has(F)) {
906
+ w = !0;
899
907
  break;
900
908
  }
901
- const M = $.substring(
909
+ const R = $.substring(
902
910
  _ + 1
903
911
  );
904
- if (!isNaN(Number(M))) {
912
+ if (!isNaN(Number(R))) {
905
913
  const E = F.lastIndexOf(".");
906
914
  if (E !== -1) {
907
- const O = F.substring(
915
+ const j = F.substring(
908
916
  0,
909
917
  E
910
918
  );
911
- if (b.paths.has(O)) {
912
- T = !0;
919
+ if (T.paths.has(j)) {
920
+ w = !0;
913
921
  break;
914
922
  }
915
923
  }
916
924
  }
917
925
  _ = F.lastIndexOf(".");
918
926
  }
919
- if (T) break;
927
+ if (w) break;
920
928
  }
921
- if (!T && C.includes("deps") && b.depsFunction) {
922
- const $ = b.depsFunction(i);
929
+ if (!w && C.includes("deps") && T.depsFunction) {
930
+ const $ = T.depsFunction(a);
923
931
  let _ = !1;
924
- typeof $ == "boolean" ? $ && (_ = !0) : G(b.deps, $) || (b.deps = $, _ = !0), _ && (T = !0);
932
+ typeof $ == "boolean" ? $ && (_ = !0) : G(T.deps, $) || (T.deps = $, _ = !0), _ && (w = !0);
925
933
  }
926
- T && b.forceUpdate();
934
+ w && T.forceUpdate();
927
935
  }
928
936
  }
929
937
  }
930
938
  };
931
939
  if (d === "validateZodSchema")
932
940
  return () => {
933
- const g = n.getState().getInitialOptions(e)?.validation, t = n.getState().addValidationError;
934
- if (!g?.zodSchema)
941
+ const u = n.getState().getInitialOptions(e)?.validation, t = n.getState().addValidationError;
942
+ if (!u?.zodSchema)
935
943
  throw new Error("Zod schema not found");
936
- if (!g?.key)
944
+ if (!u?.key)
937
945
  throw new Error("Validation key not found");
938
- z(g.key);
939
- const a = n.getState().cogsStateStore[e];
946
+ q(u.key);
947
+ const o = n.getState().cogsStateStore[e];
940
948
  try {
941
- const i = n.getState().getValidationErrors(g.key);
942
- i && i.length > 0 && i.forEach(([v]) => {
943
- v && v.startsWith(g.key) && z(v);
949
+ const a = n.getState().getValidationErrors(u.key);
950
+ a && a.length > 0 && a.forEach(([y]) => {
951
+ y && y.startsWith(u.key) && q(y);
944
952
  });
945
- const u = g.zodSchema.safeParse(a);
946
- return u.success ? !0 : (u.error.errors.forEach((f) => {
947
- const A = f.path, b = f.message, T = [g.key, ...A].join(".");
948
- 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);
949
957
  }), ie(e), !1);
950
- } catch (i) {
951
- return console.error("Zod schema validation failed", i), !1;
958
+ } catch (a) {
959
+ return console.error("Zod schema validation failed", a), !1;
952
960
  }
953
961
  };
954
962
  if (d === "_componentId") return S;
@@ -963,15 +971,15 @@ function re(e, o, S, l) {
963
971
  if (d === "_isLoading")
964
972
  return n.getState().isLoadingGlobal[e];
965
973
  if (d === "revertToInitialState")
966
- return p.revertToInitialState;
967
- if (d === "updateInitialState") return p.updateInitialState;
968
- if (d === "removeValidation") return p.removeValidation;
974
+ return h.revertToInitialState;
975
+ if (d === "updateInitialState") return h.updateInitialState;
976
+ if (d === "removeValidation") return h.removeValidation;
969
977
  }
970
978
  if (d === "getFormRef")
971
979
  return () => Se.getState().getFormRef(e + "." + r.join("."));
972
980
  if (d === "validationWrapper")
973
981
  return ({
974
- children: g,
982
+ children: u,
975
983
  hideMessage: t
976
984
  }) => /* @__PURE__ */ fe(
977
985
  Ce,
@@ -981,83 +989,83 @@ function re(e, o, S, l) {
981
989
  validationKey: n.getState().getInitialOptions(e)?.validation?.key || "",
982
990
  stateKey: e,
983
991
  validIndices: m?.validIndices,
984
- children: g
992
+ children: u
985
993
  }
986
994
  );
987
995
  if (d === "_stateKey") return e;
988
996
  if (d === "_path") return r;
989
- if (d === "_isServerSynced") return p._isServerSynced;
997
+ if (d === "_isServerSynced") return h._isServerSynced;
990
998
  if (d === "update")
991
- return (g, t) => {
999
+ return (u, t) => {
992
1000
  if (t?.debounce)
993
- Ne(() => {
994
- X(o, g, r, "");
995
- const a = n.getState().getNestedState(e, r);
996
- 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);
997
1005
  }, t.debounce);
998
1006
  else {
999
- X(o, g, r, "");
1000
- const a = n.getState().getNestedState(e, r);
1001
- t?.afterUpdate && t.afterUpdate(a);
1007
+ X(i, u, r, "");
1008
+ const o = n.getState().getNestedState(e, r);
1009
+ t?.afterUpdate && t.afterUpdate(o);
1002
1010
  }
1003
1011
  I(r);
1004
1012
  };
1005
1013
  if (d === "formElement")
1006
- return (g, t) => /* @__PURE__ */ fe(
1014
+ return (u, t) => /* @__PURE__ */ fe(
1007
1015
  _e,
1008
1016
  {
1009
- setState: o,
1017
+ setState: i,
1010
1018
  stateKey: e,
1011
1019
  path: r,
1012
- child: g,
1020
+ child: u,
1013
1021
  formOpts: t
1014
1022
  }
1015
1023
  );
1016
- const N = [...r, d], H = n.getState().getNestedState(e, N);
1017
- return s(H, N, m);
1024
+ const k = [...r, d], H = n.getState().getNestedState(e, k);
1025
+ return s(H, k, m);
1018
1026
  }
1019
- }, R = new Proxy(x, j);
1020
- return c.set(U, {
1021
- proxy: R,
1022
- stateVersion: w
1023
- }), R;
1027
+ }, M = new Proxy(x, O);
1028
+ return c.set(D, {
1029
+ proxy: M,
1030
+ stateVersion: b
1031
+ }), M;
1024
1032
  }
1025
1033
  return s(
1026
1034
  n.getState().getNestedState(e, [])
1027
1035
  );
1028
1036
  }
1029
- function pe(e) {
1030
- return ae(We, { proxy: e });
1037
+ function he(e) {
1038
+ return oe(We, { proxy: e });
1031
1039
  }
1032
1040
  function Ge({
1033
1041
  proxy: e,
1034
- rebuildStateShape: o
1042
+ rebuildStateShape: i
1035
1043
  }) {
1036
1044
  const S = n().getNestedState(e._stateKey, e._path);
1037
- return Array.isArray(S) ? o(
1045
+ return Array.isArray(S) ? i(
1038
1046
  S,
1039
1047
  e._path
1040
1048
  ).stateMapNoRender(
1041
- (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)
1042
1050
  ) : null;
1043
1051
  }
1044
1052
  function We({
1045
1053
  proxy: e
1046
1054
  }) {
1047
- const o = ee(null), S = `${e._stateKey}-${e._path.join(".")}`;
1055
+ const i = ee(null), S = `${e._stateKey}-${e._path.join(".")}`;
1048
1056
  return ce(() => {
1049
- const l = o.current;
1057
+ const l = i.current;
1050
1058
  if (!l || !l.parentElement) return;
1051
1059
  const c = l.parentElement, I = Array.from(c.childNodes).indexOf(l);
1052
- let p = c.getAttribute("data-parent-id");
1053
- p || (p = `parent-${crypto.randomUUID()}`, c.setAttribute("data-parent-id", p));
1054
- const h = {
1060
+ let h = c.getAttribute("data-parent-id");
1061
+ h || (h = `parent-${crypto.randomUUID()}`, c.setAttribute("data-parent-id", h));
1062
+ const p = {
1055
1063
  instanceId: `instance-${crypto.randomUUID()}`,
1056
- parentId: p,
1064
+ parentId: h,
1057
1065
  position: I,
1058
1066
  effect: e._effect
1059
1067
  };
1060
- n.getState().addSignalElement(S, h);
1068
+ n.getState().addSignalElement(S, p);
1061
1069
  const r = n.getState().getNestedState(e._stateKey, e._path);
1062
1070
  let m;
1063
1071
  if (e._effect)
@@ -1072,16 +1080,16 @@ function We({
1072
1080
  else
1073
1081
  m = r;
1074
1082
  m !== null && typeof m == "object" && (m = JSON.stringify(m));
1075
- const U = document.createTextNode(String(m));
1076
- l.replaceWith(U);
1077
- }, [e._stateKey, e._path.join("."), e._effect]), ae("span", {
1078
- 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,
1079
1087
  style: { display: "none" },
1080
1088
  "data-signal-id": S
1081
1089
  });
1082
1090
  }
1083
1091
  function nt(e) {
1084
- const o = Ae(
1092
+ const i = Ae(
1085
1093
  (S) => {
1086
1094
  const l = n.getState().stateComponents.get(e._stateKey) || {
1087
1095
  components: /* @__PURE__ */ new Map(),
@@ -1095,8 +1103,7 @@ function nt(e) {
1095
1103
  paths: /* @__PURE__ */ new Set([e._path.join(".")]),
1096
1104
  deps: [],
1097
1105
  depsFunction: void 0,
1098
- reactiveType: ["component"],
1099
- pathsInitialized: !0
1106
+ reactiveType: ["component"]
1100
1107
  }), l.pathTrie && ue(l.pathTrie, e._path.join("."), c), n.getState().stateComponents.set(e._stateKey, l), () => {
1101
1108
  l.components.delete(c), l.pathTrie && Ee(
1102
1109
  l.pathTrie,
@@ -1107,14 +1114,14 @@ function nt(e) {
1107
1114
  },
1108
1115
  () => n.getState().getNestedState(e._stateKey, e._path)
1109
1116
  );
1110
- return ae("text", {}, String(o));
1117
+ return oe("text", {}, String(i));
1111
1118
  }
1112
1119
  export {
1113
- pe as $cogsSignal,
1120
+ he as $cogsSignal,
1114
1121
  nt as $cogsSignalStore,
1115
1122
  Ke as addStateOptions,
1116
1123
  et as createCogsState,
1117
1124
  tt as notifyComponent,
1118
- De as useCogsStateFn
1125
+ Ue as useCogsStateFn
1119
1126
  };
1120
1127
  //# sourceMappingURL=CogsState.jsx.map