import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_schema__ from "@tachybase/schema";
import * as __WEBPACK_EXTERNAL_MODULE_click_to_react_component__ from "click-to-react-component";
import * as __WEBPACK_EXTERNAL_MODULE_react_error_boundary__ from "react-error-boundary";
import * as __WEBPACK_EXTERNAL_MODULE__context_mjs__ from "../context.mjs";
const AppComponent = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_schema__.observer)((props)=>{
    var _app_error, _app_error1;
    const { app } = props;
    const handleErrors = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((error, info)=>{
        console.error(error);
        const err = new Error();
        err.stack = info.componentStack.trim();
        console.error(err);
    }, []);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        app.load();
    }, [
        app
    ]);
    const AppError = app.getComponent('AppError');
    if (app.loading) return app.renderComponent('AppSpin', {
        app
    });
    if (!app.maintained && app.maintaining) return app.renderComponent('AppMaintaining', {
        app
    });
    if ((null === (_app_error = app.error) || void 0 === _app_error ? void 0 : _app_error.code) === 'LOAD_ERROR' || (null === (_app_error1 = app.error) || void 0 === _app_error1 ? void 0 : _app_error1.code) === 'APP_ERROR') return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(AppError, {
        app: app,
        error: app.error
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_error_boundary__.ErrorBoundary, {
        FallbackComponent: (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(AppError, {
                app: app,
                error: app.error,
                ...props
            }),
        onError: handleErrors,
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE__context_mjs__.ApplicationContext.Provider, {
                value: app,
                children: [
                    app.maintained && app.maintaining && app.renderComponent('AppMaintainingDialog', {
                        app
                    }),
                    app.renderComponent('AppMain')
                ]
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_click_to_react_component__.ClickToComponent, {})
        ]
    });
}, {
    displayName: 'AppComponent'
});
export { AppComponent };
