import { jsx as _jsx, jsxs as _jsxs } from "@emotion/react/jsx-runtime";
import React from "react";
import { Navigate, Route, Routes } from "react-router-dom";
import { useNavigations } from "./hooks/navigations";
import { RuntimeContext } from "@qorebase/app-component";
import { RuntimeThemeProvider } from "./RuntimeTheme";
export default function RuntimeProviders(props) {
    const { navigations } = useNavigations();
    const config = RuntimeContext.useSelectState((state) => state.app.config);
    const pagesMap = RuntimeContext.useSelectState((state) => state.app.maps.page);
    const firstPage = React.useMemo(() => Object.values(pagesMap).filter((page) => page.type !== "system")[0], [navigations, pagesMap]);
    const access_token = localStorage.getItem(`${config.projectID}_user_token`);
    const redirectPage = RuntimeContext.useSelectState((state) => {
        var _a, _b;
        return access_token
            ? (_a = state.app.config.initialPage) === null || _a === void 0 ? void 0 : _a.loggedInPage
            : (_b = state.app.config.initialPage) === null || _b === void 0 ? void 0 : _b.entryPage;
    });
    const firstPageNavigate = React.useMemo(() => (pagesMap[redirectPage] ? redirectPage : firstPage === null || firstPage === void 0 ? void 0 : firstPage.id), [pagesMap, firstPage === null || firstPage === void 0 ? void 0 : firstPage.id, redirectPage]);
    return (_jsx(RuntimeThemeProvider, { children: _jsxs(Routes, { children: [(firstPage || redirectPage) && (_jsx(Route, { path: "/", element: _jsx(Navigate, { to: `/page/${firstPageNavigate}` }) })), _jsx(Route, { path: "*", element: props.children })] }) }));
}
//# sourceMappingURL=RuntimeProviders.js.map