1import { createMachine, assign } from 'xstate';
2export var UpdatesStateMachineEventTypes;
3(function (UpdatesStateMachineEventTypes) {
4    UpdatesStateMachineEventTypes["CHECK"] = "check";
5    UpdatesStateMachineEventTypes["CHECK_COMPLETE_AVAILABLE"] = "checkCompleteAvailable";
6    UpdatesStateMachineEventTypes["CHECK_COMPLETE_UNAVAILABLE"] = "checkCompleteUnavailable";
7    UpdatesStateMachineEventTypes["CHECK_ERROR"] = "checkError";
8    UpdatesStateMachineEventTypes["DOWNLOAD"] = "download";
9    UpdatesStateMachineEventTypes["DOWNLOAD_COMPLETE"] = "downloadComplete";
10    UpdatesStateMachineEventTypes["DOWNLOAD_ERROR"] = "downloadError";
11    UpdatesStateMachineEventTypes["RESTART"] = "restart";
12})(UpdatesStateMachineEventTypes || (UpdatesStateMachineEventTypes = {}));
13/**
14 * Actions that modify the context
15 */
16const checkCompleteAvailableAction = assign({
17    latestManifest: (context, event) => event.body?.manifest || undefined,
18    checkError: () => undefined,
19    isChecking: () => false,
20    isUpdateAvailable: () => true,
21    isRollback: (context, event) => Boolean(event.body?.isRollBackToEmbedded),
22});
23const checkCompleteUnavailableAction = assign({
24    latestManifest: () => undefined,
25    checkError: () => undefined,
26    isChecking: () => false,
27    isUpdateAvailable: () => false,
28    isRollback: () => false,
29});
30const checkErrorAction = assign({
31    isChecking: () => false,
32    checkError: (context, event) => new Error(event.body?.message || 'checkError'),
33});
34const downloadCompleteAction = assign({
35    downloadedManifest: (context, event) => event.body?.manifest || context.downloadedManifest,
36    latestManifest: (context, event) => event.body?.manifest || context.latestManifest,
37    downloadError: () => undefined,
38    isDownloading: () => false,
39    isUpdatePending: (context, event) => !!(event.body?.manifest || context.downloadedManifest),
40    isUpdateAvailable: (context, event) => event.body?.manifest !== undefined || context.isUpdateAvailable,
41});
42const downloadErrorAction = assign({
43    downloadError: (context, event) => new Error(event.body?.message || 'downloadError'),
44    isDownloading: () => false,
45});
46const check = assign({
47    isChecking: (context) => true,
48});
49const download = assign({
50    isDownloading: (context) => true,
51});
52/**
53 * Model of the expo-updates state machine, written in Typescript.
54 * The actual implementations of this state machine will be in Swift on iOS and Kotlin on Android.
55 */
56export const UpdatesStateMachine = createMachine({
57    id: 'Updates',
58    initial: 'idle',
59    context: {
60        isChecking: false,
61        isDownloading: false,
62        isUpdateAvailable: false,
63        isUpdatePending: false,
64        isRollback: false,
65    },
66    predictableActionArguments: true,
67    states: {
68        idle: {
69            on: {
70                check: {
71                    target: 'checking',
72                    actions: check,
73                },
74                download: {
75                    target: 'downloading',
76                    actions: download,
77                },
78                restart: {
79                    target: 'restarting',
80                },
81            },
82        },
83        checking: {
84            on: {
85                checkCompleteAvailable: {
86                    target: 'idle',
87                    actions: [checkCompleteAvailableAction],
88                },
89                checkCompleteUnavailable: {
90                    target: 'idle',
91                    actions: [checkCompleteUnavailableAction],
92                },
93                checkError: {
94                    target: 'idle',
95                    actions: [checkErrorAction],
96                },
97            },
98        },
99        downloading: {
100            on: {
101                downloadComplete: {
102                    target: 'idle',
103                    actions: [downloadCompleteAction],
104                },
105                downloadError: {
106                    target: 'idle',
107                    actions: [downloadErrorAction],
108                },
109            },
110        },
111        restarting: {
112            type: 'final',
113        },
114    },
115});
116//# sourceMappingURL=UpdatesStateMachine.js.map