cogsbox-state 0.5.184 → 0.5.186

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,17 +1,17 @@
1
1
  "use client";
2
2
  import { jsx as Se } from "react/jsx-runtime";
3
- import { useState as le, useRef as X, useEffect as de, useLayoutEffect as $e, useMemo as Ne, createElement as ee, useSyncExternalStore as he, startTransition as ke } from "react";
4
- import { transformStateFunc as Ve, isDeepEqual as G, isFunction as q, getNestedValue as D, getDifferences as we, debounce as Ae } from "./utility.js";
5
- import { pushFunc as se, updateFn as Z, cutFunc as Y, ValidationWrapper as xe, FormControlComponent as Te } from "./Functions.jsx";
6
- import Ce from "superjson";
3
+ import { useState as le, useRef as Q, useEffect as de, useLayoutEffect as $e, useMemo as ke, createElement as te, useSyncExternalStore as Ne, startTransition as Te } from "react";
4
+ import { transformStateFunc as Ve, isDeepEqual as W, isFunction as q, getNestedValue as U, getDifferences as he, debounce as Ae } from "./utility.js";
5
+ import { pushFunc as se, updateFn as H, cutFunc as X, ValidationWrapper as _e, FormControlComponent as Ce } from "./Functions.jsx";
6
+ import be from "superjson";
7
7
  import { v4 as ue } from "uuid";
8
8
  import "zod";
9
- import { getGlobalStore as r, formRefStore as me } from "./store.js";
10
- import { useCogsConfig as Ee } from "./CogsStateClient.jsx";
9
+ import { getGlobalStore as n, formRefStore as me } from "./store.js";
10
+ import { useCogsConfig as we } from "./CogsStateClient.jsx";
11
11
  function ye(e, s) {
12
- const m = r.getState().getInitialOptions, u = r.getState().setInitialStateOptions, g = m(e) || {};
12
+ const m = n.getState().getInitialOptions, u = n.getState().setInitialStateOptions, f = m(e) || {};
13
13
  u(e, {
14
- ...g,
14
+ ...f,
15
15
  ...s
16
16
  });
17
17
  }
@@ -20,37 +20,37 @@ function ve({
20
20
  options: s,
21
21
  initialOptionsPart: m
22
22
  }) {
23
- const u = B(e) || {}, g = m[e] || {}, w = r.getState().setInitialStateOptions, I = { ...g, ...u };
23
+ const u = J(e) || {}, f = m[e] || {}, k = n.getState().setInitialStateOptions, h = { ...f, ...u };
24
24
  let y = !1;
25
25
  if (s)
26
26
  for (const i in s)
27
- I.hasOwnProperty(i) ? (i == "localStorage" && s[i] && I[i].key !== s[i]?.key && (y = !0, I[i] = s[i]), i == "initialState" && s[i] && I[i] !== s[i] && // Different references
28
- !G(I[i], s[i]) && (y = !0, I[i] = s[i])) : (y = !0, I[i] = s[i]);
29
- y && w(e, I);
27
+ h.hasOwnProperty(i) ? (i == "localStorage" && s[i] && h[i].key !== s[i]?.key && (y = !0, h[i] = s[i]), i == "initialState" && s[i] && h[i] !== s[i] && // Different references
28
+ !W(h[i], s[i]) && (y = !0, h[i] = s[i])) : (y = !0, h[i] = s[i]);
29
+ y && k(e, h);
30
30
  }
31
31
  function Ye(e, { formElements: s, validation: m }) {
32
32
  return { initialState: e, formElements: s, validation: m };
33
33
  }
