@modular-react/journeys 1.0.1 → 1.0.2

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.
@@ -0,0 +1,1564 @@
1
+ import { isDevEnv as e, isExitContract as t } from "@modular-react/core";
2
+ //#region src/semver.ts
3
+ var n = /* @__PURE__ */ function(e) {
4
+ return e[e.GT = 0] = "GT", e[e.GTE = 1] = "GTE", e[e.LT = 2] = "LT", e[e.LTE = 3] = "LTE", e[e.EQ = 4] = "EQ", e;
5
+ }(n || {}), r = class extends Error {
6
+ constructor(e) {
7
+ super(`[@modular-react/journeys] ${e}`), this.name = "SemverParseError";
8
+ }
9
+ };
10
+ function i(e) {
11
+ let t = x(S(e.trim()));
12
+ if (!t) throw new r(`invalid version "${e}"`);
13
+ return t;
14
+ }
15
+ function a(e) {
16
+ let t = e.trim();
17
+ if (t === "" || t === "*" || t === "x" || t === "X") return { sets: [[]] };
18
+ let n = t.split("||"), r = [];
19
+ for (let e of n) {
20
+ let t = d(e);
21
+ r.push(t);
22
+ }
23
+ return { sets: r };
24
+ }
25
+ function o(e, t) {
26
+ for (let n of t.sets) {
27
+ let t = !0;
28
+ for (let r of n) if (!u(e, r)) {
29
+ t = !1;
30
+ break;
31
+ }
32
+ if (t) return !0;
33
+ }
34
+ return !1;
35
+ }
36
+ function s(e, t) {
37
+ return o(i(e), a(t));
38
+ }
39
+ function c(e, t) {
40
+ return e[0] === t[0] ? e[1] === t[1] ? e[2] === t[2] ? 0 : e[2] < t[2] ? -1 : 1 : e[1] < t[1] ? -1 : 1 : e[0] < t[0] ? -1 : 1;
41
+ }
42
+ function l(e, t) {
43
+ return c(i(e), i(t));
44
+ }
45
+ function u(e, t) {
46
+ let r = c(e, t.v);
47
+ switch (t.op) {
48
+ case n.GT: return r > 0;
49
+ case n.GTE: return r >= 0;
50
+ case n.LT: return r < 0;
51
+ case n.LTE: return r <= 0;
52
+ case n.EQ: return r === 0;
53
+ }
54
+ }
55
+ function d(e) {
56
+ let t = [], n = f(e);
57
+ if (n) return p(n[0], n[1], t), t;
58
+ let r = e.split(/\s+/).filter((e) => e.length > 0);
59
+ if (r.length === 0) return t;
60
+ for (let e of r) m(e, t);
61
+ return t;
62
+ }
63
+ function f(e) {
64
+ let t = e.match(/^\s*(\S+)\s+-\s+(\S+)\s*$/);
65
+ return t ? [t[1], t[2]] : null;
66
+ }
67
+ function p(e, t, i) {
68
+ let a = v(S(e));
69
+ if (!a) throw new r(`invalid lower bound in hyphen range "${e}"`);
70
+ i.push({
71
+ op: n.GTE,
72
+ v: y(a, 0)
73
+ });
74
+ let o = v(S(t));
75
+ if (!o || o.major === null) throw new r(`invalid upper bound in hyphen range "${t}"`);
76
+ o.minor === null ? i.push({
77
+ op: n.LT,
78
+ v: [
79
+ o.major + 1,
80
+ 0,
81
+ 0
82
+ ]
83
+ }) : o.patch === null ? i.push({
84
+ op: n.LT,
85
+ v: [
86
+ o.major,
87
+ o.minor + 1,
88
+ 0
89
+ ]
90
+ }) : i.push({
91
+ op: n.LTE,
92
+ v: [
93
+ o.major,
94
+ o.minor,
95
+ o.patch
96
+ ]
97
+ });
98
+ }
99
+ function m(e, t) {
100
+ if (!(e === "*" || e === "x" || e === "X")) {
101
+ if (e.startsWith("^")) {
102
+ h(e.slice(1), t);
103
+ return;
104
+ }
105
+ if (e.startsWith("~")) {
106
+ g(e.slice(1), t);
107
+ return;
108
+ }
109
+ if (e.startsWith(">=")) {
110
+ t.push({
111
+ op: n.GTE,
112
+ v: b(e.slice(2), e)
113
+ });
114
+ return;
115
+ }
116
+ if (e.startsWith("<=")) {
117
+ t.push({
118
+ op: n.LTE,
119
+ v: b(e.slice(2), e)
120
+ });
121
+ return;
122
+ }
123
+ if (e.startsWith(">")) {
124
+ t.push({
125
+ op: n.GT,
126
+ v: b(e.slice(1), e)
127
+ });
128
+ return;
129
+ }
130
+ if (e.startsWith("<")) {
131
+ t.push({
132
+ op: n.LT,
133
+ v: b(e.slice(1), e)
134
+ });
135
+ return;
136
+ }
137
+ if (e.startsWith("=")) {
138
+ _(e.slice(1), e, t);
139
+ return;
140
+ }
141
+ _(e, e, t);
142
+ }
143
+ }
144
+ function h(e, t) {
145
+ let i = v(S(e));
146
+ if (!i) throw new r(`invalid caret range "^${e}"`);
147
+ let { major: a, minor: o, patch: s } = i;
148
+ if (a === null) throw new r(`invalid caret range "^${e}"`);
149
+ if (a > 0) {
150
+ t.push({
151
+ op: n.GTE,
152
+ v: [
153
+ a,
154
+ o ?? 0,
155
+ s ?? 0
156
+ ]
157
+ }), t.push({
158
+ op: n.LT,
159
+ v: [
160
+ a + 1,
161
+ 0,
162
+ 0
163
+ ]
164
+ });
165
+ return;
166
+ }
167
+ if (o === null) {
168
+ t.push({
169
+ op: n.GTE,
170
+ v: [
171
+ 0,
172
+ 0,
173
+ 0
174
+ ]
175
+ }), t.push({
176
+ op: n.LT,
177
+ v: [
178
+ 1,
179
+ 0,
180
+ 0
181
+ ]
182
+ });
183
+ return;
184
+ }
185
+ if (o > 0) {
186
+ t.push({
187
+ op: n.GTE,
188
+ v: [
189
+ 0,
190
+ o,
191
+ s ?? 0
192
+ ]
193
+ }), t.push({
194
+ op: n.LT,
195
+ v: [
196
+ 0,
197
+ o + 1,
198
+ 0
199
+ ]
200
+ });
201
+ return;
202
+ }
203
+ if (s === null) {
204
+ t.push({
205
+ op: n.GTE,
206
+ v: [
207
+ 0,
208
+ 0,
209
+ 0
210
+ ]
211
+ }), t.push({
212
+ op: n.LT,
213
+ v: [
214
+ 0,
215
+ 1,
216
+ 0
217
+ ]
218
+ });
219
+ return;
220
+ }
221
+ t.push({
222
+ op: n.GTE,
223
+ v: [
224
+ 0,
225
+ 0,
226
+ s
227
+ ]
228
+ }), t.push({
229
+ op: n.LT,
230
+ v: [
231
+ 0,
232
+ 0,
233
+ s + 1
234
+ ]
235
+ });
236
+ }
237
+ function g(e, t) {
238
+ let i = v(S(e));
239
+ if (!i) throw new r(`invalid tilde range "~${e}"`);
240
+ let { major: a, minor: o, patch: s } = i;
241
+ if (a === null) throw new r(`invalid tilde range "~${e}"`);
242
+ if (o === null) {
243
+ t.push({
244
+ op: n.GTE,
245
+ v: [
246
+ a,
247
+ 0,
248
+ 0
249
+ ]
250
+ }), t.push({
251
+ op: n.LT,
252
+ v: [
253
+ a + 1,
254
+ 0,
255
+ 0
256
+ ]
257
+ });
258
+ return;
259
+ }
260
+ t.push({
261
+ op: n.GTE,
262
+ v: [
263
+ a,
264
+ o,
265
+ s ?? 0
266
+ ]
267
+ }), t.push({
268
+ op: n.LT,
269
+ v: [
270
+ a,
271
+ o + 1,
272
+ 0
273
+ ]
274
+ });
275
+ }
276
+ function _(e, t, i) {
277
+ let a = v(S(e));
278
+ if (!a) throw new r(`invalid version or range "${t}"`);
279
+ let { major: o, minor: s, patch: c } = a;
280
+ if (o !== null) {
281
+ if (s === null) {
282
+ i.push({
283
+ op: n.GTE,
284
+ v: [
285
+ o,
286
+ 0,
287
+ 0
288
+ ]
289
+ }), i.push({
290
+ op: n.LT,
291
+ v: [
292
+ o + 1,
293
+ 0,
294
+ 0
295
+ ]
296
+ });
297
+ return;
298
+ }
299
+ if (c === null) {
300
+ i.push({
301
+ op: n.GTE,
302
+ v: [
303
+ o,
304
+ s,
305
+ 0
306
+ ]
307
+ }), i.push({
308
+ op: n.LT,
309
+ v: [
310
+ o,
311
+ s + 1,
312
+ 0
313
+ ]
314
+ });
315
+ return;
316
+ }
317
+ i.push({
318
+ op: n.EQ,
319
+ v: [
320
+ o,
321
+ s,
322
+ c
323
+ ]
324
+ });
325
+ }
326
+ }
327
+ function v(e) {
328
+ if (e === "" || e === "*" || e === "x" || e === "X") return {
329
+ major: null,
330
+ minor: null,
331
+ patch: null
332
+ };
333
+ let t = e.split(".");
334
+ if (t.length > 3) return null;
335
+ let n = [];
336
+ for (let e of t) {
337
+ if (e === "" || e === "x" || e === "X" || e === "*") {
338
+ n.push(null);
339
+ continue;
340
+ }
341
+ if (!/^\d+$/.test(e)) return null;
342
+ let t = Number(e);
343
+ if (!Number.isFinite(t)) return null;
344
+ n.push(t);
345
+ }
346
+ for (; n.length < 3;) n.push(null);
347
+ let r = !1;
348
+ for (let e of n) if (e === null) r = !0;
349
+ else if (r) return null;
350
+ return {
351
+ major: n[0],
352
+ minor: n[1],
353
+ patch: n[2]
354
+ };
355
+ }
356
+ function y(e, t) {
357
+ return [
358
+ e.major ?? t,
359
+ e.minor ?? t,
360
+ e.patch ?? t
361
+ ];
362
+ }
363
+ function b(e, t) {
364
+ let n = x(S(e));
365
+ if (!n) throw new r(`invalid version in "${t}"`);
366
+ return n;
367
+ }
368
+ function x(e) {
369
+ let t = /^(\d+)\.(\d+)\.(\d+)$/.exec(e);
370
+ if (!t) return null;
371
+ let n = [
372
+ Number(t[1]),
373
+ Number(t[2]),
374
+ Number(t[3])
375
+ ];
376
+ return n.every((e) => Number.isFinite(e)) ? n : null;
377
+ }
378
+ function S(e) {
379
+ if (e.length === 0) return e;
380
+ let t = e.charCodeAt(0);
381
+ return t === 118 || t === 86 || t === 61 ? e.slice(1) : e;
382
+ }
383
+ //#endregion
384
+ //#region src/validation.ts
385
+ var C = class extends Error {
386
+ issues;
387
+ constructor(e) {
388
+ super(`[@modular-react/journeys] Invalid journey registration:\n - ${e.join("\n - ")}`), this.name = "JourneyValidationError", this.issues = e;
389
+ }
390
+ }, w = class extends Error {
391
+ constructor(e, t) {
392
+ super(`[@modular-react/journeys] ${e}`, t), this.name = "JourneyHydrationError";
393
+ }
394
+ }, T = class extends Error {
395
+ journeyId;
396
+ constructor(e, t) {
397
+ super(`[@modular-react/journeys] Unknown journey id "${e}". Registered: ${t.join(", ") || "(none)"}`), this.name = "UnknownJourneyError", this.journeyId = e;
398
+ }
399
+ };
400
+ function E(e, t) {
401
+ let n = [], s = /* @__PURE__ */ new Map();
402
+ for (let e of t) s.set(e.id, e);
403
+ for (let t of O(e)) n.push(t);
404
+ for (let e of t) e.exitPoints && Object.prototype.hasOwnProperty.call(e.exitPoints, "allowBack") && n.push(`module "${e.id}" declares an exit named "allowBack", which collides with the reserved per-entry transition control key. Rename the exit (e.g. "allowBackExit").`);
405
+ let c = /* @__PURE__ */ new Set();
406
+ for (let l of e) {
407
+ let e = l.definition;
408
+ c.has(e.id) && n.push(`journey "${e.id}" is registered more than once`), c.add(e.id);
409
+ let u = e.transitions ?? {};
410
+ for (let [t, r] of Object.entries(u)) {
411
+ let i = s.get(t);
412
+ if (!i) {
413
+ n.push(`journey "${e.id}" references unknown module id "${t}" in transitions`);
414
+ continue;
415
+ }
416
+ if (!r || typeof r != "object") {
417
+ n.push(`journey "${e.id}" has malformed transitions for module "${t}" (expected an object)`);
418
+ continue;
419
+ }
420
+ for (let [a, o] of Object.entries(r)) {
421
+ let r = i.entryPoints?.[a];
422
+ if (!r) {
423
+ n.push(`journey "${e.id}" references unknown entry "${t}.${a}"`);
424
+ continue;
425
+ }
426
+ if (!o || typeof o != "object") {
427
+ n.push(`journey "${e.id}" has malformed transitions for entry "${t}.${a}" (expected an object)`);
428
+ continue;
429
+ }
430
+ let s = o;
431
+ for (let r of Object.keys(s)) r !== "allowBack" && (!i.exitPoints || !(r in i.exitPoints)) && n.push(`journey "${e.id}" references unknown exit "${t}.${a}.${r}"`);
432
+ if (s.allowBack === !0) {
433
+ let i = r.allowBack;
434
+ i !== "preserve-state" && i !== "rollback" && n.push(`journey "${e.id}" sets allowBack on "${t}.${a}" but the module entry does not declare allowBack`);
435
+ }
436
+ }
437
+ }
438
+ if (M(e, t, s, n), e.moduleCompat) for (let [t, c] of Object.entries(e.moduleCompat)) {
439
+ if (typeof c != "string") {
440
+ n.push(`journey "${e.id}" declares a non-string version range for module "${t}" in moduleCompat`);
441
+ continue;
442
+ }
443
+ let l = c.trim();
444
+ if (l.length === 0) {
445
+ n.push(`journey "${e.id}" declares an empty version range for module "${t}" in moduleCompat`);
446
+ continue;
447
+ }
448
+ let u = s.get(t);
449
+ if (!u) {
450
+ n.push(`journey "${e.id}" requires module "${t}" (range "${l}") in moduleCompat but it is not registered`);
451
+ continue;
452
+ }
453
+ let d;
454
+ try {
455
+ d = a(l);
456
+ } catch (i) {
457
+ let a = i instanceof r ? i.message : String(i);
458
+ n.push(`journey "${e.id}" has an unparseable moduleCompat range for "${t}": ${a}`);
459
+ continue;
460
+ }
461
+ let f;
462
+ try {
463
+ f = i(u.version);
464
+ } catch (i) {
465
+ let a = i instanceof r ? i.message : String(i);
466
+ n.push(`module "${t}" declares an unparseable version "${u.version}" (referenced by journey "${e.id}"): ${a}`);
467
+ continue;
468
+ }
469
+ o(f, d) || n.push(`journey "${e.id}" requires module "${t}" "${l}" but registered version is "${u.version}"`);
470
+ }
471
+ let d = e.resumes ?? {};
472
+ for (let [t, r] of Object.entries(d)) {
473
+ let i = s.get(t);
474
+ if (!i) {
475
+ n.push(`journey "${e.id}" references unknown module id "${t}" in resumes`);
476
+ continue;
477
+ }
478
+ if (!r || typeof r != "object") {
479
+ n.push(`journey "${e.id}" has malformed resumes for module "${t}" (expected an object)`);
480
+ continue;
481
+ }
482
+ for (let [a, o] of Object.entries(r)) {
483
+ if (!i.entryPoints?.[a]) {
484
+ n.push(`journey "${e.id}" references unknown entry "${t}.${a}" in resumes`);
485
+ continue;
486
+ }
487
+ if (!o || typeof o != "object") {
488
+ n.push(`journey "${e.id}" has malformed resumes for entry "${t}.${a}" (expected an object)`);
489
+ continue;
490
+ }
491
+ for (let r of Object.keys(o)) i.exitPoints && Object.prototype.hasOwnProperty.call(i.exitPoints, r) && n.push(`journey "${e.id}" declares resume "${t}.${a}.${r}" but "${r}" is also an exit name on that module — rename one to avoid silent confusion`), typeof o[r] != "function" && n.push(`journey "${e.id}" has non-function resume "${t}.${a}.${r}"`);
492
+ }
493
+ }
494
+ }
495
+ if (n.length > 0) throw new C(n);
496
+ }
497
+ function D(e) {
498
+ let t = O(e);
499
+ if (t.length > 0) throw new C(t);
500
+ }
501
+ function O(e) {
502
+ let t = /* @__PURE__ */ new Set();
503
+ for (let n of e) t.add(n.definition.id);
504
+ let n = /* @__PURE__ */ new Map();
505
+ for (let r of e) {
506
+ let e = [], i = r.definition.invokes;
507
+ if (Array.isArray(i)) for (let n of i) !n || typeof n.id != "string" || t.has(n.id) && e.push(n.id);
508
+ n.set(r.definition.id, e);
509
+ }
510
+ let r = [], i = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Set(), o = /* @__PURE__ */ new Set(), s = [], c = [];
511
+ for (let t of e) {
512
+ let e = t.definition.id;
513
+ if (!a.has(e)) for (c.push({
514
+ id: e,
515
+ nextIndex: 0
516
+ }), o.add(e), s.push(e); c.length > 0;) {
517
+ let e = c[c.length - 1], t = n.get(e.id);
518
+ if (!t || e.nextIndex >= t.length) {
519
+ c.pop(), s.pop(), o.delete(e.id), a.add(e.id);
520
+ continue;
521
+ }
522
+ let l = t[e.nextIndex];
523
+ if (e.nextIndex++, o.has(l)) {
524
+ let e = s.indexOf(l), t = s.slice(e), n = k(t);
525
+ if (!i.has(n)) {
526
+ i.add(n);
527
+ let e = [...t, l].map(A).join(" → ");
528
+ r.push(`journey invoke cycle detected: ${e}`);
529
+ }
530
+ continue;
531
+ }
532
+ a.has(l) || (c.push({
533
+ id: l,
534
+ nextIndex: 0
535
+ }), o.add(l), s.push(l));
536
+ }
537
+ }
538
+ return r;
539
+ }
540
+ function k(e) {
541
+ let t = 0;
542
+ for (let n = 1; n < e.length; n++) e[n] < e[t] && (t = n);
543
+ return e.slice(t).concat(e.slice(0, t)).join("→");
544
+ }
545
+ function A(e) {
546
+ return `"${e}"`;
547
+ }
548
+ function j(e) {
549
+ return typeof e == "object" && !!e && !Array.isArray(e);
550
+ }
551
+ function M(e, t, n, r) {
552
+ let i = e.wildcardTransitions;
553
+ if (i == null) return;
554
+ if (!j(i)) {
555
+ r.push(`journey "${e.id}" has malformed wildcardTransitions (expected an object, got ${P(i)})`);
556
+ return;
557
+ }
558
+ let a = i, o = new Set(Object.keys(e.transitions ?? {})), s = [];
559
+ for (let e of o) {
560
+ let t = n.get(e);
561
+ t && s.push(t);
562
+ }
563
+ let c = N(a.byEntryAndExit, `journey "${e.id}" has malformed wildcardTransitions.byEntryAndExit`, r);
564
+ c && F(e.id, c, t, s, r);
565
+ let l = N(a.byExit, `journey "${e.id}" has malformed wildcardTransitions.byExit`, r);
566
+ l && I(e.id, l, c, t, s, r);
567
+ }
568
+ function N(e, t, n) {
569
+ if (e != null) {
570
+ if (!j(e)) {
571
+ n.push(`${t} (expected an object, got ${P(e)})`);
572
+ return;
573
+ }
574
+ return e;
575
+ }
576
+ }
577
+ function P(e) {
578
+ return e === null ? "null" : Array.isArray(e) ? "array" : typeof e;
579
+ }
580
+ function F(e, t, n, r, i) {
581
+ for (let [a, o] of Object.entries(t)) {
582
+ if (!j(o)) {
583
+ i.push(`journey "${e}" has malformed wildcardTransitions.byEntryAndExit["${a}"] (expected an object, got ${P(o)})`);
584
+ continue;
585
+ }
586
+ for (let [t, s] of Object.entries(o)) {
587
+ if (typeof s != "function") {
588
+ i.push(`journey "${e}" has non-function wildcardTransitions.byEntryAndExit["${a}"]["${t}"]`);
589
+ continue;
590
+ }
591
+ if (n.filter((e) => e.entryPoints?.[a] !== void 0 && e.exitPoints !== void 0 && t in e.exitPoints).length === 0) {
592
+ i.push(`journey "${e}" declares wildcardTransitions.byEntryAndExit["${a}"]["${t}"] but no registered module pairs that entry with that exit`);
593
+ continue;
594
+ }
595
+ let o = r.filter((e) => e.entryPoints?.[a] !== void 0 && e.exitPoints !== void 0 && t in e.exitPoints);
596
+ L(i, e, `wildcardTransitions.byEntryAndExit["${a}"]["${t}"]`, t, o);
597
+ }
598
+ }
599
+ }
600
+ function I(e, t, n, r, i, a) {
601
+ for (let [o, s] of Object.entries(t)) {
602
+ if (typeof s != "function") {
603
+ a.push(`journey "${e}" has non-function wildcardTransitions.byExit["${o}"]`);
604
+ continue;
605
+ }
606
+ if (r.filter((e) => e.exitPoints !== void 0 && o in e.exitPoints).length === 0) {
607
+ a.push(`journey "${e}" declares wildcardTransitions.byExit["${o}"] but no registered module emits that exit`);
608
+ continue;
609
+ }
610
+ let t = i.filter((e) => e.exitPoints !== void 0 && o in e.exitPoints);
611
+ if (L(a, e, `wildcardTransitions.byExit["${o}"]`, o, t), n) {
612
+ let t = [];
613
+ for (let [e, r] of Object.entries(n)) j(r) && typeof r[o] == "function" && t.push(e);
614
+ t.length > 0 && console.warn(`[@modular-react/journeys] journey "${e}" declares both wildcardTransitions.byExit["${o}"] and wildcardTransitions.byEntryAndExit[${t.map(A).join(",")}]["${o}"]; the byExit handler is unreachable from those entries (the more specific one wins).`);
615
+ }
616
+ }
617
+ }
618
+ function L(e, n, r, i, a) {
619
+ let o = null, s = null;
620
+ for (let e of a) {
621
+ let n = e.exitPoints?.[i];
622
+ if (n && t(n)) {
623
+ o = n, s = e.id;
624
+ break;
625
+ }
626
+ }
627
+ if (o) for (let c of a) {
628
+ if (c.id === s) continue;
629
+ let a = c.exitPoints?.[i];
630
+ a && t(a) && a !== o && e.push(`journey "${n}" ${r}: module "${c.id}" declares exit "${i}" with a different ExitContract than module "${s}" (contract identity mismatch — share a single defineExitContract value across modules to make the wildcard's output shape uniform)`);
631
+ }
632
+ }
633
+ function R(e) {
634
+ let t = [];
635
+ return (!e.id || typeof e.id != "string") && t.push("journey is missing a string id"), (!e.version || typeof e.version != "string") && t.push(`journey "${e.id ?? "(unknown)"}" is missing a string version`), typeof e.initialState != "function" && t.push(`journey "${e.id}" must declare initialState as a function`), typeof e.start != "function" && t.push(`journey "${e.id}" must declare start as a function`), (!e.transitions || typeof e.transitions != "object") && t.push(`journey "${e.id}" must declare transitions`), t;
636
+ }
637
+ //#endregion
638
+ //#region src/runtime.ts
639
+ var ee = 16, te = 8, z = /* @__PURE__ */ new WeakMap();
640
+ function B(n, r = {}) {
641
+ let i = r.debug ?? e(), a = r.modules ?? {}, o = /* @__PURE__ */ new Set(), s = /* @__PURE__ */ new Set(), c = /* @__PURE__ */ new Map();
642
+ for (let e of n) c.set(e.definition.id, e);
643
+ let l = /* @__PURE__ */ new Map(), u = /* @__PURE__ */ new Map(), d = /* @__PURE__ */ new Map();
644
+ function f(e, t) {
645
+ return `${e}${t}`;
646
+ }
647
+ function p(e) {
648
+ e.revision += 1, e.cachedSnapshot = null;
649
+ for (let t of e.listeners) try {
650
+ t();
651
+ } catch (e) {
652
+ i && console.error("[@modular-react/journeys] listener threw", e);
653
+ }
654
+ }
655
+ function m() {
656
+ return (/* @__PURE__ */ new Date()).toISOString();
657
+ }
658
+ function h() {
659
+ try {
660
+ let e = globalThis.crypto;
661
+ if (e?.randomUUID) return `ji_${e.randomUUID()}`;
662
+ } catch {}
663
+ let e = Math.random().toString(36).slice(2, 10);
664
+ return `ji_${Date.now().toString(36)}_${e}`;
665
+ }
666
+ function g(e) {
667
+ return {
668
+ id: e.definition.id,
669
+ version: e.definition.version,
670
+ meta: e.definition.meta
671
+ };
672
+ }
673
+ function _(e) {
674
+ let t = c.get(e);
675
+ if (!t) throw new T(e, [...c.keys()]);
676
+ return t;
677
+ }
678
+ function v(e) {
679
+ return {
680
+ moduleId: e.module,
681
+ entry: e.entry,
682
+ input: e.input
683
+ };
684
+ }
685
+ function y(e) {
686
+ if (!e) return !1;
687
+ let t = a[e.moduleId]?.entryPoints?.[e.entry]?.allowBack;
688
+ return t === "rollback" || t === "preserve-state" ? t : !1;
689
+ }
690
+ function b(e, t) {
691
+ return t ? e.transitions?.[t.moduleId]?.[t.entry]?.allowBack === !0 : !1;
692
+ }
693
+ function x(e) {
694
+ if (typeof e != "object" || !e) return e;
695
+ let t = Array.isArray(e) ? [...e] : { ...e };
696
+ try {
697
+ Object.freeze(t);
698
+ } catch {}
699
+ return t;
700
+ }
701
+ function S(e, t) {
702
+ let n = t.options?.maxHistory;
703
+ if (!(n === void 0 || n <= 0)) {
704
+ for (; e.history.length > n;) e.history.shift(), e.rollbackSnapshots.shift();
705
+ e.hasRollbackSnapshot = e.rollbackSnapshots.some((e) => e !== void 0);
706
+ }
707
+ }
708
+ function C(e, t) {
709
+ let n = A(e);
710
+ if (e.saveInFlight) {
711
+ e.pendingSave = n;
712
+ return;
713
+ }
714
+ E(e, t, n);
715
+ }
716
+ async function E(e, t, n) {
717
+ e.saveInFlight = !0;
718
+ try {
719
+ if (!e.persistenceKey) return;
720
+ await t.save(e.persistenceKey, n);
721
+ } catch (t) {
722
+ i && console.error(`[@modular-react/journeys] Failed to persist "${e.journeyId}" instance ${e.id}`, t);
723
+ } finally {
724
+ if (e.saveInFlight = !1, e.pendingRemove) e.pendingRemove = !1, e.pendingSave = null, e.persistenceKey && k(t, e.persistenceKey);
725
+ else if (e.pendingSave) {
726
+ let n = e.pendingSave;
727
+ e.pendingSave = null, E(e, t, n);
728
+ }
729
+ }
730
+ }
731
+ function D(e, t) {
732
+ if (!e.persistenceKey) return;
733
+ e.pendingSave = null;
734
+ let n = e.persistenceKey;
735
+ if (u.delete(f(e.journeyId, n)), e.saveInFlight) {
736
+ e.pendingRemove = !0;
737
+ return;
738
+ }
739
+ k(t, n);
740
+ }
741
+ function O(e, t) {
742
+ k(e, t);
743
+ }
744
+ function k(e, t) {
745
+ try {
746
+ let n = e.remove(t);
747
+ n && typeof n.catch == "function" && n.catch((e) => {
748
+ i && console.error("[@modular-react/journeys] persistence.remove rejected", e);
749
+ });
750
+ } catch (e) {
751
+ i && console.error("[@modular-react/journeys] persistence.remove threw", e);
752
+ }
753
+ }
754
+ function A(e) {
755
+ let t;
756
+ if (e.activeChildId) {
757
+ let n = l.get(e.activeChildId);
758
+ if (n) {
759
+ let r = n.parent;
760
+ r && r.instanceId === e.id && (t = {
761
+ childJourneyId: n.journeyId,
762
+ childInstanceId: n.id,
763
+ childPersistenceKey: n.persistenceKey,
764
+ resumeName: r.resumeName
765
+ });
766
+ }
767
+ }
768
+ let n = e.parent ? {
769
+ parentInstanceId: e.parent.instanceId,
770
+ resumeName: e.parent.resumeName
771
+ } : void 0;
772
+ return {
773
+ definitionId: e.journeyId,
774
+ version: c.get(e.journeyId).definition.version,
775
+ instanceId: e.id,
776
+ status: e.status === "loading" ? "active" : e.status,
777
+ step: e.step,
778
+ history: [...e.history],
779
+ rollbackSnapshots: e.hasRollbackSnapshot ? e.rollbackSnapshots.map((e) => e === void 0 ? null : e) : void 0,
780
+ terminalPayload: e.status === "completed" || e.status === "aborted" ? e.terminalPayload : void 0,
781
+ state: e.state,
782
+ startedAt: e.startedAt,
783
+ updatedAt: e.updatedAt,
784
+ pendingInvoke: t,
785
+ parentLink: n,
786
+ resumeBouncesAtStep: e.resumeBouncesAtStep ?? void 0
787
+ };
788
+ }
789
+ function j(e, t, n, r, a, o) {
790
+ let s = {
791
+ journeyId: t.journeyId,
792
+ instanceId: t.id,
793
+ from: n,
794
+ to: r,
795
+ exit: a,
796
+ state: t.state,
797
+ history: [...t.history],
798
+ kind: o?.kind ?? "step",
799
+ child: o?.child,
800
+ outcome: o?.outcome,
801
+ resume: o?.resume
802
+ };
803
+ try {
804
+ e.definition.onTransition?.(s);
805
+ } catch (e) {
806
+ i && console.error("[@modular-react/journeys] onTransition (definition) threw", e);
807
+ }
808
+ try {
809
+ e.options?.onTransition?.(s);
810
+ } catch (e) {
811
+ i && console.error("[@modular-react/journeys] onTransition (registration) threw", e);
812
+ }
813
+ }
814
+ function M(e, t, n) {
815
+ if (t.terminalFired) return;
816
+ t.terminalFired = !0;
817
+ let r = {
818
+ journeyId: t.journeyId,
819
+ instanceId: t.id,
820
+ state: t.state,
821
+ history: t.history
822
+ };
823
+ try {
824
+ e.definition.onComplete?.(r, n);
825
+ } catch (e) {
826
+ i && console.error("[@modular-react/journeys] onComplete (definition) threw", e);
827
+ }
828
+ try {
829
+ e.options?.onComplete?.(r, n);
830
+ } catch (e) {
831
+ i && console.error("[@modular-react/journeys] onComplete (registration) threw", e);
832
+ }
833
+ }
834
+ function N(e, t, n) {
835
+ if (t.terminalFired) return;
836
+ t.terminalFired = !0;
837
+ let r = {
838
+ journeyId: t.journeyId,
839
+ instanceId: t.id,
840
+ state: t.state,
841
+ history: t.history
842
+ };
843
+ try {
844
+ e.definition.onAbort?.(r, n);
845
+ } catch (e) {
846
+ i && console.error("[@modular-react/journeys] onAbort (definition) threw", e);
847
+ }
848
+ try {
849
+ e.options?.onAbort?.(r, n);
850
+ } catch (e) {
851
+ i && console.error("[@modular-react/journeys] onAbort (registration) threw", e);
852
+ }
853
+ }
854
+ function P(e, t, n, r, a = "step") {
855
+ try {
856
+ e.options?.onError?.(n, {
857
+ step: r,
858
+ phase: a
859
+ });
860
+ } catch (e) {
861
+ i && console.error("[@modular-react/journeys] onError (registration) threw", e);
862
+ }
863
+ }
864
+ function F(e, t, n) {
865
+ let r = e.definition.resumes;
866
+ if (!r) return;
867
+ let i = r[t.moduleId];
868
+ if (!i) return;
869
+ let a = i[t.entry];
870
+ if (a) return a[n];
871
+ }
872
+ function I(e) {
873
+ let t = [], n = e.parent, r = new Set([e.id]);
874
+ for (; n && !r.has(n.instanceId);) {
875
+ let e = l.get(n.instanceId);
876
+ if (!e) break;
877
+ r.add(e.id), t.unshift(e), n = e.parent;
878
+ }
879
+ return t;
880
+ }
881
+ function L(e) {
882
+ let t = e.options?.maxResumeBouncesPerStep;
883
+ return typeof t == "number" && Number.isFinite(t) && t > 0 ? t : te;
884
+ }
885
+ function R(e, t, n) {
886
+ let r = ee, i = !1, a = (e) => {
887
+ let t = e?.options?.maxCallStackDepth;
888
+ typeof t == "number" && Number.isFinite(t) && t > 0 && (r = i ? Math.min(r, t) : t, i = !0);
889
+ };
890
+ for (let t of e) a(c.get(t.journeyId));
891
+ return a(t), a(n), r;
892
+ }
893
+ function B(e, t, n, r) {
894
+ let a = e.step;
895
+ if (!a) return V(e, t, { abort: {
896
+ reason: "invoke-without-step",
897
+ exit: r
898
+ } }, { kind: "invoke" }), !1;
899
+ if (!n || typeof n != "object") return V(e, t, { abort: {
900
+ reason: "invoke-missing-spec",
901
+ exit: r
902
+ } }, { kind: "invoke" }), !1;
903
+ let o = n.handle?.id;
904
+ if (!o || !c.has(o)) return i && console.error(`[@modular-react/journeys] Invoke from "${e.journeyId}.${a.moduleId}.${a.entry}" references unknown child journey id "${o}".`), P(t, e, /* @__PURE__ */ Error(`Unknown child journey "${o}"`), a, "invoke"), V(e, t, { abort: {
905
+ reason: "invoke-unknown-journey",
906
+ journeyId: o,
907
+ exit: r
908
+ } }, { kind: "invoke" }), !1;
909
+ if (typeof n.resume != "string" || n.resume.length === 0) return i && console.error(`[@modular-react/journeys] Invoke from "${e.journeyId}.${a.moduleId}.${a.entry}" is missing a resume name.`), P(t, e, /* @__PURE__ */ Error("Invoke missing resume name"), a, "invoke"), V(e, t, { abort: {
910
+ reason: "invoke-missing-resume",
911
+ exit: r
912
+ } }, { kind: "invoke" }), !1;
913
+ if (!F(t, a, n.resume)) return i && console.error(`[@modular-react/journeys] Invoke from "${e.journeyId}.${a.moduleId}.${a.entry}" names resume "${n.resume}" but no such handler is declared on def.resumes[${a.moduleId}][${a.entry}].`), P(t, e, /* @__PURE__ */ Error(`Resume "${n.resume}" not declared on ${a.moduleId}.${a.entry}`), a, "invoke"), V(e, t, { abort: {
914
+ reason: "invoke-unknown-resume",
915
+ resume: n.resume,
916
+ exit: r
917
+ } }, { kind: "invoke" }), !1;
918
+ let s = c.get(o), u = t.definition.invokes;
919
+ if (Array.isArray(u)) {
920
+ let n = !1;
921
+ for (let e of u) if (e?.id === o) {
922
+ n = !0;
923
+ break;
924
+ }
925
+ if (!n) return i && console.error(`[@modular-react/journeys] Invoke from "${e.journeyId}.${a.moduleId}.${a.entry}" dispatched handle "${o}" which is not in the parent's declared invokes[]. Add the handle to the parent journey's \`invokes\` array, or remove the declaration to opt out of static checking.`), P(t, e, /* @__PURE__ */ Error(`Child journey "${o}" is not in "${e.journeyId}".invokes[]`), a, "invoke"), V(e, t, { abort: {
926
+ reason: "invoke-undeclared-child",
927
+ parentJourneyId: e.journeyId,
928
+ childJourneyId: o,
929
+ exit: r
930
+ } }, { kind: "invoke" }), !1;
931
+ }
932
+ let f = I(e);
933
+ {
934
+ let n = f.map((e) => e.journeyId).concat(e.journeyId), s = n.indexOf(o);
935
+ if (s >= 0) {
936
+ let c = [...n.slice(s), o], l = c.map((e) => `"${e}"`).join(" → ");
937
+ return i && console.error(`[@modular-react/journeys] Invoke would re-enter journey "${o}" already on the active chain: ${l}. Aborting parent "${e.id}".`), P(t, e, /* @__PURE__ */ Error(`Invoke cycle on chain: ${l}`), a, "invoke"), V(e, t, { abort: {
938
+ reason: "invoke-cycle",
939
+ childJourneyId: o,
940
+ chain: c,
941
+ exit: r
942
+ } }, { kind: "invoke" }), !1;
943
+ }
944
+ }
945
+ let m = R(f, t, s), h = f.length + 2;
946
+ if (h > m) {
947
+ let n = f.map((e) => e.journeyId).concat(e.journeyId, o), s = n.map((e) => `"${e}"`).join(" → ");
948
+ return i && console.error(`[@modular-react/journeys] Invoke would exceed maxCallStackDepth (${m}) — chain: ${s}. Aborting parent "${e.id}".`), P(t, e, /* @__PURE__ */ Error(`Invoke would exceed depth cap ${m} on chain ${s}`), a, "invoke"), V(e, t, { abort: {
949
+ reason: "invoke-stack-overflow",
950
+ depth: h,
951
+ cap: m,
952
+ chain: n,
953
+ exit: r
954
+ } }, { kind: "invoke" }), !1;
955
+ }
956
+ let g;
957
+ try {
958
+ g = $.start(n.handle, n.input);
959
+ } catch (n) {
960
+ return i && console.error("[@modular-react/journeys] runtime.start during invoke threw", n), P(t, e, n, a, "invoke"), V(e, t, { abort: {
961
+ reason: "invoke-start-threw",
962
+ error: n,
963
+ exit: r
964
+ } }, { kind: "invoke" }), !1;
965
+ }
966
+ let _ = l.get(g);
967
+ if (!_) return V(e, t, { abort: {
968
+ reason: "invoke-start-no-record",
969
+ exit: r
970
+ } }, { kind: "invoke" }), !1;
971
+ if (_.parent && _.parent.instanceId !== e.id) return i && console.error(`[@modular-react/journeys] Invoke target "${_.id}" is already linked to parent "${_.parent.instanceId}". Aborting parent "${e.id}".`), P(t, e, /* @__PURE__ */ Error(`Child instance "${_.id}" is already linked to parent "${_.parent.instanceId}"`), a, "invoke"), V(e, t, { abort: {
972
+ reason: "invoke-child-already-linked",
973
+ childInstanceId: _.id,
974
+ existingParentId: _.parent.instanceId,
975
+ exit: r
976
+ } }, { kind: "invoke" }), !1;
977
+ e.activeChildId = g, _.parent = {
978
+ instanceId: e.id,
979
+ resumeName: n.resume
980
+ }, d.set(g, e.id);
981
+ let v = s?.options?.persistence;
982
+ return v && C(_, v), p(_), _;
983
+ }
984
+ function V(e, t, n, r) {
985
+ U(e, t, n, null, r);
986
+ }
987
+ function H(e) {
988
+ if (e.status !== "completed" && e.status !== "aborted") return;
989
+ let t = e.parent;
990
+ if (!t) return;
991
+ let n = t.instanceId, r = l.get(n);
992
+ if (d.delete(e.id), e.parent = null, p(e), !r || r.activeChildId !== e.id) return;
993
+ if (r.activeChildId = null, r.status !== "active") {
994
+ p(r);
995
+ return;
996
+ }
997
+ let a = c.get(r.journeyId), o = r.step;
998
+ if (!a || !o) {
999
+ p(r);
1000
+ return;
1001
+ }
1002
+ let s = F(a, o, t.resumeName);
1003
+ if (!s) {
1004
+ i && console.warn(`[@modular-react/journeys] Resume "${t.resumeName}" no longer declared on ${o.moduleId}.${o.entry} — aborting parent ${r.id}.`), P(a, r, /* @__PURE__ */ Error(`Resume "${t.resumeName}" missing`), o, "resume"), U(r, a, { abort: {
1005
+ reason: "resume-missing",
1006
+ resume: t.resumeName,
1007
+ childJourneyId: e.journeyId
1008
+ } }, null);
1009
+ return;
1010
+ }
1011
+ let u = e.status === "completed" ? {
1012
+ status: "completed",
1013
+ payload: e.terminalPayload
1014
+ } : {
1015
+ status: "aborted",
1016
+ reason: e.terminalPayload
1017
+ }, f;
1018
+ try {
1019
+ f = s({
1020
+ state: r.state,
1021
+ input: o.input,
1022
+ outcome: u
1023
+ });
1024
+ } catch (e) {
1025
+ i && console.error("[@modular-react/journeys] resume handler threw", e), P(a, r, e, o, "resume"), U(r, a, { abort: {
1026
+ reason: "resume-threw",
1027
+ resume: t.resumeName,
1028
+ error: e
1029
+ } }, null);
1030
+ return;
1031
+ }
1032
+ if (f && typeof f.then == "function") {
1033
+ i && console.error(`[@modular-react/journeys] Resume handler "${t.resumeName}" on ${o.moduleId}.${o.entry} returned a Promise. Resumes must be synchronous and pure.`), U(r, a, { abort: {
1034
+ reason: "resume-returned-promise",
1035
+ resume: t.resumeName
1036
+ } }, null, {
1037
+ kind: "resume",
1038
+ outcome: u,
1039
+ resume: t.resumeName
1040
+ });
1041
+ return;
1042
+ }
1043
+ U(r, a, f, null, {
1044
+ kind: "resume",
1045
+ outcome: u,
1046
+ resume: t.resumeName
1047
+ });
1048
+ }
1049
+ function U(e, t, n, r, o) {
1050
+ let s = e.step, c = e.state;
1051
+ if ("state" in n && (e.state = n.state), ("next" in n || "complete" in n || "abort" in n) && (e.resumeBouncesAtStep = null), "next" in n) {
1052
+ let l = v(n.next);
1053
+ if (i && Object.keys(a).length > 0) {
1054
+ let e = a[l.moduleId];
1055
+ e ? e.entryPoints?.[l.entry] || console.warn(`[@modular-react/journeys] Transition on "${s?.moduleId}.${s?.entry}" returned next.entry="${l.moduleId}.${l.entry}" which is not a declared entry on that module.`) : console.warn(`[@modular-react/journeys] Transition on "${s?.moduleId}.${s?.entry}" returned next.module="${l.moduleId}" which is not in the runtime's module map — the outlet will render a "no entry" error.`);
1056
+ }
1057
+ s && (e.history.push(s), W(l) === "rollback" ? (e.rollbackSnapshots.push(x(c)), e.hasRollbackSnapshot = !0) : e.rollbackSnapshots.push(void 0)), e.step = l, e.status = "active", e.stepToken += 1, e.updatedAt = m(), e.cachedCallbacks = null, S(e, t), j(t, e, s, l, r, o);
1058
+ } else if ("complete" in n) s && (e.history.push(s), e.rollbackSnapshots.push(void 0)), e.step = null, e.status = "completed", e.terminalPayload = n.complete, e.stepToken += 1, e.updatedAt = m(), e.cachedCallbacks = null, S(e, t), j(t, e, s, null, r, o), M(t, e, n.complete);
1059
+ else if ("abort" in n) s && (e.history.push(s), e.rollbackSnapshots.push(void 0)), e.step = null, e.status = "aborted", e.terminalPayload = n.abort, e.stepToken += 1, e.updatedAt = m(), e.cachedCallbacks = null, S(e, t), j(t, e, s, null, r, o), N(t, e, n.abort);
1060
+ else if ("invoke" in n) {
1061
+ if (o?.kind === "resume") {
1062
+ let n = L(t), r = e.resumeBouncesAtStep, a = r && r.stepToken === e.stepToken ? r.count + 1 : 1;
1063
+ if (a > n) {
1064
+ i && console.error(`[@modular-react/journeys] Resume on "${e.journeyId}.${s?.moduleId}.${s?.entry}" would bounce ${a} times in a row at the same step (cap ${n}). Aborting "${e.id}" to break the loop.`), P(t, e, /* @__PURE__ */ Error(`Resume bounce limit exceeded (${n}) at ${s?.moduleId}.${s?.entry}`), s, "resume"), U(e, t, { abort: {
1065
+ reason: "resume-bounce-limit",
1066
+ cap: n,
1067
+ count: a,
1068
+ resume: o.resume
1069
+ } }, null, {
1070
+ kind: "resume",
1071
+ outcome: o.outcome,
1072
+ resume: o.resume
1073
+ });
1074
+ return;
1075
+ }
1076
+ e.resumeBouncesAtStep = {
1077
+ stepToken: e.stepToken,
1078
+ count: a
1079
+ };
1080
+ }
1081
+ let a = B(e, t, n.invoke, r);
1082
+ if (!a) return;
1083
+ e.updatedAt = m(), j(t, e, s, s, r, {
1084
+ kind: "invoke",
1085
+ child: {
1086
+ instanceId: a.id,
1087
+ journeyId: a.journeyId
1088
+ }
1089
+ });
1090
+ }
1091
+ let l = t.options?.persistence;
1092
+ l && (e.status === "active" ? C(e, l) : D(e, l)), p(e), H(e);
1093
+ }
1094
+ function W(e) {
1095
+ return y(e);
1096
+ }
1097
+ function G(e, n) {
1098
+ let r = `${e.definition.id}${n}`;
1099
+ if (s.has(r)) return;
1100
+ s.add(r);
1101
+ let i = Object.keys(e.definition.transitions ?? {}), o = null;
1102
+ for (let r of i) {
1103
+ let i = a[r];
1104
+ if (!i) continue;
1105
+ let s = i.exitPoints?.[n];
1106
+ if (!(!s || !t(s))) {
1107
+ if (!o) {
1108
+ o = {
1109
+ contract: s,
1110
+ moduleId: r
1111
+ };
1112
+ continue;
1113
+ }
1114
+ if (s !== o.contract) {
1115
+ console.warn(`[@modular-react/journeys] Journey "${e.definition.id}": modules "${o.moduleId}" and "${r}" declare exit "${n}" via different ExitContract instances. Wildcard handlers may receive a non-uniform output shape. This drift was not caught at registration time — re-resolve the registry, or call validateJourneyContracts(...) after registering new modules.`);
1116
+ return;
1117
+ }
1118
+ }
1119
+ }
1120
+ }
1121
+ function ne(e, n, r, s, c) {
1122
+ if (e.status !== "active") {
1123
+ i && console.warn(`[@modular-react/journeys] Exit("${s}") dropped on instance ${e.id} — status=${e.status}. (This is the expected no-op when an exit fires before the initial async load settles; await the load or subscribe for status changes before dispatching.)`);
1124
+ return;
1125
+ }
1126
+ if (e.activeChildId) {
1127
+ i && console.warn(`[@modular-react/journeys] Exit("${s}") dropped on instance ${e.id} — a child journey is in flight (activeChildId=${e.activeChildId}). The parent advances when the child resumes.`);
1128
+ return;
1129
+ }
1130
+ if (e.stepToken !== r) {
1131
+ i && console.warn(`[@modular-react/journeys] Stale exit("${s}") dropped on instance ${e.id}`);
1132
+ return;
1133
+ }
1134
+ let l = e.step;
1135
+ if (!l) return;
1136
+ let u = c, d = a[l.moduleId];
1137
+ !d && i && !o.has(l.moduleId) && (o.add(l.moduleId), console.warn(`[@modular-react/journeys] No descriptor for module "${l.moduleId}" in the runtime's module map. ExitContract payload validation is skipped for any contract-based exit on this step. Pass it via createJourneyRuntime({ modules: ... }) to enable schema validation.`));
1138
+ let f = d?.exitPoints?.[s], p = f !== void 0 && t(f);
1139
+ if (i && p && G(n, s), p && f.schema) {
1140
+ let t = f, r;
1141
+ try {
1142
+ r = t.schema["~standard"].validate(c);
1143
+ } catch (t) {
1144
+ i && console.error(`[@modular-react/journeys] Exit contract schema for "${l.moduleId}.${s}" threw during validation.`, t), P(n, e, t, l), U(e, n, { abort: {
1145
+ reason: "transition-error",
1146
+ exit: s,
1147
+ error: t
1148
+ } }, s);
1149
+ return;
1150
+ }
1151
+ if (r && typeof r.then == "function") {
1152
+ i && console.error(`[@modular-react/journeys] Exit contract schema for "${l.moduleId}.${s}" returned a Promise. Schemas attached to ExitContracts must be synchronous — async refinements run in a loading entry point instead.`), r.catch(() => {}), U(e, n, { abort: {
1153
+ reason: "exit-payload-invalid-async",
1154
+ exit: s
1155
+ } }, s);
1156
+ return;
1157
+ }
1158
+ let a = r;
1159
+ if (a.issues) {
1160
+ i && console.warn(`[@modular-react/journeys] Exit("${s}") on ${l.moduleId} failed contract validation:`, a.issues), U(e, n, { abort: {
1161
+ reason: "exit-payload-invalid",
1162
+ exit: s,
1163
+ issues: a.issues
1164
+ } }, s);
1165
+ return;
1166
+ }
1167
+ u = a.value;
1168
+ }
1169
+ let m = n.definition.transitions?.[l.moduleId]?.[l.entry]?.[s], h = n.definition.wildcardTransitions, g = h?.byEntryAndExit?.[l.entry]?.[s], _ = h?.byExit?.[s], v = typeof m == "function" ? m : typeof g == "function" ? g : typeof _ == "function" ? _ : void 0;
1170
+ if (typeof v != "function") {
1171
+ i && console.warn(`[@modular-react/journeys] No transition for exit("${s}") on ${l.moduleId}.${l.entry} — ignoring.`);
1172
+ return;
1173
+ }
1174
+ let y;
1175
+ try {
1176
+ y = v({
1177
+ state: e.state,
1178
+ input: l.input,
1179
+ output: u
1180
+ });
1181
+ } catch (t) {
1182
+ i && console.error("[@modular-react/journeys] transition handler threw", t), P(n, e, t, l), U(e, n, { abort: {
1183
+ reason: "transition-error",
1184
+ exit: s,
1185
+ error: t
1186
+ } }, s);
1187
+ return;
1188
+ }
1189
+ if (y && typeof y.then == "function") {
1190
+ i && console.error(`[@modular-react/journeys] Transition handler for ${l.moduleId}.${l.entry}."${s}" returned a Promise. Transitions must be synchronous and pure — put async work inside a loading entry point on a module.`), U(e, n, { abort: {
1191
+ reason: "transition-returned-promise",
1192
+ exit: s
1193
+ } }, s);
1194
+ return;
1195
+ }
1196
+ U(e, n, y, s);
1197
+ }
1198
+ function re(e, t, n) {
1199
+ if (e.status !== "active" || e.activeChildId || e.stepToken !== n || e.history.length === 0) return;
1200
+ let r = e.step;
1201
+ if (!r || !b(t.definition, r)) return;
1202
+ let i = e.history.pop(), a = e.rollbackSnapshots.pop();
1203
+ y(r) === "rollback" && a !== void 0 && (e.state = a), e.hasRollbackSnapshot = e.rollbackSnapshots.some((e) => e !== void 0), e.step = i, e.resumeBouncesAtStep = null, e.stepToken += 1, e.updatedAt = m(), e.cachedCallbacks = null, j(t, e, r, i, null);
1204
+ let o = t.options?.persistence;
1205
+ o && C(e, o), p(e);
1206
+ }
1207
+ function ie(e, t) {
1208
+ if (e.cachedCallbacks && e.cachedCallbacks.stepToken === e.stepToken) return e.cachedCallbacks;
1209
+ let n = e.stepToken, r = (r, i) => {
1210
+ ne(e, t, n, r, i);
1211
+ }, i = y(e.step);
1212
+ return i === !1 && e.step && b(t.definition, e.step) && !a[e.step.moduleId] && (i = "preserve-state"), e.cachedCallbacks = {
1213
+ stepToken: n,
1214
+ exit: r,
1215
+ goBack: i !== !1 && b(t.definition, e.step) && e.history.length > 0 ? () => {
1216
+ re(e, t, n);
1217
+ } : void 0
1218
+ }, e.cachedCallbacks;
1219
+ }
1220
+ function ae(e) {
1221
+ if (e.cachedSnapshot && e.cachedSnapshot.revision === e.revision) return e.cachedSnapshot.instance;
1222
+ let t = [...e.history], n = {
1223
+ id: e.id,
1224
+ journeyId: e.journeyId,
1225
+ status: e.status,
1226
+ step: e.step,
1227
+ history: t,
1228
+ state: e.state,
1229
+ terminalPayload: e.status === "completed" || e.status === "aborted" ? e.terminalPayload : void 0,
1230
+ startedAt: e.startedAt,
1231
+ updatedAt: e.updatedAt,
1232
+ activeChildId: e.activeChildId,
1233
+ parent: e.parent ? { ...e.parent } : null,
1234
+ serialize: () => A(e)
1235
+ };
1236
+ return e.cachedSnapshot = {
1237
+ revision: e.revision,
1238
+ instance: n
1239
+ }, n;
1240
+ }
1241
+ function K(e, t, n, r) {
1242
+ let i = m();
1243
+ return {
1244
+ id: t,
1245
+ journeyId: e.definition.id,
1246
+ status: "loading",
1247
+ step: null,
1248
+ history: [],
1249
+ rollbackSnapshots: [],
1250
+ hasRollbackSnapshot: !1,
1251
+ state: r,
1252
+ terminalPayload: void 0,
1253
+ startedAt: i,
1254
+ updatedAt: i,
1255
+ stepToken: 0,
1256
+ persistenceKey: n,
1257
+ terminalFired: !1,
1258
+ retryCount: 0,
1259
+ listeners: /* @__PURE__ */ new Set(),
1260
+ pendingSave: null,
1261
+ saveInFlight: !1,
1262
+ pendingRemove: !1,
1263
+ revision: 0,
1264
+ cachedSnapshot: null,
1265
+ cachedCallbacks: null,
1266
+ parent: null,
1267
+ activeChildId: null,
1268
+ resumeBouncesAtStep: null
1269
+ };
1270
+ }
1271
+ function q(e, t, n) {
1272
+ let r = e.definition, i = n ?? K(e, h(), null, r.initialState(t));
1273
+ n ? (i.state = r.initialState(t), i.history = [], i.rollbackSnapshots = [], i.hasRollbackSnapshot = !1) : l.set(i.id, i);
1274
+ let a = v(r.start(i.state, t));
1275
+ i.step = a, i.status = "active", i.stepToken += 1, i.terminalFired = !1, i.terminalPayload = void 0, i.retryCount = 0, i.updatedAt = m(), i.cachedCallbacks = null, j(e, i, null, a, null);
1276
+ let o = e.options?.persistence;
1277
+ return o && C(i, o), p(i), i.id;
1278
+ }
1279
+ function J(e, t) {
1280
+ let n = t.history.length;
1281
+ if (t.rollbackSnapshots && t.rollbackSnapshots.length !== n) throw new w(`Blob for journey "${e.journeyId}" has rollbackSnapshots.length=${t.rollbackSnapshots.length} but history.length=${n}. Fix the persisted blob (pad rollbackSnapshots with null for non-rollback entries) or provide onHydrate to migrate.`);
1282
+ e.state = t.state, e.step = t.step, e.history = [...t.history], t.rollbackSnapshots ? (e.rollbackSnapshots = t.rollbackSnapshots.map((e) => e === null ? void 0 : e), e.hasRollbackSnapshot = e.rollbackSnapshots.some((e) => e !== void 0)) : (e.rollbackSnapshots = Array.from({ length: n }, () => void 0), e.hasRollbackSnapshot = !1), e.status = t.status, e.terminalPayload = t.terminalPayload, e.startedAt = t.startedAt, e.updatedAt = t.updatedAt, e.stepToken += 1, e.terminalFired = t.status !== "active", e.cachedCallbacks = null, t.parentLink ? e.parent = {
1283
+ instanceId: t.parentLink.parentInstanceId,
1284
+ resumeName: t.parentLink.resumeName
1285
+ } : e.parent = null, t.pendingInvoke ? e.activeChildId = t.pendingInvoke.childInstanceId : e.activeChildId = null, t.resumeBouncesAtStep && Number.isFinite(t.resumeBouncesAtStep.count) ? e.resumeBouncesAtStep = {
1286
+ stepToken: e.stepToken,
1287
+ count: Math.max(0, Math.floor(t.resumeBouncesAtStep.count))
1288
+ } : e.resumeBouncesAtStep = null;
1289
+ }
1290
+ function Y() {
1291
+ d.clear();
1292
+ for (let e of l.values()) {
1293
+ if (!e.parent) continue;
1294
+ let t = l.get(e.parent.instanceId);
1295
+ t && t.activeChildId === e.id && d.set(e.id, t.id);
1296
+ }
1297
+ }
1298
+ function X(e, t) {
1299
+ let n = t.pendingInvoke;
1300
+ if (!n || l.has(n.childInstanceId) || !n.childPersistenceKey) return;
1301
+ let r = c.get(n.childJourneyId);
1302
+ if (!r) return;
1303
+ let a = r.options?.persistence;
1304
+ if (!a) return;
1305
+ let o = f(n.childJourneyId, n.childPersistenceKey);
1306
+ if (u.has(o)) return;
1307
+ let s = Z(r, a, n.childPersistenceKey), d = (t) => {
1308
+ if (!t || t.status !== "active" || l.has(n.childInstanceId) || u.has(o) || e.activeChildId !== n.childInstanceId) return;
1309
+ let a = Q(r, t);
1310
+ if (!a.ok) return;
1311
+ let s = K(r, n.childInstanceId, n.childPersistenceKey, a.blob.state);
1312
+ l.set(n.childInstanceId, s), u.set(o, n.childInstanceId);
1313
+ try {
1314
+ J(s, a.blob);
1315
+ } catch (e) {
1316
+ i && console.error("[@modular-react/journeys] auto-rehydrate child failed", e), l.delete(n.childInstanceId), u.delete(o);
1317
+ return;
1318
+ }
1319
+ X(s, a.blob), Y(), p(s), p(e);
1320
+ };
1321
+ if (s && typeof s.then == "function") {
1322
+ s.then((e) => d(e), (e) => {
1323
+ i && console.error("[@modular-react/journeys] auto-rehydrate child persistence.load rejected", e);
1324
+ });
1325
+ return;
1326
+ }
1327
+ d(s);
1328
+ }
1329
+ function Z(e, t, n) {
1330
+ let r;
1331
+ try {
1332
+ r = t.load(n);
1333
+ } catch (e) {
1334
+ return i && console.error("[@modular-react/journeys] persistence.load threw", e), null;
1335
+ }
1336
+ return r && r.then, r;
1337
+ }
1338
+ function Q(e, t) {
1339
+ let n = t, r = !1;
1340
+ if (e.definition.onHydrate) {
1341
+ r = !0;
1342
+ try {
1343
+ n = e.definition.onHydrate(n);
1344
+ } catch (e) {
1345
+ return i && console.error("[@modular-react/journeys] onHydrate (definition) threw", e), {
1346
+ ok: !1,
1347
+ reason: "on-hydrate-threw",
1348
+ cause: e
1349
+ };
1350
+ }
1351
+ }
1352
+ let a = e.options?.onHydrate;
1353
+ if (a) {
1354
+ r = !0;
1355
+ try {
1356
+ n = a(n);
1357
+ } catch (e) {
1358
+ return i && console.error("[@modular-react/journeys] onHydrate (registration) threw", e), {
1359
+ ok: !1,
1360
+ reason: "on-hydrate-threw",
1361
+ cause: e
1362
+ };
1363
+ }
1364
+ }
1365
+ return r ? {
1366
+ ok: !0,
1367
+ blob: n
1368
+ } : t.version === e.definition.version ? {
1369
+ ok: !0,
1370
+ blob: t
1371
+ } : {
1372
+ ok: !1,
1373
+ reason: "version-mismatch"
1374
+ };
1375
+ }
1376
+ let $ = {
1377
+ start(e, ...t) {
1378
+ let n = t.length > 0 ? t[0] : void 0, r = _(typeof e == "string" ? e : e.id), a = r.options?.persistence;
1379
+ if (a) {
1380
+ let e = a.keyFor({
1381
+ journeyId: r.definition.id,
1382
+ input: n
1383
+ }), t = f(r.definition.id, e), o = u.get(t), s = o ? l.get(o) : null;
1384
+ if (s && (s.status === "active" || s.status === "loading")) return s.id;
1385
+ let c = r.definition, d = Z(r, a, e);
1386
+ if (d && typeof d.then == "function") {
1387
+ let o = h(), s = K(r, o, e, c.initialState(n));
1388
+ return l.set(o, s), u.set(t, o), p(s), d.then((t) => {
1389
+ if (s.status !== "loading") return;
1390
+ if (!t || t.status !== "active") {
1391
+ t && O(a, e), q(r, n, s);
1392
+ return;
1393
+ }
1394
+ let o = Q(r, t);
1395
+ if (!o.ok) {
1396
+ O(a, e), q(r, n, s);
1397
+ return;
1398
+ }
1399
+ try {
1400
+ J(s, o.blob);
1401
+ } catch (t) {
1402
+ i && console.error("[@modular-react/journeys] hydrate after async load failed", t), O(a, e), q(r, n, s);
1403
+ return;
1404
+ }
1405
+ X(s, o.blob), Y(), p(s);
1406
+ }, (e) => {
1407
+ i && console.error("[@modular-react/journeys] persistence.load rejected", e), s.status === "loading" && q(r, n, s);
1408
+ }), o;
1409
+ }
1410
+ let m = d;
1411
+ if (m && m.status === "active") {
1412
+ let o = Q(r, m);
1413
+ if (o.ok) {
1414
+ let s = o.blob.instanceId && !l.has(o.blob.instanceId) ? o.blob.instanceId : h(), d = K(r, s, e, c.initialState(n));
1415
+ l.set(s, d), u.set(t, s);
1416
+ try {
1417
+ J(d, o.blob);
1418
+ } catch (o) {
1419
+ i && console.error("[@modular-react/journeys] hydrate during start failed", o), l.delete(s), u.delete(t), O(a, e);
1420
+ let d = h(), f = K(r, d, e, c.initialState(n));
1421
+ return l.set(d, f), u.set(t, d), q(r, n, f);
1422
+ }
1423
+ return X(d, o.blob), Y(), p(d), s;
1424
+ }
1425
+ O(a, e);
1426
+ } else m && O(a, e);
1427
+ let g = h(), _ = K(r, g, e, c.initialState(n));
1428
+ return l.set(g, _), u.set(t, g), q(r, n, _);
1429
+ }
1430
+ return q(r, n);
1431
+ },
1432
+ hydrate(e, t) {
1433
+ let n = _(e), r = Q(n, t);
1434
+ if (!r.ok) throw r.reason === "on-hydrate-threw" ? new w(`onHydrate threw while migrating blob for "${e}" (blob=${t.version} def=${n.definition.version}).`, { cause: r.cause }) : new w(`Hydrate version mismatch for "${e}": blob=${t.version} def=${n.definition.version}. Provide onHydrate to migrate.`);
1435
+ let i = r.blob.instanceId || h();
1436
+ if (l.has(i)) throw new w(`Cannot hydrate journey "${e}" with instance id "${i}" — an instance with the same id is already in memory. Call forget(id) first if you intend to replace it.`);
1437
+ let a = K(n, i, null, r.blob.state);
1438
+ l.set(i, a);
1439
+ try {
1440
+ J(a, r.blob);
1441
+ } catch (e) {
1442
+ throw l.delete(i), e;
1443
+ }
1444
+ return X(a, r.blob), Y(), p(a), i;
1445
+ },
1446
+ getInstance(e) {
1447
+ let t = l.get(e);
1448
+ return t ? ae(t) : null;
1449
+ },
1450
+ listInstances() {
1451
+ return [...l.keys()];
1452
+ },
1453
+ listDefinitions() {
1454
+ return [...c.values()].map(g);
1455
+ },
1456
+ isRegistered(e) {
1457
+ return c.has(e);
1458
+ },
1459
+ subscribe(e, t) {
1460
+ let n = l.get(e);
1461
+ return n ? (n.listeners.add(t), () => {
1462
+ n.listeners.delete(t);
1463
+ }) : () => {};
1464
+ },
1465
+ end(e, t) {
1466
+ let n = l.get(e);
1467
+ if (!n || n.status === "completed" || n.status === "aborted") return;
1468
+ let r = c.get(n.journeyId);
1469
+ if (!r) return;
1470
+ if (n.activeChildId) {
1471
+ let e = n.activeChildId;
1472
+ n.activeChildId = null;
1473
+ let r = l.get(e);
1474
+ r && r.parent && r.parent.instanceId === n.id && (r.parent = null), d.delete(e), $.end(e, {
1475
+ reason: "parent-ended",
1476
+ parentId: n.id,
1477
+ cause: t
1478
+ });
1479
+ }
1480
+ if (n.status === "loading") {
1481
+ U(n, r, { abort: t ?? "abandoned" }, null);
1482
+ return;
1483
+ }
1484
+ let a = { abort: t ?? "abandoned" }, o = r.options?.onAbandon ?? r.definition.onAbandon;
1485
+ if (o) try {
1486
+ a = o({
1487
+ journeyId: n.journeyId,
1488
+ instanceId: n.id,
1489
+ step: n.step,
1490
+ state: n.state,
1491
+ reason: t ?? "abandoned"
1492
+ });
1493
+ } catch (e) {
1494
+ i && console.error("[@modular-react/journeys] onAbandon threw", e), P(r, n, e, n.step, "abandon"), a = { abort: {
1495
+ reason: t ?? "abandoned",
1496
+ cause: "onAbandon-threw",
1497
+ error: e
1498
+ } };
1499
+ }
1500
+ U(n, r, a, null);
1501
+ },
1502
+ forget(e) {
1503
+ let t = l.get(e);
1504
+ if (t && !(t.status !== "completed" && t.status !== "aborted")) {
1505
+ if (t.persistenceKey && u.delete(f(t.journeyId, t.persistenceKey)), d.delete(e), t.activeChildId) {
1506
+ let e = l.get(t.activeChildId);
1507
+ e && e.parent && e.parent.instanceId === t.id && (e.parent = null, p(e)), d.delete(t.activeChildId), t.activeChildId = null;
1508
+ }
1509
+ t.listeners.clear(), l.delete(e);
1510
+ }
1511
+ },
1512
+ forgetTerminal() {
1513
+ let e = 0;
1514
+ for (let [t, n] of l) if (n.status === "completed" || n.status === "aborted") {
1515
+ if (n.persistenceKey && u.delete(f(n.journeyId, n.persistenceKey)), d.delete(t), n.activeChildId) {
1516
+ let e = l.get(n.activeChildId);
1517
+ e && e.parent && e.parent.instanceId === n.id && (e.parent = null, p(e)), d.delete(n.activeChildId);
1518
+ }
1519
+ n.listeners.clear(), l.delete(t), e += 1;
1520
+ }
1521
+ return e;
1522
+ }
1523
+ };
1524
+ function oe(e, t, n) {
1525
+ let r = l.get(e);
1526
+ if (!r) return;
1527
+ let i = c.get(r.journeyId);
1528
+ i && P(i, r, t, n, "step");
1529
+ }
1530
+ let se = {
1531
+ __bindStepCallbacks: ie,
1532
+ __getRecord: (e) => l.get(e),
1533
+ __getRegistered: (e) => c.get(e),
1534
+ __moduleMap: a,
1535
+ __debug: i,
1536
+ __fireComponentError: oe,
1537
+ __synthesizeCompletion: (e, t) => {
1538
+ let n = l.get(e);
1539
+ if (!n) return;
1540
+ let r = c.get(n.journeyId);
1541
+ r && U(n, r, { complete: t }, null);
1542
+ },
1543
+ __synthesizeAbort: (e, t) => {
1544
+ let n = l.get(e);
1545
+ if (!n) return;
1546
+ let r = c.get(n.journeyId);
1547
+ r && U(n, r, { abort: t }, null);
1548
+ },
1549
+ __consumeRetry: (e, t) => {
1550
+ let n = l.get(e);
1551
+ return !n || n.retryCount >= t ? !1 : (n.retryCount += 1, !0);
1552
+ }
1553
+ };
1554
+ return z.set($, se), $;
1555
+ }
1556
+ function V(e) {
1557
+ let t = z.get(e);
1558
+ if (!t) throw Error("[@modular-react/journeys] getInternals() called on a runtime that was not produced by createJourneyRuntime().");
1559
+ return t;
1560
+ }
1561
+ //#endregion
1562
+ export { T as a, D as c, s as d, C as i, r as l, V as n, E as o, w as r, R as s, B as t, l as u };
1563
+
1564
+ //# sourceMappingURL=runtime-DPPLlqbu.js.map