cogsbox-state 0.5.63 → 0.5.65

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,14 +1,14 @@
1
1
  "use client";
2
- import { jsx as st } from "react/jsx-runtime";
3
- import { useState as K, useRef as B, useEffect as tt, useLayoutEffect as It, useMemo as Et, createElement as X, useSyncExternalStore as _t, startTransition as Z } from "react";
4
- import { transformStateFunc as pt, isFunction as q, getNestedValue as G, isDeepEqual as W, debounce as wt } from "./utility.js";
5
- import { pushFunc as ct, updateFn as J, cutFunc as lt, ValidationWrapper as Nt, FormControlComponent as Vt } from "./Functions.jsx";
2
+ import { jsx as lt } from "react/jsx-runtime";
3
+ import { useState as et, useRef as Z, useEffect as nt, useLayoutEffect as It, useMemo as Et, createElement as Y, useSyncExternalStore as _t, startTransition as H } from "react";
4
+ import { transformStateFunc as wt, isFunction as q, getNestedValue as G, isDeepEqual as W, debounce as pt } from "./utility.js";
5
+ import { pushFunc as K, updateFn as B, cutFunc as J, ValidationWrapper as Vt, FormControlComponent as Nt } from "./Functions.jsx";
6
6
  import "zod";
7
- import { getGlobalStore as r, formRefStore as dt } from "./store.js";
7
+ import { getGlobalStore as o, formRefStore as dt } from "./store.js";
8
8
  import { useCogsConfig as $t } from "./CogsStateClient.jsx";
