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__application_index_mjs__ from "../../application/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__ from "../../schema-component/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__SettingLayout_mjs__ from "./SettingLayout.mjs";
const SettingsCenterContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.useApp)();
    const navigate = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useNavigate)();
    const location = (0, __WEBPACK_EXTERNAL_MODULE_react_router_dom__.useLocation)();
    const compile = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_index_mjs__.useCompile)();
    const settings = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.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, __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 settingsMapByKey = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.useMemo)(()=>matchRoute(settingsMapByPath, location.pathname), [
        location.pathname,
        settingsMapByPath
    ]);
    if (!currentSetting || location.pathname === __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.ADMIN_SETTINGS_PATH || location.pathname === __WEBPACK_EXTERNAL_MODULE__application_index_mjs__.ADMIN_SETTINGS_PATH + '/') return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_router_dom__.Navigate, {
        replace: true,
        to: getFirstDeepChildPath(settings)
    });
    var _currentSetting_groupKey;
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE__SettingLayout_mjs__.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, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_router_dom__.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 { AdminSettingsLayout, SettingsCenterContext };
