cogsbox-state 0.5.229 → 0.5.230

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