9
- import et from "./node_modules/uuid/dist/esm-browser/v4.js";
9
+ import rt from "./node_modules/uuid/dist/esm-browser/v4.js";
10
10
  function ut(t, i) {
11
- const f = r.getState().getInitialOptions, S = r.getState().setInitialStateOptions, u = f(t) || {};
11
+ const f = o.getState().getInitialOptions, S = o.getState().setInitialStateOptions, u = f(t) || {};
12
12
  S(t, {
13
13
  ...u,
14
14
  ...i
@@ -19,19 +19,19 @@ function ft({
19
19
  options: i,
20
20
  initialOptionsPart: f
21
21
  }) {
22
- const S = nt(t) || {}, u = f[t] || {}, _ = r.getState().setInitialStateOptions, I = { ...u, ...S };
22
+ const S = ot(t) || {}, u = f[t] || {}, _ = o.getState().setInitialStateOptions, E = { ...u, ...S };
23
23
  let y = !1;
24
24
  if (i)
25
25
  for (const g in i)
26
- I.hasOwnProperty(g) ? g == "localStorage" && i[g] && I[g].key !== i[g]?.key && (y = !0, I[g] = i[g]) : (y = !0, I[g] = i[g]);
27
- y && _(t, I);
26
+ E.hasOwnProperty(g) ? g == "localStorage" && i[g] && E[g].key !== i[g]?.key && (y = !0, E[g] = i[g]) : (y = !0, E[g] = i[g]);
27
+ y && _(t, E);
28
28
  }
29
29
  function qt(t, { formElements: i, validation: f }) {
30
30
  return { initialState: t, formElements: i, validation: f };
31
31
  }
32
32
  const zt = (t, i) => {
33
33
  let f = t;
34
- const [S, u] = pt(f);
34
+ const [S, u] = wt(f);
35
35
  (i?.formElements || i?.validation) && Object.keys(u).forEach((y) => {
36
36
  u[y] = u[y] || {}, u[y].formElements = {
37
37
  ...i.formElements,
@@ -40,20 +40,20 @@ const zt = (t, i) => {
40
40
  ...u[y].formElements || {}
41
41
  // State-specific overrides
42
42
  };
43
- }), r.getState().setInitialStates(S);
43
+ }), o.getState().setInitialStates(S);
44
44
  const _ = (y, g) => {
45
- const [p] = K(g?.componentId ?? et());
45
+ const [w] = et(g?.componentId ?? rt());
46
46
  ft({
47
47
  stateKey: y,
48
48
  options: g,
49
49
  initialOptionsPart: u
50
50
  });
51
- const l = r.getState().cogsStateStore[y] || S[y], e = g?.modifyState ? g.modifyState(l) : l, [E, $] = jt(
51
+ const c = o.getState().cogsStateStore[y] || S[y], e = g?.modifyState ? g.modifyState(c) : c, [I, $] = jt(
52
52
  e,
53
53
  {
54
54
  stateKey: y,
55
55
  syncUpdate: g?.syncUpdate,
56
- componentId: p,
56
+ componentId: w,
57
57
  localStorage: g?.localStorage,
58
58
  middleware: g?.middleware,
59
59
  enabledSync: g?.enabledSync,
@@ -65,22 +65,22 @@ const zt = (t, i) => {
65
65
  );
66
66
  return $;
67
67
  };
68
- function I(y, g) {
68
+ function E(y, g) {
69
69
  ft({ stateKey: y, options: g, initialOptionsPart: u });
70
70
  }
71
- return { useCogsState: _, setCogsOptions: I };
71
+ return { useCogsState: _, setCogsOptions: E };
72
72
  }, {
73
- setUpdaterState: H,
74
- setState: x,
75
- getInitialOptions: nt,
73
+ setUpdaterState: Q,
74
+ setState: R,
75
+ getInitialOptions: ot,
76
76
  getKeyState: gt,
77
77
  getValidationErrors: ht,
78
78
  setStateLog: At,
79
- updateInitialStateGlobal: rt,
79
+ updateInitialStateGlobal: at,
80
80
  addValidationError: Ct,
81
- removeValidationError: P,
81
+ removeValidationError: U,
82
82
  setServerSyncActions: Tt
83
- } = r.getState(), St = (t) => {
83
+ } = o.getState(), St = (t) => {
84
84
  if (!t) return null;
85
85
  try {
86
86
  const i = window.localStorage.getItem(t);
@@ -100,15 +100,15 @@ const zt = (t, i) => {
100
100
  const _ = {
101
101
  state: t,
102
102
  lastUpdated: Date.now(),
103
- lastSyncedWithServer: r.getState().serverSyncLog[i]?.[0]?.timeStamp,
104
- baseServerState: r.getState().serverState[i]
105
- }, I = `${S}-${i}-${u}`;
106
- window.localStorage.setItem(I, JSON.stringify(_));
103
+ lastSyncedWithServer: o.getState().serverSyncLog[i]?.[0]?.timeStamp,
104
+ baseServerState: o.getState().serverState[i]
105
+ }, E = `${S}-${i}-${u}`;
106
+ window.localStorage.setItem(E, JSON.stringify(_));
107
107
  }
108
108
  }, Ft = (t, i, f, S, u, _) => {
109
- const I = {
109
+ const E = {
110
110
  initialState: i,
111
- updaterState: Q(
111
+ updaterState: X(
112
112
  t,
113
113
  S,
114
114
  u,
@@ -116,22 +116,22 @@ const zt = (t, i) => {
116
116
  ),
117
117
  state: f
118
118
  };
119
- Z(() => {
120
- rt(t, I.initialState), H(t, I.updaterState), x(t, I.state);
119
+ H(() => {
120
+ at(t, E.initialState), Q(t, E.updaterState), R(t, E.state);
121
121
  });
122
122
  }, mt = (t) => {
123
- const i = r.getState().stateComponents.get(t);
123
+ const i = o.getState().stateComponents.get(t);
124
124
  if (!i) return;
125
125
  const f = /* @__PURE__ */ new Set();
126
126
  i.components.forEach((S) => {
127
127
  f.add(() => S.forceUpdate());
128
128
  }), queueMicrotask(() => {
129
- Z(() => {
129
+ H(() => {
130
130
  f.forEach((S) => S());
131
131
  });
132
132
  });
133
- }, Jt = (t, i) => {
134
- const f = r.getState().stateComponents.get(t);
133
+ }, Bt = (t, i) => {
134
+ const f = o.getState().stateComponents.get(t);
135
135
  if (f) {
136
136
  const S = `${t}////${i}`, u = f.components.get(S);
137
137
  u && u.forceUpdate();
@@ -143,150 +143,150 @@ function jt(t, {
143
143
  localStorage: S,
144
144
  formElements: u,
145
145
  middleware: _,
146
- reactiveDeps: I,
146
+ reactiveDeps: E,
147
147
  reactiveType: y,
148
148
  componentId: g,
149
- initialState: p,
150
- syncUpdate: l,
149
+ initialState: w,
150
+ syncUpdate: c,
151
151
  dependencies: e
152
152
  } = {}) {
153
- const [E, $] = K({}), { sessionId: T } = $t();
154
- let R = !i;
155
- const [d] = K(i ?? et()), O = r.getState().stateLog[d], s = B(/* @__PURE__ */ new Set()), b = B(g ?? et()), h = B(null);
156
- h.current = nt(d), tt(() => {
157
- if (l && l.stateKey === d && l.path?.[0]) {
158
- x(d, (o) => ({
159
- ...o,
160
- [l.path[0]]: l.newValue
153
+ const [I, $] = et({}), { sessionId: T } = $t();
154
+ let P = !i;
155
+ const [d] = et(i ?? rt()), O = o.getState().stateLog[d], s = Z(/* @__PURE__ */ new Set()), b = Z(g ?? rt()), h = Z(null);
156
+ h.current = ot(d), nt(() => {
157
+ if (c && c.stateKey === d && c.path?.[0]) {
158
+ R(d, (a) => ({
159
+ ...a,
160
+ [c.path[0]]: c.newValue
161
161
  }));
162
- const a = `${l.stateKey}:${l.path.join(".")}`;
163
- r.getState().setSyncInfo(a, {
164
- timeStamp: l.timeStamp,
165
- userId: l.userId
162
+ const r = `${c.stateKey}:${c.path.join(".")}`;
163
+ o.getState().setSyncInfo(r, {
164
+ timeStamp: c.timeStamp,
165
+ userId: c.userId
166
166
  });
167
167
  }
168
- }, [l]), tt(() => {
168
+ }, [c]), nt(() => {
169
169
  ut(d, {
170
- initialState: p
170
+ initialState: w
171
171
  });
172
- const a = h.current;
173
- let o = null;
174
- a.log && console.log("newoptions", a);
175
- const c = q(a.localStorage?.key) ? a.localStorage?.key(p) : a.localStorage?.key;
176
- c && T && (o = St(
177
- T + "-" + d + "-" + c
172
+ const r = h.current;
173
+ let a = null;
174
+ r.log && console.log("newoptions", r);
175
+ const l = q(r.localStorage?.key) ? r.localStorage?.key(w) : r.localStorage?.key;
176
+ l && T && (a = St(
177
+ T + "-" + d + "-" + l
178
178
  ));
179
179
  let m = null;
180
- p && (m = p, o && o.lastUpdated > (o.lastSyncedWithServer || 0) && (m = o.state), Ft(
180
+ w && (m = w, a && a.lastUpdated > (a.lastSyncedWithServer || 0) && (m = a.state), Ft(
181
181
  d,
182
- p,
182
+ w,
183
183
  m,
184
184
  D,
185
185
  b.current,
186
186
  T
187
187
  ), mt(d), $({}));
188
- }, [p, ...e || []]), It(() => {
189
- R && ut(d, {
188
+ }, [w, ...e || []]), It(() => {
189
+ P && ut(d, {
190
190
  serverSync: f,
191
191
  formElements: u,
192
- initialState: p,
192
+ initialState: w,
193
193
  localStorage: S,
194
194
  middleware: _
195
195
  });
196
- const a = `${d}////${b.current}`, o = r.getState().stateComponents.get(d) || {
196
+ const r = `${d}////${b.current}`, a = o.getState().stateComponents.get(d) || {
197
197
  components: /* @__PURE__ */ new Map()
198
198
  };
199
- return o.components.set(a, {
199
+ return a.components.set(r, {
200
200
  forceUpdate: () => $({}),
201
201
  paths: /* @__PURE__ */ new Set(),
202
202
  deps: [],
203
- depsFunction: I || void 0,
203
+ depsFunction: E || void 0,
204
204
  reactiveType: y ?? ["component", "deps"]
205
- }), r.getState().stateComponents.set(d, o), $({}), () => {
206
- const c = `${d}////${b.current}`;
207
- o && (o.components.delete(c), o.components.size === 0 && r.getState().stateComponents.delete(d));
205
+ }), o.getState().stateComponents.set(d, a), $({}), () => {
206
+ const l = `${d}////${b.current}`;
207
+ a && (a.components.delete(l), a.components.size === 0 && o.getState().stateComponents.delete(d));
208
208
  };
209
209
  }, []);
210
- const D = (a, o, c, m) => {
211
- if (Array.isArray(o)) {
212
- const w = `${d}-${o.join(".")}`;
213
- s.current.add(w);
210
+ const D = (r, a, l, m) => {
211
+ if (Array.isArray(a)) {
212
+ const p = `${d}-${a.join(".")}`;
213
+ s.current.add(p);
214
214
  }
215
- x(d, (w) => {
216
- const A = q(a) ? a(w) : a, F = `${d}-${o.join(".")}`;
215
+ R(d, (p) => {
216
+ const A = q(r) ? r(p) : r, F = `${d}-${a.join(".")}`;
217
217
  if (F) {
218
- let U = !1, v = r.getState().signalDomElements.get(F);
219
- if ((!v || v.size === 0) && (c.updateType === "insert" || c.updateType === "cut")) {
220
- const V = o.slice(0, -1), C = G(A, V);
218
+ let x = !1, v = o.getState().signalDomElements.get(F);
219
+ if ((!v || v.size === 0) && (l.updateType === "insert" || l.updateType === "cut")) {
220
+ const N = a.slice(0, -1), C = G(A, N);
221
221
  if (Array.isArray(C)) {
222
- U = !0;
223
- const N = `${d}-${V.join(".")}`;
224
- v = r.getState().signalDomElements.get(N);
222
+ x = !0;
223
+ const V = `${d}-${N.join(".")}`;
224
+ v = o.getState().signalDomElements.get(V);
225
225
  }
226
226
  }
227
227
  if (v) {
228
- const V = U ? G(A, o.slice(0, -1)) : G(A, o);
229
- v.forEach(({ parentId: C, position: N, effect: L }) => {
228
+ const N = x ? G(A, a.slice(0, -1)) : G(A, a);
229
+ v.forEach(({ parentId: C, position: V, effect: L }) => {
230
230
  const j = document.querySelector(
231
231
  `[data-parent-id="${C}"]`
232
232
  );
233
233
  if (j) {
234
- const it = Array.from(j.childNodes);
235
- if (it[N]) {
236
- const vt = L ? new Function("state", `return (${L})(state)`)(V) : V;
237
- it[N].textContent = String(vt);
234
+ const ct = Array.from(j.childNodes);
235
+ if (ct[V]) {
236
+ const vt = L ? new Function("state", `return (${L})(state)`)(N) : N;
237
+ ct[V].textContent = String(vt);
238
238
  }
239
239
  }
240
240
  });
241
241
  }
242
242
  }
243
- c.updateType === "update" && (m || h.current?.validationKey) && o && P(
244
- (m || h.current?.validationKey) + "." + o.join(".")
243
+ l.updateType === "update" && (m || h.current?.validationKey) && a && U(
244
+ (m || h.current?.validationKey) + "." + a.join(".")
245
245
  );
246
- const k = o.slice(0, o.length - 1);
247
- c.updateType === "cut" && h.current?.validationKey && P(
246
+ const k = a.slice(0, a.length - 1);
247
+ l.updateType === "cut" && h.current?.validationKey && U(
248
248
  h.current?.validationKey + "." + k.join(".")
249
- ), c.updateType === "insert" && h.current?.validationKey && ht(
249
+ ), l.updateType === "insert" && h.current?.validationKey && ht(
250
250
  h.current?.validationKey + "." + k.join(".")
251
- ).filter(([v, V]) => {
251
+ ).filter(([v, N]) => {
252
252
  let C = v?.split(".").length;
253
253
  if (v == k.join(".") && C == k.length - 1) {
254
- let N = v + "." + k;
255
- P(v), Ct(N, V);
254
+ let V = v + "." + k;
255
+ U(v), Ct(V, N);
256
256
  }
257
257
  });
258
- const M = G(w, o), z = G(A, o), yt = c.updateType === "update" ? o.join(".") : [...o].slice(0, -1).join("."), ot = r.getState().stateComponents.get(d);
259
- if (ot)
260
- for (const [U, v] of ot.components.entries()) {
261
- let V = !1;
258
+ const M = G(p, a), z = G(A, a), yt = l.updateType === "update" ? a.join(".") : [...a].slice(0, -1).join("."), it = o.getState().stateComponents.get(d);
259
+ if (it)
260
+ for (const [x, v] of it.components.entries()) {
261
+ let N = !1;
262
262
  const C = Array.isArray(v.reactiveType) ? v.reactiveType : [v.reactiveType || "component"];
263
263
  if (!C.includes("none")) {
264
264
  if (C.includes("all")) {
265
265
  v.forceUpdate();
266
266
  continue;
267
267
  }
268
- if (C.includes("component") && v.paths && (v.paths.has(yt) || v.paths.has("")) && (V = !0), !V && C.includes("deps") && v.depsFunction) {
269
- const N = v.depsFunction(A);
270
- typeof N == "boolean" ? N && (V = !0) : W(v.deps, N) || (v.deps = N, V = !0);
268
+ if (C.includes("component") && v.paths && (v.paths.has(yt) || v.paths.has("")) && (N = !0), !N && C.includes("deps") && v.depsFunction) {
269
+ const V = v.depsFunction(A);
270
+ typeof V == "boolean" ? V && (N = !0) : W(v.deps, V) || (v.deps = V, N = !0);
271
271
  }
272
- V && v.forceUpdate();
272
+ N && v.forceUpdate();
273
273
  }
274
274
  }
275
- const at = {
275
+ const st = {
276
276
  timeStamp: Date.now(),
277
277
  stateKey: d,
278
- path: o,
279
- updateType: c.updateType,
278
+ path: a,
279
+ updateType: l.updateType,
280
280
  status: "new",
281
281
  oldValue: M,
282
282
  newValue: z
283
283
  };
284
- if (At(d, (U) => {
285
- const V = [...U ?? [], at].reduce((C, N) => {
286
- const L = `${N.stateKey}:${JSON.stringify(N.path)}`, j = C.get(L);
287
- return j ? (j.timeStamp = Math.max(j.timeStamp, N.timeStamp), j.newValue = N.newValue, j.oldValue = j.oldValue ?? N.oldValue, j.updateType = N.updateType) : C.set(L, { ...N }), C;
284
+ if (At(d, (x) => {
285
+ const N = [...x ?? [], st].reduce((C, V) => {
286
+ const L = `${V.stateKey}:${JSON.stringify(V.path)}`, j = C.get(L);
287
+ return j ? (j.timeStamp = Math.max(j.timeStamp, V.timeStamp), j.newValue = V.newValue, j.oldValue = j.oldValue ?? V.oldValue, j.updateType = V.updateType) : C.set(L, { ...V }), C;
288
288
  }, /* @__PURE__ */ new Map());
289
- return Array.from(V.values());
289
+ return Array.from(N.values());
290
290
  }), kt(
291
291
  A,
292
292
  d,
@@ -294,12 +294,12 @@ function jt(t, {
294
294
  T
295
295
  ), _ && _({
296
296
  updateLog: O,
297
- update: at
297
+ update: st
298
298
  }), h.current?.serverSync) {
299
- const U = r.getState().serverState[d], v = h.current?.serverSync;
299
+ const x = o.getState().serverState[d], v = h.current?.serverSync;
300
300
  Tt(d, {
301
301
  syncKey: typeof v.syncKey == "string" ? v.syncKey : v.syncKey({ state: A }),
302
- rollBackState: U,
302
+ rollBackState: x,
303
303
  actionTimeStamp: Date.now() + (v.debounce ?? 3e3),
304
304
  status: "waiting"
305
305
  });
@@ -307,16 +307,16 @@ function jt(t, {
307
307
  return A;
308
308
  });
309
309
  };
310
- r.getState().updaterState[d] || (H(
310
+ o.getState().updaterState[d] || (Q(
311
311
  d,
312
- Q(
312
+ X(
313
313
  d,
314
314
  D,
315
315
  b.current,
316
316
  T
317
317
  )
318
- ), r.getState().cogsStateStore[d] || x(d, t), r.getState().initialStateGlobal[d] || rt(d, t));
319
- const n = Et(() => Q(
318
+ ), o.getState().cogsStateStore[d] || R(d, t), o.getState().initialStateGlobal[d] || at(d, t));
319
+ const n = Et(() => X(
320
320
  d,
321
321
  D,
322
322
  b.current,
@@ -324,209 +324,219 @@ function jt(t, {
324
324
  ), [d]);
325
325
  return [gt(d), n];
326
326
  }
327
- function Q(t, i, f, S) {
327
+ function X(t, i, f, S) {
328
328
  const u = /* @__PURE__ */ new Map();
329
329
  let _ = 0;
330
- const I = (l) => {
331
- const e = l.join(".");
332
- for (const [E] of u)
333
- (E === e || E.startsWith(e + ".")) && u.delete(E);
330
+ const E = (c) => {
331
+ const e = c.join(".");
332
+ for (const [I] of u)
333
+ (I === e || I.startsWith(e + ".")) && u.delete(I);
334
334
  _++;
335
335
  }, y = /* @__PURE__ */ new Map(), g = {
336
- removeValidation: (l) => {
337
- l?.validationKey && P(l.validationKey);
336
+ removeValidation: (c) => {
337
+ c?.validationKey && U(c.validationKey);
338
338
  },
339
- revertToInitialState: (l) => {
340
- const e = r.getState().getInitialOptions(t)?.validation;
341
- e?.key && P(e?.key), l?.validationKey && P(l.validationKey);
342
- const E = r.getState().initialStateGlobal[t];
339
+ revertToInitialState: (c) => {
340
+ const e = o.getState().getInitialOptions(t)?.validation;
341
+ e?.key && U(e?.key), c?.validationKey && U(c.validationKey);
342
+ const I = o.getState().initialStateGlobal[t];
343
343
  u.clear(), _++;
344
- const $ = p(E, []);
345
- Z(() => {
346
- H(t, $), x(t, E);
347
- const T = r.getState().stateComponents.get(t);
344
+ const $ = w(I, []);
345
+ return H(() => {
346
+ Q(t, $), R(t, I);
347
+ const T = o.getState().stateComponents.get(t);
348
348
  T && T.components.forEach((O) => {
349
349
  O.forceUpdate();
350
350
  });
351
- const R = nt(t), d = q(R?.localStorage?.key) ? R?.localStorage?.key(E) : R?.localStorage?.key;
351
+ const P = ot(t), d = q(P?.localStorage?.key) ? P?.localStorage?.key(I) : P?.localStorage?.key;
352
352
  d && localStorage.removeItem(d);
353
- });
353
+ }), I;
354
354
  },
355
- updateInitialState: (l) => {
355
+ updateInitialState: (c) => {
356
356
  u.clear(), _++;
357
- const e = Q(
357
+ const e = X(
358
358
  t,
359
359
  i,
360
360
  f,
361
361
  S
362
362
  );
363
- return Z(() => {
364
- rt(t, l), H(t, e), x(t, l);
365
- const E = r.getState().stateComponents.get(t);
366
- E && E.components.forEach(($) => {
363
+ return H(() => {
364
+ at(t, c), Q(t, e), R(t, c);
365
+ const I = o.getState().stateComponents.get(t);
366
+ I && I.components.forEach(($) => {
367
367
  $.forceUpdate();
368
368
  }), localStorage.removeItem(t);
369
369
  }), {
370
- fetchId: (E) => e.get()[E]
370
+ fetchId: (I) => e.get()[I]
371
371
  };
372
372
  },
373
- _initialState: r.getState().initialStateGlobal[t],
374
- _serverState: r.getState().serverState[t],
375
- _isLoading: r.getState().isLoadingGlobal[t],
373
+ _initialState: o.getState().initialStateGlobal[t],
374
+ _serverState: o.getState().serverState[t],
375
+ _isLoading: o.getState().isLoadingGlobal[t],
376
376
  _isServerSynced: () => {
377
- const l = r.getState().serverState[t];
378
- return !!(l && W(l, gt(t)));
377
+ const c = o.getState().serverState[t];
378
+ return !!(c && W(c, gt(t)));
379
379
  }
380
380
  };
381
- function p(l, e = [], E) {
381
+ function w(c, e = [], I) {
382
382
  const $ = e.map(String).join(".");
383
383
  u.get($);
384
384
  const T = function() {
385
- return r().getNestedState(t, e);
385
+ return o().getNestedState(t, e);
386
386
  };
387
387
  Object.keys(g).forEach((O) => {
388
388
  T[O] = g[O];
389
389
  });
390
- const R = {
390
+ const P = {
391
391
  apply(O, s, b) {
392
- return r().getNestedState(t, e);
392
+ return o().getNestedState(t, e);
393
393
  },
394
394
  get(O, s) {
395
395
  if (s !== "then" && !s.startsWith("$") && s !== "stateMapNoRender") {
396
- const n = e.join("."), a = `${t}////${f}`, o = r.getState().stateComponents.get(t);
397
- if (o) {
398
- const c = o.components.get(a);
399
- c && (e.length > 0 || s === "get") && c.paths.add(n);
396
+ const n = e.join("."), r = `${t}////${f}`, a = o.getState().stateComponents.get(t);
397
+ if (a) {
398
+ const l = a.components.get(r);
399
+ l && (e.length > 0 || s === "get") && l.paths.add(n);
400
400
  }
401
401
  }
402
402
  if (s === "showValidationErrors")
403
403
  return () => {
404
- const n = r.getState().getInitialOptions(t)?.validation;
404
+ const n = o.getState().getInitialOptions(t)?.validation;
405
405
  if (!n?.key)
406
406
  throw new Error("Validation key not found");
407
- return r.getState().getValidationErrors(n.key + "." + e.join("."));
407
+ return o.getState().getValidationErrors(n.key + "." + e.join("."));
408
408
  };
409
- if (Array.isArray(l)) {
409
+ if (Array.isArray(c)) {
410
410
  if (s === "getSelected")
411
411
  return () => {
412
412
  const n = y.get(e.join("."));
413
413
  if (n !== void 0)
414
- return p(
415
- l[n],
414
+ return w(
415
+ c[n],
416
416
  [...e, n.toString()],
417
- E
417
+ I
418
418
  );
419
419
  };
420
420
  if (s === "stateMap" || s === "stateMapNoRender")
421
421
  return (n) => {
422
- const a = E?.filtered?.some(
423
- (c) => c.join(".") === e.join(".")
424
- ), o = a ? l : r.getState().getNestedState(t, e);
425
- return s !== "stateMapNoRender" && (u.clear(), _++), o.map((c, m) => {
426
- const w = a && c.__origIndex ? c.__origIndex : m, A = p(
427
- c,
428
- [...e, w.toString()],
429
- E
422
+ const r = I?.filtered?.some(
423
+ (l) => l.join(".") === e.join(".")
424
+ ), a = r ? c : o.getState().getNestedState(t, e);
425
+ return s !== "stateMapNoRender" && (u.clear(), _++), a.map((l, m) => {
426
+ const p = r && l.__origIndex ? l.__origIndex : m, A = w(
427
+ l,
428
+ [...e, p.toString()],
429
+ I
430
430
  );
431
431
  return n(
432
- c,
432
+ l,
433
433
  A,
434
434
  m,
435
- l,
436
- p(l, e, E)
435
+ c,
436
+ w(c, e, I)
437
437
  );
438
438
  });
439
439
  };
440
440
  if (s === "$stateMap")
441
- return (n) => X(bt, {
441
+ return (n) => Y(bt, {
442
442
  proxy: {
443
443
  _stateKey: t,
444
444
  _path: e,
445
445
  _mapFn: n
446
446
  // Pass the actual function, not string
447
447
  },
448
- rebuildStateShape: p
448
+ rebuildStateShape: w
449
449
  });
450
450
  if (s === "stateFlattenOn")
451
451
  return (n) => {
452
- const o = E?.filtered?.some(
452
+ const a = I?.filtered?.some(
453
453
  (m) => m.join(".") === e.join(".")
454
- ) ? l : r.getState().getNestedState(t, e);
454
+ ) ? c : o.getState().getNestedState(t, e);
455
455
  u.clear(), _++;
456
- const c = o.flatMap(
457
- (m, w) => m[n] ?? []
456
+ const l = a.flatMap(
457
+ (m, p) => m[n] ?? []
458
458
  );
459
- return p(
460
- c,
459
+ return w(
460
+ l,
461
461
  [...e, "[*]", n],
462
- E
462
+ I
463
463
  );
464
464
  };
465
465
  if (s === "findWith")
466
- return (n, a) => {
467
- const o = l.findIndex(
468
- (w) => w[n] === a
466
+ return (n, r) => {
467
+ const a = c.findIndex(
468
+ (p) => p[n] === r
469
469
  );
470
- if (o === -1) return;
471
- const c = l[o], m = [...e, o.toString()];
472
- return u.clear(), _++, u.clear(), _++, p(c, m);
470
+ if (a === -1) return;
471
+ const l = c[a], m = [...e, a.toString()];
472
+ return u.clear(), _++, u.clear(), _++, w(l, m);
473
473
  };
474
474
  if (s === "index")
475
475
  return (n) => {
476
- const a = l[n];
477
- return p(a, [...e, n.toString()]);
476
+ const r = c[n];
477
+ return w(r, [...e, n.toString()]);
478
478
  };
479
479
  if (s === "insert")
480
- return (n) => (I(e), ct(i, n, e, t), p(
481
- r.getState().cogsStateStore[t],
480
+ return (n) => (E(e), K(i, n, e, t), w(
481
+ o.getState().cogsStateStore[t],
482
482
  []
483
483
  ));
484
484
  if (s === "uniqueInsert")
485
- return (n, a, o) => {
486
- const c = r.getState().getNestedState(t, e), m = q(n) ? n(c) : n;
487
- let w = null;
488
- if (!c.some((F) => {
489
- if (a) {
490
- const M = a.every(
485
+ return (n, r, a) => {
486
+ const l = o.getState().getNestedState(t, e), m = q(n) ? n(l) : n;
487
+ let p = null;
488
+ if (!l.some((F) => {
489
+ if (r) {
490
+ const M = r.every(
491
491
  (z) => W(F[z], m[z])
492
492
  );
493
- return M && (w = F), M;
493
+ return M && (p = F), M;
494
494
  }
495
495
  const k = W(F, m);
496
- return k && (w = F), k;
496
+ return k && (p = F), k;
497
497
  }))
498
- I(e), ct(i, m, e, t);
499
- else if (o && w) {
500
- const F = o(w), k = c.map(
501
- (M) => W(M, w) ? F : M
498
+ E(e), K(i, m, e, t);
499
+ else if (a && p) {
500
+ const F = a(p), k = l.map(
501
+ (M) => W(M, p) ? F : M
502
502
  );
503
- I(e), J(i, k, e);
503
+ E(e), B(i, k, e);
504
504
  }
505
505
  };
506
506
  if (s === "cut")
507
- return (n, a) => {
508
- a?.waitForSync || (I(e), lt(i, e, t, n));
507
+ return (n, r) => {
508
+ r?.waitForSync || (E(e), J(i, e, t, n));
509
+ };
510
+ if (s === "cutByValue")
511
+ return (n) => {
512
+ for (let r = 0; r < c.length; r++)
513
+ c[r] === n && J(i, e, t, r);
514
+ };
515
+ if (s === "toggleByValue")
516
+ return (n) => {
517
+ const r = c.findIndex((a) => a === n);
518
+ r > -1 ? J(i, e, t, r) : K(i, n, e, t);
509
519
  };
510
520
  if (s === "stateFilter")
511
521
  return (n) => {
512
- const a = l.map((m, w) => ({
522
+ const r = c.map((m, p) => ({
513
523
  ...m,
514
- __origIndex: w.toString()
515
- })), o = [], c = [];
516
- for (let m = 0; m < a.length; m++)
517
- n(a[m], m) && (o.push(m), c.push(a[m]));
518
- return u.clear(), _++, p(c, e, {
519
- filtered: [...E?.filtered || [], e],
520
- validIndices: o
524
+ __origIndex: p.toString()
525
+ })), a = [], l = [];
526
+ for (let m = 0; m < r.length; m++)
527
+ n(r[m], m) && (a.push(m), l.push(r[m]));
528
+ return u.clear(), _++, w(l, e, {
529
+ filtered: [...I?.filtered || [], e],
530
+ validIndices: a
521
531
  // Always pass validIndices, even if empty
522
532
  });
523
533
  };
524
534
  }
525
535
  const b = e[e.length - 1];
526
536
  if (!isNaN(Number(b))) {
527
- const n = e.slice(0, -1), a = r.getState().getNestedState(t, n);
528
- if (Array.isArray(a) && s === "cut")
529
- return () => lt(
537
+ const n = e.slice(0, -1), r = o.getState().getNestedState(t, n);
538
+ if (Array.isArray(r) && s === "cut")
539
+ return () => J(
530
540
  i,
531
541
  n,
532
542
  t,
@@ -534,76 +544,76 @@ function Q(t, i, f, S) {
534
544
  );
535
545
  }
536
546
  if (s === "get")
537
- return () => r.getState().getNestedState(t, e);
547
+ return () => o.getState().getNestedState(t, e);
538
548
  if (s === "$derive")
539
- return (n) => Y({
549
+ return (n) => tt({
540
550
  _stateKey: t,
541
551
  _path: e,
542
552
  _effect: n.toString()
543
553
  });
544
554
  if (s === "$derive")
545
- return (n) => Y({
555
+ return (n) => tt({
546
556
  _stateKey: t,
547
557
  _path: e,
548
558
  _effect: n.toString()
549
559
  });
550
560
  if (s === "$get")
551
- return () => Y({
561
+ return () => tt({
552
562
  _stateKey: t,
553
563
  _path: e
554
564
  });
555
565
  if (s === "lastSynced") {
556
566
  const n = `${t}:${e.join(".")}`;
557
- return r.getState().getSyncInfo(n);
567
+ return o.getState().getSyncInfo(n);
558
568
  }
559
569
  if (s === "_selected") {
560
- const n = e.slice(0, -1), a = n.join("."), o = r.getState().getNestedState(t, n);
561
- return Array.isArray(o) ? Number(e[e.length - 1]) === y.get(a) : void 0;
570
+ const n = e.slice(0, -1), r = n.join("."), a = o.getState().getNestedState(t, n);
571
+ return Array.isArray(a) ? Number(e[e.length - 1]) === y.get(r) : void 0;
562
572
  }
563
573
  if (s == "getLocalStorage")
564
574
  return (n) => St(S + "-" + t + "-" + n);
565
575
  if (s === "setSelected")
566
576
  return (n) => {
567
- const a = e.slice(0, -1), o = Number(e[e.length - 1]), c = a.join(".");
568
- n ? y.set(c, o) : y.delete(c);
569
- const m = r.getState().getNestedState(t, [...a]);
570
- J(i, m, a), I(a);
577
+ const r = e.slice(0, -1), a = Number(e[e.length - 1]), l = r.join(".");
578
+ n ? y.set(l, a) : y.delete(l);
579
+ const m = o.getState().getNestedState(t, [...r]);
580
+ B(i, m, r), E(r);
571
581
  };
572
582
  if (e.length == 0) {
573
583
  if (s === "validateZodSchema")
574
584
  return () => {
575
- const n = r.getState().getInitialOptions(t)?.validation, a = r.getState().addValidationError;
585
+ const n = o.getState().getInitialOptions(t)?.validation, r = o.getState().addValidationError;
576
586
  if (!n?.zodSchema)
577
587
  throw new Error("Zod schema not found");
578
588
  if (!n?.key)
579
589
  throw new Error("Validation key not found");
580
- P(n.key);
581
- const o = r.getState().cogsStateStore[t];
590
+ U(n.key);
591
+ const a = o.getState().cogsStateStore[t];
582
592
  try {
583
- const c = r.getState().getValidationErrors(n.key);
584
- c && c.length > 0 && c.forEach(([w]) => {
585
- w && w.startsWith(n.key) && P(w);
593
+ const l = o.getState().getValidationErrors(n.key);
594
+ l && l.length > 0 && l.forEach(([p]) => {
595
+ p && p.startsWith(n.key) && U(p);
586
596
  });
587
- const m = n.zodSchema.safeParse(o);
597
+ const m = n.zodSchema.safeParse(a);
588
598
  return m.success ? !0 : (m.error.errors.forEach((A) => {
589
599
  const F = A.path, k = A.message, M = [n.key, ...F].join(".");
590
- a(M, k);
600
+ r(M, k);
591
601
  }), mt(t), !1);
592
- } catch (c) {
593
- return console.error("Zod schema validation failed", c), !1;
602
+ } catch (l) {
603
+ return console.error("Zod schema validation failed", l), !1;
594
604
  }
595
605
  };
596
606
  if (s === "_componentId") return f;
597
607
  if (s === "getComponents")
598
- return () => r().stateComponents.get(t);
608
+ return () => o().stateComponents.get(t);
599
609
  if (s === "getAllFormRefs")
600
610
  return () => dt.getState().getFormRefsByStateKey(t);
601
611
  if (s === "_initialState")
602
- return r.getState().initialStateGlobal[t];
612
+ return o.getState().initialStateGlobal[t];
603
613
  if (s === "_serverState")
604
- return r.getState().serverState[t];
614
+ return o.getState().serverState[t];
605
615
  if (s === "_isLoading")
606
- return r.getState().isLoadingGlobal[t];
616
+ return o.getState().isLoadingGlobal[t];
607
617
  if (s === "revertToInitialState")
608
618
  return g.revertToInitialState;
609
619
  if (s === "updateInitialState") return g.updateInitialState;
@@ -614,15 +624,15 @@ function Q(t, i, f, S) {
614
624
  if (s === "validationWrapper")
615
625
  return ({
616
626
  children: n,
617
- hideMessage: a
618
- }) => /* @__PURE__ */ st(
619
- Nt,
627
+ hideMessage: r
628
+ }) => /* @__PURE__ */ lt(
629
+ Vt,
620
630
  {
621
- formOpts: a ? { validation: { message: "" } } : void 0,
631
+ formOpts: r ? { validation: { message: "" } } : void 0,
622
632
  path: e,
623
- validationKey: r.getState().getInitialOptions(t)?.validation?.key || "",
633
+ validationKey: o.getState().getInitialOptions(t)?.validation?.key || "",
624
634
  stateKey: t,
625
- validIndices: E?.validIndices,
635
+ validIndices: I?.validIndices,
626
636
  children: n
627
637
  }
628
638
  );
@@ -630,102 +640,102 @@ function Q(t, i, f, S) {
630
640
  if (s === "_path") return e;
631
641
  if (s === "_isServerSynced") return g._isServerSynced;
632
642
  if (s === "update")
633
- return (n, a) => {
634
- if (a?.debounce)
635
- wt(() => {
636
- J(i, n, e, "");
637
- const o = r.getState().getNestedState(t, e);
638
- a?.afterUpdate && a.afterUpdate(o);
639
- }, a.debounce);
643
+ return (n, r) => {
644
+ if (r?.debounce)
645
+ pt(() => {
646
+ B(i, n, e, "");
647
+ const a = o.getState().getNestedState(t, e);
648
+ r?.afterUpdate && r.afterUpdate(a);
649
+ }, r.debounce);
640
650
  else {
641
- J(i, n, e, "");
642
- const o = r.getState().getNestedState(t, e);
643
- a?.afterUpdate && a.afterUpdate(o);
651
+ B(i, n, e, "");
652
+ const a = o.getState().getNestedState(t, e);
653
+ r?.afterUpdate && r.afterUpdate(a);
644
654
  }
645
- I(e);
655
+ E(e);
646
656
  };
647
657
  if (s === "formElement")
648
- return (n, a) => /* @__PURE__ */ st(
649
- Vt,
658
+ return (n, r) => /* @__PURE__ */ lt(
659
+ Nt,
650
660
  {
651
661
  setState: i,
652
662
  stateKey: t,
653
663
  path: e,
654
664
  child: n,
655
- formOpts: a
665
+ formOpts: r
656
666
  }
657
667
  );
658
- const h = [...e, s], D = r.getState().getNestedState(t, h);
659
- return p(D, h, E);
668
+ const h = [...e, s], D = o.getState().getNestedState(t, h);
669
+ return w(D, h, I);
660
670
  }
661
- }, d = new Proxy(T, R);
671
+ }, d = new Proxy(T, P);
662
672
  return u.set($, {
663
673
  proxy: d,
664
674
  stateVersion: _
665
675
  }), d;
666
676
  }
667
- return p(
668
- r.getState().getNestedState(t, [])
677
+ return w(
678
+ o.getState().getNestedState(t, [])
669
679
  );
670
680
  }
671
- function Y(t) {
672
- return X(Mt, { proxy: t });
681
+ function tt(t) {
682
+ return Y(Mt, { proxy: t });
673
683
  }
674
684
  function bt({
675
685
  proxy: t,
676
686
  rebuildStateShape: i
677
687
  }) {
678
- const f = r().getNestedState(t._stateKey, t._path);
688
+ const f = o().getNestedState(t._stateKey, t._path);
679
689
  return Array.isArray(f) ? i(
680
690
  f,
681
691
  t._path
682
692
  ).stateMapNoRender(
683
- (u, _, I, y, g) => t._mapFn(u, _, I, y, g)
693
+ (u, _, E, y, g) => t._mapFn(u, _, E, y, g)
684
694
  ) : null;
685
695
  }
686
696
  function Mt({
687
697
  proxy: t
688
698
  }) {
689
- const i = B(null), f = `${t._stateKey}-${t._path.join(".")}`;
690
- return tt(() => {
699
+ const i = Z(null), f = `${t._stateKey}-${t._path.join(".")}`;
700
+ return nt(() => {
691
701
  const S = i.current;
692
702
  if (!S || !S.parentElement) return;
693
- const u = S.parentElement, I = Array.from(u.childNodes).indexOf(S);
703
+ const u = S.parentElement, E = Array.from(u.childNodes).indexOf(S);
694
704
  let y = u.getAttribute("data-parent-id");
695
705
  y || (y = `parent-${crypto.randomUUID()}`, u.setAttribute("data-parent-id", y));
696
- const p = {
706
+ const w = {
697
707
  instanceId: `instance-${crypto.randomUUID()}`,
698
708
  parentId: y,
699
- position: I,
709
+ position: E,
700
710
  effect: t._effect
701
711
  };
702
- r.getState().addSignalElement(f, p);
703
- const l = r.getState().getNestedState(t._stateKey, t._path);
712
+ o.getState().addSignalElement(f, w);
713
+ const c = o.getState().getNestedState(t._stateKey, t._path);
704
714
  let e;
705
715
  if (t._effect)
706
716
  try {
707
717
  e = new Function(
708
718
  "state",
709
719
  `return (${t._effect})(state)`
710
- )(l);
720
+ )(c);
711
721
  } catch ($) {
712
- console.error("Error evaluating effect function during mount:", $), e = l;
722
+ console.error("Error evaluating effect function during mount:", $), e = c;
713
723
  }
714
724
  else
715
- e = l;
725
+ e = c;
716
726
  e !== null && typeof e == "object" && (e = JSON.stringify(e));
717
- const E = document.createTextNode(String(e));
718
- S.replaceWith(E);
719
- }, [t._stateKey, t._path.join("."), t._effect]), X("span", {
727
+ const I = document.createTextNode(String(e));
728
+ S.replaceWith(I);
729
+ }, [t._stateKey, t._path.join("."), t._effect]), Y("span", {
720
730
  ref: i,
721
731
  style: { display: "none" },
722
732
  "data-signal-id": f
723
733
  });
724
734
  }
725
- function Bt(t) {
735
+ function Jt(t) {
726
736
  const i = _t(
727
737
  (f) => {
728
- const S = r.getState().stateComponents.get(t._stateKey) || {
738
+ const S = o.getState().stateComponents.get(t._stateKey) || {
729
739
  components: /* @__PURE__ */ new Map()
730
740
  };
731
741
  return S.components.set(t._stateKey, {
@@ -733,16 +743,16 @@ function Bt(t) {
733
743
  paths: /* @__PURE__ */ new Set([t._path.join(".")])
734
744
  }), () => S.components.delete(t._stateKey);
735
745
  },
736
- () => r.getState().getNestedState(t._stateKey, t._path)
746
+ () => o.getState().getNestedState(t._stateKey, t._path)
737
747
  );
738
- return X("text", {}, String(i));
748
+ return Y("text", {}, String(i));
739
749
  }
740
750
  export {
741
- Y as $cogsSignal,
742
- Bt as $cogsSignalStore,
751
+ tt as $cogsSignal,
752
+ Jt as $cogsSignalStore,
743
753
  qt as addStateOptions,
744
754
  zt as createCogsState,
745
- Jt as notifyComponent,
755
+ Bt as notifyComponent,
746
756
  jt as useCogsStateFn
747
757
  };
748
758
  //# sourceMappingURL=CogsState.jsx.map