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