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__ from "react-router";
import * as __WEBPACK_EXTERNAL_MODULE__application_index_mjs__ from "../../application/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__application_UserSettingsManager_mjs__ from "../../application/UserSettingsManager.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_mjs__ from "./style.mjs";
function getMenuItems(list) {
    return list.map((item)=>{
        var _item_children;
        return {
            key: item.name,
            label: item.label,
            title: item.title,
            icon: item.icon,
            children: (null === (_item_children = item.children) || void 0 === _item_children ? void 0 : _item_children.length) ? getMenuItems(item.children) : void 0
        };
    });
}
function matchRoute(data, url) {
    const keys = Object.keys(data);
    if (data[url]) return data[url];
    for (const pattern of keys){
        const regexPattern = pattern.replace(/:[^/]+/g, '([^/]+)');
        const match = url.match(new RegExp("^".concat(regexPattern, "$")));
        if (match) return data[pattern];
    }
    return null;
}
function replaceRouteParams(urlTemplate, params) {
    return urlTemplate.replace(/:\w+/g, (match)=>{
        const paramName = match.substring(1);
        return (null == params ? void 0 : params[paramName]) || match;
    });
}
const UserSettingsLayout = ()=>{
    var _currentTopLevelSetting_children, _currentTopLevelSetting_children1, _currentTopLevelSetting_children2;
    const { styles, theme } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)();
    const app = (0, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useApp)();
    const navigate = (0, __WEBPACK_EXTERNAL_MODULE_react_router__.useNavigate)();
    const location = (0, __WEBPACK_EXTERNAL_MODULE_react_router__.useLocation)();
    const params = (0, __WEBPACK_EXTERNAL_MODULE_react_router__.useParams)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const settings = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        const list = app.userSettingsManager.getList();
        function traverse(settings) {
            settings.forEach((item)=>{
                var _item_children;
                item.title = compile(item.title);
                item.label = compile(item.title);
                if (null === (_item_children = item.children) || void 0 === _item_children ? void 0 : _item_children.length) traverse(item.children);
            });
        }
        traverse(list);
        return list;
    }, [
        app.userSettingsManager,
        compile
    ]);
    const getFirstDeepChildPath = (0, __WEBPACK_EXTERNAL_MODULE_react__.useCallback)((settings)=>{
        var _first_children;
        if (!settings || !settings.length) return '/admin';
        const first = settings[0];
        if (null === (_first_children = first.children) || void 0 === _first_children ? void 0 : _first_children.length) return getFirstDeepChildPath(first.children);
        return first.path;
    }, []);
    const settingsMapByPath = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        const map = {};
        const traverse = (settings)=>{
            settings.forEach((item)=>{
                var _item_children;
                map[item.path] = item;
                if (null === (_item_children = item.children) || void 0 === _item_children ? void 0 : _item_children.length) traverse(item.children);
            });
        };
        traverse(settings);
        return map;
    }, [
        settings
    ]);
    const currentSetting = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>matchRoute(settingsMapByPath, location.pathname), [
        location.pathname,
        settingsMapByPath
    ]);
    const currentTopLevelSetting = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>{
        if (!currentSetting) return null;
        return settings.find((item)=>item.name === currentSetting.topLevelName);
    }, [
        currentSetting,
        settings
    ]);
    const sidebarMenus = (0, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>getMenuItems(settings.filter((v)=>false !== v.isTopLevel).map((item)=>({
                ...item,
                children: null
            }))), [
        settings
    ]);
    if (!currentSetting || location.pathname === __WEBPACK_EXTERNAL_MODULE__application_UserSettingsManager_mjs__.USER_SETTINGS_PATH || location.pathname === __WEBPACK_EXTERNAL_MODULE__application_UserSettingsManager_mjs__.USER_SETTINGS_PATH + '/') return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_router__.Navigate, {
        replace: true,
        to: getFirstDeepChildPath(settings)
    });
    if (location.pathname === currentTopLevelSetting.path && (null === (_currentTopLevelSetting_children = currentTopLevelSetting.children) || void 0 === _currentTopLevelSetting_children ? void 0 : _currentTopLevelSetting_children.length) > 0) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_router__.Navigate, {
        replace: true,
        to: getFirstDeepChildPath(currentTopLevelSetting.children)
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Layout, {
            children: [
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Layout.Sider, {
                    theme: 'light',
                    children: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Menu, {
                        selectedKeys: [
                            (null == currentSetting ? void 0 : currentSetting.pluginKey) || currentSetting.topLevelName
                        ],
                        style: {
                            height: 'calc(100vh - 46px)',
                            overflowY: 'auto',
                            overflowX: 'hidden'
                        },
                        onClick: (param)=>{
                            let { key } = param;
                            var _plugin_children;
                            const plugin = settings.find((item)=>item.name === key);
                            if (null === (_plugin_children = plugin.children) || void 0 === _plugin_children ? void 0 : _plugin_children.length) return navigate(getFirstDeepChildPath(plugin.children));
                            return navigate(plugin.path);
                        },
                        items: sidebarMenus
                    })
                }),
                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_antd__.Layout.Content, {
                    children: [
                        currentSetting && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.PageHeader, {
                            className: styles.pageHeader,
                            style: {
                                paddingBottom: (null === (_currentTopLevelSetting_children1 = currentTopLevelSetting.children) || void 0 === _currentTopLevelSetting_children1 ? void 0 : _currentTopLevelSetting_children1.length) > 0 && false !== currentTopLevelSetting.showTabs ? 0 : theme.paddingSM
                            },
                            ghost: false,
                            title: currentTopLevelSetting.title,
                            footer: (null === (_currentTopLevelSetting_children2 = currentTopLevelSetting.children) || void 0 === _currentTopLevelSetting_children2 ? void 0 : _currentTopLevelSetting_children2.length) > 0 && false !== currentTopLevelSetting.showTabs && /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Menu, {
                                style: {
                                    marginLeft: -theme.margin
                                },
                                onClick: (param)=>{
                                    let { key } = param;
                                    navigate(replaceRouteParams(app.systemSettingsManager.getRoutePath(key), params));
                                },
                                selectedKeys: [
                                    null == currentSetting ? void 0 : currentSetting.name
                                ],
                                mode: "horizontal",
                                items: getMenuItems(currentTopLevelSetting.children)
                            })
                        }),
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                            className: styles.pageContent,
                            children: currentSetting ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_router__.Outlet, {}) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Result, {
                                status: "404",
                                title: "404",
                                subTitle: "Sorry, the page you visited does not exist."
                            })
                        })
                    ]
                })
            ]
        })
    });
};
export { UserSettingsLayout };
