"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__, {
    AdminSettingsLayout: ()=>AdminSettingsLayout,
    SettingsCenterContext: ()=>SettingsCenterContext
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const external_antd_namespaceObject = require("antd");
const external_react_router_dom_namespaceObject = require("react-router-dom");
const index_js_namespaceObject = require("../../application/index.js");
const external_schema_component_index_js_namespaceObject = require("../../schema-component/index.js");
const external_SettingLayout_js_namespaceObject = require("./SettingLayout.js");
const SettingsCenterContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)({});
SettingsCenterContext.displayName = 'SettingsCenterContext';
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;
}
const AdminSettingsLayout = ()=>{
    const app = (0, index_js_namespaceObject.useApp)();
    const navigate = (0, external_react_router_dom_namespaceObject.useNavigate)();
    const location = (0, external_react_router_dom_namespaceObject.useLocation)();
    const compile = (0, external_schema_component_index_js_namespaceObject.useCompile)();
    const settings = (0, external_react_namespaceObject.useMemo)(()=>{
        const list = app.systemSettingsManager.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.systemSettingsManager,
        compile
    ]);
    const routeChildren = (0, external_react_namespaceObject.useMemo)(()=>{
        const list = app.systemSettingsManager.getList();
        function traverse(settings) {
            return settings.map((item)=>{
                var _item_children;
                if (item.path.includes(':')) item.hideInMenu = true;
                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) item.children = traverse(item.children);
                return {
                    ...item,
                    name: item.label
                };
            });
        }
        return traverse(list);
    }, [
        app.systemSettingsManager,
        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 settingsMapByKey = (0, external_react_namespaceObject.useMemo)(()=>{
        const map = {};
        const traverse = (settings)=>{
            settings.forEach((item)=>{
                var _item_children;
                map[item.key] = 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
    ]);
    if (!currentSetting || location.pathname === index_js_namespaceObject.ADMIN_SETTINGS_PATH || location.pathname === index_js_namespaceObject.ADMIN_SETTINGS_PATH + '/') return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_router_dom_namespaceObject.Navigate, {
        replace: true,
        to: getFirstDeepChildPath(settings)
    });
    var _currentSetting_groupKey;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_SettingLayout_js_namespaceObject.SettingLayout, {
        fullscreen: !!(null == currentSetting ? void 0 : currentSetting.fullscreen),
        route: {
            path: '/',
            children: routeChildren
        },
        selectedKeys: [
            null !== (_currentSetting_groupKey = null == currentSetting ? void 0 : currentSetting.groupKey) && void 0 !== _currentSetting_groupKey ? _currentSetting_groupKey : null == currentSetting ? void 0 : currentSetting.key
        ],
        onClick: (param)=>{
            let { key } = param;
            const setting = settingsMapByKey[key];
            if (null == setting ? void 0 : setting.path) navigate(setting.path);
        },
        children: currentSetting ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_router_dom_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
});
