"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__, {
    UserSettingsLayout: ()=>UserSettingsLayout
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const external_antd_namespaceObject = require("antd");
const external_react_router_namespaceObject = require("react-router");
const index_js_namespaceObject = require("../../application/index.js");
const UserSettingsManager_js_namespaceObject = require("../../application/UserSettingsManager.js");
const external_schema_component_index_js_namespaceObject = require("../../schema-component/index.js");
const external_style_js_namespaceObject = require("./style.js");
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, external_style_js_namespaceObject.useStyles)();
    const app = (0, index_js_namespaceObject.useApp)();
    const navigate = (0, external_react_router_namespaceObject.useNavigate)();
    const location = (0, external_react_router_namespaceObject.useLocation)();
    const params = (0, external_react_router_namespaceObject.useParams)();
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const settings = (0, external_react_namespaceObject.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, external_react_namespaceObject.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, external_react_namespaceObject.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, external_react_namespaceObject.useMemo)(()=>matchRoute(settingsMapByPath, location.pathname), [
        location.pathname,
        settingsMapByPath
    ]);
    const currentTopLevelSetting = (0, external_react_namespaceObject.useMemo)(()=>{
        if (!currentSetting) return null;
        return settings.find((item)=>item.name === currentSetting.topLevelName);
    }, [
        currentSetting,
        settings
    ]);
    const sidebarMenus = (0, external_react_namespaceObject.useMemo)(()=>getMenuItems(settings.filter((v)=>false !== v.isTopLevel).map((item)=>({
                ...item,
                children: null
            }))), [
        settings
    ]);
    if (!currentSetting || location.pathname === UserSettingsManager_js_namespaceObject.USER_SETTINGS_PATH || location.pathname === UserSettingsManager_js_namespaceObject.USER_SETTINGS_PATH + '/') return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_router_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_react_router_namespaceObject.Navigate, {
        replace: true,
        to: getFirstDeepChildPath(currentTopLevelSetting.children)
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Layout, {
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Layout.Sider, {
                    theme: 'light',
                    children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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, jsx_runtime_namespaceObject.jsxs)(external_antd_namespaceObject.Layout.Content, {
                    children: [
                        currentSetting && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_schema_component_index_js_namespaceObject.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, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.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, jsx_runtime_namespaceObject.jsx)("div", {
                            className: styles.pageContent,
                            children: currentSetting ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_router_namespaceObject.Outlet, {}) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Result, {
                                status: "404",
                                title: "404",
                                subTitle: "Sorry, the page you visited does not exist."
                            })
                        })
                    ]
                })
            ]
        })
    });
};
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
});
