cogsbox-state 0.5.257 → 0.5.258

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