"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    MenuEditor: ()=>MenuEditor,
    SchemaIdContext: ()=>SchemaIdContext,
    useMenuProps: ()=>useMenuProps
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const external_react_router_namespaceObject = require("react-router");
const index_js_namespaceObject = require("../../api-client/index.js");
const external_hooks_index_js_namespaceObject = require("../../hooks/index.js");
const external_schema_component_index_js_namespaceObject = require("../../schema-component/index.js");
const external_acl_index_js_namespaceObject = require("../acl/index.js");
const external_document_title_index_js_namespaceObject = require("../document-title/index.js");
const external_filterByACL_js_namespaceObject = require("./filterByACL.js");
const SchemaIdContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
SchemaIdContext.displayName = 'SchemaIdContext';
const useMenuProps = ()=>{
    const defaultSelectedUid = (0, external_react_namespaceObject.useContext)(SchemaIdContext);
    return {
        selectedUid: defaultSelectedUid,
        defaultSelectedUid
    };
};
const MenuEditor = (props)=>{
    const { sideMenuRef } = props;
    const { setTitle } = (0, external_document_title_index_js_namespaceObject.useDocumentTitle)();
    const navigate = (0, external_react_router_namespaceObject.useNavigate)();
    const params = (0, external_react_router_namespaceObject.useParams)();
    const location = (0, external_react_router_namespaceObject.useLocation)();
    const isMatchAdmin = (0, external_react_router_namespaceObject.useMatch)('/admin');
    const isMatchAdminName = (0, external_react_router_namespaceObject.useMatch)('/admin/:name');
    const defaultSelectedUid = params.name;
    const ctx = (0, external_acl_index_js_namespaceObject.useACLRoleContext)();
    const [current, setCurrent] = (0, external_react_namespaceObject.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, external_hooks_index_js_namespaceObject.useAdminSchemaUid)();
    const { data, loading } = (0, index_js_namespaceObject.useRequest)({
        url: "/uiSchemas:getJsonSchema/".concat(adminSchemaUid)
    }, {
        refreshDeps: [
            adminSchemaUid
        ],
        onSuccess (data) {
            const schema = (0, external_filterByACL_js_namespaceObject.filterByACL)(null == data ? void 0 : data.data, ctx);
            if (isMatchAdmin) {
                const s = (0, external_schema_component_index_js_namespaceObject.findMenuItem)(schema);
                if (s) {
                    navigate("/admin/".concat(s['x-uid']));
                    setTitle(s.title);
                } else navigate("/admin/");
                return;
            }
            if (!isMatchAdminName) return;
            const s = (0, external_schema_component_index_js_namespaceObject.findByUid)(schema, defaultSelectedUid);
            if (s) setTitle(s.title);
            else {
                const s = (0, external_schema_component_index_js_namespaceObject.findMenuItem)(schema);
                if (s) {
                    navigate("/admin/".concat(s['x-uid']));
                    setTitle(s.title);
                } else navigate("/admin/");
            }
        }
    });
    const match = (0, external_react_router_namespaceObject.useMatch)('/admin/:name');
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (match) {
            const schema = (0, external_filterByACL_js_namespaceObject.filterByACL)(null == data ? void 0 : data.data, ctx);
            const s = (0, external_schema_component_index_js_namespaceObject.findByUid)(schema, defaultSelectedUid);
            if (s) setTitle(s.title);
        }
    }, [
        null == data ? void 0 : data.data,
        location.pathname,
        defaultSelectedUid
    ]);
    (0, external_react_namespaceObject.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, external_react_namespaceObject.useMemo)(()=>{
        const s = (0, external_filterByACL_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(SchemaIdContext.Provider, {
        value: defaultSelectedUid,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.SchemaComponent, {
            memoized: true,
            scope: {
                useMenuProps,
                onSelect,
                sideMenuRef,
                defaultSelectedUid
            },
            schema: schema
        })
    });
};
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
