@raphy-js/raph 1.0.0-dev.2 → 1.0.1-dev.25

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.
Files changed (246) hide show
  1. package/README.md +1096 -0
  2. package/dist/api/Raph.d.ts +59 -190
  3. package/dist/api/RaphFacadeSession.d.ts +90 -0
  4. package/dist/api/RaphReactivityInstance.d.ts +72 -0
  5. package/dist/api/errors.d.ts +9 -4
  6. package/dist/api/types.d.ts +162 -17
  7. package/dist/core-CClhT8WR.js +1044 -0
  8. package/dist/core-cPQWYD4K.cjs +1 -0
  9. package/dist/debug/api/Raph.cjs +1 -1
  10. package/dist/debug/api/Raph.js +368 -367
  11. package/dist/debug/api/RaphFacadeSession.cjs +1 -0
  12. package/dist/debug/api/RaphFacadeSession.js +302 -0
  13. package/dist/debug/api/RaphReactivityInstance.cjs +1 -0
  14. package/dist/debug/api/RaphReactivityInstance.js +146 -0
  15. package/dist/debug/api/errors.cjs +1 -1
  16. package/dist/debug/api/errors.js +10 -7
  17. package/dist/debug/path/DataPath.cjs +1 -0
  18. package/dist/debug/path/DataPath.js +390 -0
  19. package/dist/debug/path/PathRouter.cjs +1 -0
  20. package/dist/debug/path/PathRouter.js +80 -0
  21. package/dist/debug/path/RaphKernel.cjs +1 -1
  22. package/dist/debug/path/RaphKernel.js +316 -236
  23. package/dist/debug/path/data-path.utils.cjs +1 -0
  24. package/dist/debug/path/data-path.utils.js +56 -0
  25. package/dist/debug/path/derived/DerivedRegistry.cjs +1 -0
  26. package/dist/debug/path/derived/DerivedRegistry.js +143 -0
  27. package/dist/debug/path/derived/RaphDerivedHandle.cjs +1 -0
  28. package/dist/debug/path/{RaphDerivedHandle.js → derived/RaphDerivedHandle.js} +19 -22
  29. package/dist/debug/path/derived/derived-collection.utils.cjs +1 -0
  30. package/dist/debug/path/{derived-collection.js → derived/derived-collection.utils.js} +71 -71
  31. package/dist/debug/path/derived/derived-strategy.types.cjs +1 -0
  32. package/dist/debug/path/derived/derived-strategy.types.js +1 -0
  33. package/dist/debug/path/meta/MetaController.cjs +1 -0
  34. package/dist/debug/path/{MetaController.js → meta/MetaController.js} +58 -66
  35. package/dist/debug/path/meta/RaphMeta.cjs +1 -0
  36. package/dist/debug/path/meta/RaphMeta.js +62 -0
  37. package/dist/debug/path/meta/RaphMetaStore.cjs +1 -0
  38. package/dist/debug/path/{RaphMetaStore.js → meta/RaphMetaStore.js} +24 -30
  39. package/dist/debug/path/meta/meta.types.cjs +1 -0
  40. package/dist/debug/path/meta/meta.types.js +1 -0
  41. package/dist/debug/path/storage/AdapterPathStore.cjs +1 -0
  42. package/dist/debug/path/storage/AdapterPathStore.js +128 -0
  43. package/dist/debug/path/storage/DefaultDataAdapter.cjs +1 -0
  44. package/dist/debug/path/{data-adapter.js → storage/DefaultDataAdapter.js} +47 -57
  45. package/dist/debug/path/storage/PathEventHub.cjs +1 -0
  46. package/dist/debug/path/storage/PathEventHub.js +161 -0
  47. package/dist/debug/path/storage/PathStore.cjs +1 -0
  48. package/dist/debug/path/{PathStore.js → storage/PathStore.js} +88 -98
  49. package/dist/debug/path/storage/createDataDraft.cjs +1 -0
  50. package/dist/debug/path/{data-draft.js → storage/createDataDraft.js} +24 -28
  51. package/dist/debug/path/storage/data-adapter.types.cjs +1 -0
  52. package/dist/debug/path/storage/data-adapter.types.js +1 -0
  53. package/dist/debug/reactivity/disposal.cjs +1 -0
  54. package/dist/debug/reactivity/disposal.js +32 -0
  55. package/dist/debug/reactivity/engine.cjs +1 -1
  56. package/dist/debug/reactivity/engine.js +888 -321
  57. package/dist/debug/reactivity/reactive-object.cjs +1 -0
  58. package/dist/debug/reactivity/reactive-object.js +265 -0
  59. package/dist/debug/reactivity/scope.cjs +1 -1
  60. package/dist/debug/reactivity/scope.js +221 -117
  61. package/dist/debug/reactivity/watch.cjs +1 -1
  62. package/dist/debug/reactivity/watch.js +1 -1
  63. package/dist/debug/runtime/RaphRuntime.cjs +1 -1
  64. package/dist/debug/runtime/RaphRuntime.js +1134 -502
  65. package/dist/debug/runtime/diagnostics/RaphDebug.cjs +1 -0
  66. package/dist/debug/runtime/diagnostics/RaphDebug.js +287 -0
  67. package/dist/debug/runtime/errors.cjs +1 -1
  68. package/dist/debug/runtime/errors.js +6 -3
  69. package/dist/debug/runtime/ownership/RaphEffectScope.cjs +1 -0
  70. package/dist/debug/runtime/ownership/RaphEffectScope.js +304 -0
  71. package/dist/debug/runtime/ownership/RaphNode.cjs +1 -0
  72. package/dist/debug/runtime/ownership/RaphNode.js +665 -0
  73. package/dist/debug/runtime/ownership/RaphScope.cjs +1 -0
  74. package/dist/debug/runtime/ownership/RaphScope.js +236 -0
  75. package/dist/debug/runtime/ownership/parent-graph.types.cjs +1 -0
  76. package/dist/debug/runtime/ownership/parent-graph.types.js +1 -0
  77. package/dist/debug/runtime/phases/RaphFrameLoop.cjs +1 -0
  78. package/dist/debug/runtime/{RaphFrameLoop.js → phases/RaphFrameLoop.js} +13 -17
  79. package/dist/debug/runtime/phases/RaphPhasePipeline.cjs +1 -0
  80. package/dist/debug/runtime/phases/RaphPhasePipeline.js +848 -0
  81. package/dist/debug/runtime/phases/RaphPhaseTaskRecord.cjs +1 -0
  82. package/dist/debug/runtime/phases/RaphPhaseTaskRecord.js +176 -0
  83. package/dist/debug/runtime/phases/RaphPhaseWatch.cjs +1 -0
  84. package/dist/debug/runtime/phases/RaphPhaseWatch.js +20 -0
  85. package/dist/debug/runtime/phases/orderPhaseTasks.cjs +1 -0
  86. package/dist/debug/runtime/phases/orderPhaseTasks.js +59 -0
  87. package/dist/debug/runtime/reactivity/PathHandle.cjs +1 -0
  88. package/dist/debug/runtime/reactivity/PathHandle.js +165 -0
  89. package/dist/debug/runtime/reactivity/RaphMetaSource.cjs +1 -0
  90. package/dist/debug/runtime/reactivity/RaphMetaSource.js +108 -0
  91. package/dist/debug/runtime/reactivity/RaphSelector.cjs +1 -0
  92. package/dist/debug/runtime/reactivity/RaphSelector.js +503 -0
  93. package/dist/debug/runtime/reactivity/RaphWorkGraph.cjs +1 -0
  94. package/dist/debug/runtime/reactivity/RaphWorkGraph.js +1157 -0
  95. package/dist/debug/runtime/reactivity/RuntimeReactivity.cjs +1 -0
  96. package/dist/debug/runtime/reactivity/RuntimeReactivity.js +550 -0
  97. package/dist/debug/runtime/reactivity/RuntimeWatch.cjs +1 -0
  98. package/dist/debug/runtime/reactivity/RuntimeWatch.js +77 -0
  99. package/dist/debug/runtime/reactivity/disposal-sweep.utils.cjs +1 -0
  100. package/dist/debug/runtime/reactivity/disposal-sweep.utils.js +42 -0
  101. package/dist/debug/runtime/reactivity/runtime-reactivity.types.cjs +1 -0
  102. package/dist/debug/runtime/reactivity/runtime-reactivity.types.js +1 -0
  103. package/dist/entrypoints/machine-testing.d.ts +2 -0
  104. package/dist/entrypoints/machine.d.ts +5 -0
  105. package/dist/entrypoints/path.d.ts +14 -0
  106. package/dist/entrypoints/reactivity.d.ts +1 -0
  107. package/dist/entrypoints/runtime.d.ts +18 -0
  108. package/dist/index.cjs +1 -1
  109. package/dist/index.js +46 -38
  110. package/dist/machine/compiler.d.ts +71 -0
  111. package/dist/machine/core.d.ts +50 -0
  112. package/dist/machine/persistence.d.ts +5 -0
  113. package/dist/machine/runtime.d.ts +4 -0
  114. package/dist/machine/testing.d.ts +43 -0
  115. package/dist/machine/types.d.ts +155 -0
  116. package/dist/machine-testing.cjs +1 -0
  117. package/dist/machine-testing.js +188 -0
  118. package/dist/machine.cjs +1 -0
  119. package/dist/machine.js +822 -0
  120. package/dist/main.d.ts +20 -0
  121. package/dist/path/{data-path.d.ts → DataPath.d.ts} +16 -40
  122. package/dist/path/{path-router.d.ts → PathRouter.d.ts} +1 -1
  123. package/dist/path/RaphKernel.d.ts +37 -10
  124. package/dist/path/data-path.types.d.ts +34 -0
  125. package/dist/path/data-path.utils.d.ts +9 -0
  126. package/dist/path/{DerivedRegistry.d.ts → derived/DerivedRegistry.d.ts} +2 -2
  127. package/dist/path/{RaphDerivedHandle.d.ts → derived/RaphDerivedHandle.d.ts} +3 -3
  128. package/dist/path/derived/derived-collection.types.d.ts +12 -0
  129. package/dist/path/{derived-collection.d.ts → derived/derived-collection.utils.d.ts} +3 -13
  130. package/dist/path/{derived-strategies.d.ts → derived/derived-strategy.types.d.ts} +0 -3
  131. package/dist/path/derived/derived-strategy.utils.d.ts +4 -0
  132. package/dist/path/{MetaController.d.ts → meta/MetaController.d.ts} +4 -4
  133. package/dist/path/{RaphMeta.d.ts → meta/RaphMeta.d.ts} +2 -2
  134. package/dist/path/{RaphMetaStore.d.ts → meta/RaphMetaStore.d.ts} +1 -1
  135. package/dist/path/{meta.types.d.ts → meta/meta.types.d.ts} +2 -4
  136. package/dist/path/path.types.d.ts +3 -2
  137. package/dist/path/{segment-key.d.ts → pathSegmentKey.d.ts} +1 -1
  138. package/dist/path/{AdapterPathStore.d.ts → storage/AdapterPathStore.d.ts} +4 -4
  139. package/dist/path/{data-adapter.d.ts → storage/DefaultDataAdapter.d.ts} +2 -17
  140. package/dist/path/storage/PathEventHub.d.ts +62 -0
  141. package/dist/path/{PathStore.d.ts → storage/PathStore.d.ts} +7 -7
  142. package/dist/path/storage/data-adapter.types.d.ts +17 -0
  143. package/dist/path.cjs +1 -1
  144. package/dist/path.js +16 -16
  145. package/dist/raph.cjs +1 -1
  146. package/dist/raph.js +46 -38
  147. package/dist/reactivity/disposal.d.ts +14 -0
  148. package/dist/reactivity/engine.d.ts +193 -6
  149. package/dist/reactivity/reactive-object.d.ts +36 -0
  150. package/dist/reactivity/scope.d.ts +108 -30
  151. package/dist/reactivity/types.d.ts +25 -0
  152. package/dist/reactivity.cjs +1 -1
  153. package/dist/reactivity.js +12 -12
  154. package/dist/runtime/RaphRuntime.d.ts +319 -160
  155. package/dist/runtime/diagnostics/RaphDebug.d.ts +239 -0
  156. package/dist/runtime/errors.d.ts +5 -0
  157. package/dist/runtime/ownership/RaphEffectScope.d.ts +98 -0
  158. package/dist/runtime/ownership/RaphNode.d.ts +310 -0
  159. package/dist/runtime/ownership/RaphScope.d.ts +168 -0
  160. package/dist/runtime/{parent-graph.d.ts → ownership/parent-graph.types.d.ts} +0 -1
  161. package/dist/runtime/ownership/parentGraph.d.ts +3 -0
  162. package/dist/runtime/phases/RaphPhasePipeline.d.ts +253 -0
  163. package/dist/runtime/phases/RaphPhaseTaskRecord.d.ts +195 -0
  164. package/dist/runtime/phases/RaphPhaseWatch.d.ts +22 -0
  165. package/dist/runtime/phases/orderPhaseTasks.d.ts +6 -0
  166. package/dist/runtime/phases/phase.types.d.ts +171 -0
  167. package/dist/runtime/{PathHandle.d.ts → reactivity/PathHandle.d.ts} +14 -6
  168. package/dist/runtime/reactivity/RaphMetaSource.d.ts +32 -0
  169. package/dist/runtime/reactivity/RaphSelector.d.ts +338 -0
  170. package/dist/runtime/reactivity/RaphWorkGraph.d.ts +309 -0
  171. package/dist/runtime/reactivity/RuntimeReactivity.d.ts +293 -0
  172. package/dist/runtime/{RuntimeWatch.d.ts → reactivity/RuntimeWatch.d.ts} +12 -6
  173. package/dist/runtime/reactivity/disposal-sweep.utils.d.ts +19 -0
  174. package/dist/runtime/reactivity/runtime-reactivity.types.d.ts +56 -0
  175. package/dist/runtime.cjs +1 -1
  176. package/dist/runtime.js +21 -16
  177. package/package.json +76 -35
  178. package/ReadMe.md +0 -153
  179. package/dist/api/public.d.ts +0 -22
  180. package/dist/debug/api/public.cjs +0 -1
  181. package/dist/debug/api/public.js +0 -40
  182. package/dist/debug/path/AdapterPathStore.cjs +0 -1
  183. package/dist/debug/path/AdapterPathStore.js +0 -133
  184. package/dist/debug/path/DerivedRegistry.cjs +0 -1
  185. package/dist/debug/path/DerivedRegistry.js +0 -157
  186. package/dist/debug/path/MetaController.cjs +0 -1
  187. package/dist/debug/path/PathEventHub.cjs +0 -1
  188. package/dist/debug/path/PathEventHub.js +0 -148
  189. package/dist/debug/path/PathStore.cjs +0 -1
  190. package/dist/debug/path/RaphDerivedHandle.cjs +0 -1
  191. package/dist/debug/path/RaphMeta.cjs +0 -1
  192. package/dist/debug/path/RaphMeta.js +0 -68
  193. package/dist/debug/path/RaphMetaStore.cjs +0 -1
  194. package/dist/debug/path/data-adapter.cjs +0 -1
  195. package/dist/debug/path/data-draft.cjs +0 -1
  196. package/dist/debug/path/data-path.cjs +0 -1
  197. package/dist/debug/path/data-path.js +0 -423
  198. package/dist/debug/path/derived-collection.cjs +0 -1
  199. package/dist/debug/path/path-router.cjs +0 -1
  200. package/dist/debug/path/path-router.js +0 -86
  201. package/dist/debug/path/public.cjs +0 -1
  202. package/dist/debug/path/public.js +0 -18
  203. package/dist/debug/runtime/PathHandle.cjs +0 -1
  204. package/dist/debug/runtime/PathHandle.js +0 -139
  205. package/dist/debug/runtime/RaphDebug.cjs +0 -1
  206. package/dist/debug/runtime/RaphDebug.js +0 -15
  207. package/dist/debug/runtime/RaphFrameLoop.cjs +0 -1
  208. package/dist/debug/runtime/RaphNode.cjs +0 -1
  209. package/dist/debug/runtime/RaphNode.js +0 -143
  210. package/dist/debug/runtime/RaphPhasePipeline.cjs +0 -1
  211. package/dist/debug/runtime/RaphPhasePipeline.js +0 -526
  212. package/dist/debug/runtime/RaphScope.cjs +0 -1
  213. package/dist/debug/runtime/RaphScope.js +0 -64
  214. package/dist/debug/runtime/RuntimeReactivity.cjs +0 -1
  215. package/dist/debug/runtime/RuntimeReactivity.js +0 -154
  216. package/dist/debug/runtime/RuntimeWatch.cjs +0 -1
  217. package/dist/debug/runtime/RuntimeWatch.js +0 -71
  218. package/dist/debug/runtime/public.cjs +0 -1
  219. package/dist/debug/runtime/public.js +0 -18
  220. package/dist/path/PathEventHub.d.ts +0 -48
  221. package/dist/path/public.d.ts +0 -14
  222. package/dist/runtime/RaphDebug.d.ts +0 -32
  223. package/dist/runtime/RaphNode.d.ts +0 -68
  224. package/dist/runtime/RaphPhasePipeline.d.ts +0 -133
  225. package/dist/runtime/RaphScope.d.ts +0 -50
  226. package/dist/runtime/RuntimeReactivity.d.ts +0 -40
  227. package/dist/runtime/phase.types.d.ts +0 -49
  228. package/dist/runtime/public.d.ts +0 -13
  229. package/dist/runtime/runtime-reactivity.types.d.ts +0 -9
  230. /package/dist/debug/path/{meta.types.cjs → data-path.types.cjs} +0 -0
  231. /package/dist/debug/path/{meta.types.js → data-path.types.js} +0 -0
  232. /package/dist/debug/{runtime/runtime-reactivity.types.cjs → path/derived/derived-collection.types.cjs} +0 -0
  233. /package/dist/debug/{runtime/runtime-reactivity.types.js → path/derived/derived-collection.types.js} +0 -0
  234. /package/dist/debug/path/{derived-strategies.cjs → derived/derived-strategy.utils.cjs} +0 -0
  235. /package/dist/debug/path/{derived-strategies.js → derived/derived-strategy.utils.js} +0 -0
  236. /package/dist/debug/path/{segment-key.cjs → pathSegmentKey.cjs} +0 -0
  237. /package/dist/debug/path/{segment-key.js → pathSegmentKey.js} +0 -0
  238. /package/dist/debug/runtime/{parent-graph.cjs → ownership/parentGraph.cjs} +0 -0
  239. /package/dist/debug/runtime/{parent-graph.js → ownership/parentGraph.js} +0 -0
  240. /package/dist/debug/runtime/{disposal.cjs → ownership/throwDisposalErrors.cjs} +0 -0
  241. /package/dist/debug/runtime/{disposal.js → ownership/throwDisposalErrors.js} +0 -0
  242. /package/dist/debug/runtime/{phase.types.cjs → phases/phase.types.cjs} +0 -0
  243. /package/dist/debug/runtime/{phase.types.js → phases/phase.types.js} +0 -0
  244. /package/dist/path/{data-draft.d.ts → storage/createDataDraft.d.ts} +0 -0
  245. /package/dist/runtime/{disposal.d.ts → ownership/throwDisposalErrors.d.ts} +0 -0
  246. /package/dist/runtime/{RaphFrameLoop.d.ts → phases/RaphFrameLoop.d.ts} +0 -0
