UNPKG

@documedis-components/prescription-sign

Version:

React component for signing electronic prescriptions with HIN authentication

1,884 lines 120 kB
import { jsxs as Q, jsx as H } from "react/jsx-runtime"; import de, { useLayoutEffect as Rt, useCallback as ie, useEffect as se, useState as Ot } from "react"; var wt = Rt; function Ct() { if (typeof globalThis < "u") return globalThis; if (typeof self < "u") return self; if (typeof window < "u") return window; if (typeof global < "u") return global; } function Mt() { const t = Ct(); if (t.__xstate__) return t.__xstate__; } const Nt = (t) => { if (typeof window > "u") return; const e = Mt(); e && e.register(t); }; class Ue { constructor(e) { this._process = e, this._active = !1, this._current = null, this._last = null; } start() { this._active = !0, this.flush(); } clear() { this._current && (this._current.next = null, this._last = this._current); } enqueue(e) { const n = { value: e, next: null }; if (this._current) { this._last.next = n, this._last = n; return; } this._current = n, this._last = n, this._active && this.flush(); } flush() { for (; this._current; ) { const e = this._current; this._process(e.value), this._current = e.next; } this._last = null; } } const st = ".", kt = "", rt = "", Lt = "#", Dt = "*", it = "xstate.init", Y = "xstate.stop"; function bt(t, e) { return { type: `xstate.after.${t}.${e}` }; } function Te(t, e) { return { type: `xstate.done.state.${t}`, output: e }; } function Pt(t, e) { return { type: `xstate.done.actor.${t}`, output: e, actorId: t }; } function xt(t, e) { return { type: `xstate.error.actor.${t}`, error: e, actorId: t }; } function ot(t) { return { type: it, input: t }; } function M(t) { setTimeout(() => { throw t; }); } const Ut = typeof Symbol == "function" && Symbol.observable || "@@observable"; function at(t, e) { const n = He(t), s = He(e); return typeof s == "string" ? typeof n == "string" ? s === n : !1 : typeof n == "string" ? n in s : Object.keys(n).every((r) => r in s ? at(n[r], s[r]) : !1); } function we(t) { if (ut(t)) return t; const e = []; let n = ""; for (let s = 0; s < t.length; s++) { switch (t.charCodeAt(s)) { // \ case 92: n += t[s + 1], s++; continue; // . case 46: e.push(n), n = ""; continue; } n += t[s]; } return e.push(n), e; } function He(t) { if (Sn(t)) return t.value; if (typeof t != "string") return t; const e = we(t); return Ht(e); } function Ht(t) { if (t.length === 1) return t[0]; const e = {}; let n = e; for (let s = 0; s < t.length - 1; s++) if (s === t.length - 2) n[t[s]] = t[s + 1]; else { const r = n; n = {}, r[t[s]] = n; } return e; } function $e(t, e) { const n = {}, s = Object.keys(t); for (let r = 0; r < s.length; r++) { const i = s[r]; n[i] = e(t[i], i, t, r); } return n; } function ct(t) { return ut(t) ? t : [t]; } function N(t) { return t === void 0 ? [] : ct(t); } function Ae(t, e, n, s) { return typeof t == "function" ? t({ context: e, event: n, self: s }) : t; } function ut(t) { return Array.isArray(t); } function $t(t) { return t.type.startsWith("xstate.error.actor"); } function G(t) { return ct(t).map((e) => typeof e > "u" || typeof e == "string" ? { target: e } : e); } function dt(t) { if (!(t === void 0 || t === kt)) return N(t); } function ve(t, e, n) { var i, o, a; const s = typeof t == "object", r = s ? t : void 0; return { next: (i = s ? t.next : t) == null ? void 0 : i.bind(r), error: (o = s ? t.error : e) == null ? void 0 : o.bind(r), complete: (a = s ? t.complete : n) == null ? void 0 : a.bind(r) }; } function Ge(t, e) { return `${e}.${t}`; } function Ce(t, e) { const n = e.match(/^xstate\.invoke\.(\d+)\.(.*)/); if (!n) return t.implementations.actors[e]; const [, s, r] = n, o = t.getStateNodeById(r).config.invoke; return (Array.isArray(o) ? o[s] : o).src; } function Ve(t, e) { return `${t.sessionId}.${e}`; } let Gt = 0; function Vt(t, e) { const n = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new WeakMap(), i = /* @__PURE__ */ new Set(), o = {}, { clock: a, logger: c } = e, f = { schedule: (d, l, u, g, y = Math.random().toString(36).slice(2)) => { const _ = { source: d, target: l, event: u, delay: g, id: y, startedAt: Date.now() }, T = Ve(d, y); h._snapshot._scheduledEvents[T] = _; const v = a.setTimeout(() => { delete o[T], delete h._snapshot._scheduledEvents[T], h._relay(d, l, u); }, g); o[T] = v; }, cancel: (d, l) => { const u = Ve(d, l), g = o[u]; delete o[u], delete h._snapshot._scheduledEvents[u], g !== void 0 && a.clearTimeout(g); }, cancelAll: (d) => { for (const l in h._snapshot._scheduledEvents) { const u = h._snapshot._scheduledEvents[l]; u.source === d && f.cancel(d, u.id); } } }, p = (d) => { if (!i.size) return; const l = { ...d, rootId: t.sessionId }; i.forEach((u) => { var g; return (g = u.next) == null ? void 0 : g.call(u, l); }); }, h = { _snapshot: { _scheduledEvents: ((e == null ? void 0 : e.snapshot) && e.snapshot.scheduler) ?? {} }, _bookId: () => `x:${Gt++}`, _register: (d, l) => (n.set(d, l), d), _unregister: (d) => { n.delete(d.sessionId); const l = r.get(d); l !== void 0 && (s.delete(l), r.delete(d)); }, get: (d) => s.get(d), _set: (d, l) => { const u = s.get(d); if (u && u !== l) throw new Error(`Actor with system ID '${d}' already exists.`); s.set(d, l), r.set(l, d); }, inspect: (d) => { const l = ve(d); return i.add(l), { unsubscribe() { i.delete(l); } }; }, _sendInspectionEvent: p, _relay: (d, l, u) => { h._sendInspectionEvent({ type: "@xstate.event", sourceRef: d, actorRef: l, event: u }), l._send(u); }, scheduler: f, getSnapshot: () => ({ _scheduledEvents: { ...h._snapshot._scheduledEvents } }), start: () => { const d = h._snapshot._scheduledEvents; h._snapshot._scheduledEvents = {}; for (const l in d) { const { source: u, target: g, event: y, delay: _, id: T } = d[l]; f.schedule(u, g, y, _, T); } }, _clock: a, _logger: c }; return h; } let pe = !1; const Me = 1; let A = /* @__PURE__ */ function(t) { return t[t.NotStarted = 0] = "NotStarted", t[t.Running = 1] = "Running", t[t.Stopped = 2] = "Stopped", t; }({}); const jt = { clock: { setTimeout: (t, e) => setTimeout(t, e), clearTimeout: (t) => clearTimeout(t) }, logger: console.log.bind(console), devTools: !1 }; class Ft { /** * Creates a new actor instance for the given logic with the provided options, * if any. * * @param logic The logic to create an actor from * @param options Actor options */ constructor(e, n) { this.logic = e, this._snapshot = void 0, this.clock = void 0, this.options = void 0, this.id = void 0, this.mailbox = new Ue(this._process.bind(this)), this.observers = /* @__PURE__ */ new Set(), this.eventListeners = /* @__PURE__ */ new Map(), this.logger = void 0, this._processingStatus = A.NotStarted, this._parent = void 0, this._syncSnapshot = void 0, this.ref = void 0, this._actorScope = void 0, this._systemId = void 0, this.sessionId = void 0, this.system = void 0, this._doneEvent = void 0, this.src = void 0, this._deferred = []; const s = { ...jt, ...n }, { clock: r, logger: i, parent: o, syncSnapshot: a, id: c, systemId: f, inspect: p } = s; this.system = o ? o.system : Vt(this, { clock: r, logger: i }), p && !o && this.system.inspect(ve(p)), this.sessionId = this.system._bookId(), this.id = c ?? this.sessionId, this.logger = (n == null ? void 0 : n.logger) ?? this.system._logger, this.clock = (n == null ? void 0 : n.clock) ?? this.system._clock, this._parent = o, this._syncSnapshot = a, this.options = s, this.src = s.src ?? e, this.ref = this, this._actorScope = { self: this, id: this.id, sessionId: this.sessionId, logger: this.logger, defer: (h) => { this._deferred.push(h); }, system: this.system, stopChild: (h) => { if (h._parent !== this) throw new Error(`Cannot stop child actor ${h.id} of ${this.id} because it is not a child`); h._stop(); }, emit: (h) => { const d = this.eventListeners.get(h.type), l = this.eventListeners.get("*"); if (!d && !l) return; const u = [...d ? d.values() : [], ...l ? l.values() : []]; for (const g of u) g(h); }, actionExecutor: (h) => { const d = () => { if (this._actorScope.system._sendInspectionEvent({ type: "@xstate.action", actorRef: this, action: { type: h.type, params: h.params } }), !h.exec) return; const l = pe; try { pe = !0, h.exec(h.info, h.params); } finally { pe = l; } }; this._processingStatus === A.Running ? d() : this._deferred.push(d); } }, this.send = this.send.bind(this), this.system._sendInspectionEvent({ type: "@xstate.actor", actorRef: this }), f && (this._systemId = f, this.system._set(f, this)), this._initState((n == null ? void 0 : n.snapshot) ?? (n == null ? void 0 : n.state)), f && this._snapshot.status !== "active" && this.system._unregister(this); } _initState(e) { var n; try { this._snapshot = e ? this.logic.restoreSnapshot ? this.logic.restoreSnapshot(e, this._actorScope) : e : this.logic.getInitialSnapshot(this._actorScope, (n = this.options) == null ? void 0 : n.input); } catch (s) { this._snapshot = { status: "error", output: void 0, error: s }; } } update(e, n) { var r, i; this._snapshot = e; let s; for (; s = this._deferred.shift(); ) try { s(); } catch (o) { this._deferred.length = 0, this._snapshot = { ...e, status: "error", error: o }; } switch (this._snapshot.status) { case "active": for (const o of this.observers) try { (r = o.next) == null || r.call(o, e); } catch (a) { M(a); } break; case "done": for (const o of this.observers) try { (i = o.next) == null || i.call(o, e); } catch (a) { M(a); } this._stopProcedure(), this._complete(), this._doneEvent = Pt(this.id, this._snapshot.output), this._parent && this.system._relay(this, this._parent, this._doneEvent); break; case "error": this._error(this._snapshot.error); break; } this.system._sendInspectionEvent({ type: "@xstate.snapshot", actorRef: this, event: n, snapshot: e }); } /** * Subscribe an observer to an actor’s snapshot values. * * @remarks * The observer will receive the actor’s snapshot value when it is emitted. * The observer can be: * * - A plain function that receives the latest snapshot, or * - An observer object whose `.next(snapshot)` method receives the latest * snapshot * * @example * * ```ts * // Observer as a plain function * const subscription = actor.subscribe((snapshot) => { * console.log(snapshot); * }); * ``` * * @example * * ```ts * // Observer as an object * const subscription = actor.subscribe({ * next(snapshot) { * console.log(snapshot); * }, * error(err) { * // ... * }, * complete() { * // ... * } * }); * ``` * * The return value of `actor.subscribe(observer)` is a subscription object * that has an `.unsubscribe()` method. You can call * `subscription.unsubscribe()` to unsubscribe the observer: * * @example * * ```ts * const subscription = actor.subscribe((snapshot) => { * // ... * }); * * // Unsubscribe the observer * subscription.unsubscribe(); * ``` * * When the actor is stopped, all of its observers will automatically be * unsubscribed. * * @param observer - Either a plain function that receives the latest * snapshot, or an observer object whose `.next(snapshot)` method receives * the latest snapshot */ subscribe(e, n, s) { var i; const r = ve(e, n, s); if (this._processingStatus !== A.Stopped) this.observers.add(r); else switch (this._snapshot.status) { case "done": try { (i = r.complete) == null || i.call(r); } catch (o) { M(o); } break; case "error": { const o = this._snapshot.error; if (!r.error) M(o); else try { r.error(o); } catch (a) { M(a); } break; } } return { unsubscribe: () => { this.observers.delete(r); } }; } on(e, n) { let s = this.eventListeners.get(e); s || (s = /* @__PURE__ */ new Set(), this.eventListeners.set(e, s)); const r = n.bind(void 0); return s.add(r), { unsubscribe: () => { s.delete(r); } }; } /** Starts the Actor from the initial state */ start() { if (this._processingStatus === A.Running) return this; this._syncSnapshot && this.subscribe({ next: (s) => { s.status === "active" && this.system._relay(this, this._parent, { type: `xstate.snapshot.${this.id}`, snapshot: s }); }, error: () => { } }), this.system._register(this.sessionId, this), this._systemId && this.system._set(this._systemId, this), this._processingStatus = A.Running; const e = ot(this.options.input); switch (this.system._sendInspectionEvent({ type: "@xstate.event", sourceRef: this._parent, actorRef: this, event: e }), this._snapshot.status) { case "done": return this.update(this._snapshot, e), this; case "error": return this._error(this._snapshot.error), this; } if (this._parent || this.system.start(), this.logic.start) try { this.logic.start(this._snapshot, this._actorScope); } catch (s) { return this._snapshot = { ...this._snapshot, status: "error", error: s }, this._error(s), this; } return this.update(this._snapshot, e), this.options.devTools && this.attachDevTools(), this.mailbox.start(), this; } _process(e) { let n, s; try { n = this.logic.transition(this._snapshot, e, this._actorScope); } catch (r) { s = { err: r }; } if (s) { const { err: r } = s; this._snapshot = { ...this._snapshot, status: "error", error: r }, this._error(r); return; } this.update(n, e), e.type === Y && (this._stopProcedure(), this._complete()); } _stop() { return this._processingStatus === A.Stopped ? this : (this.mailbox.clear(), this._processingStatus === A.NotStarted ? (this._processingStatus = A.Stopped, this) : (this.mailbox.enqueue({ type: Y }), this)); } /** Stops the Actor and unsubscribe all listeners. */ stop() { if (this._parent) throw new Error("A non-root actor cannot be stopped directly."); return this._stop(); } _complete() { var e; for (const n of this.observers) try { (e = n.complete) == null || e.call(n); } catch (s) { M(s); } this.observers.clear(); } _reportError(e) { if (!this.observers.size) { this._parent || M(e); return; } let n = !1; for (const s of this.observers) { const r = s.error; n || (n = !r); try { r == null || r(e); } catch (i) { M(i); } } this.observers.clear(), n && M(e); } _error(e) { this._stopProcedure(), this._reportError(e), this._parent && this.system._relay(this, this._parent, xt(this.id, e)); } // TODO: atm children don't belong entirely to the actor so // in a way - it's not even super aware of them // so we can't stop them from here but we really should! // right now, they are being stopped within the machine's transition // but that could throw and leave us with "orphaned" active actors _stopProcedure() { return this._processingStatus !== A.Running ? this : (this.system.scheduler.cancelAll(this), this.mailbox.clear(), this.mailbox = new Ue(this._process.bind(this)), this._processingStatus = A.Stopped, this.system._unregister(this), this); } /** @internal */ _send(e) { this._processingStatus !== A.Stopped && this.mailbox.enqueue(e); } /** * Sends an event to the running Actor to trigger a transition. * * @param event The event to send */ send(e) { this.system._relay(void 0, this, e); } attachDevTools() { const { devTools: e } = this.options; e && (typeof e == "function" ? e : Nt)(this); } toJSON() { return { xstate$$type: Me, id: this.id }; } /** * Obtain the internal state of the actor, which can be persisted. * * @remarks * The internal state can be persisted from any actor, not only machines. * * Note that the persisted state is not the same as the snapshot from * {@link Actor.getSnapshot}. Persisted state represents the internal state of * the actor, while snapshots represent the actor's last emitted value. * * Can be restored with {@link ActorOptions.state} * @see https://stately.ai/docs/persistence */ getPersistedSnapshot(e) { return this.logic.getPersistedSnapshot(this._snapshot, e); } [Ut]() { return this; } /** * Read an actor’s snapshot synchronously. * * @remarks * The snapshot represent an actor's last emitted value. * * When an actor receives an event, its internal state may change. An actor * may emit a snapshot when a state transition occurs. * * Note that some actors, such as callback actors generated with * `fromCallback`, will not emit snapshots. * @see {@link Actor.subscribe} to subscribe to an actor’s snapshot values. * @see {@link Actor.getPersistedSnapshot} to persist the internal state of an actor (which is more than just a snapshot). */ getSnapshot() { return this._snapshot; } } function j(t, ...[e]) { return new Ft(t, e); } function qt(t, e, n, s, { sendId: r }) { const i = typeof r == "function" ? r(n, s) : r; return [e, { sendId: i }, void 0]; } function Jt(t, e) { t.defer(() => { t.system.scheduler.cancel(t.self, e.sendId); }); } function Kt(t) { function e(n, s) { } return e.type = "xstate.cancel", e.sendId = t, e.resolve = qt, e.execute = Jt, e; } function Bt(t, e, n, s, { id: r, systemId: i, src: o, input: a, syncSnapshot: c }) { const f = typeof o == "string" ? Ce(e.machine, o) : o, p = typeof r == "function" ? r(n) : r; let h, d; return f && (d = typeof a == "function" ? a({ context: e.context, event: n.event, self: t.self }) : a, h = j(f, { id: p, src: o, parent: t.self, syncSnapshot: c, systemId: i, input: d })), [x(e, { children: { ...e.children, [p]: h } }), { id: r, systemId: i, actorRef: h, src: o, input: d }, void 0]; } function zt(t, { actorRef: e }) { e && t.defer(() => { e._processingStatus !== A.Stopped && e.start(); }); } function Wt(...[t, { id: e, systemId: n, input: s, syncSnapshot: r = !1 } = {}]) { function i(o, a) { } return i.type = "xstate.spawnChild", i.id = e, i.systemId = n, i.src = t, i.input = s, i.syncSnapshot = r, i.resolve = Bt, i.execute = zt, i; } function Yt(t, e, n, s, { actorRef: r }) { const i = typeof r == "function" ? r(n, s) : r, o = typeof i == "string" ? e.children[i] : i; let a = e.children; return o && (a = { ...a }, delete a[o.id]), [x(e, { children: a }), o, void 0]; } function Xt(t, e) { if (e) { if (t.system._unregister(e), e._processingStatus !== A.Running) { t.stopChild(e); return; } t.defer(() => { t.stopChild(e); }); } } function ht(t) { function e(n, s) { } return e.type = "xstate.stopChild", e.actorRef = t, e.resolve = Yt, e.execute = Xt, e; } function Ne(t, e, n, s) { const { machine: r } = s, i = typeof t == "function", o = i ? t : r.implementations.guards[typeof t == "string" ? t : t.type]; if (!i && !o) throw new Error(`Guard '${typeof t == "string" ? t : t.type}' is not implemented.'.`); if (typeof o != "function") return Ne(o, e, n, s); const a = { context: e, event: n }, c = i || typeof t == "string" ? void 0 : "params" in t ? typeof t.params == "function" ? t.params({ context: e, event: n }) : t.params : void 0; return "check" in o ? o.check( s, a, o // this holds all params ) : o(a, c); } const ke = (t) => t.type === "atomic" || t.type === "final"; function F(t) { return Object.values(t.states).filter((e) => e.type !== "history"); } function X(t, e) { const n = []; if (e === t) return n; let s = t.parent; for (; s && s !== e; ) n.push(s), s = s.parent; return n; } function oe(t) { const e = new Set(t), n = ft(e); for (const s of e) if (s.type === "compound" && (!n.get(s) || !n.get(s).length)) je(s).forEach((r) => e.add(r)); else if (s.type === "parallel") { for (const r of F(s)) if (r.type !== "history" && !e.has(r)) { const i = je(r); for (const o of i) e.add(o); } } for (const s of e) { let r = s.parent; for (; r; ) e.add(r), r = r.parent; } return e; } function lt(t, e) { const n = e.get(t); if (!n) return {}; if (t.type === "compound") { const r = n[0]; if (r) { if (ke(r)) return r.key; } else return {}; } const s = {}; for (const r of n) s[r.key] = lt(r, e); return s; } function ft(t) { const e = /* @__PURE__ */ new Map(); for (const n of t) e.has(n) || e.set(n, []), n.parent && (e.has(n.parent) || e.set(n.parent, []), e.get(n.parent).push(n)); return e; } function pt(t, e) { const n = oe(e); return lt(t, ft(n)); } function Le(t, e) { return e.type === "compound" ? F(e).some((n) => n.type === "final" && t.has(n)) : e.type === "parallel" ? F(e).every((n) => Le(t, n)) : e.type === "final"; } const he = (t) => t[0] === Lt; function Zt(t, e) { return t.transitions.get(e) || [...t.transitions.keys()].filter((s) => { if (s === Dt) return !0; if (!s.endsWith(".*")) return !1; const r = s.split("."), i = e.split("."); for (let o = 0; o < r.length; o++) { const a = r[o], c = i[o]; if (a === "*") return o === r.length - 1; if (a !== c) return !1; } return !0; }).sort((s, r) => r.length - s.length).flatMap((s) => t.transitions.get(s)); } function Qt(t) { const e = t.config.after; if (!e) return []; const n = (r) => { const i = bt(r, t.id), o = i.type; return t.entry.push(At(i, { id: o, delay: r })), t.exit.push(Kt(o)), o; }; return Object.keys(e).flatMap((r) => { const i = e[r], o = typeof i == "string" ? { target: i } : i, a = Number.isNaN(+r) ? r : +r, c = n(a); return N(o).map((f) => ({ ...f, event: c, delay: a })); }).map((r) => { const { delay: i } = r; return { ...b(t, r.event, r), delay: i }; }); } function b(t, e, n) { const s = dt(n.target), r = n.reenter ?? !1, i = nn(t, s), o = { ...n, actions: N(n.actions), guard: n.guard, target: i, source: t, reenter: r, eventType: e, toJSON: () => ({ ...o, source: `#${t.id}`, target: i ? i.map((a) => `#${a.id}`) : void 0 }) }; return o; } function en(t) { const e = /* @__PURE__ */ new Map(); if (t.config.on) for (const n of Object.keys(t.config.on)) { if (n === rt) throw new Error('Null events ("") cannot be specified as a transition key. Use `always: { ... }` instead.'); const s = t.config.on[n]; e.set(n, G(s).map((r) => b(t, n, r))); } if (t.config.onDone) { const n = `xstate.done.state.${t.id}`; e.set(n, G(t.config.onDone).map((s) => b(t, n, s))); } for (const n of t.invoke) { if (n.onDone) { const s = `xstate.done.actor.${n.id}`; e.set(s, G(n.onDone).map((r) => b(t, s, r))); } if (n.onError) { const s = `xstate.error.actor.${n.id}`; e.set(s, G(n.onError).map((r) => b(t, s, r))); } if (n.onSnapshot) { const s = `xstate.snapshot.${n.id}`; e.set(s, G(n.onSnapshot).map((r) => b(t, s, r))); } } for (const n of t.after) { let s = e.get(n.eventType); s || (s = [], e.set(n.eventType, s)), s.push(n); } return e; } function tn(t, e) { const n = typeof e == "string" ? t.states[e] : e ? t.states[e.target] : void 0; if (!n && e) throw new Error( // eslint-disable-next-line @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-base-to-string `Initial state node "${e}" not found on parent state node #${t.id}` ); const s = { source: t, actions: !e || typeof e == "string" ? [] : N(e.actions), eventType: null, reenter: !1, target: n ? [n] : [], toJSON: () => ({ ...s, source: `#${t.id}`, target: n ? [`#${n.id}`] : [] }) }; return s; } function nn(t, e) { if (e !== void 0) return e.map((n) => { if (typeof n != "string") return n; if (he(n)) return t.machine.getStateNodeById(n); const s = n[0] === st; if (s && !t.parent) return ae(t, n.slice(1)); const r = s ? t.key + n : n; if (t.parent) try { return ae(t.parent, r); } catch (i) { throw new Error(`Invalid transition definition for state node '${t.id}': ${i.message}`); } else throw new Error(`Invalid target: "${n}" is not a valid target from the root node. Did you mean ".${n}"?`); }); } function gt(t) { const e = dt(t.config.target); return e ? { target: e.map((n) => typeof n == "string" ? ae(t.parent, n) : n) } : t.parent.initial; } function P(t) { return t.type === "history"; } function je(t) { const e = Et(t); for (const n of e) for (const s of X(n, t)) e.add(s); return e; } function Et(t) { const e = /* @__PURE__ */ new Set(); function n(s) { if (!e.has(s)) { if (e.add(s), s.type === "compound") n(s.initial.target[0]); else if (s.type === "parallel") for (const r of F(s)) n(r); } } return n(t), e; } function q(t, e) { if (he(e)) return t.machine.getStateNodeById(e); if (!t.states) throw new Error(`Unable to retrieve child state '${e}' from '${t.id}'; no child states exist.`); const n = t.states[e]; if (!n) throw new Error(`Child state '${e}' does not exist on '${t.id}'`); return n; } function ae(t, e) { if (typeof e == "string" && he(e)) try { return t.machine.getStateNodeById(e); } catch { } const n = we(e).slice(); let s = t; for (; n.length; ) { const r = n.shift(); if (!r.length) break; s = q(s, r); } return s; } function ce(t, e) { if (typeof e == "string") { const r = t.states[e]; if (!r) throw new Error(`State '${e}' does not exist on '${t.id}'`); return [t, r]; } const n = Object.keys(e), s = n.map((r) => q(t, r)).filter(Boolean); return [t.machine.root, t].concat(s, n.reduce((r, i) => { const o = q(t, i); if (!o) return r; const a = ce(o, e[i]); return r.concat(a); }, [])); } function sn(t, e, n, s) { const i = q(t, e).next(n, s); return !i || !i.length ? t.next(n, s) : i; } function rn(t, e, n, s) { const r = Object.keys(e), i = q(t, r[0]), o = De(i, e[r[0]], n, s); return !o || !o.length ? t.next(n, s) : o; } function on(t, e, n, s) { const r = []; for (const i of Object.keys(e)) { const o = e[i]; if (!o) continue; const a = q(t, i), c = De(a, o, n, s); c && r.push(...c); } return r.length ? r : t.next(n, s); } function De(t, e, n, s) { return typeof e == "string" ? sn(t, e, n, s) : Object.keys(e).length === 1 ? rn(t, e, n, s) : on(t, e, n, s); } function an(t) { return Object.keys(t.states).map((e) => t.states[e]).filter((e) => e.type === "history"); } function k(t, e) { let n = t; for (; n.parent && n.parent !== e; ) n = n.parent; return n.parent === e; } function cn(t, e) { const n = new Set(t), s = new Set(e); for (const r of n) if (s.has(r)) return !0; for (const r of s) if (n.has(r)) return !0; return !1; } function yt(t, e, n) { const s = /* @__PURE__ */ new Set(); for (const r of t) { let i = !1; const o = /* @__PURE__ */ new Set(); for (const a of s) if (cn(Ie([r], e, n), Ie([a], e, n))) if (k(r.source, a.source)) o.add(a); else { i = !0; break; } if (!i) { for (const a of o) s.delete(a); s.add(r); } } return Array.from(s); } function un(t) { const [e, ...n] = t; for (const s of X(e, void 0)) if (n.every((r) => k(r, s))) return s; } function be(t, e) { if (!t.target) return []; const n = /* @__PURE__ */ new Set(); for (const s of t.target) if (P(s)) if (e[s.id]) for (const r of e[s.id]) n.add(r); else for (const r of be(gt(s), e)) n.add(r); else n.add(s); return [...n]; } function St(t, e) { const n = be(t, e); if (!n) return; if (!t.reenter && n.every((r) => r === t.source || k(r, t.source))) return t.source; const s = un(n.concat(t.source)); if (s) return s; if (!t.reenter) return t.source.machine.root; } function Ie(t, e, n) { var r; const s = /* @__PURE__ */ new Set(); for (const i of t) if ((r = i.target) != null && r.length) { const o = St(i, n); i.reenter && i.source === o && s.add(o); for (const a of e) k(a, o) && s.add(a); } return [...s]; } function dn(t, e) { if (t.length !== e.size) return !1; for (const n of t) if (!e.has(n)) return !1; return !0; } function Re(t, e, n, s, r, i) { if (!t.length) return e; const o = new Set(e._nodes); let a = e.historyValue; const c = yt(t, o, a); let f = e; r || ([f, a] = pn(f, s, n, c, o, a, i, n.actionExecutor)), f = J(f, s, n, c.flatMap((h) => h.actions), i, void 0), f = ln(f, s, n, c, o, i, a, r); const p = [...o]; f.status === "done" && (f = J(f, s, n, p.sort((h, d) => d.order - h.order).flatMap((h) => h.exit), i, void 0)); try { return a === e.historyValue && dn(e._nodes, o) ? f : x(f, { _nodes: p, historyValue: a }); } catch (h) { throw h; } } function hn(t, e, n, s, r) { if (s.output === void 0) return; const i = Te(r.id, r.output !== void 0 && r.parent ? Ae(r.output, t.context, e, n.self) : void 0); return Ae(s.output, t.context, i, n.self); } function ln(t, e, n, s, r, i, o, a) { let c = t; const f = /* @__PURE__ */ new Set(), p = /* @__PURE__ */ new Set(); fn(s, o, p, f), a && p.add(t.machine.root); const h = /* @__PURE__ */ new Set(); for (const d of [...f].sort((l, u) => l.order - u.order)) { r.add(d); const l = []; l.push(...d.entry); for (const u of d.invoke) l.push(Wt(u.src, { ...u, syncSnapshot: !!u.onSnapshot })); if (p.has(d)) { const u = d.initial.actions; l.push(...u); } if (c = J(c, e, n, l, i, d.invoke.map((u) => u.id)), d.type === "final") { const u = d.parent; let g = (u == null ? void 0 : u.type) === "parallel" ? u : u == null ? void 0 : u.parent, y = g || d; for ((u == null ? void 0 : u.type) === "compound" && i.push(Te(u.id, d.output !== void 0 ? Ae(d.output, c.context, e, n.self) : void 0)); (g == null ? void 0 : g.type) === "parallel" && !h.has(g) && Le(r, g); ) h.add(g), i.push(Te(g.id)), y = g, g = g.parent; if (g) continue; c = x(c, { status: "done", output: hn(c, e, n, c.machine.root, y) }); } } return c; } function fn(t, e, n, s) { for (const r of t) { const i = St(r, e); for (const a of r.target || []) !P(a) && // if the target is different than the source then it will *definitely* be entered (r.source !== a || // we know that the domain can't lie within the source // if it's different than the source then it's outside of it and it means that the target has to be entered as well r.source !== i || // reentering transitions always enter the target, even if it's the source itself r.reenter) && (s.add(a), n.add(a)), V(a, e, n, s); const o = be(r, e); for (const a of o) { const c = X(a, i); (i == null ? void 0 : i.type) === "parallel" && c.push(i), _t(s, e, n, c, !r.source.parent && r.reenter ? void 0 : i); } } } function V(t, e, n, s) { var r; if (P(t)) if (e[t.id]) { const i = e[t.id]; for (const o of i) s.add(o), V(o, e, n, s); for (const o of i) ge(o, t.parent, s, e, n); } else { const i = gt(t); for (const o of i.target) s.add(o), i === ((r = t.parent) == null ? void 0 : r.initial) && n.add(t.parent), V(o, e, n, s); for (const o of i.target) ge(o, t.parent, s, e, n); } else if (t.type === "compound") { const [i] = t.initial.target; P(i) || (s.add(i), n.add(i)), V(i, e, n, s), ge(i, t, s, e, n); } else if (t.type === "parallel") for (const i of F(t).filter((o) => !P(o))) [...s].some((o) => k(o, i)) || (P(i) || (s.add(i), n.add(i)), V(i, e, n, s)); } function _t(t, e, n, s, r) { for (const i of s) if ((!r || k(i, r)) && t.add(i), i.type === "parallel") for (const o of F(i).filter((a) => !P(a))) [...t].some((a) => k(a, o)) || (t.add(o), V(o, e, n, t)); } function ge(t, e, n, s, r) { _t(n, s, r, X(t, e)); } function pn(t, e, n, s, r, i, o, a) { let c = t; const f = Ie(s, r, i); f.sort((h, d) => d.order - h.order); let p; for (const h of f) for (const d of an(h)) { let l; d.history === "deep" ? l = (u) => ke(u) && k(u, h) : l = (u) => u.parent === h, p ?? (p = { ...i }), p[d.id] = Array.from(r).filter(l); } for (const h of f) c = J(c, e, n, [...h.exit, ...h.invoke.map((d) => ht(d.id))], o, void 0), r.delete(h); return [c, p || i]; } function gn(t, e) { return t.implementations.actions[e]; } function mt(t, e, n, s, r, i) { const { machine: o } = t; let a = t; for (const c of s) { const f = typeof c == "function", p = f ? c : ( // the existing type of `.actions` assumes non-nullable `TExpressionAction` // it's fine to cast this here to get a common type and lack of errors in the rest of the code // our logic below makes sure that we call those 2 "variants" correctly gn(o, typeof c == "string" ? c : c.type) ), h = { context: a.context, event: e, self: n.self, system: n.system }, d = f || typeof c == "string" ? void 0 : "params" in c ? typeof c.params == "function" ? c.params({ context: a.context, event: e }) : c.params : void 0; if (!p || !("resolve" in p)) { n.actionExecutor({ type: typeof c == "string" ? c : typeof c == "object" ? c.type : c.name || "(anonymous)", info: h, params: d, exec: p }); continue; } const l = p, [u, g, y] = l.resolve( n, a, h, d, p, // this holds all params r ); a = u, "retryResolve" in l && (i == null || i.push([l, g])), "execute" in l && n.actionExecutor({ type: l.type, info: h, params: g, exec: l.execute.bind(null, n, g) }), y && (a = mt(a, e, n, y, r, i)); } return a; } function J(t, e, n, s, r, i) { const o = i ? [] : void 0, a = mt(t, e, n, s, { internalQueue: r, deferredActorIds: i }, o); return o == null || o.forEach(([c, f]) => { c.retryResolve(n, a, f); }), a; } function Ee(t, e, n, s) { let r = t; const i = []; function o(f, p, h) { n.system._sendInspectionEvent({ type: "@xstate.microstep", actorRef: n.self, event: p, snapshot: f, _transitions: h }), i.push(f); } if (e.type === Y) return r = x(Fe(r, e, n), { status: "stopped" }), o(r, e, []), { snapshot: r, microstates: i }; let a = e; if (a.type !== it) { const f = a, p = $t(f), h = qe(f, r); if (p && !h.length) return r = x(t, { status: "error", error: f.error }), o(r, f, []), { snapshot: r, microstates: i }; r = Re( h, t, n, a, !1, // isInitial s ), o(r, f, h); } let c = !0; for (; r.status === "active"; ) { let f = c ? En(r, a) : []; const p = f.length ? r : void 0; if (!f.length) { if (!s.length) break; a = s.shift(), f = qe(a, r); } r = Re(f, r, n, a, !1, s), c = r !== p, o(r, a, f); } return r.status !== "active" && Fe(r, a, n), { snapshot: r, microstates: i }; } function Fe(t, e, n) { return J(t, e, n, Object.values(t.children).map((s) => ht(s)), [], void 0); } function qe(t, e) { return e.machine.getTransitionData(e, t); } function En(t, e) { const n = /* @__PURE__ */ new Set(), s = t._nodes.filter(ke); for (const r of s) e: for (const i of [r].concat(X(r, void 0))) if (i.always) { for (const o of i.always) if (o.guard === void 0 || Ne(o.guard, t.context, e, t)) { n.add(o); break e; } } return yt(Array.from(n), new Set(t._nodes), t.historyValue); } function yn(t, e) { const n = oe(ce(t, e)); return pt(t, [...n]); } function Sn(t) { return !!t && typeof t == "object" && "machine" in t && "value" in t; } const _n = function(e) { return at(e, this.value); }, mn = function(e) { return this.tags.has(e); }, Tn = function(e) { const n = this.machine.getTransitionData(this, e); return !!(n != null && n.length) && // Check that at least one transition is not forbidden n.some((s) => s.target !== void 0 || s.actions.length); }, An = function() { const { _nodes: e, tags: n, machine: s, getMeta: r, toJSON: i, can: o, hasTag: a, matches: c, ...f } = this; return { ...f, tags: Array.from(n) }; }, vn = function() { return this._nodes.reduce((e, n) => (n.meta !== void 0 && (e[n.id] = n.meta), e), {}); }; function re(t, e) { return { status: t.status, output: t.output, error: t.error, machine: e, context: t.context, _nodes: t._nodes, value: pt(e.root, t._nodes), tags: new Set(t._nodes.flatMap((n) => n.tags)), children: t.children, historyValue: t.historyValue || {}, matches: _n, hasTag: mn, can: Tn, getMeta: vn, toJSON: An }; } function x(t, e = {}) { return re({ ...t, ...e }, t.machine); } function In(t) { if (typeof t != "object" || t === null) return {}; const e = {}; for (const n in t) { const s = t[n]; Array.isArray(s) && (e[n] = s.map((r) => ({ id: r.id }))); } return e; } function Rn(t, e) { const { _nodes: n, tags: s, machine: r, children: i, context: o, can: a, hasTag: c, matches: f, getMeta: p, toJSON: h, ...d } = t, l = {}; for (const g in i) { const y = i[g]; l[g] = { snapshot: y.getPersistedSnapshot(e), src: y.src, systemId: y._systemId, syncSnapshot: y._syncSnapshot }; } return { ...d, context: Tt(o), children: l, historyValue: In(d.historyValue) }; } function Tt(t) { let e; for (const n in t) { const s = t[n]; if (s && typeof s == "object") if ("sessionId" in s && "send" in s && "ref" in s) e ?? (e = Array.isArray(t) ? t.slice() : { ...t }), e[n] = { xstate$$type: Me, id: s.id }; else { const r = Tt(s); r !== s && (e ?? (e = Array.isArray(t) ? t.slice() : { ...t }), e[n] = r); } } return e ?? t; } function On(t, e, n, s, { event: r, id: i, delay: o }, { internalQueue: a }) { const c = e.machine.implementations.delays; if (typeof r == "string") throw new Error( // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `Only event objects may be used with raise; use raise({ type: "${r}" }) instead` ); const f = typeof r == "function" ? r(n, s) : r; let p; if (typeof o == "string") { const h = c && c[o]; p = typeof h == "function" ? h(n, s) : h; } else p = typeof o == "function" ? o(n, s) : o; return typeof p != "number" && a.push(f), [e, { event: f, id: i, delay: p }, void 0]; } function wn(t, e) { const { event: n, delay: s, id: r } = e; if (typeof s == "number") { t.defer(() => { const i = t.self; t.system.scheduler.schedule(i, i, n, s, r); }); return; } } function At(t, e) { function n(s, r) { } return n.type = "xstate.raise", n.event = t, n.id = e == null ? void 0 : e.id, n.delay = e == null ? void 0 : e.delay, n.resolve = On, n.execute = wn, n; } const Je = /* @__PURE__ */ new WeakMap(); function L(t) { return { config: t, start: (n, s) => { const { self: r, system: i, emit: o } = s, a = { receivers: void 0, dispose: void 0 }; Je.set(r, a), a.dispose = t({ input: n.input, system: i, self: r, sendBack: (c) => { r.getSnapshot().status !== "stopped" && r._parent && i._relay(r, r._parent, c); }, receive: (c) => { a.receivers ?? (a.receivers = /* @__PURE__ */ new Set()), a.receivers.add(c); }, emit: o }); }, transition: (n, s, r) => { var o, a; const i = Je.get(r.self); return s.type === Y ? (n = { ...n, status: "stopped", error: void 0 }, (o = i.dispose) == null || o.call(i), n) : ((a = i.receivers) == null || a.forEach((c) => c(s)), n); }, getInitialSnapshot: (n, s) => ({ status: "active", output: void 0, error: void 0, input: s }), getPersistedSnapshot: (n) => n, restoreSnapshot: (n) => n }; } const Ke = "xstate.promise.resolve", Be = "xstate.promise.reject", ee = /* @__PURE__ */ new WeakMap(); function U(t) { return { config: t, transition: (n, s, r) => { var i; if (n.status !== "active") return n; switch (s.type) { case Ke: { const o = s.data; return { ...n, status: "done", output: o, input: void 0 }; } case Be: return { ...n, status: "error", error: s.data, input: void 0 }; case Y: return (i = ee.get(r.self)) == null || i.abort(), { ...n, status: "stopped", input: void 0 }; default: return n; } }, start: (n, { self: s, system: r, emit: i }) => { if (n.status !== "active") return; const o = new AbortController(); ee.set(s, o), Promise.resolve(t({ input: n.input, system: r, self: s, signal: o.signal, emit: i })).then((c) => { s.getSnapshot().status === "active" && (ee.delete(s), r._relay(s, s, { type: Ke, data: c })); }, (c) => { s.getSnapshot().status === "active" && (ee.delete(s), r._relay(s, s, { type: Be, data: c })); }); }, getInitialSnapshot: (n, s) => ({ status: "active", output: void 0, error: void 0, input: s }), getPersistedSnapshot: (n) => n, restoreSnapshot: (n) => n }; } function Cn(t, { machine: e, context: n }, s, r) { const i = (o, a) => { if (typeof o == "string") { const c = Ce(e, o); if (!c) throw new Error(`Actor logic '${o}' not implemented in machine '${e.id}'`); const f = j(c, { id: a == null ? void 0 : a.id, parent: t.self, syncSnapshot: a == null ? void 0 : a.syncSnapshot, input: typeof (a == null ? void 0 : a.input) == "function" ? a.input({ context: n, event: s, self: t.self }) : a == null ? void 0 : a.input, src: o, systemId: a == null ? void 0 : a.systemId }); return r[f.id] = f, f; } else return j(o, { id: a == null ? void 0 : a.id, parent: t.self, syncSnapshot: a == null ? void 0 : a.syncSnapshot, input: a == null ? void 0 : a.input, src: o, systemId: a == null ? void 0 : a.systemId }); }; return (o, a) => { const c = i(o, a); return r[c.id] = c, t.defer(() => { c._processingStatus !== A.Stopped && c.start(); }), c; }; } function Mn(t, e, n, s, { assignment: r }) { if (!e.context) throw new Error("Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config."); const i = {}, o = { context: e.context, event: n.event, spawn: Cn(t, e, n.event, i), self: t.self, system: t.system }; let a = {}; if (typeof r == "function") a = r(o, s); else for (const f of Object.keys(r)) { const p = r[f]; a[f] = typeof p == "function" ? p(o, s) : p; } const c = Object.assign({}, e.context, a); return [x(e, { context: c, children: Object.keys(i).length ? { ...e.children, ...i } : e.children }), void 0, void 0]; } function w(t) { function e(n, s) { } return e.type = "xstate.assign", e.assignment = t, e.resolve = Mn, e; } const ze = /* @__PURE__ */ new WeakMap(); function $(t, e, n) { let s = ze.get(t); return s ? e in s || (s[e] = n()) : (s = { [e]: n() }, ze.set(t, s)), s[e]; } const Nn = {}, B = (t) => typeof t == "string" ? { type: t } : typeof t == "function" ? "resolve" in t ? { type: t.type } : { type: t.name } : t; class ue { constructor(e, n) { if (this.config = e, this.key = void 0, this.id = void 0, this.type = void 0, this.path = void 0, this.states = void 0, this.history = void 0, this.entry = void 0, this.exit = void 0, this.parent = void 0, this.machine = void 0, this.meta = void 0, this.output = void 0, this.order = -1, this.description = void 0, this.tags = [], this.transitions = void 0, this.always = void 0, this.parent = n._parent, this.key = n._key, this.machine = n._machine, this.path = this.parent ? this.parent.path.concat(this.key) : [], this.id = this.config.id || [this.machine.id, ...this.path].join(st), this.type = this.config.type || (this.config.states && Object.keys(this.config.states).length ? "compound" : this.config.history ? "history" : "atomic"), this.description = this.config.description, this.order = this.machine.idMap.size, this.machine.idMap.set(this.id, this), this.states = this.config.states ? $e(this.config.states, (s, r) => new ue(s, { _parent: this, _key: r, _machine: this.machine })) : Nn, this.type === "compound" && !this.config.initial) throw new Error(`No initial state specified for compound state node "#${this.id}". Try adding { initial: "${Object.keys(this.states)[0]}" } to the state config.`); this.history = this.config.history === !0 ? "shallow" : this.config.history || !1, this.entry = N(this.config.entry).slice(), this.exit = N(this.config.exit).slice(), this.meta = this.config.meta, this.output = this.type === "final" || !this.parent ? this.config.output : void 0, this.tags = N(e.tags).slice(); } /** @internal */ _initialize() { this.transitions = en(this), this.config.always && (this.always = G(this.config.always).map((e) => b(this, rt, e))), Object.keys(this.states).forEach((e) => { this.states[e]._initialize(); }); } /** The well-structured state node definition. */ get definition() { return { id: this.id, key: this.key, version: this.machine.version, type: this.type, initial: this.initial ? { target: this.initial.target, source: this, actions: this.initial.actions.map(B), eventType: null, reenter: !1, toJSON: () => ({ target: this.initial.target.map((e) => `#${e.id}`), source: `#${this.id}`, actions: this.initial.actions.map(B), eventType: null }) } : void 0, history: this.history, states: $e(this.sta