UNPKG

@documedis-components/prescription-sign

Version:

React component for signing electronic prescriptions with HIN authentication

1,883 lines 112 kB
import { jsxs as X, jsx as U } from "react/jsx-runtime"; import ce, { useLayoutEffect as Rt, useCallback as V, useEffect as Mt, useState as Ct, useRef as He, useMemo as de } from "react"; function xt() { 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 It() { const t = xt(); if (t.__xstate__) return t.__xstate__; } const bt = (t) => { if (typeof window > "u") return; const e = It(); e && e.register(t); }; class $e { 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 = ".", Dt = "", rt = "", kt = "#", Lt = "*", it = "xstate.init", Y = "xstate.stop"; function Ht(t, e) { return { type: `xstate.after.${t}.${e}` }; } function Ee(t, e) { return { type: `xstate.done.state.${t}`, output: e }; } function $t(t, e) { return { type: `xstate.done.actor.${t}`, output: e, actorId: t }; } function Pt(t, e) { return { type: `xstate.error.actor.${t}`, error: e, actorId: t }; } function ot(t) { return { type: it, input: t }; } function x(t) { setTimeout(() => { throw t; }); } const Nt = typeof Symbol == "function" && Symbol.observable || "@@observable"; function at(t, e) { const n = Pe(t), s = Pe(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 Te(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 Pe(t) { if (En(t)) return t.value; if (typeof t != "string") return t; const e = Te(t); return Ut(e); } function Ut(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 Ne(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 I(t) { return t === void 0 ? [] : ct(t); } function Se(t, e, n, s) { return typeof t == "function" ? t({ context: e, event: n, self: s }) : t; } function ut(t) { return Array.isArray(t); } function jt(t) { return t.type.startsWith("xstate.error.actor"); } function z(t) { return ct(t).map((e) => typeof e > "u" || typeof e == "string" ? { target: e } : e); } function dt(t) { if (!(t === void 0 || t === Dt)) return I(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 Ue(t, e) { return `${e}.${t}`; } function Oe(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 je(t, e) { return `${t.sessionId}.${e}`; } let zt = 0; function Gt(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: (u, l, h, m, S = Math.random().toString(36).slice(2)) => { const w = { source: u, target: l, event: h, delay: m, id: S, startedAt: Date.now() }, T = je(u, S); d._snapshot._scheduledEvents[T] = w; const O = a.setTimeout(() => { delete o[T], delete d._snapshot._scheduledEvents[T], d._relay(u, l, h); }, m); o[T] = O; }, cancel: (u, l) => { const h = je(u, l), m = o[h]; delete o[h], delete d._snapshot._scheduledEvents[h], m !== void 0 && a.clearTimeout(m); }, cancelAll: (u) => { for (const l in d._snapshot._scheduledEvents) { const h = d._snapshot._scheduledEvents[l]; h.source === u && f.cancel(u, h.id); } } }, y = (u) => { if (!i.size) return; const l = { ...u, rootId: t.sessionId }; i.forEach((h) => { var m; return (m = h.next) == null ? void 0 : m.call(h, l); }); }, d = { _snapshot: { _scheduledEvents: ((e == null ? void 0 : e.snapshot) && e.snapshot.scheduler) ?? {} }, _bookId: () => `x:${zt++}`, _register: (u, l) => (n.set(u, l), u), _unregister: (u) => { n.delete(u.sessionId); const l = r.get(u); l !== void 0 && (s.delete(l), r.delete(u)); }, get: (u) => s.get(u), _set: (u, l) => { const h = s.get(u); if (h && h !== l) throw new Error(`Actor with system ID '${u}' already exists.`); s.set(u, l), r.set(l, u); }, inspect: (u) => { const l = ve(u); return i.add(l), { unsubscribe() { i.delete(l); } }; }, _sendInspectionEvent: y, _relay: (u, l, h) => { d._sendInspectionEvent({ type: "@xstate.event", sourceRef: u, actorRef: l, event: h }), l._send(h); }, scheduler: f, getSnapshot: () => ({ _scheduledEvents: { ...d._snapshot._scheduledEvents } }), start: () => { const u = d._snapshot._scheduledEvents; d._snapshot._scheduledEvents = {}; for (const l in u) { const { source: h, target: m, event: S, delay: w, id: T } = u[l]; f.schedule(h, m, S, w, T); } }, _clock: a, _logger: c }; return d; } let he = !1; const Re = 1; let R = /* @__PURE__ */ function(t) { return t[t.NotStarted = 0] = "NotStarted", t[t.Running = 1] = "Running", t[t.Stopped = 2] = "Stopped", t; }({}); const Vt = { clock: { setTimeout: (t, e) => setTimeout(t, e), clearTimeout: (t) => clearTimeout(t) }, logger: console.log.bind(console), devTools: !1 }; class Kt { /** * 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 $e(this._process.bind(this)), this.observers = /* @__PURE__ */ new Set(), this.eventListeners = /* @__PURE__ */ new Map(), this.logger = void 0, this._processingStatus = R.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 = { ...Vt, ...n }, { clock: r, logger: i, parent: o, syncSnapshot: a, id: c, systemId: f, inspect: y } = s; this.system = o ? o.system : Gt(this, { clock: r, logger: i }), y && !o && this.system.inspect(ve(y)), 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: (d) => { this._deferred.push(d); }, system: this.system, stopChild: (d) => { if (d._parent !== this) throw new Error(`Cannot stop child actor ${d.id} of ${this.id} because it is not a child`); d._stop(); }, emit: (d) => { const u = this.eventListeners.get(d.type), l = this.eventListeners.get("*"); if (!u && !l) return; const h = [...u ? u.values() : [], ...l ? l.values() : []]; for (const m of h) m(d); }, actionExecutor: (d) => { const u = () => { if (this._actorScope.system._sendInspectionEvent({ type: "@xstate.action", actorRef: this, action: { type: d.type, params: d.params } }), !d.exec) return; const l = he; try { he = !0, d.exec(d.info, d.params); } finally { he = l; } }; this._processingStatus === R.Running ? u() : this._deferred.push(u); } }, 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) { x(a); } break; case "done": for (const o of this.observers) try { (i = o.next) == null || i.call(o, e); } catch (a) { x(a); } this._stopProcedure(), this._complete(), this._doneEvent = $t(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 !== R.Stopped) this.observers.add(r); else switch (this._snapshot.status) { case "done": try { (i = r.complete) == null || i.call(r); } catch (o) { x(o); } break; case "error": { const o = this._snapshot.error; if (!r.error) x(o); else try { r.error(o); } catch (a) { x(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 === R.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 = R.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 === R.Stopped ? this : (this.mailbox.clear(), this._processingStatus === R.NotStarted ? (this._processingStatus = R.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) { x(s); } this.observers.clear(); } _reportError(e) { if (!this.observers.size) { this._parent || x(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) { x(i); } } this.observers.clear(), n && x(e); } _error(e) { this._stopProcedure(), this._reportError(e), this._parent && this.system._relay(this, this._parent, Pt(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 !== R.Running ? this : (this.system.scheduler.cancelAll(this), this.mailbox.clear(), this.mailbox = new $e(this._process.bind(this)), this._processingStatus = R.Stopped, this.system._unregister(this), this); } /** @internal */ _send(e) { this._processingStatus !== R.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 : bt)(this); } toJSON() { return { xstate$$type: Re, 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); } [Nt]() { 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 K(t, ...[e]) { return new Kt(t, e); } function Wt(t, e, n, s, { sendId: r }) { const i = typeof r == "function" ? r(n, s) : r; return [e, { sendId: i }, void 0]; } function Bt(t, e) { t.defer(() => { t.system.scheduler.cancel(t.self, e.sendId); }); } function Jt(t) { function e(n, s) { } return e.type = "xstate.cancel", e.sendId = t, e.resolve = Wt, e.execute = Bt, e; } function qt(t, e, n, s, { id: r, systemId: i, src: o, input: a, syncSnapshot: c }) { const f = typeof o == "string" ? Oe(e.machine, o) : o, y = typeof r == "function" ? r(n) : r; let d, u; return f && (u = typeof a == "function" ? a({ context: e.context, event: n.event, self: t.self }) : a, d = K(f, { id: y, src: o, parent: t.self, syncSnapshot: c, systemId: i, input: u })), [P(e, { children: { ...e.children, [y]: d } }), { id: r, systemId: i, actorRef: d, src: o, input: u }, void 0]; } function Ft(t, { actorRef: e }) { e && t.defer(() => { e._processingStatus !== R.Stopped && e.start(); }); } function Yt(...[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 = qt, i.execute = Ft, i; } function Zt(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]), [P(e, { children: a }), o, void 0]; } function Xt(t, e) { if (e) { if (t.system._unregister(e), e._processingStatus !== R.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 = Zt, e.execute = Xt, e; } function Me(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 Me(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 Ce = (t) => t.type === "atomic" || t.type === "final"; function W(t) { return Object.values(t.states).filter((e) => e.type !== "history"); } function Z(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 re(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)) ze(s).forEach((r) => e.add(r)); else if (s.type === "parallel") { for (const r of W(s)) if (r.type !== "history" && !e.has(r)) { const i = ze(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 (Ce(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 = re(e); return lt(t, ft(n)); } function xe(t, e) { return e.type === "compound" ? W(e).some((n) => n.type === "final" && t.has(n)) : e.type === "parallel" ? W(e).every((n) => xe(t, n)) : e.type === "final"; } const ue = (t) => t[0] === kt; function Qt(t, e) { return t.transitions.get(e) || [...t.transitions.keys()].filter((s) => { if (s === Lt) 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 en(t) { const e = t.config.after; if (!e) return []; const n = (r) => { const i = Ht(r, t.id), o = i.type; return t.entry.push(wt(i, { id: o, delay: r })), t.exit.push(Jt(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 I(o).map((f) => ({ ...f, event: c, delay: a })); }).map((r) => { const { delay: i } = r; return { ...H(t, r.event, r), delay: i }; }); } function H(t, e, n) { const s = dt(n.target), r = n.reenter ?? !1, i = sn(t, s), o = { ...n, actions: I(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 tn(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, z(s).map((r) => H(t, n, r))); } if (t.config.onDone) { const n = `xstate.done.state.${t.id}`; e.set(n, z(t.config.onDone).map((s) => H(t, n, s))); } for (const n of t.invoke) { if (n.onDone) { const s = `xstate.done.actor.${n.id}`; e.set(s, z(n.onDone).map((r) => H(t, s, r))); } if (n.onError) { const s = `xstate.error.actor.${n.id}`; e.set(s, z(n.onError).map((r) => H(t, s, r))); } if (n.onSnapshot) { const s = `xstate.snapshot.${n.id}`; e.set(s, z(n.onSnapshot).map((r) => H(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 nn(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" ? [] : I(e.actions), eventType: null, reenter: !1, target: n ? [n] : [], toJSON: () => ({ ...s, source: `#${t.id}`, target: n ? [`#${n.id}`] : [] }) }; return s; } function sn(t, e) { if (e !== void 0) return e.map((n) => { if (typeof n != "string") return n; if (ue(n)) return t.machine.getStateNodeById(n); const s = n[0] === st; if (s && !t.parent) return ie(t, n.slice(1)); const r = s ? t.key + n : n; if (t.parent) try { return ie(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" ? ie(t.parent, n) : n) } : t.parent.initial; } function $(t) { return t.type === "history"; } function ze(t) { const e = mt(t); for (const n of e) for (const s of Z(n, t)) e.add(s); return e; } function mt(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 W(s)) n(r); } } return n(t), e; } function B(t, e) { if (ue(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 ie(t, e) { if (typeof e == "string" && ue(e)) try { return t.machine.getStateNodeById(e); } catch { } const n = Te(e).slice(); let s = t; for (; n.length; ) { const r = n.shift(); if (!r.length) break; s = B(s, r); } return s; } function oe(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) => B(t, r)).filter(Boolean); return [t.machine.root, t].concat(s, n.reduce((r, i) => { const o = B(t, i); if (!o) return r; const a = oe(o, e[i]); return r.concat(a); }, [])); } function rn(t, e, n, s) { const i = B(t, e).next(n, s); return !i || !i.length ? t.next(n, s) : i; } function on(t, e, n, s) { const r = Object.keys(e), i = B(t, r[0]), o = Ie(i, e[r[0]], n, s); return !o || !o.length ? t.next(n, s) : o; } function an(t, e, n, s) { const r = []; for (const i of Object.keys(e)) { const o = e[i]; if (!o) continue; const a = B(t, i), c = Ie(a, o, n, s); c && r.push(...c); } return r.length ? r : t.next(n, s); } function Ie(t, e, n, s) { return typeof e == "string" ? rn(t, e, n, s) : Object.keys(e).length === 1 ? on(t, e, n, s) : an(t, e, n, s); } function cn(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 un(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 (un(we([r], e, n), we([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 dn(t) { const [e, ...n] = t; for (const s of Z(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 ($(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 _t(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 = dn(n.concat(t.source)); if (s) return s; if (!t.reenter) return t.source.machine.root; } function we(t, e, n) { var r; const s = /* @__PURE__ */ new Set(); for (const i of t) if ((r = i.target) != null && r.length) { const o = _t(i, n); i.reenter && i.source === o && s.add(o); for (const a of e) k(a, o) && s.add(a); } return [...s]; } function hn(t, e) { if (t.length !== e.size) return !1; for (const n of t) if (!e.has(n)) return !1; return !0; } function Ae(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] = gn(f, s, n, c, o, a, i, n.actionExecutor)), f = J(f, s, n, c.flatMap((d) => d.actions), i, void 0), f = fn(f, s, n, c, o, i, a, r); const y = [...o]; f.status === "done" && (f = J(f, s, n, y.sort((d, u) => u.order - d.order).flatMap((d) => d.exit), i, void 0)); try { return a === e.historyValue && hn(e._nodes, o) ? f : P(f, { _nodes: y, historyValue: a }); } catch (d) { throw d; } } function ln(t, e, n, s, r) { if (s.output === void 0) return; const i = Ee(r.id, r.output !== void 0 && r.parent ? Se(r.output, t.context, e, n.self) : void 0); return Se(s.output, t.context, i, n.self); } function fn(t, e, n, s, r, i, o, a) { let c = t; const f = /* @__PURE__ */ new Set(), y = /* @__PURE__ */ new Set(); pn(s, o, y, f), a && y.add(t.machine.root); const d = /* @__PURE__ */ new Set(); for (const u of [...f].sort((l, h) => l.order - h.order)) { r.add(u); const l = []; l.push(...u.entry); for (const h of u.invoke) l.push(Yt(h.src, { ...h, syncSnapshot: !!h.onSnapshot })); if (y.has(u)) { const h = u.initial.actions; l.push(...h); } if (c = J(c, e, n, l, i, u.invoke.map((h) => h.id)), u.type === "final") { const h = u.parent; let m = (h == null ? void 0 : h.type) === "parallel" ? h : h == null ? void 0 : h.parent, S = m || u; for ((h == null ? void 0 : h.type) === "compound" && i.push(Ee(h.id, u.output !== void 0 ? Se(u.output, c.context, e, n.self) : void 0)); (m == null ? void 0 : m.type) === "parallel" && !d.has(m) && xe(r, m); ) d.add(m), i.push(Ee(m.id)), S = m, m = m.parent; if (m) continue; c = P(c, { status: "done", output: ln(c, e, n, c.machine.root, S) }); } } return c; } function pn(t, e, n, s) { for (const r of t) { const i = _t(r, e); for (const a of r.target || []) !$(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)), G(a, e, n, s); const o = be(r, e); for (const a of o) { const c = Z(a, i); (i == null ? void 0 : i.type) === "parallel" && c.push(i), Et(s, e, n, c, !r.source.parent && r.reenter ? void 0 : i); } } } function G(t, e, n, s) { var r; if ($(t)) if (e[t.id]) { const i = e[t.id]; for (const o of i) s.add(o), G(o, e, n, s); for (const o of i) le(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), G(o, e, n, s); for (const o of i.target) le(o, t.parent, s, e, n); } else if (t.type === "compound") { const [i] = t.initial.target; $(i) || (s.add(i), n.add(i)), G(i, e, n, s), le(i, t, s, e, n); } else if (t.type === "parallel") for (const i of W(t).filter((o) => !$(o))) [...s].some((o) => k(o, i)) || ($(i) || (s.add(i), n.add(i)), G(i, e, n, s)); } function Et(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 W(i).filter((a) => !$(a))) [...t].some((a) => k(a, o)) || (t.add(o), G(o, e, n, t)); } function le(t, e, n, s, r) { Et(n, s, r, Z(t, e)); } function gn(t, e, n, s, r, i, o, a) { let c = t; const f = we(s, r, i); f.sort((d, u) => u.order - d.order); let y; for (const d of f) for (const u of cn(d)) { let l; u.history === "deep" ? l = (h) => Ce(h) && k(h, d) : l = (h) => h.parent === d, y ?? (y = { ...i }), y[u.id] = Array.from(r).filter(l); } for (const d of f) c = J(c, e, n, [...d.exit, ...d.invoke.map((u) => ht(u.id))], o, void 0), r.delete(d); return [c, y || i]; } function mn(t, e) { return t.implementations.actions[e]; } function St(t, e, n, s, r, i) { const { machine: o } = t; let a = t; for (const c of s) { const f = typeof c == "function", y = 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 mn(o, typeof c == "string" ? c : c.type) ), d = { context: a.context, event: e, self: n.self, system: n.system }, u = 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 (!y || !("resolve" in y)) { n.actionExecutor({ type: typeof c == "string" ? c : typeof c == "object" ? c.type : c.name || "(anonymous)", info: d, params: u, exec: y }); continue; } const l = y, [h, m, S] = l.resolve( n, a, d, u, y, // this holds all params r ); a = h, "retryResolve" in l && (i == null || i.push([l, m])), "execute" in l && n.actionExecutor({ type: l.type, info: d, params: m, exec: l.execute.bind(null, n, m) }), S && (a = St(a, e, n, S, r, i)); } return a; } function J(t, e, n, s, r, i) { const o = i ? [] : void 0, a = St(t, e, n, s, { internalQueue: r, deferredActorIds: i }, o); return o == null || o.forEach(([c, f]) => { c.retryResolve(n, a, f); }), a; } function fe(t, e, n, s) { let r = t; const i = []; function o(f, y, d) { n.system._sendInspectionEvent({ type: "@xstate.microstep", actorRef: n.self, event: y, snapshot: f, _transitions: d }), i.push(f); } if (e.type === Y) return r = P(Ge(r, e, n), { status: "stopped" }), o(r, e, []), { snapshot: r, microstates: i }; let a = e; if (a.type !== it) { const f = a, y = jt(f), d = Ve(f, r); if (y && !d.length) return r = P(t, { status: "error", error: f.error }), o(r, f, []), { snapshot: r, microstates: i }; r = Ae( d, t, n, a, !1, // isInitial s ), o(r, f, d); } let c = !0; for (; r.status === "active"; ) { let f = c ? yn(r, a) : []; const y = f.length ? r : void 0; if (!f.length) { if (!s.length) break; a = s.shift(), f = Ve(a, r); } r = Ae(f, r, n, a, !1, s), c = r !== y, o(r, a, f); } return r.status !== "active" && Ge(r, a, n), { snapshot: r, microstates: i }; } function Ge(t, e, n) { return J(t, e, n, Object.values(t.children).map((s) => ht(s)), [], void 0); } function Ve(t, e) { return e.machine.getTransitionData(e, t); } function yn(t, e) { const n = /* @__PURE__ */ new Set(), s = t._nodes.filter(Ce); for (const r of s) e: for (const i of [r].concat(Z(r, void 0))) if (i.always) { for (const o of i.always) if (o.guard === void 0 || Me(o.guard, t.context, e, t)) { n.add(o); break e; } } return yt(Array.from(n), new Set(t._nodes), t.historyValue); } function _n(t, e) { const n = re(oe(t, e)); return pt(t, [...n]); } function En(t) { return !!t && typeof t == "object" && "machine" in t && "value" in t; } const Sn = function(e) { return at(e, this.value); }, vn = function(e) { return this.tags.has(e); }, wn = 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) }; }, Tn = function() { return this._nodes.reduce((e, n) => (n.meta !== void 0 && (e[n.id] = n.meta), e), {}); }; function ne(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: Sn, hasTag: vn, can: wn, getMeta: Tn, toJSON: An }; } function P(t, e = {}) { return ne({ ...t, ...e }, t.machine); } function On(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: y, toJSON: d, ...u } = t, l = {}; for (const m in i) { const S = i[m]; l[m] = { snapshot: S.getPersistedSnapshot(e), src: S.src, systemId: S._systemId, syncSnapshot: S._syncSnapshot }; } return { ...u, context: vt(o), children: l, historyValue: On(u.historyValue) }; } function vt(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: Re, id: s.id }; else { const r = vt(s); r !== s && (e ?? (e = Array.isArray(t) ? t.slice() : { ...t }), e[n] = r); } } return e ?? t; } function Mn(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 y; if (typeof o == "string") { const d = c && c[o]; y = typeof d == "function" ? d(n, s) : d; } else y = typeof o == "function" ? o(n, s) : o; return typeof y != "number" && a.push(f), [e, { event: f, id: i, delay: y }, void 0]; } function Cn(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 wt(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 = Mn, n.execute = Cn, n; } const Ke = /* @__PURE__ */ new WeakMap(); function b(t) { return { config: t, start: (n, s) => { const { self: r, system: i, emit: o } = s, a = { receivers: void 0, dispose: void 0 }; Ke.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 = Ke.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 We = "xstate.promise.resolve", Be = "xstate.promise.reject", Q = /* @__PURE__ */ new WeakMap(); function L(t) { return { config: t, transition: (n, s, r) => { var i; if (n.status !== "active") return n; switch (s.type) { case We: { 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 = Q.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(); Q.set(s, o), Promise.resolve(t({ input: n.input, system: r, self: s, signal: o.signal, emit: i })).then((c) => { s.getSnapshot().status === "active" && (Q.delete(s), r._relay(s, s, { type: We, data: c })); }, (c) => { s.getSnapshot().status === "active" && (Q.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 xn(t, { machine: e, context: n }, s, r) { const i = (o, a) => { if (typeof o == "string") { const c = Oe(e, o); if (!c) throw new Error(`Actor logic '${o}' not implemented in machine '${e.id}'`); const f = K(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 K(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 !== R.Stopped && c.start(); }), c; }; } function In(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: xn(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 y = r[f]; a[f] = typeof y == "function" ? y(o, s) : y; } const c = Object.assign({}, e.context, a); return [P(e, { context: c, children: Object.keys(i).length ? { ...e.children, ...i } : e.children }), void 0, void 0]; } function D(t) { function e(n, s) { } return e.type = "xstate.assign", e.assignment = t, e.resolve = In, e; } const Je = /* @__PURE__ */ new WeakMap(); function j(t, e, n) { let s = Je.get(t); return s ? e in s || (s[e] = n()) : (s = { [e]: n() }, Je.set(t, s)), s[e]; } const bn = {}, q = (t) => typeof t == "string" ? { type: t } : typeof t == "function" ? "resolve" in t ? { type: t.type } : { type: t.name } : t; class ae { 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 ? Ne(this.config.states, (s, r) => new ae(s, { _parent: this, _key: r, _machine: this.machine })) : bn, 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 = I(this.config.entry).slice(), this.exit = I(this.config.exit).slice(), this.meta = this.config.meta, this.output = this.type === "final" || !this.parent ? this.config.output : void 0, this.tags = I(e.tags).slice(); } /** @internal */ _initialize() { this.transitions = tn(this), this.config.always && (this.always = z(this.config.always).map((e) => H(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(q), eventType: null, reenter: !1, toJSON: () => ({ target: this.initial.target.map((e) => `#${e.id}`), source: `#${this.id}`, actions: this.initial.actions.map(q), eventType: null }) } : void 0, history: this.history, states: N