cogsbox-state 0.5.260 → 0.5.261

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