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_react_router_dom__ from "react-router-dom";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__built_in_acl_index_mjs__ from "../built-in/acl/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../schema-component/index.mjs";
const CurrentUserContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
CurrentUserContext.displayName = 'CurrentUserContext';
const useCurrentUserContext = ()=>(0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(CurrentUserContext);
const useCurrentRoles = ()=>{
    var _data_data;
    const { allowAnonymous } = (0, __WEBPACK_EXTERNAL_MODULE__built_in_acl_index_mjs__.useACLRoleContext)();
    const { data } = useCurrentUserContext();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const options = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        var _data_data;
        const roles = ((null == data ? void 0 : null === (_data_data = data.data) || void 0 === _data_data ? void 0 : _data_data.roles) || []).map((param)=>{
            let { name, title } = param;
            return {
                name,
                title: compile(title)
            };
        });
        if (allowAnonymous) roles.push({
            title: 'Anonymous',
            name: 'anonymous'
        });
        return roles;
    }, [
        allowAnonymous,
        null == data ? void 0 : null === (_data_data = data.data) || void 0 === _data_data ? void 0 : _data_data.roles
    ]);
    return options;
};
const CurrentUserProvider = (props)=>{
    const api = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useAPIClient)();
    const result = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)(()=>api.request({
            url: '/auth:check',
            skipNotify: true,
            skipAuth: true
        }).then((res)=>null == res ? void 0 : res.data));
    if (result.loading) return;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(CurrentUserContext.Provider, {
        value: result,
        children: props.children
    });
};
const NavigateIfNotSignIn = (param)=>{
    let { children } = param;
    var _result_data_data, _result_data;
    const result = useCurrentUserContext();
    const { pathname, search } = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useLocation)();
    const redirect = "?redirect=".concat(pathname).concat(search);
    if (!(null == result ? void 0 : null === (_result_data = result.data) || void 0 === _result_data ? void 0 : null === (_result_data_data = _result_data.data) || void 0 === _result_data_data ? void 0 : _result_data_data.id)) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_router_dom__.Navigate, {
        replace: true,
        to: "/signin".concat(redirect)
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
        children: children
    });
};
export { CurrentUserContext, CurrentUserProvider, NavigateIfNotSignIn, useCurrentRoles, useCurrentUserContext };
