cogsbox-state 0.5.425 → 0.5.427

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,18 +1,19 @@
1
1
  "use client";
2
- import { jsx as pt } from "react/jsx-runtime";
3
- import { useState as it, useRef as q, useEffect as rt, useLayoutEffect as Ct, useMemo as Et, createElement as dt, useSyncExternalStore as Ft, startTransition as Dt, useCallback as St } from "react";
4
- import { transformStateFunc as Lt, isDeepEqual as Y, isFunction as tt, getNestedValue as Z, getDifferences as Tt, debounce as Gt } from "./utility.js";
5
- import { pushFunc as wt, updateFn as lt, cutFunc as ft, ValidationWrapper as _t, FormControlComponent as Wt } from "./Functions.jsx";
6
- import Ht from "superjson";
7
- import { v4 as At } from "uuid";
2
+ import { jsx as At } from "react/jsx-runtime";
3
+ import { ulid as Dt } from "ulid";
4
+ import { useState as st, useRef as J, useEffect as rt, useLayoutEffect as Ct, useMemo as Et, createElement as ut, useSyncExternalStore as Ut, startTransition as Ft, useCallback as mt } from "react";
5
+ import { transformStateFunc as Lt, isDeepEqual as Y, isFunction as et, getNestedValue as Z, getDifferences as Tt, debounce as Gt } from "./utility.js";
6
+ import { pushFunc as pt, updateFn as dt, cutFunc as St, ValidationWrapper as Wt, FormControlComponent as Ht } from "./Functions.jsx";
7
+ import zt from "superjson";
8
+ import { v4 as kt } from "uuid";
8
9
  import "zod";
9
10
  import { getGlobalStore as r, formRefStore as bt } from "./store.js";
