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_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_react_router_dom__ from "react-router-dom";
import * as __WEBPACK_EXTERNAL_MODULE__index_mjs__ from "../../index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__application_Plugin_mjs__ from "../../application/Plugin.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__variables_index_mjs__ from "../../variables/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__AdminContent_mjs__ from "./AdminContent.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__AdminTabs_mjs__ from "./AdminTabs.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_mjs__ from "./style.mjs";
export * from "./NoticeArea.mjs";
const filterByACL = (schema, options)=>{
    const { allowAll, allowMenuItemIds = [] } = options;
    if (allowAll) return schema;
    const filterSchema = (s)=>{
        if (!s) return;
        for(const key in s.properties)if (Object.prototype.hasOwnProperty.call(s.properties, key)) {
            const element = s.properties[key];
            if (element['x-uid'] && !allowMenuItemIds.includes(element['x-uid'])) delete s.properties[key];
            if (element['x-uid']) filterSchema(element);
        }
    };
    filterSchema(schema);
    return schema;
};
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 { setTitle } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useDocumentTitle)();
    const navigate = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useNavigate)();
    const params = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useParams)();
    const location1 = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useLocation)();
    const isMatchAdmin = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useMatch)('/admin');
    const isMatchAdminName = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useMatch)('/admin/:name');
    const defaultSelectedUid = params.name;
    const { sideMenuRef } = props;
    const ctx = (0, __WEBPACK_EXTERNAL_MODULE__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__index_mjs__.useAdminSchemaUid)();
    const { data, loading } = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useRequest)({
        url: "/uiSchemas:getJsonSchema/".concat(adminSchemaUid)
    }, {
        refreshDeps: [
            adminSchemaUid
        ],
        onSuccess (data) {
            const schema = filterByACL(null == data ? void 0 : data.data, ctx);
            if (isMatchAdmin) {
                const s = (0, __WEBPACK_EXTERNAL_MODULE__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__index_mjs__.findByUid)(schema, defaultSelectedUid);
            if (s) setTitle(s.title);
            else {
                const s = (0, __WEBPACK_EXTERNAL_MODULE__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_dom__.useMatch)('/admin/:name');
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        if (match) {
            const schema = filterByACL(null == data ? void 0 : data.data, ctx);
            const s = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.findByUid)(schema, defaultSelectedUid);
            if (s) setTitle(s.title);
        }
    }, [
        null == data ? void 0 : data.data,
        location1.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 == location1 ? void 0 : location1.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 = 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__index_mjs__.SchemaComponent, {
            memoized: true,
            scope: {
                useMenuProps,
                onSelect,
                sideMenuRef,
                defaultSelectedUid
            },
            schema: schema
        })
    });
};
const InternalAdminLayout = (props)=>{
    var _result_data_data_logo, _result_data_data, _result_data, _result_data_data1, _result_data1;
    const sideMenuRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)();
    const result = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useSystemSettings)();
    const app = (0, __WEBPACK_EXTERNAL_MODULE__index_mjs__.useApp)();
    const params = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useParams)();
    const { styles } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)();
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Layout, {
        className: styles.layout,
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Layout.Header, {
                className: styles.header,
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                    className: styles.headerA,
                    children: [
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                            className: styles.headerB,
                            children: [
                                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                                    className: styles.titleContainer,
                                    onClick: ()=>{
                                        location.href = app.adminUrl;
                                    },
                                    children: [
                                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("img", {
                                            className: styles.logo,
                                            src: 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 : null === (_result_data_data_logo = _result_data_data.logo) || void 0 === _result_data_data_logo ? void 0 : _result_data_data_logo.url
                                        }),
                                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("h1", {
                                            className: styles.title,
                                            children: null == result ? void 0 : null === (_result_data1 = result.data) || void 0 === _result_data1 ? void 0 : null === (_result_data_data1 = _result_data1.data) || void 0 === _result_data_data1 ? void 0 : _result_data_data1.title
                                        })
                                    ]
                                }),
                                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(MenuEditor, {
                                    sideMenuRef: sideMenuRef
                                }),
                                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                                    className: styles.editor,
                                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__AdminTabs_mjs__.AdminTabs, {})
                                })
                            ]
                        }),
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                            className: styles.right,
                            children: [
                                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.PinnedPluginList, {}),
                                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.CurrentUser, {})
                            ]
                        })
                    ]
                })
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Layout, {
                children: [
                    params.name && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Layout.Sider, {
                        className: styles.sider,
                        theme: 'light',
                        ref: sideMenuRef
                    }),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Layout.Content, {
                        className: styles.main,
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                            className: "amplifier-block",
                            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__AdminContent_mjs__.AdminContent, {})
                        })
                    })
                ]
            })
        ]
    });
};
const AdminProvider = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.NoticeManagerProvider, {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.CurrentAppInfoProvider, {
            children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.NavigateIfNotSignIn, {
                children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.RemoteCollectionManagerProvider, {
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__variables_index_mjs__.VariablesProvider, {
                        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__index_mjs__.ACLRolesCheckProvider, {
                            children: props.children
                        })
                    })
                })
            })
        })
    });
const AdminLayout = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(AdminProvider, {
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(NoticeArea, {}),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(InternalAdminLayout, {
                ...props
            })
        ]
    });
class AdminLayoutPlugin extends __WEBPACK_EXTERNAL_MODULE__application_Plugin_mjs__.Plugin {
    async afterAdd() {
        await this.app.pm.add(__WEBPACK_EXTERNAL_MODULE__index_mjs__.RemoteSchemaTemplateManagerPlugin);
    }
    async load() {
        this.app.addComponents({
            AdminLayout
        });
    }
}
export { AdminLayout, AdminLayoutPlugin, AdminProvider, InternalAdminLayout, MenuEditor };
