cogsbox-state 0.5.187 → 0.5.188

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