@raphy-js/raph 1.0.0-dev.18

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