34
34
  const Xe = (e, s) => {
35
35
  let m = e;
36
- const [u, g] = Ve(m);
37
- (Object.keys(g).length > 0 || s && Object.keys(s).length > 0) && Object.keys(g).forEach((y) => {
38
- g[y] = g[y] || {}, g[y].formElements = {
36
+ const [u, f] = Ve(m);
37
+ (Object.keys(f).length > 0 || s && Object.keys(s).length > 0) && Object.keys(f).forEach((y) => {
38
+ f[y] = f[y] || {}, f[y].formElements = {
39
39
  ...s?.formElements,
40
40
  // Global defaults first
41
41
  ...s?.validation,
42
- ...g[y].formElements || {}
42
+ ...f[y].formElements || {}
43
43
  // State-specific overrides
44
- }, B(y) || r.getState().setInitialStateOptions(y, g[y]);
45
- }), r.getState().setInitialStates(u), r.getState().setCreatedState(u);
46
- const w = (y, i) => {
44
+ }, J(y) || n.getState().setInitialStateOptions(y, f[y]);
45
+ }), n.getState().setInitialStates(u), n.getState().setCreatedState(u);
46
+ const k = (y, i) => {
47
47
  const [S] = le(i?.componentId ?? ue());
48
48
  ve({
49
49
  stateKey: y,
50
50
  options: i,
51
- initialOptionsPart: g
51
+ initialOptionsPart: f
52
52
  });
53
- const t = r.getState().cogsStateStore[y] || u[y], v = i?.modifyState ? i.modifyState(t) : t, [F, V] = De(
53
+ const t = n.getState().cogsStateStore[y] || u[y], v = i?.modifyState ? i.modifyState(t) : t, [p, V] = Me(
54
54
  v,
55
55
  {
56
56
  stateKey: y,
@@ -68,47 +68,47 @@ const Xe = (e, s) => {
68
68
  );
69
69
  return V;
70
70
  };
71
- function I(y, i) {
72
- ve({ stateKey: y, options: i, initialOptionsPart: g }), i.localStorage && Oe(y, i), ne(y);
71
+ function h(y, i) {
72
+ ve({ stateKey: y, options: i, initialOptionsPart: f }), i.localStorage && Fe(y, i), re(y);
73
73
  }
74
- return { useCogsState: w, setCogsOptions: I };
74
+ return { useCogsState: k, setCogsOptions: h };
75
75
  }, {
76
- setUpdaterState: Q,
76
+ setUpdaterState: K,
77
77
  setState: z,
78
- getInitialOptions: B,
79
- getKeyState: _e,
80
- getValidationErrors: be,
78
+ getInitialOptions: J,
79
+ getKeyState: Ee,
80
+ getValidationErrors: xe,
81
81
  setStateLog: Pe,
82
82
  updateInitialStateGlobal: ge,
83
- addValidationError: je,
84
- removeValidationError: W,
85
- setServerSyncActions: Fe
86
- } = r.getState(), Ie = (e, s, m, u, g) => {
83
+ addValidationError: Oe,
84
+ removeValidationError: L,
85
+ setServerSyncActions: pe
86
+ } = n.getState(), Ie = (e, s, m, u, f) => {
87
87
  m?.log && console.log(
88
88
  "saving to localstorage",
89
89
  s,
90
90
  m.localStorage?.key,
91
91
  u
92
92
  );
93
- const w = q(m?.localStorage?.key) ? m.localStorage?.key(e) : m?.localStorage?.key;
94
- if (w && u) {
95
- const I = `${u}-${s}-${w}`;
93
+ const k = q(m?.localStorage?.key) ? m.localStorage?.key(e) : m?.localStorage?.key;
94
+ if (k && u) {
95
+ const h = `${u}-${s}-${k}`;
96
96
  let y;
97
97
  try {
98
- y = te(I)?.lastSyncedWithServer;
98
+ y = ne(h)?.lastSyncedWithServer;
99
99
  } catch {
100
100
  }
101
101
  const i = {
102
102
  state: e,
103
103
  lastUpdated: Date.now(),
104
- lastSyncedWithServer: g ?? y
105
- }, S = Ce.serialize(i);
104
+ lastSyncedWithServer: f ?? y
105
+ }, S = be.serialize(i);
106
106
  window.localStorage.setItem(
107
- I,
107
+ h,
108
108
  JSON.stringify(S.json)
109
109
  );
110
110
  }
111
- }, te = (e) => {
111
+ }, ne = (e) => {
112
112
  if (!e) return null;
113
113
  try {
114
114
  const s = window.localStorage.getItem(e);
@@ -116,30 +116,30 @@ const Xe = (e, s) => {
116
116
  } catch (s) {
117
117
  return console.error("Error loading from localStorage:", s), null;
118
118
  }
119
- }, Oe = (e, s) => {
120
- const m = r.getState().cogsStateStore[e], { sessionId: u } = Ee(), g = q(s?.localStorage?.key) ? s.localStorage.key(m) : s?.localStorage?.key;
121
- if (g && u) {
122
- const w = te(
123
- `${u}-${e}-${g}`
119
+ }, Fe = (e, s) => {
120
+ const m = n.getState().cogsStateStore[e], { sessionId: u } = we(), f = q(s?.localStorage?.key) ? s.localStorage.key(m) : s?.localStorage?.key;
121
+ if (f && u) {
122
+ const k = ne(
123
+ `${u}-${e}-${f}`
124
124
  );
125
- if (w && w.lastUpdated > (w.lastSyncedWithServer || 0))
126
- return z(e, w.state), ne(e), !0;
125
+ if (k && k.lastUpdated > (k.lastSyncedWithServer || 0))
126
+ return z(e, k.state), re(e), !0;
127
127
  }
128
128
  return !1;
129
- }, Re = (e, s, m, u, g, w) => {
130
- const I = {
129
+ }, je = (e, s, m, u, f, k) => {
130
+ const h = {
131
131
  initialState: s,
132
- updaterState: K(
132
+ updaterState: ee(
133
133
  e,
134
134
  u,
135
- g,
136
- w
135
+ f,
136
+ k
137
137
  ),
138
138
  state: m
139
139
  };
140
- ge(e, I.initialState), Q(e, I.updaterState), z(e, I.state);
141
- }, ne = (e) => {
142
- const s = r.getState().stateComponents.get(e);
140
+ ge(e, h.initialState), K(e, h.updaterState), z(e, h.state);
141
+ }, re = (e) => {
142
+ const s = n.getState().stateComponents.get(e);
143
143
  if (!s) return;
144
144
  const m = /* @__PURE__ */ new Set();
145
145
  s.components.forEach((u) => {
@@ -148,29 +148,29 @@ const Xe = (e, s) => {
148
148
  m.forEach((u) => u());
149
149
  });
150
150
  }, Qe = (e, s) => {
151
- const m = r.getState().stateComponents.get(e);
151
+ const m = n.getState().stateComponents.get(e);
152
152
  if (m) {
153
- const u = `${e}////${s}`, g = m.components.get(u);
154
- if ((g ? Array.isArray(g.reactiveType) ? g.reactiveType : [g.reactiveType || "component"] : null)?.includes("none"))
153
+ const u = `${e}////${s}`, f = m.components.get(u);
154
+ if ((f ? Array.isArray(f.reactiveType) ? f.reactiveType : [f.reactiveType || "component"] : null)?.includes("none"))
155
155
  return;
156
- g && g.forceUpdate();
156
+ f && f.forceUpdate();
157
157
  }
158
- }, Ue = (e, s, m, u) => {
158
+ }, Re = (e, s, m, u) => {
159
159
  switch (e) {
160
160
  case "update":
161
161
  return {
162
- oldValue: D(s, u),
163
- newValue: D(m, u)
162
+ oldValue: U(s, u),
163
+ newValue: U(m, u)
164
164
  };
165
165
  case "insert":
166
166
  return {
167
167
  oldValue: null,
168
168
  // or undefined
169
- newValue: D(m, u)
169
+ newValue: U(m, u)
170
170
  };
171
171
  case "cut":
172
172
  return {
173
- oldValue: D(s, u),
173
+ oldValue: U(s, u),
174
174
  newValue: null
175
175
  // or undefined
176
176
  };
@@ -178,57 +178,57 @@ const Xe = (e, s) => {
178
178
  return { oldValue: null, newValue: null };
179
179
  }
180
180
  };
181
- function De(e, {
181
+ function Me(e, {
182
182
  stateKey: s,
183
183
  serverSync: m,
184
184
  localStorage: u,
185
- formElements: g,
186
- reactiveDeps: w,
187
- reactiveType: I,
185
+ formElements: f,
186
+ reactiveDeps: k,
187
+ reactiveType: h,
188
188
  componentId: y,
189
189
  initialState: i,
190
190
  syncUpdate: S,
191
191
  dependencies: t,
192
192
  serverState: v
193
193
  } = {}) {
194
- const [F, V] = le({}), { sessionId: A } = Ee();
195
- let O = !s;
196
- const [f] = le(s ?? ue()), l = r.getState().stateLog[f], J = X(/* @__PURE__ */ new Set()), p = X(y ?? ue()), x = X(
194
+ const [p, V] = le({}), { sessionId: A } = we();
195
+ let F = !s;
196
+ const [g] = le(s ?? ue()), c = n.getState().stateLog[g], Z = Q(/* @__PURE__ */ new Set()), G = Q(y ?? ue()), _ = Q(
197
197
  null
198
198
  );
199
- x.current = B(f) ?? null, de(() => {
200
- if (S && S.stateKey === f && S.path?.[0]) {
201
- z(f, (a) => ({
199
+ _.current = J(g) ?? null, de(() => {
200
+ if (S && S.stateKey === g && S.path?.[0]) {
201
+ z(g, (a) => ({
202
202
  ...a,
203
203
  [S.path[0]]: S.newValue
204
204
  }));
205
- const c = `${S.stateKey}:${S.path.join(".")}`;
206
- r.getState().setSyncInfo(c, {
205
+ const o = `${S.stateKey}:${S.path.join(".")}`;
206
+ n.getState().setSyncInfo(o, {
207
207
  timeStamp: S.timeStamp,
208
208
  userId: S.userId
209
209
  });
210
210
  }
211
211
  }, [S]), de(() => {
212
212
  if (i) {
213
- ye(f, {
213
+ ye(g, {
214
214
  initialState: i
215
215
  });
216
- const c = x.current, d = c?.serverState?.id !== void 0 && c?.serverState?.status === "success" && c?.serverState?.data, E = r.getState().initialStateGlobal[f];
217
- if (!(E && !G(E, i) || !E) && !d)
216
+ const o = _.current, d = o?.serverState?.id !== void 0 && o?.serverState?.status === "success" && o?.serverState?.data, w = n.getState().initialStateGlobal[g];
217
+ if (!(w && !W(w, i) || !w) && !d)
218
218
  return;
219
- let _ = null;
220
- const b = q(c?.localStorage?.key) ? c?.localStorage?.key(i) : c?.localStorage?.key;
221
- b && A && (_ = te(`${A}-${f}-${b}`));
222
- let h = i, L = !1;
223
- const re = d ? Date.now() : 0, H = _?.lastUpdated || 0, ae = _?.lastSyncedWithServer || 0;
224
- d && re > H ? (h = c.serverState.data, L = !0) : _ && H > ae && (h = _.state, c?.localStorage?.onChange && c?.localStorage?.onChange(h)), Re(
225
- f,
219
+ let E = null;
220
+ const C = q(o?.localStorage?.key) ? o?.localStorage?.key(i) : o?.localStorage?.key;
221
+ C && A && (E = ne(`${A}-${g}-${C}`));
222
+ let T = i, R = !1;
223
+ const B = d ? Date.now() : 0, Y = E?.lastUpdated || 0, ae = E?.lastSyncedWithServer || 0;
224
+ d && B > Y ? (T = o.serverState.data, R = !0) : E && Y > ae && (T = E.state, o?.localStorage?.onChange && o?.localStorage?.onChange(T)), je(
225
+ g,
226
226
  i,
227
- h,
228
- n,
229
- p.current,
227
+ T,
228
+ l,
229
+ G.current,
230
230
  A
231
- ), L && b && A && Ie(h, f, c, A, Date.now()), ne(f), (Array.isArray(I) ? I : [I || "component"]).includes("none") || V({});
231
+ ), R && C && A && Ie(T, g, o, A, Date.now()), re(g), (Array.isArray(h) ? h : [h || "component"]).includes("none") || V({});
232
232
  }
233
233
  }, [
234
234
  i,
@@ -236,670 +236,669 @@ function De(e, {
236
236
  v?.data,
237
237
  ...t || []
238
238
  ]), $e(() => {
239
- O && ye(f, {
239
+ F && ye(g, {
240
240
  serverSync: m,
241
- formElements: g,
241
+ formElements: f,
242
242
  initialState: i,
243
243
  localStorage: u,
244
- middleware: x.current?.middleware
244
+ middleware: _.current?.middleware
245
245
  });
246
- const c = `${f}////${p.current}`, a = r.getState().stateComponents.get(f) || {
246
+ const o = `${g}////${G.current}`, a = n.getState().stateComponents.get(g) || {
247
247
  components: /* @__PURE__ */ new Map()
248
248
  };
249
- return a.components.set(c, {
249
+ return a.components.set(o, {
250
250
  forceUpdate: () => V({}),
251
251
  paths: /* @__PURE__ */ new Set(),
252
252
  deps: [],
253
- depsFunction: w || void 0,
254
- reactiveType: I ?? ["component", "deps"]
255
- }), r.getState().stateComponents.set(f, a), V({}), () => {
256
- const d = `${f}////${p.current}`;
257
- a && (a.components.delete(d), a.components.size === 0 && r.getState().stateComponents.delete(f));
253
+ depsFunction: k || void 0,
254
+ reactiveType: h ?? ["component", "deps"]
255
+ }), n.getState().stateComponents.set(g, a), V({}), () => {
256
+ const d = `${g}////${G.current}`;
257
+ a && (a.components.delete(d), a.components.size === 0 && n.getState().stateComponents.delete(g));
258
258
  };
259
259
  }, []);
260
- const n = (c, a, d, E) => {
260
+ const l = (o, a, d, w) => {
261
261
  if (Array.isArray(a)) {
262
- const k = `${f}-${a.join(".")}`;
263
- J.current.add(k);
262
+ const I = `${g}-${a.join(".")}`;
263
+ Z.current.add(I);
264
264
  }
265
- z(f, (k) => {
266
- const _ = q(c) ? c(k) : c, b = `${f}-${a.join(".")}`;
267
- if (b) {
268
- let U = !1, N = r.getState().signalDomElements.get(b);
265
+ z(g, (I) => {
266
+ const E = q(o) ? o(I) : o, C = `${g}-${a.join(".")}`;
267
+ if (C) {
268
+ let M = !1, N = n.getState().signalDomElements.get(C);
269
269
  if ((!N || N.size === 0) && (d.updateType === "insert" || d.updateType === "cut")) {
270
- const P = a.slice(0, -1), R = D(_, P);
271
- if (Array.isArray(R)) {
272
- U = !0;
273
- const $ = `${f}-${P.join(".")}`;
274
- N = r.getState().signalDomElements.get($);
270
+ const P = a.slice(0, -1), j = U(E, P);
271
+ if (Array.isArray(j)) {
272
+ M = !0;
273
+ const $ = `${g}-${P.join(".")}`;
274
+ N = n.getState().signalDomElements.get($);
275
275
  }
276
276
  }
277
277
  if (N) {
278
- const P = U ? D(_, a.slice(0, -1)) : D(_, a);
279
- N.forEach(({ parentId: R, position: $, effect: C }) => {
280
- const T = document.querySelector(
281
- `[data-parent-id="${R}"]`
278
+ const P = M ? U(E, a.slice(0, -1)) : U(E, a);
279
+ N.forEach(({ parentId: j, position: $, effect: x }) => {
280
+ const b = document.querySelector(
281
+ `[data-parent-id="${j}"]`
282
282
  );
283
- if (T) {
284
- const M = Array.from(T.childNodes);
285
- if (M[$]) {
286
- const j = C ? new Function("state", `return (${C})(state)`)(P) : P;
287
- M[$].textContent = String(j);
283
+ if (b) {
284
+ const D = Array.from(b.childNodes);
285
+ if (D[$]) {
286
+ const O = x ? new Function("state", `return (${x})(state)`)(P) : P;
287
+ D[$].textContent = String(O);
288
288
  }
289
289
  }
290
290
  });
291
291
  }
292
292
  }
293
- d.updateType === "update" && (E || x.current?.validation?.key) && a && W(
294
- (E || x.current?.validation?.key) + "." + a.join(".")
293
+ d.updateType === "update" && (w || _.current?.validation?.key) && a && L(
294
+ (w || _.current?.validation?.key) + "." + a.join(".")
295
295
  );
296
- const h = a.slice(0, a.length - 1);
297
- d.updateType === "cut" && x.current?.validation?.key && W(
298
- x.current?.validation?.key + "." + h.join(".")
299
- ), d.updateType === "insert" && x.current?.validation?.key && be(
300
- x.current?.validation?.key + "." + h.join(".")
296
+ const T = a.slice(0, a.length - 1);
297
+ d.updateType === "cut" && _.current?.validation?.key && L(
298
+ _.current?.validation?.key + "." + T.join(".")
299
+ ), d.updateType === "insert" && _.current?.validation?.key && xe(
300
+ _.current?.validation?.key + "." + T.join(".")
301
301
  ).filter(([N, P]) => {
302
- let R = N?.split(".").length;
303
- if (N == h.join(".") && R == h.length - 1) {
304
- let $ = N + "." + h;
305
- W(N), je($, P);
302
+ let j = N?.split(".").length;
303
+ if (N == T.join(".") && j == T.length - 1) {
304
+ let $ = N + "." + T;
305
+ L(N), Oe($, P);
306
306
  }
307
307
  });
308
- const L = r.getState().stateComponents.get(f);
309
- if (L) {
310
- const U = we(k, _), N = new Set(U), P = d.updateType === "update" ? a.join(".") : a.slice(0, -1).join(".") || "";
308
+ const R = n.getState().stateComponents.get(g);
309
+ if (R) {
310
+ const M = he(I, E), N = new Set(M), P = d.updateType === "update" ? a.join(".") : a.slice(0, -1).join(".") || "";
311
311
  for (const [
312
- R,
312
+ j,
313
313
  $
314
- ] of L.components.entries()) {
315
- let C = !1;
316
- const T = Array.isArray($.reactiveType) ? $.reactiveType : [$.reactiveType || "component"];
317
- if (!T.includes("none")) {
318
- if (T.includes("all")) {
314
+ ] of R.components.entries()) {
315
+ let x = !1;
316
+ const b = Array.isArray($.reactiveType) ? $.reactiveType : [$.reactiveType || "component"];
317
+ if (!b.includes("none")) {
318
+ if (b.includes("all")) {
319
319
  $.forceUpdate();
320
320
  continue;
321
321
  }
322
- if (T.includes("component") && (($.paths.has(P) || $.paths.has("")) && (C = !0), !C))
323
- for (const M of N) {
324
- let j = M;
322
+ if (b.includes("component") && (($.paths.has(P) || $.paths.has("")) && (x = !0), !x))
323
+ for (const D of N) {
324
+ let O = D;
325
325
  for (; ; ) {
326
- if ($.paths.has(j)) {
327
- C = !0;
326
+ if ($.paths.has(O)) {
327
+ x = !0;
328
328
  break;
329
329
  }
330
- const ie = j.lastIndexOf(".");
330
+ const ie = O.lastIndexOf(".");
331
331
  if (ie !== -1) {
332
- const fe = j.substring(
332
+ const fe = O.substring(
333
333
  0,
334
334
  ie
335
335
  );
336
336
  if (!isNaN(
337
- Number(j.substring(ie + 1))
337
+ Number(O.substring(ie + 1))
338
338
  ) && $.paths.has(fe)) {
339
- C = !0;
339
+ x = !0;
340
340
  break;
341
341
  }
342
- j = fe;
342
+ O = fe;
343
343
  } else
344
- j = "";
345
- if (j === "")
344
+ O = "";
345
+ if (O === "")
346
346
  break;
347
347
  }
348
- if (C) break;
348
+ if (x) break;
349
349
  }
350
- if (!C && T.includes("deps") && $.depsFunction) {
351
- const M = $.depsFunction(_);
352
- let j = !1;
353
- typeof M == "boolean" ? M && (j = !0) : G($.deps, M) || ($.deps = M, j = !0), j && (C = !0);
350
+ if (!x && b.includes("deps") && $.depsFunction) {
351
+ const D = $.depsFunction(E);
352
+ let O = !1;
353
+ typeof D == "boolean" ? D && (O = !0) : W($.deps, D) || ($.deps = D, O = !0), O && (x = !0);
354
354
  }
355
- C && $.forceUpdate();
355
+ x && $.forceUpdate();
356
356
  }
357
357
  }
358
358
  }
359
- const re = Date.now();
360
- a = a.map((U, N) => {
361
- const P = a.slice(0, -1), R = D(_, P);
362
- return N === a.length - 1 && ["insert", "cut"].includes(d.updateType) ? (R.length - 1).toString() : U;
359
+ const B = Date.now();
360
+ a = a.map((M, N) => {
361
+ const P = a.slice(0, -1), j = U(E, P);
362
+ return N === a.length - 1 && ["insert", "cut"].includes(d.updateType) ? (j.length - 1).toString() : M;
363
363
  }), console.log(
364
364
  "mmmmmmmmmmmmmmmmm22222222222222",
365
365
  d.updateType,
366
- k,
367
- _,
366
+ I,
367
+ E,
368
368
  a
369
369
  );
370
- const { oldValue: H, newValue: ae } = Ue(
370
+ const { oldValue: Y, newValue: ae } = Re(
371
371
  d.updateType,
372
- k,
373
- _,
372
+ I,
373
+ E,
374
374
  a
375
375
  ), oe = {
376
- timeStamp: re,
377
- stateKey: f,
376
+ timeStamp: B,
377
+ stateKey: g,
378
378
  path: a,
379
379
  updateType: d.updateType,
380
380
  status: "new",
381
- oldValue: H,
381
+ oldValue: Y,
382
382
  newValue: ae
383
383
  };
384
- if (Pe(f, (U) => {
385
- const P = [...U ?? [], oe].reduce((R, $) => {
386
- const C = `${$.stateKey}:${JSON.stringify($.path)}`, T = R.get(C);
387
- return T ? (T.timeStamp = Math.max(T.timeStamp, $.timeStamp), T.newValue = $.newValue, T.oldValue = T.oldValue ?? $.oldValue, T.updateType = $.updateType) : R.set(C, { ...$ }), R;
384
+ if (Pe(g, (M) => {
385
+ const P = [...M ?? [], oe].reduce((j, $) => {
386
+ const x = `${$.stateKey}:${JSON.stringify($.path)}`, b = j.get(x);
387
+ return b ? (b.timeStamp = Math.max(b.timeStamp, $.timeStamp), b.newValue = $.newValue, b.oldValue = b.oldValue ?? $.oldValue, b.updateType = $.updateType) : j.set(x, { ...$ }), j;
388
388
  }, /* @__PURE__ */ new Map());
389
389
  return Array.from(P.values());
390
390
  }), Ie(
391
- _,
392
- f,
393
- x.current,
391
+ E,
392
+ g,
393
+ _.current,
394
394
  A
395
- ), x.current?.middleware && x.current.middleware({
396
- updateLog: l,
395
+ ), _.current?.middleware && _.current.middleware({
396
+ updateLog: c,
397
397
  update: oe
398
- }), x.current?.serverSync) {
399
- const U = r.getState().serverState[f], N = x.current?.serverSync;
400
- Fe(f, {
401
- syncKey: typeof N.syncKey == "string" ? N.syncKey : N.syncKey({ state: _ }),
402
- rollBackState: U,
398
+ }), _.current?.serverSync) {
399
+ const M = n.getState().serverState[g], N = _.current?.serverSync;
400
+ pe(g, {
401
+ syncKey: typeof N.syncKey == "string" ? N.syncKey : N.syncKey({ state: E }),
402
+ rollBackState: M,
403
403
  actionTimeStamp: Date.now() + (N.debounce ?? 3e3),
404
404
  status: "waiting"
405
405
  });
406
406
  }
407
- return _;
407
+ return E;
408
408
  });
409
409
  };
410
- r.getState().updaterState[f] || (Q(
411
- f,
412
- K(
413
- f,
414
- n,
415
- p.current,
410
+ n.getState().updaterState[g] || (K(
411
+ g,
412
+ ee(
413
+ g,
414
+ l,
415
+ G.current,
416
416
  A
417
417
  )
418
- ), r.getState().cogsStateStore[f] || z(f, e), r.getState().initialStateGlobal[f] || ge(f, e));
419
- const o = Ne(() => K(
420
- f,
421
- n,
422
- p.current,
418
+ ), n.getState().cogsStateStore[g] || z(g, e), n.getState().initialStateGlobal[g] || ge(g, e));
419
+ const r = ke(() => ee(
420
+ g,
421
+ l,
422
+ G.current,
423
423
  A
424
- ), [f]);
425
- return [_e(f), o];
424
+ ), [g]);
425
+ return [Ee(g), r];
426
426
  }
427
- function K(e, s, m, u) {
428
- const g = /* @__PURE__ */ new Map();
429
- let w = 0;
430
- const I = (S) => {
427
+ function ee(e, s, m, u) {
428
+ const f = /* @__PURE__ */ new Map();
429
+ let k = 0;
430
+ const h = (S) => {
431
431
  const t = S.join(".");
432
- for (const [v] of g)
433
- (v === t || v.startsWith(t + ".")) && g.delete(v);
434
- w++;
432
+ for (const [v] of f)
433
+ (v === t || v.startsWith(t + ".")) && f.delete(v);
434
+ k++;
435
435
  }, y = {
436
436
  removeValidation: (S) => {
437
- S?.validationKey && W(S.validationKey);
437
+ S?.validationKey && L(S.validationKey);
438
438
  },
439
439
  revertToInitialState: (S) => {
440
- const t = r.getState().getInitialOptions(e)?.validation;
441
- t?.key && W(t?.key), S?.validationKey && W(S.validationKey);
442
- const v = r.getState().initialStateGlobal[e];
443
- r.getState().clearSelectedIndexesForState(e), g.clear(), w++;
444
- const F = i(v, []), V = B(e), A = q(V?.localStorage?.key) ? V?.localStorage?.key(v) : V?.localStorage?.key, O = `${u}-${e}-${A}`;
445
- O && localStorage.removeItem(O), Q(e, F), z(e, v);
446
- const f = r.getState().stateComponents.get(e);
447
- return f && f.components.forEach((l) => {
448
- l.forceUpdate();
440
+ const t = n.getState().getInitialOptions(e)?.validation;
441
+ t?.key && L(t?.key), S?.validationKey && L(S.validationKey);
442
+ const v = n.getState().initialStateGlobal[e];
443
+ n.getState().clearSelectedIndexesForState(e), f.clear(), k++;
444
+ const p = i(v, []), V = J(e), A = q(V?.localStorage?.key) ? V?.localStorage?.key(v) : V?.localStorage?.key, F = `${u}-${e}-${A}`;
445
+ F && localStorage.removeItem(F), K(e, p), z(e, v);
446
+ const g = n.getState().stateComponents.get(e);
447
+ return g && g.components.forEach((c) => {
448
+ c.forceUpdate();
449
449
  }), v;
450
450
  },
451
451
  updateInitialState: (S) => {
452
- g.clear(), w++;
453
- const t = K(
452
+ f.clear(), k++;
453
+ const t = ee(
454
454
  e,
455
455
  s,
456
456
  m,
457
457
  u
458
- ), v = r.getState().initialStateGlobal[e], F = B(e), V = q(F?.localStorage?.key) ? F?.localStorage?.key(v) : F?.localStorage?.key, A = `${u}-${e}-${V}`;
459
- return console.log("removing storage", A), localStorage.getItem(A) && localStorage.removeItem(A), ke(() => {
460
- ge(e, S), Q(e, t), z(e, S);
461
- const O = r.getState().stateComponents.get(e);
462
- O && O.components.forEach((f) => {
463
- f.forceUpdate();
458
+ ), v = n.getState().initialStateGlobal[e], p = J(e), V = q(p?.localStorage?.key) ? p?.localStorage?.key(v) : p?.localStorage?.key, A = `${u}-${e}-${V}`;
459
+ return console.log("removing storage", A), localStorage.getItem(A) && localStorage.removeItem(A), Te(() => {
460
+ ge(e, S), K(e, t), z(e, S);
461
+ const F = n.getState().stateComponents.get(e);
462
+ F && F.components.forEach((g) => {
463
+ g.forceUpdate();
464
464
  });
465
465
  }), {
466
- fetchId: (O) => t.get()[O]
466
+ fetchId: (F) => t.get()[F]
467
467
  };
468
468
  },
469
- _initialState: r.getState().initialStateGlobal[e],
470
- _serverState: r.getState().serverState[e],
471
- _isLoading: r.getState().isLoadingGlobal[e],
469
+ _initialState: n.getState().initialStateGlobal[e],
470
+ _serverState: n.getState().serverState[e],
471
+ _isLoading: n.getState().isLoadingGlobal[e],
472
472
  _isServerSynced: () => {
473
- const S = r.getState().serverState[e];
474
- return !!(S && G(S, _e(e)));
473
+ const S = n.getState().serverState[e];
474
+ return !!(S && W(S, Ee(e)));
475
475
  }
476
476
  };
477
477
  function i(S, t = [], v) {
478
- const F = t.map(String).join(".");
479
- g.get(F);
478
+ const p = t.map(String).join(".");
479
+ f.get(p);
480
480
  const V = function() {
481
- return r().getNestedState(e, t);
481
+ return n().getNestedState(e, t);
482
482
  };
483
- Object.keys(y).forEach((f) => {
484
- V[f] = y[f];
483
+ Object.keys(y).forEach((g) => {
484
+ V[g] = y[g];
485
485
  });
486
486
  const A = {
487
- apply(f, l, J) {
487
+ apply(g, c, Z) {
488
488
  return console.log(
489
489
  `PROXY APPLY TRAP HIT: stateKey=${e}, path=${t.join(".")}`
490
- ), console.trace("Apply trap stack trace"), r().getNestedState(e, t);
490
+ ), console.trace("Apply trap stack trace"), n().getNestedState(e, t);
491
491
  },
492
- get(f, l) {
493
- if (l !== "then" && !l.startsWith("$") && l !== "stateMapNoRender") {
494
- const n = t.join("."), o = `${e}////${m}`, c = r.getState().stateComponents.get(e);
495
- if (c) {
496
- const a = c.components.get(o);
497
- a && (t.length > 0 || l === "get") && a.paths.add(n);
492
+ get(g, c) {
493
+ if (c !== "then" && !c.startsWith("$") && c !== "stateMapNoRender") {
494
+ const l = t.join("."), r = `${e}////${m}`, o = n.getState().stateComponents.get(e);
495
+ if (o) {
496
+ const a = o.components.get(r);
497
+ a && (t.length > 0 || c === "get") && a.paths.add(l);
498
498
  }
499
499
  }
500
- if (l === "getDifferences")
501
- return () => we(
502
- r.getState().cogsStateStore[e],
503
- r.getState().initialStateGlobal[e]
500
+ if (c === "getDifferences")
501
+ return () => he(
502
+ n.getState().cogsStateStore[e],
503
+ n.getState().initialStateGlobal[e]
504
504
  );
505
- if (l === "sync" && t.length === 0)
505
+ if (c === "sync" && t.length === 0)
506
506
  return async function() {
507
- const n = r.getState().getInitialOptions(e), o = n?.sync;
508
- if (!o)
507
+ const l = n.getState().getInitialOptions(e), r = l?.sync;
508
+ if (!r)
509
509
  return console.error(`No mutation defined for state key "${e}"`), { success: !1, error: "No mutation defined" };
510
- const c = r.getState().getNestedState(e, []), a = n?.validation?.key;
510
+ const o = n.getState().getNestedState(e, []), a = l?.validation?.key;
511
511
  try {
512
- const d = await o.action(c);
512
+ const d = await r.action(o);
513
513
  if (d && !d.success && d.errors && a) {
514
- r.getState().removeValidationError(a), d.errors.forEach((k) => {
515
- const _ = [a, ...k.path].join(".");
516
- r.getState().addValidationError(_, k.message);
514
+ n.getState().removeValidationError(a), d.errors.forEach((I) => {
515
+ const E = [a, ...I.path].join(".");
516
+ n.getState().addValidationError(E, I.message);
517
517
  });
518
- const E = r.getState().stateComponents.get(e);
519
- E && E.components.forEach((k) => {
520
- k.forceUpdate();
518
+ const w = n.getState().stateComponents.get(e);
519
+ w && w.components.forEach((I) => {
520
+ I.forceUpdate();
521
521
  });
522
522
  }
523
- return d?.success && o.onSuccess ? o.onSuccess(d.data) : !d?.success && o.onError && o.onError(d.error), d;
523
+ return d?.success && r.onSuccess ? r.onSuccess(d.data) : !d?.success && r.onError && r.onError(d.error), d;
524
524
  } catch (d) {
525
- return o.onError && o.onError(d), { success: !1, error: d };
525
+ return r.onError && r.onError(d), { success: !1, error: d };
526
526
  }
527
527
  };
528
- if (l === "_status") {
529
- const n = r.getState().getNestedState(e, t), o = r.getState().initialStateGlobal[e], c = D(o, t);
530
- return G(n, c) ? "fresh" : "stale";
528
+ if (c === "_status") {
529
+ const l = n.getState().getNestedState(e, t), r = n.getState().initialStateGlobal[e], o = U(r, t);
530
+ return W(l, o) ? "fresh" : "stale";
531
531
  }
532
- if (l === "getStatus")
532
+ if (c === "getStatus")
533
533
  return function() {
534
- const n = r().getNestedState(
534
+ const l = n().getNestedState(
535
535
  e,
536
536
  t
537
- ), o = r.getState().initialStateGlobal[e], c = D(o, t);
538
- return G(n, c) ? "fresh" : "stale";
537
+ ), r = n.getState().initialStateGlobal[e], o = U(r, t);
538
+ return W(l, o) ? "fresh" : "stale";
539
539
  };
540
- if (l === "removeStorage")
540
+ if (c === "removeStorage")
541
541
  return () => {
542
- const n = r.getState().initialStateGlobal[e], o = B(e), c = q(o?.localStorage?.key) ? o?.localStorage?.key(n) : o?.localStorage?.key, a = `${u}-${e}-${c}`;
542
+ const l = n.getState().initialStateGlobal[e], r = J(e), o = q(r?.localStorage?.key) ? r?.localStorage?.key(l) : r?.localStorage?.key, a = `${u}-${e}-${o}`;
543
543
  console.log("removing storage", a), a && localStorage.removeItem(a);
544
544
  };
545
- if (l === "showValidationErrors")
545
+ if (c === "showValidationErrors")
546
546
  return () => {
547
- const n = r.getState().getInitialOptions(e)?.validation;
548
- if (!n?.key)
547
+ const l = n.getState().getInitialOptions(e)?.validation;
548
+ if (!l?.key)
549
549
  throw new Error("Validation key not found");
550
- return r.getState().getValidationErrors(n.key + "." + t.join("."));
550
+ return n.getState().getValidationErrors(l.key + "." + t.join("."));
551
551
  };
552
552
  if (Array.isArray(S)) {
553
- if (l === "getSelected")
553
+ const l = () => v?.validIndices ? S.map((o, a) => ({
554
+ item: o,
555
+ originalIndex: v.validIndices[a]
556
+ })) : n.getState().getNestedState(e, t).map((o, a) => ({
557
+ item: o,
558
+ originalIndex: a
559
+ }));
560
+ if (c === "getSelected")
554
561
  return () => {
555
- const n = r.getState().getSelectedIndex(e, t.join("."));
556
- if (n !== void 0)
562
+ const r = n.getState().getSelectedIndex(e, t.join("."));
563
+ if (r !== void 0)
557
564
  return i(
558
- S[n],
559
- [...t, n.toString()],
565
+ S[r],
566
+ [...t, r.toString()],
560
567
  v
561
568
  );
562
569
  };
563
- if (l === "clearSelected")
570
+ if (c === "clearSelected")
564
571
  return () => {
565
- r.getState().clearSelectedIndex({ stateKey: e, path: t });
572
+ n.getState().clearSelectedIndex({ stateKey: e, path: t });
566
573
  };
567
- if (l === "getSelectedIndex")
568
- return () => r.getState().getSelectedIndex(e, t.join(".")) ?? -1;
569
- if (l === "stateSort")
570
- return (n) => {
571
- const a = [...r.getState().getNestedState(e, t).map((d, E) => ({
572
- ...d,
573
- __origIndex: E.toString()
574
- }))].sort(n);
575
- return g.clear(), w++, i(a, t, {
576
- filtered: [...v?.filtered || [], t],
574
+ if (c === "getSelectedIndex")
575
+ return () => n.getState().getSelectedIndex(e, t.join(".")) ?? -1;
576
+ if (c === "stateSort")
577
+ return (r) => {
578
+ const a = [...l()].sort(
579
+ (I, E) => r(I.item, E.item)
580
+ ), d = a.map(({ item: I }) => I), w = {
581
+ ...v,
577
582
  validIndices: a.map(
578
- (d) => parseInt(d.__origIndex)
583
+ ({ originalIndex: I }) => I
579
584
  )
580
- });
585
+ };
586
+ return i(d, t, w);
581
587
  };
582
- if (l === "stateMap" || l === "stateMapNoRender")
583
- return (n) => {
584
- const o = v?.filtered?.some(
585
- (a) => a.join(".") === t.join(".")
586
- ), c = o ? S : r.getState().getNestedState(e, t);
587
- return l !== "stateMapNoRender" && (g.clear(), w++), c.map((a, d) => {
588
- const E = o && a.__origIndex ? a.__origIndex : d, k = i(
589
- a,
590
- [...t, E.toString()],
591
- v
592
- );
593
- return n(
594
- a,
595
- k,
596
- d,
597
- S,
598
- i(S, t, v)
599
- );
600
- });
588
+ if (c === "stateFilter")
589
+ return (r) => {
590
+ const a = l().filter(
591
+ ({ item: I }, E) => r(I, E)
592
+ ), d = a.map(({ item: I }) => I), w = {
593
+ ...v,
594
+ validIndices: a.map(
595
+ ({ originalIndex: I }) => I
596
+ )
597
+ };
598
+ return i(d, t, w);
601
599
  };
602
- if (l === "$stateMap")
603
- return (n) => ee(Me, {
600
+ if (c === "stateMap" || c === "stateMapNoRender")
601
+ return (r) => S.map((a, d) => {
602
+ let w;
603
+ v?.validIndices && v.validIndices[d] !== void 0 ? w = v.validIndices[d] : w = d;
604
+ const I = [...t, w.toString()], E = i(a, I, v);
605
+ return r(
606
+ a,
607
+ E,
608
+ d,
609
+ S,
610
+ i(S, t, v)
611
+ );
612
+ });
613
+ if (c === "$stateMap")
614
+ return (r) => te(Ue, {
604
615
  proxy: {
605
616
  _stateKey: e,
606
617
  _path: t,
607
- _mapFn: n
618
+ _mapFn: r
608
619
  // Pass the actual function, not string
609
620
  },
610
621
  rebuildStateShape: i
611
622
  });
612
- if (l === "stateFlattenOn")
613
- return (n) => {
614
- const c = v?.filtered?.some(
615
- (d) => d.join(".") === t.join(".")
616
- ) ? S : r.getState().getNestedState(e, t);
617
- g.clear(), w++;
618
- const a = c.flatMap(
619
- (d, E) => d[n] ?? []
623
+ if (c === "stateFlattenOn")
624
+ return (r) => {
625
+ const o = S;
626
+ f.clear(), k++;
627
+ const a = o.flatMap(
628
+ (d) => d[r] ?? []
620
629
  );
621
630
  return i(
622
631
  a,
623
- [...t, "[*]", n],
632
+ [...t, "[*]", r],
624
633
  v
625
634
  );
626
635
  };
627
- if (l === "findWith")
628
- return (n, o) => {
629
- const c = S.findIndex((E) => E[n] === o);
630
- if (c === -1) return;
631
- const a = S[c], d = [...t, c.toString()];
632
- return g.clear(), w++, i(a, d);
633
- };
634
- if (l === "index")
635
- return (n) => {
636
- const o = S[n];
637
- return i(o, [...t, n.toString()]);
636
+ if (c === "index")
637
+ return (r) => {
638
+ const o = S[r];
639
+ return i(o, [...t, r.toString()]);
638
640
  };
639
- if (l === "last")
641
+ if (c === "last")
640
642
  return () => {
641
- const n = r.getState().getNestedState(e, t);
642
- if (n.length === 0) return;
643
- const o = n.length - 1, c = n[o], a = [...t, o.toString()];
644
- return i(c, a);
643
+ const r = n.getState().getNestedState(e, t);
644
+ if (r.length === 0) return;
645
+ const o = r.length - 1, a = r[o], d = [...t, o.toString()];
646
+ return i(a, d);
645
647
  };
646
- if (l === "insert")
647
- return (n) => (I(t), se(s, n, t, e), i(
648
- r.getState().getNestedState(e, t),
648
+ if (c === "insert")
649
+ return (r) => (h(t), se(s, r, t, e), i(
650
+ n.getState().getNestedState(e, t),
649
651
  t
650
652
  ));
651
- if (l === "uniqueInsert")
652
- return (n, o, c) => {
653
- const a = r.getState().getNestedState(e, t), d = q(n) ? n(a) : n;
654
- let E = null;
655
- if (!a.some((_) => {
653
+ if (c === "uniqueInsert")
654
+ return (r, o, a) => {
655
+ const d = n.getState().getNestedState(e, t), w = q(r) ? r(d) : r;
656
+ let I = null;
657
+ if (!d.some((C) => {
656
658
  if (o) {
657
- const h = o.every(
658
- (L) => G(_[L], d[L])
659
+ const R = o.every(
660
+ (B) => W(C[B], w[B])
659
661
  );
660
- return h && (E = _), h;
662
+ return R && (I = C), R;
661
663
  }
662
- const b = G(_, d);
663
- return b && (E = _), b;
664
+ const T = W(C, w);
665
+ return T && (I = C), T;
664
666
  }))
665
- I(t), se(s, d, t, e);
666
- else if (c && E) {
667
- const _ = c(E), b = a.map(
668
- (h) => G(h, E) ? _ : h
667
+ h(t), se(s, w, t, e);
668
+ else if (a && I) {
669
+ const C = a(I), T = d.map(
670
+ (R) => W(R, I) ? C : R
669
671
  );
670
- I(t), Z(s, b, t);
672
+ h(t), H(s, T, t);
671
673
  }
672
674
  };
673
- if (l === "cut")
674
- return (n, o) => {
675
+ if (c === "cut")
676
+ return (r, o) => {
675
677
  if (!o?.waitForSync)
676
- return I(t), Y(s, t, e, n), i(
677
- r.getState().getNestedState(e, t),
678
+ return h(t), X(s, t, e, r), i(
679
+ n.getState().getNestedState(e, t),
678
680
  t
679
681
  );
680
682
  };
681
- if (l === "cutByValue")
682
- return (n) => {
683
+ if (c === "cutByValue")
684
+ return (r) => {
683
685
  for (let o = 0; o < S.length; o++)
684
- S[o] === n && Y(s, t, e, o);
686
+ S[o] === r && X(s, t, e, o);
685
687
  };
686
- if (l === "toggleByValue")
687
- return (n) => {
688
- const o = S.findIndex((c) => c === n);
689
- o > -1 ? Y(s, t, e, o) : se(s, n, t, e);
688
+ if (c === "toggleByValue")
689
+ return (r) => {
690
+ const o = S.findIndex((a) => a === r);
691
+ o > -1 ? X(s, t, e, o) : se(s, r, t, e);
690
692
  };
691
- if (l === "stateFind")
692
- return (n) => {
693
- const o = r.getState().getNestedState(e, t), c = o.findIndex(n);
694
- if (c === -1)
695
- return;
696
- const a = o[c], d = [...t, c.toString()];
697
- return i(a, d);
693
+ if (c === "stateFind")
694
+ return (r) => {
695
+ const a = l().find(
696
+ ({ item: w }, I) => r(w, I)
697
+ );
698
+ if (!a) return;
699
+ const d = [...t, a.originalIndex.toString()];
700
+ return i(a.item, d, v);
698
701
  };
699
- if (l === "stateFilter")
700
- return (n) => {
701
- const c = r.getState().getNestedState(e, t).filter((a, d) => {
702
- const E = { ...a, __origIndex: d.toString() };
703
- return n(E, d);
704
- });
705
- return g.clear(), w++, i(c, t, {
706
- filtered: [...v?.filtered || [], t],
707
- validIndices: c.map(
708
- (a) => parseInt(a.__origIndex)
709
- )
710
- });
702
+ if (c === "findWith")
703
+ return (r, o) => {
704
+ const d = l().find(
705
+ ({ item: I }) => I[r] === o
706
+ );
707
+ if (!d) return;
708
+ const w = [...t, d.originalIndex.toString()];
709
+ return i(d.item, w, v);
711
710
  };
712
711
  }
713
- const J = t[t.length - 1];
714
- if (!isNaN(Number(J))) {
715
- const n = t.slice(0, -1), o = r.getState().getNestedState(e, n);
716
- if (Array.isArray(o) && l === "cut")
717
- return () => Y(
712
+ const Z = t[t.length - 1];
713
+ if (!isNaN(Number(Z))) {
714
+ const l = t.slice(0, -1), r = n.getState().getNestedState(e, l);
715
+ if (Array.isArray(r) && c === "cut")
716
+ return () => X(
718
717
  s,
719
- n,
718
+ l,
720
719
  e,
721
- Number(J)
720
+ Number(Z)
722
721
  );
723
722
  }
724
- if (l === "get")
725
- return () => r.getState().getNestedState(e, t);
726
- if (l === "$derive")
727
- return (n) => ce({
723
+ if (c === "get")
724
+ return () => n.getState().getNestedState(e, t);
725
+ if (c === "$derive")
726
+ return (l) => ce({
728
727
  _stateKey: e,
729
728
  _path: t,
730
- _effect: n.toString()
729
+ _effect: l.toString()
731
730
  });
732
- if (l === "$derive")
733
- return (n) => ce({
731
+ if (c === "$derive")
732
+ return (l) => ce({
734
733
  _stateKey: e,
735
734
  _path: t,
736
- _effect: n.toString()
735
+ _effect: l.toString()
737
736
  });
738
- if (l === "$get")
737
+ if (c === "$get")
739
738
  return () => ce({
740
739
  _stateKey: e,
741
740
  _path: t
742
741
  });
743
- if (l === "lastSynced") {
744
- const n = `${e}:${t.join(".")}`;
745
- return r.getState().getSyncInfo(n);
742
+ if (c === "lastSynced") {
743
+ const l = `${e}:${t.join(".")}`;
744
+ return n.getState().getSyncInfo(l);
746
745
  }
747
- if (l == "getLocalStorage")
748
- return (n) => te(u + "-" + e + "-" + n);
749
- if (l === "_selected") {
750
- const n = t.slice(0, -1), o = n.join("."), c = r.getState().getNestedState(e, n);
751
- return Array.isArray(c) ? Number(t[t.length - 1]) === r.getState().getSelectedIndex(e, o) : void 0;
746
+ if (c == "getLocalStorage")
747
+ return (l) => ne(u + "-" + e + "-" + l);
748
+ if (c === "_selected") {
749
+ const l = t.slice(0, -1), r = l.join("."), o = n.getState().getNestedState(e, l);
750
+ return Array.isArray(o) ? Number(t[t.length - 1]) === n.getState().getSelectedIndex(e, r) : void 0;
752
751
  }
753
- if (l === "setSelected")
754
- return (n) => {
755
- const o = t.slice(0, -1), c = Number(t[t.length - 1]), a = o.join(".");
756
- n ? r.getState().setSelectedIndex(e, a, c) : r.getState().setSelectedIndex(e, a, void 0);
757
- const d = r.getState().getNestedState(e, [...o]);
758
- Z(s, d, o), I(o);
752
+ if (c === "setSelected")
753
+ return (l) => {
754
+ const r = t.slice(0, -1), o = Number(t[t.length - 1]), a = r.join(".");
755
+ l ? n.getState().setSelectedIndex(e, a, o) : n.getState().setSelectedIndex(e, a, void 0);
756
+ const d = n.getState().getNestedState(e, [...r]);
757
+ H(s, d, r), h(r);
759
758
  };
760
- if (l === "toggleSelected")
759
+ if (c === "toggleSelected")
761
760
  return () => {
762
- const n = t.slice(0, -1), o = Number(t[t.length - 1]), c = n.join("."), a = r.getState().getSelectedIndex(e, c);
763
- r.getState().setSelectedIndex(
761
+ const l = t.slice(0, -1), r = Number(t[t.length - 1]), o = l.join("."), a = n.getState().getSelectedIndex(e, o);
762
+ n.getState().setSelectedIndex(
764
763
  e,
765
- c,
766
- a === o ? void 0 : o
764
+ o,
765
+ a === r ? void 0 : r
767
766
  );
768
- const d = r.getState().getNestedState(e, [...n]);
769
- Z(s, d, n), I(n);
767
+ const d = n.getState().getNestedState(e, [...l]);
768
+ H(s, d, l), h(l);
770
769
  };
771
770
  if (t.length == 0) {
772
- if (l === "validateZodSchema")
771
+ if (c === "validateZodSchema")
773
772
  return () => {
774
- const n = r.getState().getInitialOptions(e)?.validation, o = r.getState().addValidationError;
775
- if (!n?.zodSchema)
773
+ const l = n.getState().getInitialOptions(e)?.validation, r = n.getState().addValidationError;
774
+ if (!l?.zodSchema)
776
775
  throw new Error("Zod schema not found");
777
- if (!n?.key)
776
+ if (!l?.key)
778
777
  throw new Error("Validation key not found");
779
- W(n.key);
780
- const c = r.getState().cogsStateStore[e];
778
+ L(l.key);
779
+ const o = n.getState().cogsStateStore[e];
781
780
  try {
782
- const a = r.getState().getValidationErrors(n.key);
783
- a && a.length > 0 && a.forEach(([E]) => {
784
- E && E.startsWith(n.key) && W(E);
781
+ const a = n.getState().getValidationErrors(l.key);
782
+ a && a.length > 0 && a.forEach(([w]) => {
783
+ w && w.startsWith(l.key) && L(w);
785
784
  });
786
- const d = n.zodSchema.safeParse(c);
787
- return d.success ? !0 : (d.error.errors.forEach((k) => {
788
- const _ = k.path, b = k.message, h = [n.key, ..._].join(".");
789
- o(h, b);
790
- }), ne(e), !1);
785
+ const d = l.zodSchema.safeParse(o);
786
+ return d.success ? !0 : (d.error.errors.forEach((I) => {
787
+ const E = I.path, C = I.message, T = [l.key, ...E].join(".");
788
+ r(T, C);
789
+ }), re(e), !1);
791
790
  } catch (a) {
792
791
  return console.error("Zod schema validation failed", a), !1;
793
792
  }
794
793
  };
795
- if (l === "_componentId") return m;
796
- if (l === "getComponents")
797
- return () => r().stateComponents.get(e);
798
- if (l === "getAllFormRefs")
794
+ if (c === "_componentId") return m;
795
+ if (c === "getComponents")
796
+ return () => n().stateComponents.get(e);
797
+ if (c === "getAllFormRefs")
799
798
  return () => me.getState().getFormRefsByStateKey(e);
800
- if (l === "_initialState")
801
- return r.getState().initialStateGlobal[e];
802
- if (l === "_serverState")
803
- return r.getState().serverState[e];
804
- if (l === "_isLoading")
805
- return r.getState().isLoadingGlobal[e];
806
- if (l === "revertToInitialState")
799
+ if (c === "_initialState")
800
+ return n.getState().initialStateGlobal[e];
801
+ if (c === "_serverState")
802
+ return n.getState().serverState[e];
803
+ if (c === "_isLoading")
804
+ return n.getState().isLoadingGlobal[e];
805
+ if (c === "revertToInitialState")
807
806
  return y.revertToInitialState;
808
- if (l === "updateInitialState") return y.updateInitialState;
809
- if (l === "removeValidation") return y.removeValidation;
807
+ if (c === "updateInitialState") return y.updateInitialState;
808
+ if (c === "removeValidation") return y.removeValidation;
810
809
  }
811
- if (l === "getFormRef")
810
+ if (c === "getFormRef")
812
811
  return () => me.getState().getFormRef(e + "." + t.join("."));
813
- if (l === "validationWrapper")
812
+ if (c === "validationWrapper")
814
813
  return ({
815
- children: n,
816
- hideMessage: o
814
+ children: l,
815
+ hideMessage: r
817
816
  }) => /* @__PURE__ */ Se(
818
- xe,
817
+ _e,
819
818
  {
820
- formOpts: o ? { validation: { message: "" } } : void 0,
819
+ formOpts: r ? { validation: { message: "" } } : void 0,
821
820
  path: t,
822
- validationKey: r.getState().getInitialOptions(e)?.validation?.key || "",
821
+ validationKey: n.getState().getInitialOptions(e)?.validation?.key || "",
823
822
  stateKey: e,
824
823
  validIndices: v?.validIndices,
825
- children: n
824
+ children: l
826
825
  }
827
826
  );
828
- if (l === "_stateKey") return e;
829
- if (l === "_path") return t;
830
- if (l === "_isServerSynced") return y._isServerSynced;
831
- if (l === "update")
832
- return (n, o) => {
833
- if (o?.debounce)
827
+ if (c === "_stateKey") return e;
828
+ if (c === "_path") return t;
829
+ if (c === "_isServerSynced") return y._isServerSynced;
830
+ if (c === "update")
831
+ return (l, r) => {
832
+ if (r?.debounce)
834
833
  Ae(() => {
835
- Z(s, n, t, "");
836
- const c = r.getState().getNestedState(e, t);
837
- o?.afterUpdate && o.afterUpdate(c);
838
- }, o.debounce);
834
+ H(s, l, t, "");
835
+ const o = n.getState().getNestedState(e, t);
836
+ r?.afterUpdate && r.afterUpdate(o);
837
+ }, r.debounce);
839
838
  else {
840
- Z(s, n, t, "");
841
- const c = r.getState().getNestedState(e, t);
842
- o?.afterUpdate && o.afterUpdate(c);
839
+ H(s, l, t, "");
840
+ const o = n.getState().getNestedState(e, t);
841
+ r?.afterUpdate && r.afterUpdate(o);
843
842
  }
844
- I(t);
843
+ h(t);
845
844
  };
846
- if (l === "formElement")
847
- return (n, o) => /* @__PURE__ */ Se(
848
- Te,
845
+ if (c === "formElement")
846
+ return (l, r) => /* @__PURE__ */ Se(
847
+ Ce,
849
848
  {
850
849
  setState: s,
851
850
  stateKey: e,
852
851
  path: t,
853
- child: n,
854
- formOpts: o
852
+ child: l,
853
+ formOpts: r
855
854
  }
856
855
  );
857
- const p = [...t, l], x = r.getState().getNestedState(e, p);
858
- return i(x, p, v);
856
+ const G = [...t, c], _ = n.getState().getNestedState(e, G);
857
+ return i(_, G, v);
859
858
  }
860
- }, O = new Proxy(V, A);
861
- return g.set(F, {
862
- proxy: O,
863
- stateVersion: w
864
- }), O;
859
+ }, F = new Proxy(V, A);
860
+ return f.set(p, {
861
+ proxy: F,
862
+ stateVersion: k
863
+ }), F;
865
864
  }
866
865
  return i(
867
- r.getState().getNestedState(e, [])
866
+ n.getState().getNestedState(e, [])
868
867
  );
869
868
  }
870
869
  function ce(e) {
871
- return ee(Ge, { proxy: e });
870
+ return te(De, { proxy: e });
872
871
  }
873
- function Me({
872
+ function Ue({
874
873
  proxy: e,
875
874
  rebuildStateShape: s
876
875
  }) {
877
- const m = r().getNestedState(e._stateKey, e._path);
876
+ const m = n().getNestedState(e._stateKey, e._path);
878
877
  return Array.isArray(m) ? s(
879
878
  m,
880
879
  e._path
881
880
  ).stateMapNoRender(
882
- (g, w, I, y, i) => e._mapFn(g, w, I, y, i)
881
+ (f, k, h, y, i) => e._mapFn(f, k, h, y, i)
883
882
  ) : null;
884
883
  }
885
- function Ge({
884
+ function De({
886
885
  proxy: e
887
886
  }) {
888
- const s = X(null), m = `${e._stateKey}-${e._path.join(".")}`;
887
+ const s = Q(null), m = `${e._stateKey}-${e._path.join(".")}`;
889
888
  return de(() => {
890
889
  const u = s.current;
891
890
  if (!u || !u.parentElement) return;
892
- const g = u.parentElement, I = Array.from(g.childNodes).indexOf(u);
893
- let y = g.getAttribute("data-parent-id");
894
- y || (y = `parent-${crypto.randomUUID()}`, g.setAttribute("data-parent-id", y));
891
+ const f = u.parentElement, h = Array.from(f.childNodes).indexOf(u);
892
+ let y = f.getAttribute("data-parent-id");
893
+ y || (y = `parent-${crypto.randomUUID()}`, f.setAttribute("data-parent-id", y));
895
894
  const S = {
896
895
  instanceId: `instance-${crypto.randomUUID()}`,
897
896
  parentId: y,
898
- position: I,
897
+ position: h,
899
898
  effect: e._effect
900
899
  };
901
- r.getState().addSignalElement(m, S);
902
- const t = r.getState().getNestedState(e._stateKey, e._path);
900
+ n.getState().addSignalElement(m, S);
901
+ const t = n.getState().getNestedState(e._stateKey, e._path);
903
902
  let v;
904
903
  if (e._effect)
905
904
  try {
@@ -913,18 +912,18 @@ function Ge({
913
912
  else
914
913
  v = t;
915
914
  v !== null && typeof v == "object" && (v = JSON.stringify(v));
916
- const F = document.createTextNode(String(v));
917
- u.replaceWith(F);
918
- }, [e._stateKey, e._path.join("."), e._effect]), ee("span", {
915
+ const p = document.createTextNode(String(v));
916
+ u.replaceWith(p);
917
+ }, [e._stateKey, e._path.join("."), e._effect]), te("span", {
919
918
  ref: s,
920
919
  style: { display: "none" },
921
920
  "data-signal-id": m
922
921
  });
923
922
  }
924
923
  function Ke(e) {
925
- const s = he(
924
+ const s = Ne(
926
925
  (m) => {
927
- const u = r.getState().stateComponents.get(e._stateKey) || {
926
+ const u = n.getState().stateComponents.get(e._stateKey) || {
928
927
  components: /* @__PURE__ */ new Map()
929
928
  };
930
929
  return u.components.set(e._stateKey, {
@@ -932,9 +931,9 @@ function Ke(e) {
932
931
  paths: /* @__PURE__ */ new Set([e._path.join(".")])
933
932
  }), () => u.components.delete(e._stateKey);
934
933
  },
935
- () => r.getState().getNestedState(e._stateKey, e._path)
934
+ () => n.getState().getNestedState(e._stateKey, e._path)
936
935
  );
937
- return ee("text", {}, String(s));
936
+ return te("text", {}, String(s));
938
937
  }
939
938
  export {
940
939
  ce as $cogsSignal,
@@ -942,6 +941,6 @@ export {
942
941
  Ye as addStateOptions,
943
942
  Xe as createCogsState,
944
943
  Qe as notifyComponent,
945
- De as useCogsStateFn
944
+ Me as useCogsStateFn
946
945
  };
947
946
  //# sourceMappingURL=CogsState.jsx.map