import { jsx } from "react/jsx-runtime";
import react, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import lodash from "lodash";
import { useHref, useLocation, useMatch, useNavigate, useParams, useSearchParams } from "react-router-dom";
const NavigateNoUpdateContext = /*#__PURE__*/ react.createContext(null);
NavigateNoUpdateContext.displayName = 'NavigateNoUpdateContext';
const LocationNoUpdateContext = /*#__PURE__*/ react.createContext(null);
LocationNoUpdateContext.displayName = 'LocationNoUpdateContext';
const LocationSearchContext = /*#__PURE__*/ react.createContext('');
LocationSearchContext.displayName = 'LocationSearchContext';
const IsAdminPageContext = /*#__PURE__*/ react.createContext(false);
IsAdminPageContext.displayName = 'IsAdminPageContext';
const CurrentPageUidContext = /*#__PURE__*/ react.createContext('');
CurrentPageUidContext.displayName = 'CurrentPageUidContext';
const MatchAdminContext = /*#__PURE__*/ react.createContext(false);
MatchAdminContext.displayName = 'MatchAdminContext';
const MatchAdminNameContext = /*#__PURE__*/ react.createContext(false);
MatchAdminNameContext.displayName = 'MatchAdminNameContext';
const IsInSettingsPageContext = /*#__PURE__*/ react.createContext(false);
IsInSettingsPageContext.displayName = 'IsInSettingsPageContext';
const CurrentTabUidContext = /*#__PURE__*/ react.createContext('');
CurrentTabUidContext.displayName = 'CurrentTabUidContext';
const SearchParamsContext = /*#__PURE__*/ react.createContext(new URLSearchParams());
SearchParamsContext.displayName = 'SearchParamsContext';
const RouterBasenameContext = /*#__PURE__*/ react.createContext('');
RouterBasenameContext.displayName = 'RouterBasenameContext';
const IsSubPageClosedByPageMenuContext = /*#__PURE__*/ react.createContext({
    isSubPageClosedByPageMenu: ()=>false,
    setFieldSchema: ()=>{},
    reset: ()=>{}
});
IsSubPageClosedByPageMenuContext.displayName = 'IsSubPageClosedByPageMenuContext';
const IsSubPageClosedByPageMenuProvider = (param)=>{
    let { children } = param;
    const params = useParams();
    const prevParamsRef = useRef({});
    const [fieldSchema, setFieldSchema] = useState(null);
    const isSubPageClosedByPageMenu = useCallback(()=>{
        var _fieldSchema_xcomponentprops, _prevParamsRef_current_;
        const result = lodash.isEmpty(params['*']) && (null == fieldSchema ? void 0 : null == (_fieldSchema_xcomponentprops = fieldSchema['x-component-props']) ? void 0 : _fieldSchema_xcomponentprops.openMode) === 'page' && !!(null == (_prevParamsRef_current_ = prevParamsRef.current['*']) ? void 0 : _prevParamsRef_current_.includes(fieldSchema['x-uid']));
        prevParamsRef.current = params;
        return result;
    }, [
        fieldSchema,
        params
    ]);
    const reset = useCallback(()=>{
        prevParamsRef.current = {};
    }, []);
    const value = useMemo(()=>({
            isSubPageClosedByPageMenu,
            setFieldSchema,
            reset
        }), [
        isSubPageClosedByPageMenu,
        reset
    ]);
    return /*#__PURE__*/ jsx(IsSubPageClosedByPageMenuContext.Provider, {
        value: value,
        children: children
    });
};
const RouterBasenameProvider = (param)=>{
    let { children } = param;
    const basenameOfCurrentRouter = useHref('/');
    return /*#__PURE__*/ jsx(RouterBasenameContext.Provider, {
        value: basenameOfCurrentRouter,
        children: children
    });
};
const SearchParamsProvider = (param)=>{
    let { children } = param;
    const [searchParams] = useSearchParams();
    return /*#__PURE__*/ jsx(SearchParamsContext.Provider, {
        value: searchParams,
        children: children
    });
};
const IsInSettingsPageProvider = (param)=>{
    let { children } = param;
    const isInSettingsPage = useLocation().pathname.includes('/settings');
    return /*#__PURE__*/ jsx(IsInSettingsPageContext.Provider, {
        value: isInSettingsPage,
        children: children
    });
};
const MatchAdminProvider = (param)=>{
    let { children } = param;
    const isMatchAdmin = !!useMatch('/admin');
    return /*#__PURE__*/ jsx(MatchAdminContext.Provider, {
        value: isMatchAdmin,
        children: children
    });
};
const MatchAdminNameProvider = (param)=>{
    let { children } = param;
    const isMatchAdminName = !!useMatch('/admin/:name');
    return /*#__PURE__*/ jsx(MatchAdminNameContext.Provider, {
        value: isMatchAdminName,
        children: children
    });
};
const IsAdminPageProvider = (param)=>{
    let { children } = param;
    const location = useLocation();
    const isAdminPage = location.pathname.startsWith('/admin');
    return /*#__PURE__*/ jsx(IsAdminPageContext.Provider, {
        value: isAdminPage,
        children: children
    });
};
const CurrentPageUidProvider = (param)=>{
    let { children } = param;
    const params = useParams();
    return /*#__PURE__*/ jsx(CurrentPageUidContext.Provider, {
        value: params.name,
        children: children
    });
};
const CurrentTabUidProvider = (param)=>{
    let { children } = param;
    const params = useParams();
    return /*#__PURE__*/ jsx(CurrentTabUidContext.Provider, {
        value: params.tabUid,
        children: children
    });
};
const NavigateNoUpdateProvider = (param)=>{
    let { children } = param;
    const navigate = useNavigate();
    const navigateRef = react.useRef(navigate);
    navigateRef.current = navigate;
    const navigateNoUpdate = react.useCallback((to, options)=>{
        navigateRef.current(to, options);
    }, []);
    return /*#__PURE__*/ jsx(NavigateNoUpdateContext.Provider, {
        value: navigateNoUpdate,
        children: children
    });
};
const LocationNoUpdateProvider = (param)=>{
    let { children } = param;
    const location = useLocation();
    const locationRef = react.useRef({});
    useEffect(()=>{
        Object.assign(locationRef.current, location);
    }, [
        location
    ]);
    return /*#__PURE__*/ jsx(LocationNoUpdateContext.Provider, {
        value: locationRef.current,
        children: children
    });
};
const LocationSearchProvider = (param)=>{
    let { children } = param;
    const location = useLocation();
    return /*#__PURE__*/ jsx(LocationSearchContext.Provider, {
        value: location.search,
        children: children
    });
};
const useNavigateNoUpdate = ()=>react.useContext(NavigateNoUpdateContext);
const useLocationNoUpdate = ()=>react.useContext(LocationNoUpdateContext);
const useLocationSearch = ()=>react.useContext(LocationSearchContext);
const useIsAdminPage = ()=>react.useContext(IsAdminPageContext);
const useCurrentPageUid = ()=>react.useContext(CurrentPageUidContext);
const useMatchAdmin = ()=>react.useContext(MatchAdminContext);
const useMatchAdminName = ()=>react.useContext(MatchAdminNameContext);
const useIsInSettingsPage = ()=>react.useContext(IsInSettingsPageContext);
const useCurrentTabUid = ()=>react.useContext(CurrentTabUidContext);
const useCurrentSearchParams = ()=>react.useContext(SearchParamsContext);
const useRouterBasename = ()=>react.useContext(RouterBasenameContext);
const useIsSubPageClosedByPageMenu = (fieldSchema)=>{
    const { isSubPageClosedByPageMenu, setFieldSchema, reset } = react.useContext(IsSubPageClosedByPageMenuContext);
    useEffect(()=>{
        setFieldSchema(fieldSchema);
    }, [
        fieldSchema,
        setFieldSchema
    ]);
    return {
        isSubPageClosedByPageMenu,
        reset
    };
};
const CustomRouterContextProvider = (param)=>{
    let { children } = param;
    return /*#__PURE__*/ jsx(NavigateNoUpdateProvider, {
        children: /*#__PURE__*/ jsx(LocationNoUpdateProvider, {
            children: /*#__PURE__*/ jsx(IsAdminPageProvider, {
                children: /*#__PURE__*/ jsx(LocationSearchProvider, {
                    children: /*#__PURE__*/ jsx(MatchAdminProvider, {
                        children: /*#__PURE__*/ jsx(MatchAdminNameProvider, {
                            children: /*#__PURE__*/ jsx(SearchParamsProvider, {
                                children: /*#__PURE__*/ jsx(RouterBasenameProvider, {
                                    children: /*#__PURE__*/ jsx(IsInSettingsPageProvider, {
                                        children: children
                                    })
                                })
                            })
                        })
                    })
                })
            })
        })
    });
};
export { CurrentPageUidContext, CurrentPageUidProvider, CurrentTabUidContext, CurrentTabUidProvider, CustomRouterContextProvider, IsSubPageClosedByPageMenuProvider, LocationSearchContext, useCurrentPageUid, useCurrentSearchParams, useCurrentTabUid, useIsAdminPage, useIsInSettingsPage, useIsSubPageClosedByPageMenu, useLocationNoUpdate, useLocationSearch, useMatchAdmin, useMatchAdminName, useNavigateNoUpdate, useRouterBasename };
