import { jsx } from "react/jsx-runtime";
import { createContext, useCallback, useMemo } from "react";
import { Result } from "antd";
import { Navigate, Outlet, useLocation, useNavigate } from "react-router-dom";
import { ADMIN_SETTINGS_PATH, useApp } from "../../application/index.mjs";
import { useCompile } from "../../schema-component/index.mjs";
import { SettingLayout } from "./SettingLayout.mjs";
const SettingsCenterContext = /*#__PURE__*/ 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 = useApp();
    const navigate = useNavigate();
    const location = useLocation();
    const compile = useCompile();
    const settings = 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.length) traverse(item.children);
            });
        }
        traverse(list);
        return list;
    }, [
        app.systemSettingsManager,
        compile
    ]);
    const routeChildren = 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.length) item.children = traverse(item.children);
                return {
                    ...item,
                    name: item.label
                };
            });
        }
        return traverse(list);
    }, [
        app.systemSettingsManager,
        compile
    ]);
    const getFirstDeepChildPath = 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.length) return getFirstDeepChildPath(first.children);
        return first.path;
    }, []);
    const settingsMapByPath = 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.length) traverse(item.children);
            });
        };
        traverse(settings);
        return map;
    }, [
        settings
    ]);
    const settingsMapByKey = 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.length) traverse(item.children);
            });
        };
        traverse(settings);
        return map;
    }, [
        settings
    ]);
    const currentSetting = useMemo(()=>matchRoute(settingsMapByPath, location.pathname), [
        location.pathname,
        settingsMapByPath
    ]);
    if (!currentSetting || location.pathname === ADMIN_SETTINGS_PATH || location.pathname === ADMIN_SETTINGS_PATH + '/') return /*#__PURE__*/ jsx(Navigate, {
        replace: true,
        to: getFirstDeepChildPath(settings)
    });
    var _currentSetting_groupKey;
    return /*#__PURE__*/ jsx(SettingLayout, {
        fullscreen: !!(null == currentSetting ? void 0 : currentSetting.fullscreen),
        route: {
            path: '/',
            children: routeChildren
        },
        selectedKeys: [
            null != (_currentSetting_groupKey = null == currentSetting ? 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__*/ jsx(Outlet, {}) : /*#__PURE__*/ jsx(Result, {
            status: "404",
            title: "404",
            subTitle: "Sorry, the page you visited does not exist."
        })
    });
};
export { AdminSettingsLayout, SettingsCenterContext };
