cogsbox-state 0.5.242 → 0.5.243

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