@@ -0,0 +1,1044 @@
1
+ const Z = /* @__PURE__ */ new WeakMap(), mt = 16384;
2
+ function Jt(t) {
3
+ if (Tt(t.id, t.version), typeof t.states != "object" || t.states === null || Array.isArray(t.states))
4
+ throw new TypeError("[Raph machine] states must be an object.");
5
+ const e = t, n = [], r = /* @__PURE__ */ new Map(), o = [];
6
+ for (const [f, w] of Object.entries(e.states))
7
+ o.push(ct(f, w, void 0, 0, n, r));
8
+ const i = r.get(t.initial);
9
+ if (i === void 0 || n[i]?.parent !== void 0)
10
+ throw new Error(`[Raph machine] Initial state "${t.initial}" must be a root state key.`);
11
+ const s = /* @__PURE__ */ new Set(), a = n.map((f) => {
12
+ const w = f.initialKey === void 0 ? void 0 : f.children.find((g) => n[g]?.key === f.initialKey);
13
+ if (f.kind === "compound" && w === void 0)
14
+ throw new Error(`[Raph machine] Compound state "${f.path}" requires a valid direct initial child.`);
15
+ if (w !== void 0 && n[w].kind === "history")
16
+ throw new Error(`[Raph machine] Compound state "${f.path}" cannot use history as its initial child.`);
17
+ const b = f.historyFallbackPath === void 0 ? void 0 : r.get(f.historyFallbackPath);
18
+ if (f.historyFallbackPath !== void 0 && b === void 0)
19
+ throw new Error(
20
+ `[Raph machine] History state "${f.path}" has unknown fallback "${f.historyFallbackPath}".`
21
+ );
22
+ for (const g of f.on.keys())
23
+ s.add(g);
24
+ return {
25
+ after: f.after,
26
+ always: f.always,
27
+ children: Object.freeze([...f.children]),
28
+ depth: f.depth,
29
+ entry: f.entry,
30
+ exit: f.exit,
31
+ history: f.history,
32
+ ...b === void 0 ? {} : { historyFallback: b },
33
+ id: f.id,
34
+ ...w === void 0 ? {} : { initial: w },
35
+ invokes: f.invokes,
36
+ key: f.key,
37
+ kind: f.kind,
38
+ ...f.meta === void 0 ? {} : { meta: f.meta },
39
+ on: f.on,
40
+ onDone: f.onDone,
41
+ ...f.output === void 0 ? {} : { output: f.output },
42
+ ...f.parent === void 0 ? {} : { parent: f.parent },
43
+ path: f.path,
44
+ resources: f.resources,
45
+ tags: f.tags
46
+ };
47
+ });
48
+ for (const f of a)
49
+ St(f, r, a);
50
+ const u = t.version ?? 1;
51
+ $t(a);
52
+ const h = Object.freeze([...s]), c = new Map(h.map((f, w) => [f, w])), d = Object.freeze(a.map((f) => Object.freeze(Et(a, f.id)))), l = gt(a), y = l && h.length <= Math.floor(
53
+ mt / a.length
54
+ ) ? jt(a, c, h.length) : void 0, v = Object.freeze({
55
+ ancestry: d,
56
+ context: It(e.context),
57
+ eventIndexes: c,
58
+ eventTypes: h,
59
+ flat: l,
60
+ ...y === void 0 ? {} : { flatDispatch: y },
61
+ id: t.id,
62
+ initial: i,
63
+ migrations: At(t.migrations, u),
64
+ ...e.output === void 0 ? {} : { output: e.output },
65
+ pathToState: r,
66
+ rootChildren: Object.freeze(o),
67
+ states: Object.freeze(a),
68
+ version: u
69
+ }), m = Object.freeze({ id: t.id, version: u });
70
+ return Z.set(m, v), m;
71
+ }
72
+ function jt(t, e, n) {
73
+ const r = Array.from({ length: t.length * n }).fill(void 0);
74
+ for (const o of t) {
75
+ const i = o.id * n;
76
+ for (const [s, a] of o.on)
77
+ r[i + e.get(s)] = a;
78
+ }
79
+ return Object.freeze(r);
80
+ }
81
+ function Et(t, e) {
82
+ const n = [];
83
+ for (let r = e; r !== void 0; r = t[r].parent)
84
+ n.push(r);
85
+ return n;
86
+ }
87
+ function gt(t) {
88
+ return t.every((e) => {
89
+ if (e.parent !== void 0 || e.kind !== "atomic" && e.kind !== "final" || e.always.length !== 0 || e.onDone.length !== 0 || e.after.size !== 0 || e.entry.length !== 0 || e.exit.length !== 0 || e.resources.length !== 0 || e.invokes.length !== 0)
90
+ return !1;
91
+ for (const n of e.on.values())
92
+ if (n.some((r) => r.raise !== void 0))
93
+ return !1;
94
+ return !0;
95
+ });
96
+ }
97
+ function F(t) {
98
+ const e = Z.get(t);
99
+ if (e === void 0)
100
+ throw new TypeError("[Raph machine] Model was not created by defineMachine().");
101
+ return e;
102
+ }
103
+ function ct(t, e, n, r, o, i) {
104
+ if (typeof e != "object" || e === null || Array.isArray(e))
105
+ throw new TypeError(`[Raph machine] State "${t}" must be an object.`);
106
+ kt(e, t), Mt(t);
107
+ const s = n === void 0 ? "" : o[n].path, a = s ? `${s}.${t}` : t, u = o.length;
108
+ if (i.has(a))
109
+ throw new Error(`[Raph machine] Duplicate state path "${a}".`);
110
+ const h = zt(e), c = {
111
+ after: /* @__PURE__ */ new Map(),
112
+ always: Object.freeze([]),
113
+ children: [],
114
+ depth: r,
115
+ entry: P(e.entry),
116
+ exit: P(e.exit),
117
+ history: e.history ?? "shallow",
118
+ ...e.target === void 0 ? {} : { historyFallbackPath: e.target },
119
+ id: u,
120
+ ...e.initial === void 0 ? {} : { initialKey: e.initial },
121
+ invokes: Object.freeze([]),
122
+ key: t,
123
+ kind: h,
124
+ ...e.meta === void 0 ? {} : { meta: Object.freeze({ ...e.meta }) },
125
+ on: /* @__PURE__ */ new Map(),
126
+ onDone: Object.freeze([]),
127
+ ...e.output === void 0 ? {} : { output: e.output },
128
+ ...n === void 0 ? {} : { parent: n },
129
+ path: a,
130
+ resources: ot(e.resources),
131
+ tags: ot(e.tags)
132
+ };
133
+ o.push(c), i.set(a, u);
134
+ for (const [y, v] of Object.entries(e.states ?? {}))
135
+ c.children.push(ct(y, v, u, r + 1, o, i));
136
+ let d = 0;
137
+ for (const [y, v] of Object.entries(e.on ?? {})) {
138
+ if (!y.trim())
139
+ throw new Error(`[Raph machine] State "${a}" has an empty event type.`);
140
+ c.on.set(y, M(v, u, () => d++));
141
+ }
142
+ c.always = M(e.always, u, () => d++), c.onDone = M(e.onDone, u, () => d++);
143
+ for (const [y, v] of Object.entries(e.after ?? {})) {
144
+ if (!y.trim())
145
+ throw new Error(`[Raph machine] State "${a}" has an empty delay key.`);
146
+ c.after.set(y, M(v, u, () => d++));
147
+ }
148
+ const l = Pt(e.invoke);
149
+ return c.invokes = Object.freeze(l.map((y, v) => xt(
150
+ y,
151
+ c,
152
+ v,
153
+ () => d++
154
+ ))), Rt(c, o), u;
155
+ }
156
+ function xt(t, e, n, r) {
157
+ if (typeof t != "object" || t === null || Array.isArray(t))
158
+ throw new TypeError(`[Raph machine] Invoke in "${e.path}" must be an object.`);
159
+ const o = typeof t.src == "string" ? "task" : "machine", i = t.type ?? o, s = t.id ?? `${e.path}:invoke#${n + 1}`;
160
+ if (!s.trim())
161
+ throw new Error(`[Raph machine] Invoke id in "${e.path}" must not be empty.`);
162
+ if (i !== "machine" && i !== "task")
163
+ throw new Error(`[Raph machine] Invoke "${s}" has unsupported type "${String(i)}".`);
164
+ if (typeof t.src == "string") {
165
+ if (!t.src.trim())
166
+ throw new Error(`[Raph machine] Invoke "${s}" source must not be empty.`);
167
+ } else if (!Z.has(t.src))
168
+ throw new TypeError(`[Raph machine] Invoke "${s}" actor source must come from defineMachine().`);
169
+ if (i === "task" && typeof t.src != "string")
170
+ throw new TypeError(`[Raph machine] Task invoke "${s}" requires a string source.`);
171
+ return Object.freeze({
172
+ id: s,
173
+ ...t.input === void 0 ? {} : { input: t.input },
174
+ onDone: M(t.onDone, e.id, r),
175
+ onError: M(t.onError, e.id, r),
176
+ src: t.src,
177
+ type: i
178
+ });
179
+ }
180
+ function M(t, e, n) {
181
+ if (t === void 0)
182
+ return Object.freeze([]);
183
+ const r = Array.isArray(t) ? t : [t];
184
+ return Object.freeze(r.map((o) => {
185
+ const i = typeof o == "string" ? { target: o } : o;
186
+ if (i === null || typeof i != "object")
187
+ throw new TypeError("[Raph machine] Transition must be a target string or object.");
188
+ if (i.reenter === !0 && i.target === void 0)
189
+ throw new Error("[Raph machine] A targetless transition cannot declare reenter: true.");
190
+ if (i.reason !== void 0 && (typeof i.reason != "string" || !i.reason.trim()))
191
+ throw new Error("[Raph machine] Transition reason must be a non-empty string.");
192
+ return W(i.guard === void 0 ? [] : [i.guard], "guard"), W(P(i.reduce), "reducer"), W(P(i.emit), "output"), {
193
+ emit: P(i.emit),
194
+ ...i.guard === void 0 ? {} : { guard: i.guard },
195
+ index: n(),
196
+ ...i.raise === void 0 ? {} : { raise: i.raise },
197
+ ...i.reason === void 0 ? {} : { reason: i.reason },
198
+ reduce: P(i.reduce),
199
+ reenter: i.reenter === !0,
200
+ source: e,
201
+ ...i.target === void 0 ? {} : { target: i.target }
202
+ };
203
+ }));
204
+ }
205
+ function St(t, e, n) {
206
+ const r = [t.always, t.onDone, ...t.on.values(), ...t.after.values()];
207
+ for (const o of t.invokes)
208
+ r.push(o.onDone, o.onError);
209
+ for (const o of r)
210
+ for (const i of o) {
211
+ if (i.target !== void 0) {
212
+ const s = i.target, a = e.get(s);
213
+ if (a === void 0)
214
+ throw new Error(
215
+ `[Raph machine] Transition from "${t.path}" targets unknown state "${s}".`
216
+ );
217
+ i.target = a;
218
+ }
219
+ Object.freeze(i);
220
+ }
221
+ if (t.kind === "history" && t.historyFallback !== void 0) {
222
+ const o = [...e].find(([, s]) => s === t.historyFallback)?.[0], i = t.path.slice(0, t.path.lastIndexOf("."));
223
+ if (o === void 0 || o === t.path || t.historyFallback === void 0 || !o.startsWith(`${i}.`))
224
+ throw new Error(
225
+ `[Raph machine] History fallback for "${t.path}" must stay inside its parent state.`
226
+ );
227
+ if (n[t.historyFallback].kind === "history")
228
+ throw new Error(`[Raph machine] History fallback for "${t.path}" cannot target another history state.`);
229
+ }
230
+ }
231
+ function zt(t) {
232
+ if (t.type !== void 0) {
233
+ if (t.type !== "atomic" && t.type !== "compound" && t.type !== "final" && t.type !== "history" && t.type !== "parallel")
234
+ throw new TypeError(`[Raph machine] Unsupported state type "${String(t.type)}".`);
235
+ return t.type;
236
+ }
237
+ return t.states === void 0 ? "atomic" : "compound";
238
+ }
239
+ function Rt(t, e) {
240
+ if ((t.kind === "atomic" || t.kind === "final" || t.kind === "history") && t.children.length !== 0)
241
+ throw new Error(`[Raph machine] ${t.kind} state "${t.path}" cannot have children.`);
242
+ if (t.kind === "parallel" && t.initialKey !== void 0)
243
+ throw new Error(`[Raph machine] Parallel state "${t.path}" cannot declare initial.`);
244
+ if (t.kind === "parallel" && !t.children.some((n) => e[n].kind !== "history"))
245
+ throw new Error(`[Raph machine] Parallel state "${t.path}" requires at least one region.`);
246
+ if (t.kind === "history" && t.parent === void 0)
247
+ throw new Error(`[Raph machine] History state "${t.path}" must have a parent.`);
248
+ if (t.kind !== "history" && t.historyFallbackPath !== void 0)
249
+ throw new Error("[Raph machine] Only a history state may declare target as fallback.");
250
+ if ((t.kind === "atomic" || t.kind === "final" || t.kind === "history") && t.initialKey !== void 0)
251
+ throw new Error(`[Raph machine] ${t.kind} state "${t.path}" cannot declare initial.`);
252
+ if ((t.kind === "final" || t.kind === "history") && (t.on.size !== 0 || t.always.length !== 0 || t.after.size !== 0 || t.onDone.length !== 0 || t.invokes.length !== 0))
253
+ throw new Error(`[Raph machine] ${t.kind} state "${t.path}" cannot declare transitions or invokes.`);
254
+ }
255
+ function kt(t, e) {
256
+ if (V(t.states, `State "${e}" states`), V(t.on, `State "${e}" on`), V(t.after, `State "${e}" after`), t.history !== void 0 && t.history !== "deep" && t.history !== "shallow")
257
+ throw new TypeError(`[Raph machine] State "${e}" has unsupported history mode.`);
258
+ }
259
+ function V(t, e) {
260
+ if (t !== void 0 && (typeof t != "object" || t === null || Array.isArray(t)))
261
+ throw new TypeError(`[Raph machine] ${e} must be an object.`);
262
+ }
263
+ function Tt(t, e) {
264
+ if (typeof t != "string" || !t.trim())
265
+ throw new TypeError("[Raph machine] Model id must be a non-empty string.");
266
+ const n = e ?? 1;
267
+ if (!Number.isSafeInteger(n) || n < 1)
268
+ throw new TypeError("[Raph machine] Model version must be a positive safe integer.");
269
+ }
270
+ function $t(t) {
271
+ const e = /* @__PURE__ */ new Set();
272
+ for (const n of t)
273
+ for (const r of n.invokes) {
274
+ if (e.has(r.id))
275
+ throw new Error(`[Raph machine] Invoke id "${r.id}" is duplicated.`);
276
+ e.add(r.id);
277
+ }
278
+ }
279
+ function At(t, e) {
280
+ const n = /* @__PURE__ */ new Map();
281
+ for (const [r, o] of Object.entries(t ?? {})) {
282
+ const i = Number(r);
283
+ if (!Number.isSafeInteger(i) || i < 1 || i >= e)
284
+ throw new Error(`[Raph machine] Migration version "${r}" must be a positive integer below ${e}.`);
285
+ if (typeof o != "function")
286
+ throw new TypeError(`[Raph machine] Migration from version ${i} must be a function.`);
287
+ n.set(i, o);
288
+ }
289
+ return n;
290
+ }
291
+ function W(t, e) {
292
+ for (const n of t)
293
+ if (typeof n != "function" && (typeof n != "string" || !n.trim()))
294
+ throw new TypeError(`[Raph machine] ${e} reference must be a function or non-empty string.`);
295
+ }
296
+ function Mt(t) {
297
+ if (!t || t.includes("."))
298
+ throw new Error("[Raph machine] State keys must be non-empty and cannot contain dots.");
299
+ }
300
+ function P(t) {
301
+ return Object.freeze(t === void 0 ? [] : Array.isArray(t) ? [...t] : [t]);
302
+ }
303
+ function ot(t) {
304
+ if (t === void 0)
305
+ return Object.freeze([]);
306
+ const e = typeof t == "string" ? [t] : [...t];
307
+ for (const n of e)
308
+ if (typeof n != "string" || !n.trim())
309
+ throw new TypeError("[Raph machine] Tags and resource names must be non-empty strings.");
310
+ return Object.freeze(e);
311
+ }
312
+ function Pt(t) {
313
+ return t === void 0 ? [] : Array.isArray(t) ? t : [t];
314
+ }
315
+ function It(t) {
316
+ return Array.isArray(t) ? Object.freeze([...t]) : typeof t == "object" && t !== null && Object.getPrototypeOf(t) === Object.prototype ? Object.freeze({ ...t }) : t;
317
+ }
318
+ const T = Symbol("RaphMachineSnapshot"), it = /* @__PURE__ */ new WeakMap();
319
+ function ut(t) {
320
+ const e = Object.getPrototypeOf(t);
321
+ return e !== null && Object.hasOwn(e, T) ? e : void 0;
322
+ }
323
+ function ft(t) {
324
+ return t[T];
325
+ }
326
+ function L(t, e, n) {
327
+ const r = n ?? Object.freeze({ [T]: e }), o = Object.create(r);
328
+ return o.active = t.active, o.context = t.context, o.history = t.history, t.output !== void 0 && (o.output = t.output), o.revision = t.revision, o.status = t.status, Object.freeze(o);
329
+ }
330
+ function J(t, e, n, r, o) {
331
+ let i = t?.flatPrototypes;
332
+ i === void 0 && (n === void 0 ? (i = it.get(r), i === void 0 && (i = [], it.set(r, i))) : i = []);
333
+ const s = i[e];
334
+ if (s !== void 0)
335
+ return s;
336
+ const a = Object.freeze({
337
+ [T]: Object.freeze({
338
+ active: Object.freeze([e]),
339
+ flatPrototypes: i,
340
+ input: n,
341
+ model: r,
342
+ publicActive: Object.freeze([o])
343
+ })
344
+ });
345
+ return i[e] = a, a;
346
+ }
347
+ const Dt = 100, G = Object.freeze({ type: "@@init" }), ht = Object.freeze([]), q = Object.freeze({}), K = Object.freeze([]), D = Object.freeze([]), Ft = Object.freeze([]), Q = Object.freeze({
348
+ entered: K,
349
+ entryOutputs: D,
350
+ event: G,
351
+ exited: K,
352
+ exitOutputs: D,
353
+ transitionOutputs: D
354
+ });
355
+ function Qt(t, e) {
356
+ return _t(t, e, void 0).snapshot;
357
+ }
358
+ function te(t, e, n, r, o) {
359
+ const i = ft(e);
360
+ if (i !== void 0 && i.model !== t)
361
+ throw new TypeError("[Raph machine] Snapshot belongs to another model.");
362
+ const s = F(t);
363
+ return tt(
364
+ t,
365
+ e,
366
+ n,
367
+ r,
368
+ i?.input,
369
+ o,
370
+ s
371
+ ).snapshot;
372
+ }
373
+ function Ct(t, e) {
374
+ if (ut(e)?.[T].model !== t)
375
+ throw new TypeError("[Raph machine] Snapshot belongs to another model or was not created by Raph.");
376
+ }
377
+ function ee(t, e) {
378
+ if (Ct(
379
+ t,
380
+ e
381
+ ), e.status === "stopped")
382
+ return e;
383
+ const n = ut(e);
384
+ return L({
385
+ active: e.active,
386
+ context: e.context,
387
+ history: e.history,
388
+ ...e.output === void 0 ? {} : { output: e.output },
389
+ revision: e.revision + 1,
390
+ status: "stopped"
391
+ }, n[T], n);
392
+ }
393
+ function _t(t, e, n, r) {
394
+ const o = F(t), i = o.context, s = typeof i == "function" ? i(e) : i;
395
+ if (o.flat) {
396
+ const h = o.states[o.initial], c = h.kind === "final" ? "done" : "active", d = c === "done" ? h.output ?? o.output : void 0, l = typeof d == "function" ? d({ context: s, event: G, input: e }) : d, y = J(
397
+ void 0,
398
+ o.initial,
399
+ e,
400
+ t,
401
+ h.path
402
+ ), v = y[T], m = L({
403
+ active: v.publicActive,
404
+ context: rt(s),
405
+ history: q,
406
+ ...l === void 0 ? {} : { output: l },
407
+ revision: 0,
408
+ status: c
409
+ }, v, y);
410
+ return { changed: !1, plan: Q, snapshot: m };
411
+ }
412
+ const a = $(o, o.initial, q), u = C(t, e, {
413
+ active: a,
414
+ context: s,
415
+ history: q
416
+ }, 0, "active", void 0, o);
417
+ return tt(t, u, G, n, e, r, o, !0);
418
+ }
419
+ function tt(t, e, n, r, o, i, s, a = !1, u = ht) {
420
+ return Ot(n), Nt(
421
+ t,
422
+ e,
423
+ n,
424
+ r,
425
+ o,
426
+ i,
427
+ s,
428
+ void 0,
429
+ a,
430
+ u
431
+ );
432
+ }
433
+ function Nt(t, e, n, r, o, i, s, a, u = !1, h = ht) {
434
+ const c = s ?? F(t);
435
+ if (e.status !== "active")
436
+ return I(e);
437
+ if (!u && c.flat) {
438
+ const E = a ?? c.eventIndexes.get(n.type);
439
+ return E === void 0 ? I(e) : Ht(t, c, e, n, E, r, o, i);
440
+ }
441
+ if (!u && !Gt(c, e.active, n.type))
442
+ return I(e);
443
+ const d = {
444
+ active: e.active.map((E) => N(c, E)),
445
+ context: e.context,
446
+ history: { ...e.history }
447
+ }, l = r, y = /* @__PURE__ */ new Set(), v = /* @__PURE__ */ new Set(), m = [], f = [], w = [], b = [...h];
448
+ let g = !1;
449
+ if (!u) {
450
+ const E = st(c, d, n, l, o, i);
451
+ at(E, y, v, m, f, w), g = E.changed, b.push(...E.raised);
452
+ }
453
+ const p = /* @__PURE__ */ new Set();
454
+ let O = 0;
455
+ for (; ; ) {
456
+ const E = b.shift();
457
+ let x;
458
+ if (E !== void 0 ? x = st(c, d, E, l, o, i) : (x = Kt(c, d, n, l, o, i), x.changed || (x = Lt(
459
+ c,
460
+ d,
461
+ n,
462
+ l,
463
+ o,
464
+ p,
465
+ i
466
+ ))), !x.changed) {
467
+ if (b.length !== 0)
468
+ continue;
469
+ break;
470
+ }
471
+ if (++O > Dt)
472
+ throw new Error("[Raph machine] Macrostep exceeded 100 stabilization microsteps.");
473
+ for (const U of x.exited)
474
+ p.delete(U);
475
+ for (const U of x.entered)
476
+ p.delete(U);
477
+ g = !0, at(x, y, v, m, f, w), b.push(...x.raised);
478
+ }
479
+ const R = nt(c, d.active) ? "done" : "active";
480
+ if (!g && R === e.status)
481
+ return I(e);
482
+ const z = R === "done" ? yt(c, d, n, o) : void 0, k = C(
483
+ t,
484
+ o,
485
+ d,
486
+ e.revision + 1,
487
+ R,
488
+ z,
489
+ c
490
+ ), j = new Set(_(c, d.active));
491
+ return Object.freeze({
492
+ changed: !0,
493
+ plan: Object.freeze({
494
+ entered: Object.freeze([...v].filter((E) => j.has(E)).sort((E, x) => c.states[E].depth - c.states[x].depth)),
495
+ entryOutputs: Object.freeze(w),
496
+ event: n,
497
+ exited: Object.freeze([...y].sort((E, x) => c.states[x].depth - c.states[E].depth)),
498
+ exitOutputs: Object.freeze(m),
499
+ transitionOutputs: Object.freeze(f)
500
+ }),
501
+ snapshot: k
502
+ });
503
+ }
504
+ function Ht(t, e, n, r, o, i, s, a) {
505
+ const u = ft(n), h = u?.active[0] ?? N(e, n.active[0]), c = e.flatDispatch === void 0 ? e.states[h].on.get(r.type) : e.flatDispatch[h * e.eventTypes.length + o], d = H(
506
+ c ?? Ft,
507
+ n.context,
508
+ r,
509
+ i,
510
+ s,
511
+ a
512
+ );
513
+ if (d === void 0)
514
+ return I(n);
515
+ dt(e, d, a);
516
+ let l = n.context;
517
+ for (const p of d.reduce)
518
+ l = vt(
519
+ p,
520
+ i
521
+ )({ context: l, event: r, input: s });
522
+ const y = d.target ?? h, v = e.states[y], m = v.kind === "final" ? "done" : "active", f = m === "done" ? v.output ?? e.output : void 0, w = typeof f == "function" ? f({ context: l, event: r, input: s }) : f, b = J(u, y, s, t, v.path), g = L({
523
+ active: y === h ? n.active : b[T].publicActive,
524
+ context: l === n.context ? n.context : rt(l),
525
+ history: n.history,
526
+ ...w === void 0 ? {} : { output: w },
527
+ revision: n.revision + 1,
528
+ status: m
529
+ }, b[T], b);
530
+ return {
531
+ changed: !0,
532
+ plan: d.emit.length === 0 ? Q : {
533
+ entered: K,
534
+ entryOutputs: D,
535
+ event: r,
536
+ exited: K,
537
+ exitOutputs: D,
538
+ transitionOutputRefs: d.emit,
539
+ transitionOutputs: D
540
+ },
541
+ snapshot: g
542
+ };
543
+ }
544
+ function ne(t, e, n, r, o, i, s) {
545
+ const a = F(t), u = H(
546
+ r,
547
+ e.context,
548
+ n,
549
+ o,
550
+ i,
551
+ s
552
+ );
553
+ if (u === void 0)
554
+ return I(e);
555
+ const h = {
556
+ active: e.active.map((b) => N(a, b)),
557
+ context: e.context,
558
+ history: { ...e.history }
559
+ }, c = Y(
560
+ a,
561
+ h,
562
+ [u],
563
+ n,
564
+ o,
565
+ i,
566
+ s
567
+ ), d = C(
568
+ t,
569
+ i,
570
+ h,
571
+ e.revision,
572
+ "active",
573
+ void 0,
574
+ a
575
+ ), l = tt(
576
+ t,
577
+ d,
578
+ n,
579
+ o,
580
+ i,
581
+ s,
582
+ a,
583
+ !0,
584
+ c.raised
585
+ ), y = l.changed ? {
586
+ active: l.snapshot.active.map((b) => N(a, b)),
587
+ context: l.snapshot.context,
588
+ history: { ...l.snapshot.history }
589
+ } : h, v = l.changed ? l.snapshot.status : nt(a, y.active) ? "done" : "active", m = v === "done" ? l.snapshot.output ?? yt(a, y, n, i) : void 0, f = C(
590
+ t,
591
+ i,
592
+ y,
593
+ e.revision + 1,
594
+ v,
595
+ m,
596
+ a
597
+ ), w = new Set(_(a, y.active));
598
+ return Object.freeze({
599
+ changed: !0,
600
+ plan: Object.freeze({
601
+ entered: Object.freeze(A([...c.entered, ...l.plan.entered]).filter((b) => w.has(b)).sort((b, g) => a.states[b].depth - a.states[g].depth)),
602
+ entryOutputs: Object.freeze([...c.entryOutputs, ...l.plan.entryOutputs]),
603
+ event: n,
604
+ exited: Object.freeze(A([...c.exited, ...l.plan.exited]).sort((b, g) => a.states[g].depth - a.states[b].depth)),
605
+ exitOutputs: Object.freeze([...c.exitOutputs, ...l.plan.exitOutputs]),
606
+ transitionOutputs: Object.freeze([
607
+ ...c.transitionOutputs,
608
+ ...l.plan.transitionOutputs
609
+ ])
610
+ }),
611
+ snapshot: f
612
+ });
613
+ }
614
+ function re(t, e, n) {
615
+ const r = F(t);
616
+ if (e.status !== "active" && e.status !== "done" && e.status !== "stopped")
617
+ throw new Error(`[Raph machine] Unknown persisted status "${String(e.status)}".`);
618
+ if (!Number.isSafeInteger(e.revision) || e.revision < 0)
619
+ throw new Error("[Raph machine] Persisted revision must be a non-negative safe integer.");
620
+ const o = e.active.map((i) => N(r, i));
621
+ return qt(r, o, e.history, e.status), C(
622
+ t,
623
+ n,
624
+ {
625
+ active: A(o),
626
+ context: e.context,
627
+ history: { ...e.history }
628
+ },
629
+ e.revision,
630
+ e.status,
631
+ e.output,
632
+ r
633
+ );
634
+ }
635
+ function qt(t, e, n, r) {
636
+ if (e.length === 0 || new Set(e).size !== e.length)
637
+ throw new Error("[Raph machine] Persisted active configuration must contain unique leaf states.");
638
+ for (const s of e) {
639
+ const a = t.states[s].kind;
640
+ if (a !== "atomic" && a !== "final")
641
+ throw new Error(`[Raph machine] Persisted active state "${t.states[s].path}" is not a leaf.`);
642
+ }
643
+ if (new Set(e.map((s) => B(t, s))).size !== 1)
644
+ throw new Error("[Raph machine] Persisted configuration spans multiple root states.");
645
+ for (const s of t.states) {
646
+ if (s.kind !== "compound" && s.kind !== "parallel")
647
+ continue;
648
+ const a = s.children.filter((h) => t.states[h].kind !== "history" && e.some((c) => c === h || S(t, h, c)));
649
+ if (e.some((h) => S(t, s.id, h))) {
650
+ if (s.kind === "compound" && a.length !== 1)
651
+ throw new Error(`[Raph machine] Compound state "${s.path}" requires one active child.`);
652
+ if (s.kind === "parallel" && a.length !== s.children.filter((h) => t.states[h].kind !== "history").length)
653
+ throw new Error(`[Raph machine] Parallel state "${s.path}" requires every region.`);
654
+ }
655
+ }
656
+ const i = nt(t, e);
657
+ if (r === "done" && !i)
658
+ throw new Error("[Raph machine] Done persisted snapshot is not in a final configuration.");
659
+ if (r === "active" && i)
660
+ throw new Error("[Raph machine] Active persisted snapshot is already final.");
661
+ for (const [s, a] of Object.entries(n)) {
662
+ const u = t.pathToState.get(s);
663
+ if (u === void 0)
664
+ throw new Error(`[Raph machine] Persisted history has unknown owner "${s}".`);
665
+ const h = t.states[u];
666
+ if (h.kind !== "compound" && h.kind !== "parallel")
667
+ throw new Error(`[Raph machine] Persisted history owner "${s}" cannot own history.`);
668
+ if (!Array.isArray(a))
669
+ throw new TypeError(`[Raph machine] Persisted history for "${s}" must be an array.`);
670
+ for (const c of a) {
671
+ const d = t.pathToState.get(c);
672
+ if (d === void 0 || !S(t, u, d))
673
+ throw new Error(`[Raph machine] Persisted history path "${c}" is invalid for "${s}".`);
674
+ }
675
+ }
676
+ }
677
+ function st(t, e, n, r, o, i) {
678
+ const s = [], a = /* @__PURE__ */ new Set(), u = [...e.active].sort((h, c) => t.states[c].depth - t.states[h].depth || h - c);
679
+ for (const h of u)
680
+ for (let c = h; c !== void 0 && !a.has(c); c = t.states[c].parent) {
681
+ const d = t.states[c].on.get(n.type);
682
+ if (d === void 0)
683
+ continue;
684
+ const l = H(d, e.context, n, r, o, i);
685
+ if (l !== void 0) {
686
+ a.add(c), s.push(l);
687
+ break;
688
+ }
689
+ }
690
+ return Y(
691
+ t,
692
+ e,
693
+ pt(t, s),
694
+ n,
695
+ r,
696
+ o,
697
+ i
698
+ );
699
+ }
700
+ function Kt(t, e, n, r, o, i) {
701
+ const s = [], a = /* @__PURE__ */ new Set();
702
+ for (const u of e.active)
703
+ for (let h = u; h !== void 0; h = t.states[h].parent) {
704
+ if (a.has(h))
705
+ continue;
706
+ a.add(h);
707
+ const c = H(
708
+ t.states[h].always,
709
+ e.context,
710
+ n,
711
+ r,
712
+ o,
713
+ i
714
+ );
715
+ c !== void 0 && s.push(c);
716
+ }
717
+ return Y(
718
+ t,
719
+ e,
720
+ pt(t, s),
721
+ n,
722
+ r,
723
+ o,
724
+ i
725
+ );
726
+ }
727
+ function Lt(t, e, n, r, o, i, s) {
728
+ const a = _(t, e.active).filter((u) => !i.has(u) && et(t, u, e.active)).sort((u, h) => t.states[h].depth - t.states[u].depth);
729
+ for (const u of a) {
730
+ i.add(u);
731
+ const h = H(
732
+ t.states[u].onDone,
733
+ e.context,
734
+ n,
735
+ r,
736
+ o,
737
+ s
738
+ );
739
+ if (h !== void 0)
740
+ return Y(t, e, [h], n, r, o, s);
741
+ }
742
+ return bt();
743
+ }
744
+ function Y(t, e, n, r, o, i, s) {
745
+ if (n.length === 0)
746
+ return bt();
747
+ const a = [...e.active], u = new Set(_(t, a)), h = /* @__PURE__ */ new Set(), c = /* @__PURE__ */ new Set(), d = /* @__PURE__ */ new Set(), l = [], y = [];
748
+ let v = !1;
749
+ for (const p of n) {
750
+ dt(t, p, s);
751
+ for (const j of p.reduce)
752
+ e.context = vt(j, o)({
753
+ context: e.context,
754
+ event: r,
755
+ input: i
756
+ }), v = !0;
757
+ for (const j of p.emit)
758
+ l.push({ event: r, ref: j });
759
+ if (p.emit.length !== 0 && (v = !0), p.raise !== void 0) {
760
+ const j = typeof p.raise == "function" ? p.raise({ context: e.context, event: r, input: i }) : p.raise;
761
+ Ot(j), y.push(j), v = !0;
762
+ }
763
+ if (p.target === void 0) {
764
+ v = !0;
765
+ continue;
766
+ }
767
+ const O = p.source, R = p.target;
768
+ if (O === R && !p.reenter) {
769
+ v = !0;
770
+ continue;
771
+ }
772
+ const z = X(t, O, R);
773
+ if (p.reenter) {
774
+ d.add(z);
775
+ for (const j of u)
776
+ (j === z || S(t, z, j)) && h.add(j);
777
+ }
778
+ e.active = e.active.filter((j) => j !== z && !S(t, z, j));
779
+ const k = lt(t, R, e.history);
780
+ e.active.push(...k), v = !0;
781
+ }
782
+ e.active = Bt(
783
+ t,
784
+ A(e.active),
785
+ e.history
786
+ );
787
+ const m = new Set(_(t, e.active));
788
+ for (const p of d)
789
+ for (const O of m)
790
+ (O === p || S(t, p, O)) && c.add(O);
791
+ const f = [...u].filter((p) => !m.has(p) || h.has(p)).sort((p, O) => t.states[O].depth - t.states[p].depth), w = [...m].filter((p) => !u.has(p) || c.has(p)).sort((p, O) => t.states[p].depth - t.states[O].depth);
792
+ for (const p of f) {
793
+ const O = t.states[p];
794
+ if (O.kind === "compound" || O.kind === "parallel") {
795
+ const R = a.filter((z) => S(t, p, z));
796
+ if (R.length !== 0) {
797
+ const z = O.children.map((k) => t.states[k]).find((k) => k.kind === "history")?.history ?? O.history;
798
+ e.history[O.path] = Object.freeze(R.map((k) => {
799
+ if (z === "deep")
800
+ return t.states[k].path;
801
+ const j = wt(t, p, k);
802
+ return t.states[j].path;
803
+ }));
804
+ }
805
+ }
806
+ }
807
+ const b = f.flatMap((p) => t.states[p].exit.map((O) => ({ event: r, ref: O }))), g = w.flatMap((p) => t.states[p].entry.map((O) => ({ event: r, ref: O })));
808
+ return Object.freeze({
809
+ changed: v || f.length !== 0 || w.length !== 0,
810
+ entered: Object.freeze(w),
811
+ entryOutputs: Object.freeze(g),
812
+ exited: Object.freeze(f),
813
+ exitOutputs: Object.freeze(b),
814
+ raised: Object.freeze(y),
815
+ transitionOutputs: Object.freeze(l)
816
+ });
817
+ }
818
+ function dt(t, e, n) {
819
+ n?.transition !== void 0 && n.transition({
820
+ ...e.reason === void 0 ? {} : { reason: e.reason },
821
+ source: t.states[e.source].path,
822
+ ...e.target === void 0 ? {} : { target: t.states[e.target].path }
823
+ });
824
+ }
825
+ function H(t, e, n, r, o, i) {
826
+ for (const s of t) {
827
+ if (s.guard === void 0)
828
+ return s;
829
+ const a = Ut(s.guard, r), u = a({ context: e, event: n, input: o });
830
+ if (i?.guard?.(
831
+ typeof s.guard == "string" ? s.guard : a.name || "inline",
832
+ u,
833
+ Zt(e) ? e : void 0
834
+ ), u)
835
+ return s;
836
+ }
837
+ }
838
+ function pt(t, e) {
839
+ const n = [...e].sort((o, i) => t.states[i.source].depth - t.states[o.source].depth || o.source - i.source || o.index - i.index), r = [];
840
+ for (const o of n)
841
+ r.some((s) => Yt(t, o, s)) || r.push(o);
842
+ return r;
843
+ }
844
+ function Yt(t, e, n) {
845
+ const r = X(t, e.source, e.target ?? e.source), o = X(t, n.source, n.target ?? n.source);
846
+ return e.source === n.source || S(t, e.source, n.source) || S(t, n.source, e.source) || r === o || S(t, r, o) || S(t, o, r);
847
+ }
848
+ function X(t, e, n) {
849
+ if (e === n || S(t, e, n))
850
+ return e;
851
+ const r = Vt(t, e, n);
852
+ return r !== void 0 && t.states[r].kind === "parallel" ? r : r === void 0 ? B(t, e) : wt(t, r, e);
853
+ }
854
+ function lt(t, e, n) {
855
+ const r = t.states[e];
856
+ if (r.kind !== "history")
857
+ return $(t, e, n);
858
+ const o = r.parent, i = n[t.states[o].path];
859
+ if (i !== void 0 && i.length !== 0) {
860
+ const s = [];
861
+ for (const a of i) {
862
+ const u = t.pathToState.get(a);
863
+ u !== void 0 && s.push(...r.history === "deep" ? $(t, u, n) : $(t, u, {}));
864
+ }
865
+ if (s.length !== 0)
866
+ return A(s);
867
+ }
868
+ return r.historyFallback !== void 0 ? $(t, r.historyFallback, n) : $(t, o, {});
869
+ }
870
+ function Bt(t, e, n) {
871
+ const r = [...e];
872
+ for (let o = 0; o < r.length; o++)
873
+ for (const i of t.ancestry[r[o]].slice(1)) {
874
+ const s = t.states[i];
875
+ if (s.kind === "parallel")
876
+ for (const a of s.children)
877
+ t.states[a].kind !== "history" && (r.some((u) => u === a || S(t, a, u)) || r.push(...$(t, a, n)));
878
+ }
879
+ return A(r);
880
+ }
881
+ function $(t, e, n) {
882
+ const r = t.states[e];
883
+ if (r.kind === "history")
884
+ return lt(t, e, n);
885
+ if (r.kind === "atomic" || r.kind === "final")
886
+ return [e];
887
+ if (r.kind === "compound")
888
+ return $(t, r.initial, n);
889
+ const o = [];
890
+ for (const i of r.children)
891
+ t.states[i].kind !== "history" && o.push(...$(t, i, n));
892
+ return A(o);
893
+ }
894
+ function et(t, e, n) {
895
+ const r = t.states[e];
896
+ return r.kind === "final" ? n.includes(e) : r.kind === "compound" ? r.children.some((o) => t.states[o].kind === "final" && n.includes(o)) : r.kind === "parallel" ? r.children.filter((o) => t.states[o].kind !== "history").every((o) => et(t, o, n)) : !1;
897
+ }
898
+ function nt(t, e) {
899
+ const n = Wt(t, e[0]);
900
+ return n === void 0 ? !1 : et(t, n, e);
901
+ }
902
+ function yt(t, e, n, r) {
903
+ const o = B(t, e.active[0]), i = t.states[o], s = i.kind === "compound" ? e.active.find((u) => t.states[u].parent === o && t.states[u].kind === "final") : i.kind === "final" ? o : void 0, a = s === void 0 ? t.output : t.states[s].output ?? t.output;
904
+ return typeof a == "function" ? a({ context: e.context, event: n, input: r }) : a;
905
+ }
906
+ function Ut(t, e) {
907
+ if (typeof t == "function")
908
+ return t;
909
+ const n = e?.guards?.[t];
910
+ if (n === void 0)
911
+ throw new Error(`[Raph machine] Guard binding "${t}" is not defined.`);
912
+ return n;
913
+ }
914
+ function vt(t, e) {
915
+ if (typeof t == "function")
916
+ return t;
917
+ const n = e?.reducers?.[t];
918
+ if (n === void 0)
919
+ throw new Error(`[Raph machine] Reducer binding "${t}" is not defined.`);
920
+ return n;
921
+ }
922
+ function C(t, e, n, r, o, i, s) {
923
+ const a = s ?? F(t), u = a.flat && n.active.length === 1 ? n.active[0] : void 0, h = u === void 0 ? void 0 : J(void 0, u, e, t, a.states[u].path), c = h === void 0 ? Object.freeze([...n.active]) : void 0, d = h?.[T] ?? Object.freeze({
924
+ active: c,
925
+ input: e,
926
+ model: t
927
+ });
928
+ return L({
929
+ active: d.publicActive ?? Object.freeze(c.map((l) => a.states[l].path)),
930
+ context: rt(n.context),
931
+ history: a.flat ? q : Xt(n.history),
932
+ ...i === void 0 ? {} : { output: i },
933
+ revision: r,
934
+ status: o
935
+ }, d, h);
936
+ }
937
+ function I(t, e) {
938
+ return {
939
+ changed: !1,
940
+ plan: Q,
941
+ snapshot: t
942
+ };
943
+ }
944
+ function bt() {
945
+ return Object.freeze({
946
+ changed: !1,
947
+ entered: Object.freeze([]),
948
+ entryOutputs: Object.freeze([]),
949
+ exited: Object.freeze([]),
950
+ exitOutputs: Object.freeze([]),
951
+ raised: Object.freeze([]),
952
+ transitionOutputs: Object.freeze([])
953
+ });
954
+ }
955
+ function at(t, e, n, r, o, i) {
956
+ for (const s of t.exited)
957
+ e.add(s);
958
+ for (const s of t.entered)
959
+ n.add(s);
960
+ r.push(...t.exitOutputs), o.push(...t.transitionOutputs), i.push(...t.entryOutputs);
961
+ }
962
+ function _(t, e) {
963
+ const n = /* @__PURE__ */ new Set();
964
+ for (const r of e)
965
+ for (const o of t.ancestry[r])
966
+ n.add(o);
967
+ return [...n];
968
+ }
969
+ function Vt(t, e, n) {
970
+ const r = new Set(t.ancestry[e]);
971
+ for (const o of t.ancestry[n])
972
+ if (r.has(o))
973
+ return o;
974
+ }
975
+ function S(t, e, n) {
976
+ const r = t.ancestry[n];
977
+ return r.length > 1 && r.includes(e, 1);
978
+ }
979
+ function wt(t, e, n) {
980
+ let r = n;
981
+ for (; t.states[r].parent !== e; ) {
982
+ const o = t.states[r].parent;
983
+ if (o === void 0)
984
+ return r;
985
+ r = o;
986
+ }
987
+ return r;
988
+ }
989
+ function B(t, e) {
990
+ return t.ancestry[e].at(-1);
991
+ }
992
+ function Wt(t, e) {
993
+ return e === void 0 ? void 0 : B(t, e);
994
+ }
995
+ function N(t, e) {
996
+ const n = t.pathToState.get(e);
997
+ if (n === void 0)
998
+ throw new Error(`[Raph machine] Snapshot contains unknown state "${e}".`);
999
+ return n;
1000
+ }
1001
+ function Gt(t, e, n) {
1002
+ if (!t.eventIndexes.has(n))
1003
+ return !1;
1004
+ for (const r of e) {
1005
+ const o = t.pathToState.get(r);
1006
+ if (o !== void 0) {
1007
+ for (const i of t.ancestry[o])
1008
+ if (t.states[i].on.has(n))
1009
+ return !0;
1010
+ }
1011
+ }
1012
+ return !1;
1013
+ }
1014
+ function A(t) {
1015
+ return [...new Set(t)].sort((e, n) => e - n);
1016
+ }
1017
+ function rt(t) {
1018
+ return typeof t == "object" && t !== null ? Object.freeze(t) : t;
1019
+ }
1020
+ function Xt(t) {
1021
+ const e = {};
1022
+ for (const [n, r] of Object.entries(t))
1023
+ e[n] = Object.freeze([...r]);
1024
+ return Object.freeze(e);
1025
+ }
1026
+ function Ot(t) {
1027
+ if (typeof t != "object" || t === null || typeof t.type != "string" || !t.type)
1028
+ throw new TypeError("[Raph machine] Event must contain a non-empty string type.");
1029
+ }
1030
+ function Zt(t) {
1031
+ return typeof t == "object" && t !== null && !Array.isArray(t);
1032
+ }
1033
+ export {
1034
+ Qt as a,
1035
+ Ct as b,
1036
+ _t as c,
1037
+ ne as d,
1038
+ Jt as e,
1039
+ F as g,
1040
+ re as h,
1041
+ Nt as r,
1042
+ ee as s,
1043
+ te as t
1044
+ };