10
- import { useCogsConfig as Rt } from "./CogsStateClient.jsx";
11
- import { applyPatch as zt } from "fast-json-patch";
12
- import Bt from "react-use-measure";
11
+ import { useCogsConfig as _t } from "./CogsStateClient.jsx";
12
+ import { applyPatch as Bt } from "fast-json-patch";
13
+ import qt from "react-use-measure";
13
14
  function Vt(t, i) {
14
- const f = r.getState().getInitialOptions, u = r.getState().setInitialStateOptions, h = f(t) || {};
15
- u(t, {
15
+ const S = r.getState().getInitialOptions, f = r.getState().setInitialStateOptions, h = S(t) || {};
16
+ f(t, {
16
17
  ...h,
17
18
  ...i
18
19
  });
@@ -20,22 +21,22 @@ function Vt(t, i) {
20
21
  function $t({
21
22
  stateKey: t,
22
23
  options: i,
23
- initialOptionsPart: f
24
+ initialOptionsPart: S
24
25
  }) {
25
- const u = st(t) || {}, h = f[t] || {}, x = r.getState().setInitialStateOptions, p = { ...h, ...u };
26
+ const f = at(t) || {}, h = S[t] || {}, b = r.getState().setInitialStateOptions, p = { ...h, ...f };
26
27
  let y = !1;
27
28
  if (i)
28
29
  for (const s in i)
29
30
  p.hasOwnProperty(s) ? (s == "localStorage" && i[s] && p[s].key !== i[s]?.key && (y = !0, p[s] = i[s]), s == "initialState" && i[s] && p[s] !== i[s] && // Different references
30
31
  !Y(p[s], i[s]) && (y = !0, p[s] = i[s])) : (y = !0, p[s] = i[s]);
31
- y && x(t, p);
32
+ y && b(t, p);
32
33
  }
33
- function fe(t, { formElements: i, validation: f }) {
34
- return { initialState: t, formElements: i, validation: f };
34
+ function me(t, { formElements: i, validation: S }) {
35
+ return { initialState: t, formElements: i, validation: S };
35
36
  }
36
- const Se = (t, i) => {
37
- let f = t;
38
- const [u, h] = Lt(f);
37
+ const he = (t, i) => {
38
+ let S = t;
39
+ const [f, h] = Lt(S);
39
40
  (Object.keys(h).length > 0 || i && Object.keys(i).length > 0) && Object.keys(h).forEach((y) => {
40
41
  h[y] = h[y] || {}, h[y].formElements = {
41
42
  ...i?.formElements,
@@ -43,17 +44,17 @@ const Se = (t, i) => {
43
44
  ...i?.validation,
44
45
  ...h[y].formElements || {}
45
46
  // State-specific overrides
46
- }, st(y) || r.getState().setInitialStateOptions(y, h[y]);
47
- }), r.getState().setInitialStates(u), r.getState().setCreatedState(u);
48
- const x = (y, s) => {
49
- const [m] = it(s?.componentId ?? At());
47
+ }, at(y) || r.getState().setInitialStateOptions(y, h[y]);
48
+ }), r.getState().setInitialStates(f), r.getState().setCreatedState(f);
49
+ const b = (y, s) => {
50
+ const [m] = st(s?.componentId ?? kt());
50
51
  $t({
51
52
  stateKey: y,
52
53
  options: s,
53
54
  initialOptionsPart: h
54
55
  });
55
- const n = r.getState().cogsStateStore[y] || u[y], g = s?.modifyState ? s.modifyState(n) : n, [L, F] = Qt(
56
- g,
56
+ const n = r.getState().cogsStateStore[y] || f[y], u = s?.modifyState ? s.modifyState(n) : n, [G, R] = Kt(
57
+ u,
57
58
  {
58
59
  stateKey: y,
59
60
  syncUpdate: s?.syncUpdate,
@@ -68,49 +69,49 @@ const Se = (t, i) => {
68
69
  serverState: s?.serverState
69
70
  }
70
71
  );
71
- return F;
72
+ return R;
72
73
  };
73
74
  function p(y, s) {
74
- $t({ stateKey: y, options: s, initialOptionsPart: h }), s.localStorage && Zt(y, s), ut(y);
75
+ $t({ stateKey: y, options: s, initialOptionsPart: h }), s.localStorage && Xt(y, s), ft(y);
75
76
  }
76
- return { useCogsState: x, setCogsOptions: p };
77
+ return { useCogsState: b, setCogsOptions: p };
77
78
  }, {
78
- setUpdaterState: mt,
79
+ setUpdaterState: ht,
79
80
  setState: ot,
80
- getInitialOptions: st,
81
- getKeyState: Mt,
82
- getValidationErrors: qt,
83
- setStateLog: Jt,
84
- updateInitialStateGlobal: kt,
85
- addValidationError: Ot,
81
+ getInitialOptions: at,
82
+ getKeyState: Rt,
83
+ getValidationErrors: Jt,
84
+ setStateLog: Yt,
85
+ updateInitialStateGlobal: xt,
86
+ addValidationError: Mt,
86
87
  removeValidationError: Q,
87
- setServerSyncActions: Yt
88
- } = r.getState(), Nt = (t, i, f, u, h) => {
89
- f?.log && console.log(
88
+ setServerSyncActions: Zt
89
+ } = r.getState(), Nt = (t, i, S, f, h) => {
90
+ S?.log && console.log(
90
91
  "saving to localstorage",
91
92
  i,
92
- f.localStorage?.key,
93
- u
93
+ S.localStorage?.key,
94
+ f
94
95
  );
95
- const x = tt(f?.localStorage?.key) ? f.localStorage?.key(t) : f?.localStorage?.key;
96
- if (x && u) {
97
- const p = `${u}-${i}-${x}`;
96
+ const b = et(S?.localStorage?.key) ? S.localStorage?.key(t) : S?.localStorage?.key;
97
+ if (b && f) {
98
+ const p = `${f}-${i}-${b}`;
98
99
  let y;
99
100
  try {
100
- y = yt(p)?.lastSyncedWithServer;
101
+ y = It(p)?.lastSyncedWithServer;
101
102
  } catch {
102
103
  }
103
104
  const s = {
104
105
  state: t,
105
106
  lastUpdated: Date.now(),
106
107
  lastSyncedWithServer: h ?? y
107
- }, m = Ht.serialize(s);
108
+ }, m = zt.serialize(s);
108
109
  window.localStorage.setItem(
109
110
  p,
110
111
  JSON.stringify(m.json)
111
112
  );
112
113
  }
113
- }, yt = (t) => {
114
+ }, It = (t) => {
114
115
  if (!t) return null;
115
116
  try {
116
117
  const i = window.localStorage.getItem(t);
@@ -118,61 +119,61 @@ const Se = (t, i) => {
118
119
  } catch (i) {
119
120
  return console.error("Error loading from localStorage:", i), null;
120
121
  }
121
- }, Zt = (t, i) => {
122
- const f = r.getState().cogsStateStore[t], { sessionId: u } = Rt(), h = tt(i?.localStorage?.key) ? i.localStorage.key(f) : i?.localStorage?.key;
123
- if (h && u) {
124
- const x = yt(
125
- `${u}-${t}-${h}`
122
+ }, Xt = (t, i) => {
123
+ const S = r.getState().cogsStateStore[t], { sessionId: f } = _t(), h = et(i?.localStorage?.key) ? i.localStorage.key(S) : i?.localStorage?.key;
124
+ if (h && f) {
125
+ const b = It(
126
+ `${f}-${t}-${h}`
126
127
  );
127
- if (x && x.lastUpdated > (x.lastSyncedWithServer || 0))
128
- return ot(t, x.state), ut(t), !0;
128
+ if (b && b.lastUpdated > (b.lastSyncedWithServer || 0))
129
+ return ot(t, b.state), ft(t), !0;
129
130
  }
130
131
  return !1;
131
- }, jt = (t, i, f, u, h, x) => {
132
+ }, Ot = (t, i, S, f, h, b) => {
132
133
  const p = {
133
134
  initialState: i,
134
- updaterState: ht(
135
+ updaterState: yt(
135
136
  t,
136
- u,
137
+ f,
137
138
  h,
138
- x
139
+ b
139
140
  ),
140
- state: f
141
+ state: S
141
142
  };
142
- kt(t, p.initialState), mt(t, p.updaterState), ot(t, p.state);
143
- }, ut = (t) => {
143
+ xt(t, p.initialState), ht(t, p.updaterState), ot(t, p.state);
144
+ }, ft = (t) => {
144
145
  const i = r.getState().stateComponents.get(t);
145
146
  if (!i) return;
146
- const f = /* @__PURE__ */ new Set();
147
- i.components.forEach((u) => {
148
- (u ? Array.isArray(u.reactiveType) ? u.reactiveType : [u.reactiveType || "component"] : null)?.includes("none") || f.add(() => u.forceUpdate());
147
+ const S = /* @__PURE__ */ new Set();
148
+ i.components.forEach((f) => {
149
+ (f ? Array.isArray(f.reactiveType) ? f.reactiveType : [f.reactiveType || "component"] : null)?.includes("none") || S.add(() => f.forceUpdate());
149
150
  }), queueMicrotask(() => {
150
- f.forEach((u) => u());
151
+ S.forEach((f) => f());
151
152
  });
152
- }, me = (t, i) => {
153
- const f = r.getState().stateComponents.get(t);
154
- if (f) {
155
- const u = `${t}////${i}`, h = f.components.get(u);
153
+ }, ye = (t, i) => {
154
+ const S = r.getState().stateComponents.get(t);
155
+ if (S) {
156
+ const f = `${t}////${i}`, h = S.components.get(f);
156
157
  if ((h ? Array.isArray(h.reactiveType) ? h.reactiveType : [h.reactiveType || "component"] : null)?.includes("none"))
157
158
  return;
158
159
  h && h.forceUpdate();
159
160
  }
160
- }, Xt = (t, i, f, u) => {
161
+ }, Qt = (t, i, S, f) => {
161
162
  switch (t) {
162
163
  case "update":
163
164
  return {
164
- oldValue: Z(i, u),
165
- newValue: Z(f, u)
165
+ oldValue: Z(i, f),
166
+ newValue: Z(S, f)
166
167
  };
167
168
  case "insert":
168
169
  return {
169
170
  oldValue: null,
170
171
  // or undefined
171
- newValue: Z(f, u)
172
+ newValue: Z(S, f)
172
173
  };
173
174
  case "cut":
174
175
  return {
175
- oldValue: Z(i, u),
176
+ oldValue: Z(i, f),
176
177
  newValue: null
177
178
  // or undefined
178
179
  };
@@ -180,27 +181,27 @@ const Se = (t, i) => {
180
181
  return { oldValue: null, newValue: null };
181
182
  }
182
183
  };
183
- function Qt(t, {
184
+ function Kt(t, {
184
185
  stateKey: i,
185
- serverSync: f,
186
- localStorage: u,
186
+ serverSync: S,
187
+ localStorage: f,
187
188
  formElements: h,
188
- reactiveDeps: x,
189
+ reactiveDeps: b,
189
190
  reactiveType: p,
190
191
  componentId: y,
191
192
  initialState: s,
192
193
  syncUpdate: m,
193
194
  dependencies: n,
194
- serverState: g
195
+ serverState: u
195
196
  } = {}) {
196
- const [L, F] = it({}), { sessionId: D } = Rt();
197
- let H = !i;
198
- const [S] = it(i ?? At()), c = r.getState().stateLog[S], gt = q(/* @__PURE__ */ new Set()), et = q(y ?? At()), M = q(
197
+ const [G, R] = st({}), { sessionId: L } = _t();
198
+ let z = !i;
199
+ const [g] = st(i ?? kt()), c = r.getState().stateLog[g], gt = J(/* @__PURE__ */ new Set()), nt = J(y ?? kt()), j = J(
199
200
  null
200
201
  );
201
- M.current = st(S) ?? null, rt(() => {
202
- if (m && m.stateKey === S && m.path?.[0]) {
203
- ot(S, (o) => ({
202
+ j.current = at(g) ?? null, rt(() => {
203
+ if (m && m.stateKey === g && m.path?.[0]) {
204
+ ot(g, (o) => ({
204
205
  ...o,
205
206
  [m.path[0]]: m.newValue
206
207
  }));
@@ -212,235 +213,235 @@ function Qt(t, {
212
213
  }
213
214
  }, [m]), rt(() => {
214
215
  if (s) {
215
- Vt(S, {
216
+ Vt(g, {
216
217
  initialState: s
217
218
  });
218
- const e = M.current, a = e?.serverState?.id !== void 0 && e?.serverState?.status === "success" && e?.serverState?.data, d = r.getState().initialStateGlobal[S];
219
+ const e = j.current, a = e?.serverState?.id !== void 0 && e?.serverState?.status === "success" && e?.serverState?.data, d = r.getState().initialStateGlobal[g];
219
220
  if (!(d && !Y(d, s) || !d) && !a)
220
221
  return;
221
- let v = null;
222
- const T = tt(e?.localStorage?.key) ? e?.localStorage?.key(s) : e?.localStorage?.key;
223
- T && D && (v = yt(`${D}-${S}-${T}`));
224
- let E = s, k = !1;
225
- const C = a ? Date.now() : 0, $ = v?.lastUpdated || 0, R = v?.lastSyncedWithServer || 0;
226
- a && C > $ ? (E = e.serverState.data, k = !0) : v && $ > R && (E = v.state, e?.localStorage?.onChange && e?.localStorage?.onChange(E)), r.getState().initializeShadowState(S, s), jt(
227
- S,
222
+ let I = null;
223
+ const E = et(e?.localStorage?.key) ? e?.localStorage?.key(s) : e?.localStorage?.key;
224
+ E && L && (I = It(`${L}-${g}-${E}`));
225
+ let A = s, k = !1;
226
+ const P = a ? Date.now() : 0, $ = I?.lastUpdated || 0, O = I?.lastSyncedWithServer || 0;
227
+ a && P > $ ? (A = e.serverState.data, k = !0) : I && $ > O && (A = I.state, e?.localStorage?.onChange && e?.localStorage?.onChange(A)), r.getState().initializeShadowState(g, s), Ot(
228
+ g,
228
229
  s,
229
- E,
230
- ct,
231
- et.current,
232
- D
233
- ), k && T && D && Nt(E, S, e, D, Date.now()), ut(S), (Array.isArray(p) ? p : [p || "component"]).includes("none") || F({});
230
+ A,
231
+ it,
232
+ nt.current,
233
+ L
234
+ ), k && E && L && Nt(A, g, e, L, Date.now()), ft(g), (Array.isArray(p) ? p : [p || "component"]).includes("none") || R({});
234
235
  }
235
236
  }, [
236
237
  s,
237
- g?.status,
238
- g?.data,
238
+ u?.status,
239
+ u?.data,
239
240
  ...n || []
240
241
  ]), Ct(() => {
241
- H && Vt(S, {
242
- serverSync: f,
242
+ z && Vt(g, {
243
+ serverSync: S,
243
244
  formElements: h,
244
245
  initialState: s,
245
- localStorage: u,
246
- middleware: M.current?.middleware
246
+ localStorage: f,
247
+ middleware: j.current?.middleware
247
248
  });
248
- const e = `${S}////${et.current}`, o = r.getState().stateComponents.get(S) || {
249
+ const e = `${g}////${nt.current}`, o = r.getState().stateComponents.get(g) || {
249
250
  components: /* @__PURE__ */ new Map()
250
251
  };
251
252
  return o.components.set(e, {
252
- forceUpdate: () => F({}),
253
+ forceUpdate: () => R({}),
253
254
  paths: /* @__PURE__ */ new Set(),
254
255
  deps: [],
255
- depsFunction: x || void 0,
256
+ depsFunction: b || void 0,
256
257
  reactiveType: p ?? ["component", "deps"]
257
- }), r.getState().stateComponents.set(S, o), F({}), () => {
258
- o && (o.components.delete(e), o.components.size === 0 && r.getState().stateComponents.delete(S));
258
+ }), r.getState().stateComponents.set(g, o), R({}), () => {
259
+ o && (o.components.delete(e), o.components.size === 0 && r.getState().stateComponents.delete(g));
259
260
  };
260
261
  }, []);
261
- const ct = (e, o, a, d) => {
262
+ const it = (e, o, a, d) => {
262
263
  if (Array.isArray(o)) {
263
- const v = `${S}-${o.join(".")}`;
264
- gt.current.add(v);
264
+ const I = `${g}-${o.join(".")}`;
265
+ gt.current.add(I);
265
266
  }
266
- const w = r.getState();
267
- ot(S, (v) => {
268
- const T = tt(e) ? e(v) : e, E = `${S}-${o.join(".")}`;
269
- if (E) {
270
- let _ = !1, I = w.signalDomElements.get(E);
271
- if ((!I || I.size === 0) && (a.updateType === "insert" || a.updateType === "cut")) {
272
- const O = o.slice(0, -1), N = Z(T, O);
273
- if (Array.isArray(N)) {
274
- _ = !0;
275
- const A = `${S}-${O.join(".")}`;
276
- I = w.signalDomElements.get(A);
267
+ const v = r.getState();
268
+ ot(g, (I) => {
269
+ const E = et(e) ? e(I) : e, A = `${g}-${o.join(".")}`;
270
+ if (A) {
271
+ let C = !1, w = v.signalDomElements.get(A);
272
+ if ((!w || w.size === 0) && (a.updateType === "insert" || a.updateType === "cut")) {
273
+ const D = o.slice(0, -1), V = Z(E, D);
274
+ if (Array.isArray(V)) {
275
+ C = !0;
276
+ const T = `${g}-${D.join(".")}`;
277
+ w = v.signalDomElements.get(T);
277
278
  }
278
279
  }
279
- if (I) {
280
- const O = _ ? Z(T, o.slice(0, -1)) : Z(T, o);
281
- I.forEach(({ parentId: N, position: A, effect: j }) => {
282
- const U = document.querySelector(
283
- `[data-parent-id="${N}"]`
280
+ if (w) {
281
+ const D = C ? Z(E, o.slice(0, -1)) : Z(E, o);
282
+ w.forEach(({ parentId: V, position: T, effect: U }) => {
283
+ const F = document.querySelector(
284
+ `[data-parent-id="${V}"]`
284
285
  );
285
- if (U) {
286
- const B = Array.from(U.childNodes);
287
- if (B[A]) {
288
- const G = j ? new Function("state", `return (${j})(state)`)(O) : O;
289
- B[A].textContent = String(G);
286
+ if (F) {
287
+ const B = Array.from(F.childNodes);
288
+ if (B[T]) {
289
+ const W = U ? new Function("state", `return (${U})(state)`)(D) : D;
290
+ B[T].textContent = String(W);
290
291
  }
291
292
  }
292
293
  });
293
294
  }
294
295
  }
295
- console.log("shadowState", w.shadowStateStore), a.updateType === "update" && (d || M.current?.validation?.key) && o && Q(
296
- (d || M.current?.validation?.key) + "." + o.join(".")
296
+ console.log("shadowState", v.shadowStateStore), a.updateType === "update" && (d || j.current?.validation?.key) && o && Q(
297
+ (d || j.current?.validation?.key) + "." + o.join(".")
297
298
  );
298
299
  const k = o.slice(0, o.length - 1);
299
- a.updateType === "cut" && M.current?.validation?.key && Q(
300
- M.current?.validation?.key + "." + k.join(".")
301
- ), a.updateType === "insert" && M.current?.validation?.key && qt(
302
- M.current?.validation?.key + "." + k.join(".")
303
- ).filter(([I, O]) => {
304
- let N = I?.split(".").length;
305
- if (I == k.join(".") && N == k.length - 1) {
306
- let A = I + "." + k;
307
- Q(I), Ot(A, O);
300
+ a.updateType === "cut" && j.current?.validation?.key && Q(
301
+ j.current?.validation?.key + "." + k.join(".")
302
+ ), a.updateType === "insert" && j.current?.validation?.key && Jt(
303
+ j.current?.validation?.key + "." + k.join(".")
304
+ ).filter(([w, D]) => {
305
+ let V = w?.split(".").length;
306
+ if (w == k.join(".") && V == k.length - 1) {
307
+ let T = w + "." + k;
308
+ Q(w), Mt(T, D);
308
309
  }
309
310
  });
310
- const C = w.stateComponents.get(S);
311
- if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", C), C) {
312
- const _ = Tt(v, T), I = new Set(_), O = a.updateType === "update" ? o.join(".") : o.slice(0, -1).join(".") || "";
311
+ const P = v.stateComponents.get(g);
312
+ if (console.log("stateEntry >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", P), P) {
313
+ const C = Tt(I, E), w = new Set(C), D = a.updateType === "update" ? o.join(".") : o.slice(0, -1).join(".") || "";
313
314
  for (const [
314
- N,
315
- A
316
- ] of C.components.entries()) {
317
- let j = !1;
318
- const U = Array.isArray(A.reactiveType) ? A.reactiveType : [A.reactiveType || "component"];
319
- if (console.log("component", A), !U.includes("none")) {
320
- if (U.includes("all")) {
321
- A.forceUpdate();
315
+ V,
316
+ T
317
+ ] of P.components.entries()) {
318
+ let U = !1;
319
+ const F = Array.isArray(T.reactiveType) ? T.reactiveType : [T.reactiveType || "component"];
320
+ if (console.log("component", T), !F.includes("none")) {
321
+ if (F.includes("all")) {
322
+ T.forceUpdate();
322
323
  continue;
323
324
  }
324
- if (U.includes("component") && ((A.paths.has(O) || A.paths.has("")) && (j = !0), !j))
325
- for (const B of I) {
326
- let G = B;
325
+ if (F.includes("component") && ((T.paths.has(D) || T.paths.has("")) && (U = !0), !U))
326
+ for (const B of w) {
327
+ let W = B;
327
328
  for (; ; ) {
328
- if (A.paths.has(G)) {
329
- j = !0;
329
+ if (T.paths.has(W)) {
330
+ U = !0;
330
331
  break;
331
332
  }
332
- const b = G.lastIndexOf(".");
333
- if (b !== -1) {
334
- const V = G.substring(
333
+ const x = W.lastIndexOf(".");
334
+ if (x !== -1) {
335
+ const _ = W.substring(
335
336
  0,
336
- b
337
+ x
337
338
  );
338
339
  if (!isNaN(
339
- Number(G.substring(b + 1))
340
- ) && A.paths.has(V)) {
341
- j = !0;
340
+ Number(W.substring(x + 1))
341
+ ) && T.paths.has(_)) {
342
+ U = !0;
342
343
  break;
343
344
  }
344
- G = V;
345
+ W = _;
345
346
  } else
346
- G = "";
347
- if (G === "")
347
+ W = "";
348
+ if (W === "")
348
349
  break;
349
350
  }
350
- if (j) break;
351
+ if (U) break;
351
352
  }
352
- if (!j && U.includes("deps") && A.depsFunction) {
353
- const B = A.depsFunction(T);
354
- let G = !1;
355
- typeof B == "boolean" ? B && (G = !0) : Y(A.deps, B) || (A.deps = B, G = !0), G && (j = !0);
353
+ if (!U && F.includes("deps") && T.depsFunction) {
354
+ const B = T.depsFunction(E);
355
+ let W = !1;
356
+ typeof B == "boolean" ? B && (W = !0) : Y(T.deps, B) || (T.deps = B, W = !0), W && (U = !0);
356
357
  }
357
- j && A.forceUpdate();
358
+ U && T.forceUpdate();
358
359
  }
359
360
  }
360
361
  }
361
362
  const $ = Date.now();
362
- o = o.map((_, I) => {
363
- const O = o.slice(0, -1), N = Z(T, O);
364
- return I === o.length - 1 && ["insert", "cut"].includes(a.updateType) ? (N.length - 1).toString() : _;
363
+ o = o.map((C, w) => {
364
+ const D = o.slice(0, -1), V = Z(E, D);
365
+ return w === o.length - 1 && ["insert", "cut"].includes(a.updateType) ? (V.length - 1).toString() : C;
365
366
  });
366
- const { oldValue: R, newValue: z } = Xt(
367
+ const { oldValue: O, newValue: H } = Qt(
367
368
  a.updateType,
368
- v,
369
- T,
369
+ I,
370
+ E,
370
371
  o
371
372
  ), K = {
372
373
  timeStamp: $,
373
- stateKey: S,
374
+ stateKey: g,
374
375
  path: o,
375
376
  updateType: a.updateType,
376
377
  status: "new",
377
- oldValue: R,
378
- newValue: z
378
+ oldValue: O,
379
+ newValue: H
379
380
  };
380
381
  switch (a.updateType) {
381
382
  case "update":
382
- w.updateShadowAtPath(S, o, T);
383
+ v.updateShadowAtPath(g, o, E);
383
384
  break;
384
385
  case "insert":
385
- const _ = o.slice(0, -1);
386
- w.insertShadowArrayElement(S, _, z);
386
+ const C = o.slice(0, -1), D = { _cogsId: H.id ?? H.key ?? Dt() };
387
+ v.insertShadowArrayElement(g, C, D);
387
388
  break;
388
389
  case "cut":
389
- const I = o.slice(0, -1), O = parseInt(o[o.length - 1]);
390
- w.removeShadowArrayElement(S, I, O);
390
+ const V = o.slice(0, -1), T = parseInt(o[o.length - 1]);
391
+ v.removeShadowArrayElement(g, V, T);
391
392
  break;
392
393
  }
393
- if (Jt(S, (_) => {
394
- const O = [..._ ?? [], K].reduce((N, A) => {
395
- const j = `${A.stateKey}:${JSON.stringify(A.path)}`, U = N.get(j);
396
- return U ? (U.timeStamp = Math.max(U.timeStamp, A.timeStamp), U.newValue = A.newValue, U.oldValue = U.oldValue ?? A.oldValue, U.updateType = A.updateType) : N.set(j, { ...A }), N;
394
+ if (Yt(g, (C) => {
395
+ const D = [...C ?? [], K].reduce((V, T) => {
396
+ const U = `${T.stateKey}:${JSON.stringify(T.path)}`, F = V.get(U);
397
+ return F ? (F.timeStamp = Math.max(F.timeStamp, T.timeStamp), F.newValue = T.newValue, F.oldValue = F.oldValue ?? T.oldValue, F.updateType = T.updateType) : V.set(U, { ...T }), V;
397
398
  }, /* @__PURE__ */ new Map());
398
- return Array.from(O.values());
399
+ return Array.from(D.values());
399
400
  }), Nt(
400
- T,
401
- S,
402
- M.current,
403
- D
404
- ), M.current?.middleware && M.current.middleware({
401
+ E,
402
+ g,
403
+ j.current,
404
+ L
405
+ ), j.current?.middleware && j.current.middleware({
405
406
  updateLog: c,
406
407
  update: K
407
- }), M.current?.serverSync) {
408
- const _ = w.serverState[S], I = M.current?.serverSync;
409
- Yt(S, {
410
- syncKey: typeof I.syncKey == "string" ? I.syncKey : I.syncKey({ state: T }),
411
- rollBackState: _,
412
- actionTimeStamp: Date.now() + (I.debounce ?? 3e3),
408
+ }), j.current?.serverSync) {
409
+ const C = v.serverState[g], w = j.current?.serverSync;
410
+ Zt(g, {
411
+ syncKey: typeof w.syncKey == "string" ? w.syncKey : w.syncKey({ state: E }),
412
+ rollBackState: C,
413
+ actionTimeStamp: Date.now() + (w.debounce ?? 3e3),
413
414
  status: "waiting"
414
415
  });
415
416
  }
416
- return T;
417
+ return E;
417
418
  });
418
419
  };
419
- r.getState().updaterState[S] || (mt(
420
- S,
421
- ht(
422
- S,
423
- ct,
424
- et.current,
425
- D
420
+ r.getState().updaterState[g] || (ht(
421
+ g,
422
+ yt(
423
+ g,
424
+ it,
425
+ nt.current,
426
+ L
426
427
  )
427
- ), r.getState().cogsStateStore[S] || ot(S, t), r.getState().initialStateGlobal[S] || kt(S, t));
428
- const l = Et(() => ht(
429
- S,
430
- ct,
431
- et.current,
432
- D
433
- ), [S, D]);
434
- return [Mt(S), l];
428
+ ), r.getState().cogsStateStore[g] || ot(g, t), r.getState().initialStateGlobal[g] || xt(g, t));
429
+ const l = Et(() => yt(
430
+ g,
431
+ it,
432
+ nt.current,
433
+ L
434
+ ), [g, L]);
435
+ return [Rt(g), l];
435
436
  }
436
- function ht(t, i, f, u) {
437
+ function yt(t, i, S, f) {
437
438
  const h = /* @__PURE__ */ new Map();
438
- let x = 0;
439
+ let b = 0;
439
440
  const p = (m) => {
440
441
  const n = m.join(".");
441
- for (const [g] of h)
442
- (g === n || g.startsWith(n + ".")) && h.delete(g);
443
- x++;
442
+ for (const [u] of h)
443
+ (u === n || u.startsWith(n + ".")) && h.delete(u);
444
+ b++;
444
445
  }, y = {
445
446
  removeValidation: (m) => {
446
447
  m?.validationKey && Q(m.validationKey);
@@ -448,31 +449,31 @@ function ht(t, i, f, u) {
448
449
  revertToInitialState: (m) => {
449
450
  const n = r.getState().getInitialOptions(t)?.validation;
450
451
  n?.key && Q(n?.key), m?.validationKey && Q(m.validationKey);
451
- const g = r.getState().initialStateGlobal[t];
452
- r.getState().clearSelectedIndexesForState(t), h.clear(), x++;
453
- const L = s(g, []), F = st(t), D = tt(F?.localStorage?.key) ? F?.localStorage?.key(g) : F?.localStorage?.key, H = `${u}-${t}-${D}`;
454
- H && localStorage.removeItem(H), mt(t, L), ot(t, g);
455
- const S = r.getState().stateComponents.get(t);
456
- return S && S.components.forEach((c) => {
452
+ const u = r.getState().initialStateGlobal[t];
453
+ r.getState().clearSelectedIndexesForState(t), h.clear(), b++;
454
+ const G = s(u, []), R = at(t), L = et(R?.localStorage?.key) ? R?.localStorage?.key(u) : R?.localStorage?.key, z = `${f}-${t}-${L}`;
455
+ z && localStorage.removeItem(z), ht(t, G), ot(t, u);
456
+ const g = r.getState().stateComponents.get(t);
457
+ return g && g.components.forEach((c) => {
457
458
  c.forceUpdate();
458
- }), g;
459
+ }), u;
459
460
  },
460
461
  updateInitialState: (m) => {
461
- h.clear(), x++;
462
- const n = ht(
462
+ h.clear(), b++;
463
+ const n = yt(
463
464
  t,
464
465
  i,
465
- f,
466
- u
467
- ), g = r.getState().initialStateGlobal[t], L = st(t), F = tt(L?.localStorage?.key) ? L?.localStorage?.key(g) : L?.localStorage?.key, D = `${u}-${t}-${F}`;
468
- return localStorage.getItem(D) && localStorage.removeItem(D), Dt(() => {
469
- kt(t, m), r.getState().initializeShadowState(t, m), mt(t, n), ot(t, m);
470
- const H = r.getState().stateComponents.get(t);
471
- H && H.components.forEach((S) => {
472
- S.forceUpdate();
466
+ S,
467
+ f
468
+ ), u = r.getState().initialStateGlobal[t], G = at(t), R = et(G?.localStorage?.key) ? G?.localStorage?.key(u) : G?.localStorage?.key, L = `${f}-${t}-${R}`;
469
+ return localStorage.getItem(L) && localStorage.removeItem(L), Ft(() => {
470
+ xt(t, m), r.getState().initializeShadowState(t, m), ht(t, n), ot(t, m);
471
+ const z = r.getState().stateComponents.get(t);
472
+ z && z.components.forEach((g) => {
473
+ g.forceUpdate();
473
474
  });
474
475
  }), {
475
- fetchId: (H) => n.get()[H]
476
+ fetchId: (z) => n.get()[z]
476
477
  };
477
478
  },
478
479
  _initialState: r.getState().initialStateGlobal[t],
@@ -480,26 +481,26 @@ function ht(t, i, f, u) {
480
481
  _isLoading: r.getState().isLoadingGlobal[t],
481
482
  _isServerSynced: () => {
482
483
  const m = r.getState().serverState[t];
483
- return !!(m && Y(m, Mt(t)));
484
+ return !!(m && Y(m, Rt(t)));
484
485
  }
485
486
  };
486
- function s(m, n = [], g) {
487
- const L = n.map(String).join(".");
488
- h.get(L);
489
- const F = function() {
487
+ function s(m, n = [], u) {
488
+ const G = n.map(String).join(".");
489
+ h.get(G);
490
+ const R = function() {
490
491
  return r().getNestedState(t, n);
491
492
  };
492
- Object.keys(y).forEach((S) => {
493
- F[S] = y[S];
493
+ Object.keys(y).forEach((g) => {
494
+ R[g] = y[g];
494
495
  });
495
- const D = {
496
- apply(S, c, gt) {
496
+ const L = {
497
+ apply(g, c, gt) {
497
498
  return console.log(
498
499
  `PROXY APPLY TRAP HIT: stateKey=${t}, path=${n.join(".")}`
499
500
  ), console.trace("Apply trap stack trace"), r().getNestedState(t, n);
500
501
  },
501
- get(S, c) {
502
- g?.validIndices && !Array.isArray(m) && (g = { ...g, validIndices: void 0 });
502
+ get(g, c) {
503
+ u?.validIndices && !Array.isArray(m) && (u = { ...u, validIndices: void 0 });
503
504
  const gt = /* @__PURE__ */ new Set([
504
505
  "insert",
505
506
  "cut",
@@ -524,14 +525,14 @@ function ht(t, i, f, u) {
524
525
  "getComponents"
525
526
  ]);
526
527
  if (c !== "then" && !c.startsWith("$") && c !== "stateMapNoRender" && !gt.has(c)) {
527
- const l = `${t}////${f}`, e = r.getState().stateComponents.get(t);
528
+ const l = `${t}////${S}`, e = r.getState().stateComponents.get(t);
528
529
  if (e) {
529
530
  const o = e.components.get(l);
530
531
  if (o && !o.paths.has("")) {
531
532
  const a = n.join(".");
532
533
  let d = !0;
533
- for (const w of o.paths)
534
- if (a.startsWith(w) && (a === w || a[w.length] === ".")) {
534
+ for (const v of o.paths)
535
+ if (a.startsWith(v) && (a === v || a[v.length] === ".")) {
535
536
  d = !1;
536
537
  break;
537
538
  }
@@ -553,13 +554,13 @@ function ht(t, i, f, u) {
553
554
  try {
554
555
  const d = await e.action(o);
555
556
  if (d && !d.success && d.errors && a) {
556
- r.getState().removeValidationError(a), d.errors.forEach((v) => {
557
- const T = [a, ...v.path].join(".");
558
- r.getState().addValidationError(T, v.message);
557
+ r.getState().removeValidationError(a), d.errors.forEach((I) => {
558
+ const E = [a, ...I.path].join(".");
559
+ r.getState().addValidationError(E, I.message);
559
560
  });
560
- const w = r.getState().stateComponents.get(t);
561
- w && w.components.forEach((v) => {
562
- v.forceUpdate();
561
+ const v = r.getState().stateComponents.get(t);
562
+ v && v.components.forEach((I) => {
563
+ I.forceUpdate();
563
564
  });
564
565
  }
565
566
  return d?.success && e.onSuccess ? e.onSuccess(d.data) : !d?.success && e.onError && e.onError(d.error), d;
@@ -581,7 +582,7 @@ function ht(t, i, f, u) {
581
582
  };
582
583
  if (c === "removeStorage")
583
584
  return () => {
584
- const l = r.getState().initialStateGlobal[t], e = st(t), o = tt(e?.localStorage?.key) ? e?.localStorage?.key(l) : e?.localStorage?.key, a = `${u}-${t}-${o}`;
585
+ const l = r.getState().initialStateGlobal[t], e = at(t), o = et(e?.localStorage?.key) ? e?.localStorage?.key(l) : e?.localStorage?.key, a = `${f}-${t}-${o}`;
585
586
  a && localStorage.removeItem(a);
586
587
  };
587
588
  if (c === "showValidationErrors")
@@ -592,9 +593,9 @@ function ht(t, i, f, u) {
592
593
  return r.getState().getValidationErrors(l.key + "." + n.join("."));
593
594
  };
594
595
  if (Array.isArray(m)) {
595
- const l = () => g?.validIndices ? m.map((o, a) => ({
596
+ const l = () => u?.validIndices ? m.map((o, a) => ({
596
597
  item: o,
597
- originalIndex: g.validIndices[a]
598
+ originalIndex: u.validIndices[a]
598
599
  })) : r.getState().getNestedState(t, n).map((o, a) => ({
599
600
  item: o,
600
601
  originalIndex: a
@@ -606,7 +607,7 @@ function ht(t, i, f, u) {
606
607
  return s(
607
608
  m[e],
608
609
  [...n, e.toString()],
609
- g
610
+ u
610
611
  );
611
612
  };
612
613
  if (c === "clearSelected")
@@ -621,50 +622,50 @@ function ht(t, i, f, u) {
621
622
  itemHeight: o = 50,
622
623
  overscan: a = 6,
623
624
  stickToBottom: d = !1,
624
- dependencies: w = []
625
- } = e, v = q(null), [T, E] = it({
625
+ dependencies: v = []
626
+ } = e, I = J(null), [E, A] = st({
626
627
  startIndex: 0,
627
628
  endIndex: 10
628
- }), [k, C] = it(0), $ = q(!0), R = q(!1), z = q(0), K = q(T);
629
+ }), [k, P] = st(0), $ = J(!0), O = J(!1), H = J(0), K = J(E);
629
630
  rt(() => r.getState().subscribeToShadowState(t, () => {
630
- C((V) => V + 1);
631
+ P((_) => _ + 1);
631
632
  }), [t]);
632
- const _ = r().getNestedState(
633
+ const C = r().getNestedState(
633
634
  t,
634
635
  n
635
- ), I = _.length, { totalHeight: O, positions: N } = Et(() => {
636
- const b = r.getState().getShadowMetadata(t, n) || [];
637
- let V = 0;
638
- const P = [];
639
- for (let W = 0; W < I; W++) {
640
- P[W] = V;
641
- const J = b[W]?.virtualizer?.itemHeight;
642
- V += J || o;
636
+ ), w = C.length, { totalHeight: D, positions: V } = Et(() => {
637
+ const x = r.getState().getShadowMetadata(t, n), _ = Array.isArray(x) ? x : [];
638
+ let N = 0;
639
+ const q = [];
640
+ for (let M = 0; M < w; M++) {
641
+ q[M] = N;
642
+ const tt = _[M]?.virtualizer?.itemHeight;
643
+ N += tt || o;
643
644
  }
644
- return { totalHeight: V, positions: P };
645
+ return { totalHeight: N, positions: q };
645
646
  }, [
646
- I,
647
+ w,
647
648
  t,
648
649
  n.join("."),
649
650
  o,
650
651
  k
651
- ]), A = Et(() => {
652
- const b = Math.max(0, T.startIndex), V = Math.min(I, T.endIndex), P = Array.from(
653
- { length: V - b },
654
- (J, at) => b + at
655
- ), W = P.map((J) => _[J]);
656
- return s(W, n, {
657
- ...g,
658
- validIndices: P
652
+ ]), T = Et(() => {
653
+ const x = Math.max(0, E.startIndex), _ = Math.min(w, E.endIndex), N = Array.from(
654
+ { length: _ - x },
655
+ (M, tt) => x + tt
656
+ ), q = N.map((M) => C[M]);
657
+ return s(q, n, {
658
+ ...u,
659
+ validIndices: N
659
660
  });
660
- }, [T.startIndex, T.endIndex, _, I]), j = St(() => {
661
- const b = r.getState().getShadowMetadata(t, n) || [], V = I - 1;
662
- if (V >= 0) {
663
- const P = b[V];
664
- if (P?.virtualizer?.domRef) {
665
- const W = P.virtualizer.domRef;
666
- if (W && W.scrollIntoView)
667
- return W.scrollIntoView({
661
+ }, [E.startIndex, E.endIndex, C, w]), U = mt(() => {
662
+ const x = r.getState().getShadowMetadata(t, n), _ = Array.isArray(x) ? x : [], N = w - 1;
663
+ if (N >= 0) {
664
+ const q = _[N];
665
+ if (q?.virtualizer?.domRef) {
666
+ const M = q.virtualizer.domRef;
667
+ if (M && M.scrollIntoView)
668
+ return M.scrollIntoView({
668
669
  behavior: "auto",
669
670
  block: "end",
670
671
  inline: "nearest"
@@ -672,128 +673,128 @@ function ht(t, i, f, u) {
672
673
  }
673
674
  }
674
675
  return !1;
675
- }, [t, n, I]);
676
+ }, [t, n, w]);
676
677
  rt(() => {
677
- if (!d || I === 0) return;
678
- const b = I > z.current, V = z.current === 0 && I > 0;
679
- if ((b || V) && $.current && !R.current) {
680
- const P = Math.ceil(
681
- (v.current?.clientHeight || 0) / o
682
- ), W = {
678
+ if (!d || w === 0) return;
679
+ const x = w > H.current, _ = H.current === 0 && w > 0;
680
+ if ((x || _) && $.current && !O.current) {
681
+ const N = Math.ceil(
682
+ (I.current?.clientHeight || 0) / o
683
+ ), q = {
683
684
  startIndex: Math.max(
684
685
  0,
685
- I - P - a
686
+ w - N - a
686
687
  ),
687
- endIndex: I
688
+ endIndex: w
688
689
  };
689
- E(W);
690
- const J = setTimeout(() => {
691
- B(I - 1, "smooth");
690
+ A(q);
691
+ const M = setTimeout(() => {
692
+ B(w - 1, "smooth");
692
693
  }, 50);
693
- return () => clearTimeout(J);
694
+ return () => clearTimeout(M);
694
695
  }
695
- z.current = I;
696
- }, [I, o, a]), rt(() => {
697
- const b = v.current;
698
- if (!b) return;
699
- const V = () => {
700
- const { scrollTop: P, scrollHeight: W, clientHeight: J } = b, at = W - P - J;
701
- $.current = at < 5, at > 100 && (R.current = !0), at < 5 && (R.current = !1);
702
- let nt = 0;
703
- for (let X = 0; X < N.length; X++)
704
- if (N[X] > P - o * a) {
705
- nt = Math.max(0, X - 1);
696
+ H.current = w;
697
+ }, [w, o, a]), rt(() => {
698
+ const x = I.current;
699
+ if (!x) return;
700
+ const _ = () => {
701
+ const { scrollTop: N, scrollHeight: q, clientHeight: M } = x, tt = q - N - M;
702
+ $.current = tt < 5, tt > 100 && (O.current = !0), tt < 5 && (O.current = !1);
703
+ let ct = 0;
704
+ for (let X = 0; X < V.length; X++)
705
+ if (V[X] > N - o * a) {
706
+ ct = Math.max(0, X - 1);
706
707
  break;
707
708
  }
708
- let xt = nt;
709
- const Ut = P + J;
710
- for (let X = nt; X < N.length && !(N[X] > Ut + o * a); X++)
711
- xt = X;
712
- const vt = Math.max(0, nt), It = Math.min(
713
- I,
714
- xt + 1 + a
709
+ let lt = ct;
710
+ const jt = N + M;
711
+ for (let X = ct; X < V.length && !(V[X] > jt + o * a); X++)
712
+ lt = X;
713
+ const vt = Math.max(0, ct), wt = Math.min(
714
+ w,
715
+ lt + 1 + a
715
716
  );
716
- (vt !== K.current.startIndex || It !== K.current.endIndex) && (K.current = {
717
+ (vt !== K.current.startIndex || wt !== K.current.endIndex) && (K.current = {
717
718
  startIndex: vt,
718
- endIndex: It
719
- }, E({
719
+ endIndex: wt
720
+ }, A({
720
721
  startIndex: vt,
721
- endIndex: It
722
+ endIndex: wt
722
723
  }));
723
724
  };
724
- if (b.addEventListener("scroll", V, {
725
+ if (x.addEventListener("scroll", _, {
725
726
  passive: !0
726
- }), d && I > 0 && !R.current) {
727
- const { scrollTop: P } = b;
728
- P === 0 && (b.scrollTop = b.scrollHeight, $.current = !0);
727
+ }), d && w > 0 && !O.current) {
728
+ const { scrollTop: N } = x;
729
+ N === 0 && (x.scrollTop = x.scrollHeight, $.current = !0);
729
730
  }
730
- return V(), () => {
731
- b.removeEventListener("scroll", V);
731
+ return _(), () => {
732
+ x.removeEventListener("scroll", _);
732
733
  };
733
- }, [N, I, o, a, d]);
734
- const U = St(() => {
735
- $.current = !0, R.current = !1, !j() && v.current && (v.current.scrollTop = v.current.scrollHeight);
736
- }, [j]), B = St(
737
- (b, V = "smooth") => {
738
- const P = v.current;
739
- if (!P) return;
740
- if (b === I - 1) {
741
- P.scrollTo({
742
- top: P.scrollHeight,
743
- behavior: V
734
+ }, [V, w, o, a, d]);
735
+ const F = mt(() => {
736
+ $.current = !0, O.current = !1, !U() && I.current && (I.current.scrollTop = I.current.scrollHeight);
737
+ }, [U]), B = mt(
738
+ (x, _ = "smooth") => {
739
+ const N = I.current;
740
+ if (!N) return;
741
+ if (x === w - 1) {
742
+ N.scrollTo({
743
+ top: N.scrollHeight,
744
+ behavior: _
744
745
  });
745
746
  return;
746
747
  }
747
- const nt = (r.getState().getShadowMetadata(t, n) || [])[b]?.virtualizer?.domRef;
748
- nt ? nt.scrollIntoView({
749
- behavior: V,
748
+ const M = r.getState().getShadowMetadata(t, n), lt = (Array.isArray(M) ? M : [])[x]?.virtualizer?.domRef;
749
+ lt ? lt.scrollIntoView({
750
+ behavior: _,
750
751
  block: "center"
751
- }) : N[b] !== void 0 && P.scrollTo({
752
- top: N[b],
753
- behavior: V
752
+ }) : V[x] !== void 0 && N.scrollTo({
753
+ top: V[x],
754
+ behavior: _
754
755
  });
755
756
  },
756
- [N, t, n, I]
757
+ [V, t, n, w]
757
758
  // Add totalCount to the dependencies
758
- ), G = {
759
+ ), W = {
759
760
  outer: {
760
- ref: v,
761
+ ref: I,
761
762
  style: { overflowY: "auto", height: "100%" }
762
763
  },
763
764
  inner: {
764
765
  style: {
765
- height: `${O}px`,
766
+ height: `${D}px`,
766
767
  position: "relative"
767
768
  }
768
769
  },
769
770
  list: {
770
771
  style: {
771
- transform: `translateY(${N[T.startIndex] || 0}px)`
772
+ transform: `translateY(${V[E.startIndex] || 0}px)`
772
773
  }
773
774
  }
774
775
  };
775
776
  return {
776
- virtualState: A,
777
- virtualizerProps: G,
778
- scrollToBottom: U,
777
+ virtualState: T,
778
+ virtualizerProps: W,
779
+ scrollToBottom: F,
779
780
  scrollToIndex: B
780
781
  };
781
782
  };
782
783
  if (c === "stateMapNoRender")
783
784
  return (e) => m.map((a, d) => {
784
- let w;
785
- g?.validIndices && g.validIndices[d] !== void 0 ? w = g.validIndices[d] : w = d;
786
- const v = [...n, w.toString()], T = s(a, v, g);
785
+ let v;
786
+ u?.validIndices && u.validIndices[d] !== void 0 ? v = u.validIndices[d] : v = d;
787
+ const I = [...n, v.toString()], E = s(a, I, u);
787
788
  return e(
788
789
  a,
789
- T,
790
+ E,
790
791
  d,
791
792
  m,
792
- s(m, n, g)
793
+ s(m, n, u)
793
794
  );
794
795
  });
795
796
  if (c === "$stateMap")
796
- return (e) => dt(Kt, {
797
+ return (e) => ut(te, {
797
798
  proxy: {
798
799
  _stateKey: t,
799
800
  _path: n,
@@ -805,20 +806,20 @@ function ht(t, i, f, u) {
805
806
  if (c === "stateList")
806
807
  return (e, o) => {
807
808
  const a = r.getState().getNestedState(t, n);
808
- return Array.isArray(a) ? (g?.validIndices || Array.from({ length: a.length }, (w, v) => v)).map((w, v) => {
809
- const T = a[w], E = [...n, w.toString()], k = s(T, E, g), C = `${f}-${n.join(".")}-${w}`;
810
- return dt(ee, {
811
- key: w,
809
+ return Array.isArray(a) ? (u?.validIndices || Array.from({ length: a.length }, (v, I) => I)).map((v, I) => {
810
+ const E = a[v], A = [...n, v.toString()], k = s(E, A, u), P = `${S}-${n.join(".")}-${v}`;
811
+ return ut(ne, {
812
+ key: v,
812
813
  stateKey: t,
813
- itemComponentId: C,
814
+ itemComponentId: P,
814
815
  formOpts: o,
815
- itemPath: E,
816
+ itemPath: A,
816
817
  children: e(
817
- T,
818
+ E,
818
819
  k,
819
- { localIndex: v, originalIndex: w },
820
+ { localIndex: I, originalIndex: v },
820
821
  a,
821
- s(a, n, g)
822
+ s(a, n, u)
822
823
  )
823
824
  });
824
825
  }) : (console.warn(
@@ -828,14 +829,14 @@ function ht(t, i, f, u) {
828
829
  if (c === "stateFlattenOn")
829
830
  return (e) => {
830
831
  const o = m;
831
- h.clear(), x++;
832
+ h.clear(), b++;
832
833
  const a = o.flatMap(
833
834
  (d) => d[e] ?? []
834
835
  );
835
836
  return s(
836
837
  a,
837
838
  [...n, "[*]", e],
838
- g
839
+ u
839
840
  );
840
841
  };
841
842
  if (c === "index")
@@ -851,36 +852,36 @@ function ht(t, i, f, u) {
851
852
  return s(a, d);
852
853
  };
853
854
  if (c === "insert")
854
- return (e) => (p(n), wt(i, e, n, t), s(
855
+ return (e) => (p(n), pt(i, e, n, t), s(
855
856
  r.getState().getNestedState(t, n),
856
857
  n
857
858
  ));
858
859
  if (c === "uniqueInsert")
859
860
  return (e, o, a) => {
860
- const d = r.getState().getNestedState(t, n), w = tt(e) ? e(d) : e;
861
- let v = null;
862
- if (!d.some((E) => {
861
+ const d = r.getState().getNestedState(t, n), v = et(e) ? e(d) : e;
862
+ let I = null;
863
+ if (!d.some((A) => {
863
864
  if (o) {
864
- const C = o.every(
865
- ($) => Y(E[$], w[$])
865
+ const P = o.every(
866
+ ($) => Y(A[$], v[$])
866
867
  );
867
- return C && (v = E), C;
868
+ return P && (I = A), P;
868
869
  }
869
- const k = Y(E, w);
870
- return k && (v = E), k;
870
+ const k = Y(A, v);
871
+ return k && (I = A), k;
871
872
  }))
872
- p(n), wt(i, w, n, t);
873
- else if (a && v) {
874
- const E = a(v), k = d.map(
875
- (C) => Y(C, v) ? E : C
873
+ p(n), pt(i, v, n, t);
874
+ else if (a && I) {
875
+ const A = a(I), k = d.map(
876
+ (P) => Y(P, I) ? A : P
876
877
  );
877
- p(n), lt(i, k, n);
878
+ p(n), dt(i, k, n);
878
879
  }
879
880
  };
880
881
  if (c === "cut")
881
882
  return (e, o) => {
882
883
  if (!o?.waitForSync)
883
- return p(n), ft(i, n, t, e), s(
884
+ return p(n), St(i, n, t, e), s(
884
885
  r.getState().getNestedState(t, n),
885
886
  n
886
887
  );
@@ -888,48 +889,48 @@ function ht(t, i, f, u) {
888
889
  if (c === "cutByValue")
889
890
  return (e) => {
890
891
  for (let o = 0; o < m.length; o++)
891
- m[o] === e && ft(i, n, t, o);
892
+ m[o] === e && St(i, n, t, o);
892
893
  };
893
894
  if (c === "toggleByValue")
894
895
  return (e) => {
895
896
  const o = m.findIndex((a) => a === e);
896
- o > -1 ? ft(i, n, t, o) : wt(i, e, n, t);
897
+ o > -1 ? St(i, n, t, o) : pt(i, e, n, t);
897
898
  };
898
899
  if (c === "stateFind")
899
900
  return (e) => {
900
901
  const a = l().find(
901
- ({ item: w }, v) => e(w, v)
902
+ ({ item: v }, I) => e(v, I)
902
903
  );
903
904
  if (!a) return;
904
905
  const d = [...n, a.originalIndex.toString()];
905
- return s(a.item, d, g);
906
+ return s(a.item, d, u);
906
907
  };
907
908
  if (c === "findWith")
908
909
  return (e, o) => {
909
910
  const d = l().find(
910
- ({ item: v }) => v[e] === o
911
+ ({ item: I }) => I[e] === o
911
912
  );
912
913
  if (!d) return;
913
- const w = [...n, d.originalIndex.toString()];
914
- return s(d.item, w, g);
914
+ const v = [...n, d.originalIndex.toString()];
915
+ return s(d.item, v, u);
915
916
  };
916
917
  }
917
- const et = n[n.length - 1];
918
- if (!isNaN(Number(et))) {
918
+ const nt = n[n.length - 1];
919
+ if (!isNaN(Number(nt))) {
919
920
  const l = n.slice(0, -1), e = r.getState().getNestedState(t, l);
920
921
  if (Array.isArray(e) && c === "cut")
921
- return () => ft(
922
+ return () => St(
922
923
  i,
923
924
  l,
924
925
  t,
925
- Number(et)
926
+ Number(nt)
926
927
  );
927
928
  }
928
929
  if (c === "get")
929
930
  return () => {
930
- if (g?.validIndices && Array.isArray(m)) {
931
+ if (u?.validIndices && Array.isArray(m)) {
931
932
  const l = r.getState().getNestedState(t, n);
932
- return g.validIndices.map((e) => l[e]);
933
+ return u.validIndices.map((e) => l[e]);
933
934
  }
934
935
  return r.getState().getNestedState(t, n);
935
936
  };
@@ -949,7 +950,7 @@ function ht(t, i, f, u) {
949
950
  return r.getState().getSyncInfo(l);
950
951
  }
951
952
  if (c == "getLocalStorage")
952
- return (l) => yt(u + "-" + t + "-" + l);
953
+ return (l) => It(f + "-" + t + "-" + l);
953
954
  if (c === "_selected") {
954
955
  const l = n.slice(0, -1), e = l.join("."), o = r.getState().getNestedState(t, l);
955
956
  return Array.isArray(o) ? Number(n[n.length - 1]) === r.getState().getSelectedIndex(t, e) : void 0;
@@ -959,7 +960,7 @@ function ht(t, i, f, u) {
959
960
  const e = n.slice(0, -1), o = Number(n[n.length - 1]), a = e.join(".");
960
961
  l ? r.getState().setSelectedIndex(t, a, o) : r.getState().setSelectedIndex(t, a, void 0);
961
962
  const d = r.getState().getNestedState(t, [...e]);
962
- lt(i, d, e), p(e);
963
+ dt(i, d, e), p(e);
963
964
  };
964
965
  if (c === "toggleSelected")
965
966
  return () => {
@@ -970,7 +971,7 @@ function ht(t, i, f, u) {
970
971
  a === e ? void 0 : e
971
972
  );
972
973
  const d = r.getState().getNestedState(t, [...l]);
973
- lt(i, d, l), p(l);
974
+ dt(i, d, l), p(l);
974
975
  };
975
976
  if (n.length == 0) {
976
977
  if (c === "addValidation")
@@ -980,73 +981,73 @@ function ht(t, i, f, u) {
980
981
  throw new Error("Validation key not found");
981
982
  Q(e.key), console.log("addValidationError", l), l.forEach((o) => {
982
983
  const a = [e.key, ...o.path].join(".");
983
- console.log("fullErrorPath", a), Ot(a, o.message);
984
- }), ut(t);
984
+ console.log("fullErrorPath", a), Mt(a, o.message);
985
+ }), ft(t);
985
986
  };
986
987
  if (c === "applyJsonPatch")
987
988
  return (l) => {
988
- const e = r.getState().cogsStateStore[t], a = zt(e, l).newDocument;
989
- jt(
989
+ const e = r.getState().cogsStateStore[t], a = Bt(e, l).newDocument;
990
+ Ot(
990
991
  t,
991
992
  r.getState().initialStateGlobal[t],
992
993
  a,
993
994
  i,
994
- f,
995
- u
995
+ S,
996
+ f
996
997
  );
997
998
  const d = r.getState().stateComponents.get(t);
998
999
  if (d) {
999
- const w = Tt(e, a), v = new Set(w);
1000
+ const v = Tt(e, a), I = new Set(v);
1000
1001
  for (const [
1001
- T,
1002
- E
1002
+ E,
1003
+ A
1003
1004
  ] of d.components.entries()) {
1004
1005
  let k = !1;
1005
- const C = Array.isArray(E.reactiveType) ? E.reactiveType : [E.reactiveType || "component"];
1006
- if (!C.includes("none")) {
1007
- if (C.includes("all")) {
1008
- E.forceUpdate();
1006
+ const P = Array.isArray(A.reactiveType) ? A.reactiveType : [A.reactiveType || "component"];
1007
+ if (!P.includes("none")) {
1008
+ if (P.includes("all")) {
1009
+ A.forceUpdate();
1009
1010
  continue;
1010
1011
  }
1011
- if (C.includes("component") && (E.paths.has("") && (k = !0), !k))
1012
- for (const $ of v) {
1013
- if (E.paths.has($)) {
1012
+ if (P.includes("component") && (A.paths.has("") && (k = !0), !k))
1013
+ for (const $ of I) {
1014
+ if (A.paths.has($)) {
1014
1015
  k = !0;
1015
1016
  break;
1016
1017
  }
1017
- let R = $.lastIndexOf(".");
1018
- for (; R !== -1; ) {
1019
- const z = $.substring(0, R);
1020
- if (E.paths.has(z)) {
1018
+ let O = $.lastIndexOf(".");
1019
+ for (; O !== -1; ) {
1020
+ const H = $.substring(0, O);
1021
+ if (A.paths.has(H)) {
1021
1022
  k = !0;
1022
1023
  break;
1023
1024
  }
1024
1025
  const K = $.substring(
1025
- R + 1
1026
+ O + 1
1026
1027
  );
1027
1028
  if (!isNaN(Number(K))) {
1028
- const _ = z.lastIndexOf(".");
1029
- if (_ !== -1) {
1030
- const I = z.substring(
1029
+ const C = H.lastIndexOf(".");
1030
+ if (C !== -1) {
1031
+ const w = H.substring(
1031
1032
  0,
1032
- _
1033
+ C
1033
1034
  );
1034
- if (E.paths.has(I)) {
1035
+ if (A.paths.has(w)) {
1035
1036
  k = !0;
1036
1037
  break;
1037
1038
  }
1038
1039
  }
1039
1040
  }
1040
- R = z.lastIndexOf(".");
1041
+ O = H.lastIndexOf(".");
1041
1042
  }
1042
1043
  if (k) break;
1043
1044
  }
1044
- if (!k && C.includes("deps") && E.depsFunction) {
1045
- const $ = E.depsFunction(a);
1046
- let R = !1;
1047
- typeof $ == "boolean" ? $ && (R = !0) : Y(E.deps, $) || (E.deps = $, R = !0), R && (k = !0);
1045
+ if (!k && P.includes("deps") && A.depsFunction) {
1046
+ const $ = A.depsFunction(a);
1047
+ let O = !1;
1048
+ typeof $ == "boolean" ? $ && (O = !0) : Y(A.deps, $) || (A.deps = $, O = !0), O && (k = !0);
1048
1049
  }
1049
- k && E.forceUpdate();
1050
+ k && A.forceUpdate();
1050
1051
  }
1051
1052
  }
1052
1053
  }
@@ -1062,19 +1063,19 @@ function ht(t, i, f, u) {
1062
1063
  const o = r.getState().cogsStateStore[t];
1063
1064
  try {
1064
1065
  const a = r.getState().getValidationErrors(l.key);
1065
- a && a.length > 0 && a.forEach(([w]) => {
1066
- w && w.startsWith(l.key) && Q(w);
1066
+ a && a.length > 0 && a.forEach(([v]) => {
1067
+ v && v.startsWith(l.key) && Q(v);
1067
1068
  });
1068
1069
  const d = l.zodSchema.safeParse(o);
1069
- return d.success ? !0 : (d.error.errors.forEach((v) => {
1070
- const T = v.path, E = v.message, k = [l.key, ...T].join(".");
1071
- e(k, E);
1072
- }), ut(t), !1);
1070
+ return d.success ? !0 : (d.error.errors.forEach((I) => {
1071
+ const E = I.path, A = I.message, k = [l.key, ...E].join(".");
1072
+ e(k, A);
1073
+ }), ft(t), !1);
1073
1074
  } catch (a) {
1074
1075
  return console.error("Zod schema validation failed", a), !1;
1075
1076
  }
1076
1077
  };
1077
- if (c === "_componentId") return f;
1078
+ if (c === "_componentId") return S;
1078
1079
  if (c === "getComponents")
1079
1080
  return () => r().stateComponents.get(t);
1080
1081
  if (c === "getAllFormRefs")
@@ -1096,13 +1097,13 @@ function ht(t, i, f, u) {
1096
1097
  return ({
1097
1098
  children: l,
1098
1099
  hideMessage: e
1099
- }) => /* @__PURE__ */ pt(
1100
- _t,
1100
+ }) => /* @__PURE__ */ At(
1101
+ Wt,
1101
1102
  {
1102
1103
  formOpts: e ? { validation: { message: "" } } : void 0,
1103
1104
  path: n,
1104
1105
  stateKey: t,
1105
- validIndices: g?.validIndices,
1106
+ validIndices: u?.validIndices,
1106
1107
  children: l
1107
1108
  }
1108
1109
  );
@@ -1113,20 +1114,20 @@ function ht(t, i, f, u) {
1113
1114
  return (l, e) => {
1114
1115
  if (e?.debounce)
1115
1116
  Gt(() => {
1116
- lt(i, l, n, "");
1117
+ dt(i, l, n, "");
1117
1118
  const o = r.getState().getNestedState(t, n);
1118
1119
  e?.afterUpdate && e.afterUpdate(o);
1119
1120
  }, e.debounce);
1120
1121
  else {
1121
- lt(i, l, n, "");
1122
+ dt(i, l, n, "");
1122
1123
  const o = r.getState().getNestedState(t, n);
1123
1124
  e?.afterUpdate && e.afterUpdate(o);
1124
1125
  }
1125
1126
  p(n);
1126
1127
  };
1127
1128
  if (c === "formElement")
1128
- return (l, e) => /* @__PURE__ */ pt(
1129
- Wt,
1129
+ return (l, e) => /* @__PURE__ */ At(
1130
+ Ht,
1130
1131
  {
1131
1132
  setState: i,
1132
1133
  stateKey: t,
@@ -1135,42 +1136,42 @@ function ht(t, i, f, u) {
1135
1136
  formOpts: e
1136
1137
  }
1137
1138
  );
1138
- const M = [...n, c], ct = r.getState().getNestedState(t, M);
1139
- return s(ct, M, g);
1139
+ const j = [...n, c], it = r.getState().getNestedState(t, j);
1140
+ return s(it, j, u);
1140
1141
  }
1141
- }, H = new Proxy(F, D);
1142
- return h.set(L, {
1143
- proxy: H,
1144
- stateVersion: x
1145
- }), H;
1142
+ }, z = new Proxy(R, L);
1143
+ return h.set(G, {
1144
+ proxy: z,
1145
+ stateVersion: b
1146
+ }), z;
1146
1147
  }
1147
1148
  return s(
1148
1149
  r.getState().getNestedState(t, [])
1149
1150
  );
1150
1151
  }
1151
1152
  function Pt(t) {
1152
- return dt(te, { proxy: t });
1153
+ return ut(ee, { proxy: t });
1153
1154
  }
1154
- function Kt({
1155
+ function te({
1155
1156
  proxy: t,
1156
1157
  rebuildStateShape: i
1157
1158
  }) {
1158
- const f = r().getNestedState(t._stateKey, t._path);
1159
- return Array.isArray(f) ? i(
1160
- f,
1159
+ const S = r().getNestedState(t._stateKey, t._path);
1160
+ return Array.isArray(S) ? i(
1161
+ S,
1161
1162
  t._path
1162
1163
  ).stateMapNoRender(
1163
- (h, x, p, y, s) => t._mapFn(h, x, p, y, s)
1164
+ (h, b, p, y, s) => t._mapFn(h, b, p, y, s)
1164
1165
  ) : null;
1165
1166
  }
1166
- function te({
1167
+ function ee({
1167
1168
  proxy: t
1168
1169
  }) {
1169
- const i = q(null), f = `${t._stateKey}-${t._path.join(".")}`;
1170
+ const i = J(null), S = `${t._stateKey}-${t._path.join(".")}`;
1170
1171
  return rt(() => {
1171
- const u = i.current;
1172
- if (!u || !u.parentElement) return;
1173
- const h = u.parentElement, p = Array.from(h.childNodes).indexOf(u);
1172
+ const f = i.current;
1173
+ if (!f || !f.parentElement) return;
1174
+ const h = f.parentElement, p = Array.from(h.childNodes).indexOf(f);
1174
1175
  let y = h.getAttribute("data-parent-id");
1175
1176
  y || (y = `parent-${crypto.randomUUID()}`, h.setAttribute("data-parent-id", y));
1176
1177
  const m = {
@@ -1179,84 +1180,84 @@ function te({
1179
1180
  position: p,
1180
1181
  effect: t._effect
1181
1182
  };
1182
- r.getState().addSignalElement(f, m);
1183
+ r.getState().addSignalElement(S, m);
1183
1184
  const n = r.getState().getNestedState(t._stateKey, t._path);
1184
- let g;
1185
+ let u;
1185
1186
  if (t._effect)
1186
1187
  try {
1187
- g = new Function(
1188
+ u = new Function(
1188
1189
  "state",
1189
1190
  `return (${t._effect})(state)`
1190
1191
  )(n);
1191
- } catch (F) {
1192
- console.error("Error evaluating effect function during mount:", F), g = n;
1192
+ } catch (R) {
1193
+ console.error("Error evaluating effect function during mount:", R), u = n;
1193
1194
  }
1194
1195
  else
1195
- g = n;
1196
- g !== null && typeof g == "object" && (g = JSON.stringify(g));
1197
- const L = document.createTextNode(String(g));
1198
- u.replaceWith(L);
1199
- }, [t._stateKey, t._path.join("."), t._effect]), dt("span", {
1196
+ u = n;
1197
+ u !== null && typeof u == "object" && (u = JSON.stringify(u));
1198
+ const G = document.createTextNode(String(u));
1199
+ f.replaceWith(G);
1200
+ }, [t._stateKey, t._path.join("."), t._effect]), ut("span", {
1200
1201
  ref: i,
1201
1202
  style: { display: "none" },
1202
- "data-signal-id": f
1203
+ "data-signal-id": S
1203
1204
  });
1204
1205
  }
1205
- function he(t) {
1206
- const i = Ft(
1207
- (f) => {
1208
- const u = r.getState().stateComponents.get(t._stateKey) || {
1206
+ function Ie(t) {
1207
+ const i = Ut(
1208
+ (S) => {
1209
+ const f = r.getState().stateComponents.get(t._stateKey) || {
1209
1210
  components: /* @__PURE__ */ new Map()
1210
1211
  };
1211
- return u.components.set(t._stateKey, {
1212
- forceUpdate: f,
1212
+ return f.components.set(t._stateKey, {
1213
+ forceUpdate: S,
1213
1214
  paths: /* @__PURE__ */ new Set([t._path.join(".")])
1214
- }), () => u.components.delete(t._stateKey);
1215
+ }), () => f.components.delete(t._stateKey);
1215
1216
  },
1216
1217
  () => r.getState().getNestedState(t._stateKey, t._path)
1217
1218
  );
1218
- return dt("text", {}, String(i));
1219
+ return ut("text", {}, String(i));
1219
1220
  }
1220
- function ee({
1221
+ function ne({
1221
1222
  stateKey: t,
1222
1223
  itemComponentId: i,
1223
- itemPath: f,
1224
- formOpts: u,
1224
+ itemPath: S,
1225
+ formOpts: f,
1225
1226
  children: h
1226
1227
  }) {
1227
- const [, x] = it({}), [p, y] = Bt(), s = q(null), m = q(null);
1228
- return St(
1229
- (n) => {
1230
- p(n), s.current = n;
1228
+ const [, b] = st({}), [p, y] = qt(), s = J(null), m = J(null), n = mt(
1229
+ (u) => {
1230
+ p(u), s.current = u;
1231
1231
  },
1232
1232
  [p]
1233
- ), rt(() => {
1234
- y.height > 0 && y.height !== m.current && (m.current = y.height, r.getState().setShadowMetadata(t, f, {
1233
+ );
1234
+ return rt(() => {
1235
+ y.height > 0 && y.height !== m.current && (m.current = y.height, r.getState().setShadowMetadata(t, S, {
1235
1236
  virtualizer: {
1236
1237
  itemHeight: y.height,
1237
1238
  domRef: s.current
1238
1239
  // Store the actual DOM element reference
1239
1240
  }
1240
1241
  }));
1241
- }, [y.height, t, f]), Ct(() => {
1242
- const n = `${t}////${i}`, g = r.getState().stateComponents.get(t) || {
1242
+ }, [y.height, t, S]), Ct(() => {
1243
+ const u = `${t}////${i}`, G = r.getState().stateComponents.get(t) || {
1243
1244
  components: /* @__PURE__ */ new Map()
1244
1245
  };
1245
- return g.components.set(n, {
1246
- forceUpdate: () => x({}),
1247
- paths: /* @__PURE__ */ new Set([f.join(".")])
1248
- }), r.getState().stateComponents.set(t, g), () => {
1249
- const L = r.getState().stateComponents.get(t);
1250
- L && L.components.delete(n);
1246
+ return G.components.set(u, {
1247
+ forceUpdate: () => b({}),
1248
+ paths: /* @__PURE__ */ new Set([S.join(".")])
1249
+ }), r.getState().stateComponents.set(t, G), () => {
1250
+ const R = r.getState().stateComponents.get(t);
1251
+ R && R.components.delete(u);
1251
1252
  };
1252
- }, [t, i, f.join(".")]), /* @__PURE__ */ pt(_t, { formOpts: u, path: f, stateKey: t, children: h });
1253
+ }, [t, i, S.join(".")]), /* @__PURE__ */ At("div", { ref: n, children: h });
1253
1254
  }
1254
1255
  export {
1255
1256
  Pt as $cogsSignal,
1256
- he as $cogsSignalStore,
1257
- fe as addStateOptions,
1258
- Se as createCogsState,
1259
- me as notifyComponent,
1260
- Qt as useCogsStateFn
1257
+ Ie as $cogsSignalStore,
1258
+ me as addStateOptions,
1259
+ he as createCogsState,
1260
+ ye as notifyComponent,
1261
+ Kt as useCogsStateFn
1261
1262
  };
1262
1263
  //# sourceMappingURL=CogsState.jsx.map