1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3    return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.renderRootComponent = void 0;
7const expo_1 = require("expo");
8const react_1 = __importDefault(require("react"));
9const react_native_1 = require("react-native");
10const Splash_1 = require("./views/Splash");
11function isBaseObject(obj) {
12    if (Object.prototype.toString.call(obj) !== '[object Object]') {
13        return false;
14    }
15    const proto = Object.getPrototypeOf(obj);
16    if (proto === null) {
17        return true;
18    }
19    return proto === Object.prototype;
20}
21function isErrorShaped(error) {
22    return (error &&
23        typeof error === 'object' &&
24        typeof error.name === 'string' &&
25        typeof error.message === 'string');
26}
27/**
28 * After we throw this error, any number of tools could handle it.
29 * This check ensures the error is always in a reason state before surfacing it to the runtime.
30 */
31function convertError(error) {
32    if (isErrorShaped(error)) {
33        return error;
34    }
35    if (process.env.NODE_ENV === 'development') {
36        if (error == null) {
37            return new Error('A null/undefined error was thrown.');
38        }
39    }
40    if (isBaseObject(error)) {
41        return new Error(JSON.stringify(error));
42    }
43    return new Error(String(error));
44}
45/**
46 * Register and mount the root component using the predefined rendering
47 * method. This function ensures the Splash Screen and errors are handled correctly.
48 */
49function renderRootComponent(Component) {
50    try {
51        // This must be delayed so the user has a chance to call it first.
52        setTimeout(() => {
53            (0, Splash_1._internal_preventAutoHideAsync)();
54        });
55        if (process.env.NODE_ENV !== 'production') {
56            const { withErrorOverlay } = require('@expo/metro-runtime/error-overlay');
57            (0, expo_1.registerRootComponent)(withErrorOverlay(Component));
58        }
59        else {
60            (0, expo_1.registerRootComponent)(Component);
61        }
62    }
63    catch (e) {
64        // Hide the splash screen if there was an error so the user can see it.
65        Splash_1.SplashScreen.hideAsync();
66        const error = convertError(e);
67        // Prevent the app from throwing confusing:
68        //  ERROR  Invariant Violation: "main" has not been registered. This can happen if:
69        // * Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
70        // * A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.
71        (0, expo_1.registerRootComponent)(() => react_1.default.createElement(react_native_1.View, null));
72        // Console is pretty useless on native, on web you get interactive stack traces.
73        if (react_native_1.Platform.OS === 'web') {
74            console.error(error);
75            console.error(`A runtime error has occurred while rendering the root component.`);
76        }
77        // Give React a tick to render before throwing.
78        setTimeout(() => {
79            throw error;
80        });
81        // TODO: Render a production-only error screen.
82    }
83}
84exports.renderRootComponent = renderRootComponent;
85//# sourceMappingURL=renderRootComponent.js.map