cogsbox-state 0.5.306 → 0.5.308

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