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__ from "react-router";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__ from "../../hooks/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__acl_index_mjs__ from "../acl/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__document_title_index_mjs__ from "../document-title/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__filterByACL_mjs__ from "./filterByACL.mjs";
const SchemaIdContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
SchemaIdContext.displayName = 'SchemaIdContext';
const useMenuProps = ()=>{
    const defaultSelectedUid = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(SchemaIdContext);
    return {
        selectedUid: defaultSelectedUid,
        defaultSelectedUid
    };
};
const MenuEditor = (props)=>{
    const { sideMenuRef } = props;
    const { setTitle } = (0, __WEBPACK_EXTERNAL_MODULE__document_title_index_mjs__.useDocumentTitle)();
    const navigate = (0, __WEBPACK_EXTERNAL_MODULE_react_router__.useNavigate)();
    const params = (0, __WEBPACK_EXTERNAL_MODULE_react_router__.useParams)();
    const location = (0, __WEBPACK_EXTERNAL_MODULE_react_router__.useLocation)();
    const isMatchAdmin = (0, __WEBPACK_EXTERNAL_MODULE_react_router__.useMatch)('/admin');
    const isMatchAdminName = (0, __WEBPACK_EXTERNAL_MODULE_react_router__.useMatch)('/admin/:name');
    const defaultSelectedUid = params.name;
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__acl_index_mjs__.useACLRoleContext)();
    const [current, setCurrent] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(null);
    const onSelect = (param)=>{
        let { item } = param;
        const schema = item.props.schema || {};
        setTitle(schema.title);
        setCurrent(schema);
        navigate("/admin/".concat(schema['x-uid']));
    };
    const adminSchemaUid = (0, __WEBPACK_EXTERNAL_MODULE__hooks_index_mjs__.useAdminSchemaUid)();
    const { data, loading } = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)({
        url: "/uiSchemas:getJsonSchema/".concat(adminSchemaUid)
    }, {
        refreshDeps: [
            adminSchemaUid
        ],
        onSuccess (data) {
            const schema = (0, __WEBPACK_EXTERNAL_MODULE__filterByACL_mjs__.filterByACL)(null == data ? void 0 : data.data, ctx);
            if (isMatchAdmin) {
                const s = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.findMenuItem)(schema);
                if (s) {
                    navigate("/admin/".concat(s['x-uid']));
                    setTitle(s.title);
                } else navigate("/admin/");
                return;
            }
            if (!isMatchAdminName) return;
            const s = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.findByUid)(schema, defaultSelectedUid);
            if (s) setTitle(s.title);
            else {
                const s = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.findMenuItem)(schema);
                if (s) {
                    navigate("/admin/".concat(s['x-uid']));
                    setTitle(s.title);
                } else navigate("/admin/");
            }
        }
    });
    const match = (0, __WEBPACK_EXTERNAL_MODULE_react_router__.useMatch)('/admin/:name');
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (match) {
            const schema = (0, __WEBPACK_EXTERNAL_MODULE__filterByACL_mjs__.filterByACL)(null == data ? void 0 : data.data, ctx);
            const s = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.findByUid)(schema, defaultSelectedUid);
            if (s) setTitle(s.title);
        }
    }, [
        null == data ? void 0 : data.data,
        location.pathname,
        defaultSelectedUid
    ]);
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        var _Object_values_shift, _current_root, _data_data;
        const properties = (null === (_Object_values_shift = Object.values((null == current ? void 0 : null === (_current_root = current.root) || void 0 === _current_root ? void 0 : _current_root.properties) || {}).shift()) || void 0 === _Object_values_shift ? void 0 : _Object_values_shift['properties']) || (null == data ? void 0 : null === (_data_data = data.data) || void 0 === _data_data ? void 0 : _data_data.properties);
        if (null == sideMenuRef ? void 0 : sideMenuRef.current) {
            const pageType = properties && Object.values(properties).find((item)=>item['x-uid'] === params.name && 'Menu.Item' === item['x-component']);
            const isSettingPage = null == location ? void 0 : location.pathname.includes('/settings');
            if (pageType || isSettingPage) sideMenuRef.current.style.display = 'none';
            else sideMenuRef.current.style.display = 'block';
        }
    }, [
        null == data ? void 0 : data.data,
        params.name,
        sideMenuRef
    ]);
    const schema = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        const s = (0, __WEBPACK_EXTERNAL_MODULE__filterByACL_mjs__.filterByACL)(null == data ? void 0 : data.data, ctx);
        if (null == s ? void 0 : s['x-component-props']) s['x-component-props']['useProps'] = useMenuProps;
        return s;
    }, [
        null == data ? void 0 : data.data
    ]);
    if (loading) return;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(SchemaIdContext.Provider, {
        value: defaultSelectedUid,
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.SchemaComponent, {
            memoized: true,
            scope: {
                useMenuProps,
                onSelect,
                sideMenuRef,
                defaultSelectedUid
            },
            schema: schema
        })
    });
};
export { MenuEditor, SchemaIdContext, useMenuProps };
