"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const BlueBase_1 = require("../../BlueBase");
const react_native_1 = require("react-native");
const react_1 = tslib_1.__importDefault(require("react"));
const utils_1 = require("../../utils");
const MISSING_ERROR = 'An unknown error occured.';
/**
 * # 🚀 BlueBaseApp
 *
 * The main BlueBase app. This is the top level component in BlueBase. Takes care
 * of initialisation, and renders either children, or app with routing.
 *
 * ## Usage
 * ```jsx
 * <BlueBaseApp BB={BB} plugins={{}} filter={{}} themes={{}} />
 * ```
 */
class BlueBaseApp extends react_1.default.Component {
    constructor(props) {
        super(props);
        this.state = {
            AppComponent: () => null,
            BB: props.BB || new BlueBase_1.BlueBase(),
            booted: false,
            error: null,
            loading: true,
        };
    }
    async componentDidMount() {
        const BB = this.state.BB;
        try {
            const AppComponent = await BB.boot(this.props);
            this.setState({
                AppComponent: AppComponent || this.state.AppComponent,
                booted: BB.booted,
                loading: false,
            });
        }
        catch (error) {
            this.setState({
                booted: false,
                error,
                loading: false,
            });
        }
    }
    componentDidCatch(error) {
        this.setState({
            error: error || new Error(MISSING_ERROR)
        });
    }
    render() {
        const { loading, error, AppComponent, BB } = this.state;
        if (loading) {
            return (react_1.default.createElement(react_native_1.View, { style: { alignItems: 'center', flex: 1, justifyContent: 'center' } },
                react_1.default.createElement(react_native_1.Text, null, "Loading")));
        }
        if (error) {
            let development = BB.Configs.getValue('development');
            if (development === undefined) {
                development = !utils_1.isProduction();
            }
            const message = (development === true) ? error.message : MISSING_ERROR;
            return (react_1.default.createElement(react_native_1.ScrollView, null,
                react_1.default.createElement(react_native_1.SafeAreaView, null,
                    react_1.default.createElement(react_native_1.View, { style: { alignItems: 'center', flex: 1, justifyContent: 'center' } },
                        react_1.default.createElement(react_native_1.Text, { style: { fontWeight: 'bold', } }, "\uD83D\uDEA8 BlueBase Error"),
                        react_1.default.createElement(react_native_1.Text, null, message)))));
        }
        return react_1.default.createElement(AppComponent, { children: this.props.children });
    }
}
exports.BlueBaseApp = BlueBaseApp;
