import { jsx as _jsx } from "@emotion/react/jsx-runtime";
import { QoreClient } from "@qorebase/client";
import React from "react";
import { useLocation, useNavigate } from "react-router-dom";
import qore from "@qorebase/app-runtime/src/hooks/qore";
import { RuntimeContext } from "@qorebase/app-runtime/src/RuntimeContext";
export default function PreviewUser(props) {
    const config = RuntimeContext.useSelectState((state) => state.config);
    const loggedInPage = RuntimeContext.useSelectState((state) => { var _a; return (_a = state.config.initialPage) === null || _a === void 0 ? void 0 : _a.loggedInPage; });
    const location = useLocation();
    const navigate = useNavigate();
    const client = React.useMemo(() => new QoreClient({
        endpoint: config.endpoint,
        onError: (error) => {
            var _a, _b;
            if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401 &&
                !["/page/login", "/page/register"].includes(location.pathname) &&
                error.config.url !== "/v1/user") {
                navigate(`/page/login?nextPage=${loggedInPage || location.pathname}`);
            }
            if (((_b = error.response) === null || _b === void 0 ? void 0 : _b.status) === 403) {
                navigate("/page/forbidden");
            }
        },
        getToken: () => localStorage.getItem(`${config.projectID}_user_token`),
    }), [config, location, loggedInPage]);
    return _jsx(qore.QoreProvider, Object.assign({ value: client }, { children: props.children }));
}
//# sourceMappingURL=PreviewUser.js.map