import { jsx } from "react/jsx-runtime";
import { createContext, useContext, useEffect, useMemo, useState } from "react";
import { useLocation, useMatch, useNavigate, useParams } from "react-router";
import { useRequest } from "../../api-client/index.mjs";
import { useAdminSchemaUid } from "../../hooks/index.mjs";
import { SchemaComponent, findByUid, findMenuItem } from "../../schema-component/index.mjs";
import { useACLRoleContext } from "../acl/index.mjs";
import { useDocumentTitle } from "../document-title/index.mjs";
import { filterByACL } from "./filterByACL.mjs";
const SchemaIdContext = /*#__PURE__*/ createContext(null);
SchemaIdContext.displayName = 'SchemaIdContext';
const useMenuProps = ()=>{
    const defaultSelectedUid = useContext(SchemaIdContext);
    return {
        selectedUid: defaultSelectedUid,
        defaultSelectedUid
    };
};
const MenuEditor = (props)=>{
    const { sideMenuRef } = props;
    const { setTitle } = useDocumentTitle();
    const navigate = useNavigate();
    const params = useParams();
    const location = useLocation();
    const isMatchAdmin = useMatch('/admin');
    const isMatchWelcome = useMatch('/admin/welcome');
    const isMatchAdminName = useMatch('/admin/:name');
    const defaultSelectedUid = params.name;
    const ctx = useACLRoleContext();
    const [current, setCurrent] = 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 = useAdminSchemaUid();
    const { data, loading } = useRequest({
        url: "/uiSchemas:getJsonSchema/".concat(adminSchemaUid)
    }, {
        refreshDeps: [
            adminSchemaUid
        ],
        onSuccess (data) {
            const schema = filterByACL(null == data ? void 0 : data.data, ctx);
            if (isMatchAdmin && !isMatchWelcome) {
                const s = findMenuItem(schema);
                if (s) {
                    navigate("/admin/".concat(s['x-uid']));
                    setTitle(s.title);
                } else navigate("/admin/welcome");
                return;
            }
            if (!isMatchAdminName || isMatchWelcome) return;
            const s = findByUid(schema, defaultSelectedUid);
            if (s) setTitle(s.title);
            else {
                const s = findMenuItem(schema);
                if (s) {
                    navigate("/admin/".concat(s['x-uid']));
                    setTitle(s.title);
                } else navigate("/admin/");
            }
        }
    });
    const match = useMatch('/admin/:name');
    useEffect(()=>{
        if (match) {
            const schema = filterByACL(null == data ? void 0 : data.data, ctx);
            const s = findByUid(schema, defaultSelectedUid);
            if (s) setTitle(s.title);
        }
    }, [
        null == data ? void 0 : data.data,
        location.pathname,
        defaultSelectedUid
    ]);
    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.properties) || {}).shift()) ? void 0 : _Object_values_shift['properties']) || (null == data ? void 0 : null == (_data_data = 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 = useMemo(()=>{
        const s = 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__*/ jsx(SchemaIdContext.Provider, {
        value: defaultSelectedUid,
        children: /*#__PURE__*/ jsx(SchemaComponent, {
            memoized: true,
            scope: {
                useMenuProps,
                onSelect,
                sideMenuRef,
                defaultSelectedUid
            },
            schema: schema
        })
    });
};
export { MenuEditor, SchemaIdContext, useMenuProps };
