/**
 * 路由调试工具（开发环境）
 * 提供路由状态查看、路由匹配可视化、性能分析等功能
 */ "use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "RouterDevTools", {
    enumerable: true,
    get: function() {
        return RouterDevTools;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _antd = require("antd");
const _RouterManager = require("../RouterManager");
const _lifecycle = require("../lifecycle");
const _middleware = require("../middleware");
const _navigation = require("../navigation");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const { Title, Text, Paragraph } = _antd.Typography;
const { TabPane } = _antd.Tabs;
function RouterDevTools() {
    // 只在开发环境显示
    if (process.env.NODE_ENV !== 'development') {
        return null;
    }
    const [visible, setVisible] = (0, _react.useState)(false);
    const [currentRoute, setCurrentRoute] = (0, _react.useState)(null);
    const [routes, setRoutes] = (0, _react.useState)([]);
    const [lifecycleHooks, setLifecycleHooks] = (0, _react.useState)([]);
    const [middlewares, setMiddlewares] = (0, _react.useState)([]);
    // 更新路由信息
    const updateRouteInfo = ()=>{
        try {
            const route = _navigation.RouterNavigation.getCurrentRoute();
            setCurrentRoute(route);
            const routerManager = (0, _RouterManager.getRouterManager)();
            const router = routerManager.getRouter();
            if (router) {
                // 获取路由状态（需要从 react-router-dom 获取）
                const state = router.state;
                setRoutes([
                    {
                        key: 'location',
                        name: '当前路径',
                        value: state.location.pathname
                    },
                    {
                        key: 'search',
                        name: '查询参数',
                        value: state.location.search || '无'
                    },
                    {
                        key: 'hash',
                        name: 'Hash',
                        value: state.location.hash || '无'
                    },
                    {
                        key: 'state',
                        name: '状态',
                        value: JSON.stringify(state.location.state || {}, null, 2)
                    }
                ]);
            }
            // 获取生命周期钩子
            const lifecycleManager = (0, _lifecycle.getRouterLifecycleManager)();
            const hooks = lifecycleManager.getAll();
            setLifecycleHooks(hooks.map((hook, index)=>({
                    key: index,
                    name: `钩子 ${index + 1}`,
                    hasBeforeEach: !!hook.beforeEach,
                    hasAfterEach: !!hook.afterEach
                })));
            // 获取中间件
            const middlewareManager = (0, _middleware.getRouterMiddlewareManager)();
            const middlewareList = middlewareManager.getAll();
            setMiddlewares(middlewareList.map((m, index)=>({
                    key: index,
                    name: m.name || `中间件 ${index + 1}`,
                    priority: m.priority || 100,
                    enabled: m.enabled !== false
                })));
        } catch (error) {
            console.error('更新路由信息失败', error);
        }
    };
    (0, _react.useEffect)(()=>{
        if (visible) {
            updateRouteInfo();
            // 定期更新路由信息
            const interval = setInterval(updateRouteInfo, 1000);
            return ()=>clearInterval(interval);
        }
        return undefined;
    }, [
        visible
    ]);
    const routeColumns = [
        {
            title: '属性',
            dataIndex: 'name',
            key: 'name'
        },
        {
            title: '值',
            dataIndex: 'value',
            key: 'value',
            render: (value)=>/*#__PURE__*/ (0, _jsxruntime.jsx)(Text, {
                    code: true,
                    style: {
                        wordBreak: 'break-all'
                    },
                    children: value
                })
        }
    ];
    const lifecycleColumns = [
        {
            title: '钩子名称',
            dataIndex: 'name',
            key: 'name'
        },
        {
            title: 'beforeEach',
            dataIndex: 'hasBeforeEach',
            key: 'hasBeforeEach',
            render: (has)=>has ? /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Tag, {
                    color: "green",
                    children: "已注册"
                }) : /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Tag, {
                    children: "未注册"
                })
        },
        {
            title: 'afterEach',
            dataIndex: 'hasAfterEach',
            key: 'hasAfterEach',
            render: (has)=>has ? /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Tag, {
                    color: "green",
                    children: "已注册"
                }) : /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Tag, {
                    children: "未注册"
                })
        }
    ];
    const middlewareColumns = [
        {
            title: '中间件名称',
            dataIndex: 'name',
            key: 'name'
        },
        {
            title: '优先级',
            dataIndex: 'priority',
            key: 'priority',
            sorter: (a, b)=>a.priority - b.priority
        },
        {
            title: '状态',
            dataIndex: 'enabled',
            key: 'enabled',
            render: (enabled)=>enabled ? /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Tag, {
                    color: "green",
                    children: "启用"
                }) : /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Tag, {
                    color: "red",
                    children: "禁用"
                })
        }
    ];
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)(_jsxruntime.Fragment, {
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Button, {
                type: "primary",
                onClick: ()=>setVisible(true),
                style: {
                    position: 'fixed',
                    bottom: 20,
                    right: 20,
                    zIndex: 9999
                },
                children: "路由调试"
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Drawer, {
                title: /*#__PURE__*/ (0, _jsxruntime.jsx)("span", {
                    children: "路由调试工具"
                }),
                placement: "right",
                width: 800,
                onClose: ()=>setVisible(false),
                visible: visible,
                extra: /*#__PURE__*/ (0, _jsxruntime.jsxs)(_antd.Space, {
                    children: [
                        /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Button, {
                            onClick: updateRouteInfo,
                            children: "刷新"
                        }),
                        /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Button, {
                            onClick: ()=>setVisible(false),
                            children: "关闭"
                        })
                    ]
                }),
                children: /*#__PURE__*/ (0, _jsxruntime.jsxs)(_antd.Tabs, {
                    defaultActiveKey: "route",
                    children: [
                        /*#__PURE__*/ (0, _jsxruntime.jsxs)(TabPane, {
                            tab: "路由信息",
                            children: [
                                /*#__PURE__*/ (0, _jsxruntime.jsxs)(_antd.Card, {
                                    children: [
                                        /*#__PURE__*/ (0, _jsxruntime.jsx)(Title, {
                                            level: 4,
                                            children: "当前路由"
                                        }),
                                        currentRoute ? /*#__PURE__*/ (0, _jsxruntime.jsxs)(_antd.Space, {
                                            direction: "vertical",
                                            style: {
                                                width: '100%'
                                            },
                                            children: [
                                                /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                                                    children: [
                                                        /*#__PURE__*/ (0, _jsxruntime.jsx)(Text, {
                                                            strong: true,
                                                            children: "路径:"
                                                        }),
                                                        " ",
                                                        /*#__PURE__*/ (0, _jsxruntime.jsx)(Text, {
                                                            code: true,
                                                            children: currentRoute.path
                                                        })
                                                    ]
                                                }),
                                                currentRoute.query && Object.keys(currentRoute.query).length > 0 && /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
                                                    children: [
                                                        /*#__PURE__*/ (0, _jsxruntime.jsx)(Text, {
                                                            strong: true,
                                                            children: "查询参数:"
                                                        }),
                                                        /*#__PURE__*/ (0, _jsxruntime.jsx)("pre", {
                                                            style: {
                                                                background: '#f5f5f5',
                                                                padding: 8,
                                                                borderRadius: 4
                                                            },
                                                            children: JSON.stringify(currentRoute.query, null, 2)
                                                        })
                                                    ]
                                                })
                                            ]
                                        }) : /*#__PURE__*/ (0, _jsxruntime.jsx)(Text, {
                                            type: "secondary",
                                            children: "无法获取当前路由信息"
                                        })
                                    ]
                                }),
                                /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Divider, {}),
                                /*#__PURE__*/ (0, _jsxruntime.jsxs)(_antd.Card, {
                                    children: [
                                        /*#__PURE__*/ (0, _jsxruntime.jsx)(Title, {
                                            level: 4,
                                            children: "路由状态"
                                        }),
                                        /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Table, {
                                            columns: routeColumns,
                                            dataSource: routes,
                                            pagination: false,
                                            size: "small"
                                        })
                                    ]
                                })
                            ]
                        }, "route"),
                        /*#__PURE__*/ (0, _jsxruntime.jsx)(TabPane, {
                            tab: "生命周期钩子",
                            children: /*#__PURE__*/ (0, _jsxruntime.jsxs)(_antd.Card, {
                                children: [
                                    /*#__PURE__*/ (0, _jsxruntime.jsx)(Title, {
                                        level: 4,
                                        children: "已注册的生命周期钩子"
                                    }),
                                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Table, {
                                        columns: lifecycleColumns,
                                        dataSource: lifecycleHooks,
                                        pagination: false,
                                        size: "small"
                                    }),
                                    lifecycleHooks.length === 0 && /*#__PURE__*/ (0, _jsxruntime.jsx)(Text, {
                                        type: "secondary",
                                        children: "暂无注册的生命周期钩子"
                                    })
                                ]
                            })
                        }, "lifecycle"),
                        /*#__PURE__*/ (0, _jsxruntime.jsx)(TabPane, {
                            tab: "中间件",
                            children: /*#__PURE__*/ (0, _jsxruntime.jsxs)(_antd.Card, {
                                children: [
                                    /*#__PURE__*/ (0, _jsxruntime.jsx)(Title, {
                                        level: 4,
                                        children: "已注册的中间件"
                                    }),
                                    /*#__PURE__*/ (0, _jsxruntime.jsx)(_antd.Table, {
                                        columns: middlewareColumns,
                                        dataSource: middlewares,
                                        pagination: false,
                                        size: "small"
                                    }),
                                    middlewares.length === 0 && /*#__PURE__*/ (0, _jsxruntime.jsx)(Text, {
                                        type: "secondary",
                                        children: "暂无注册的中间件"
                                    })
                                ]
                            })
                        }, "middleware"),
                        /*#__PURE__*/ (0, _jsxruntime.jsx)(TabPane, {
                            tab: "使用说明",
                            children: /*#__PURE__*/ (0, _jsxruntime.jsxs)(_antd.Card, {
                                children: [
                                    /*#__PURE__*/ (0, _jsxruntime.jsx)(Title, {
                                        level: 4,
                                        children: "路由调试工具使用说明"
                                    }),
                                    /*#__PURE__*/ (0, _jsxruntime.jsxs)(Paragraph, {
                                        children: [
                                            /*#__PURE__*/ (0, _jsxruntime.jsx)(Text, {
                                                strong: true,
                                                children: "路由信息:"
                                            }),
                                            " 查看当前路由的路径、查询参数、状态等信息"
                                        ]
                                    }),
                                    /*#__PURE__*/ (0, _jsxruntime.jsxs)(Paragraph, {
                                        children: [
                                            /*#__PURE__*/ (0, _jsxruntime.jsx)(Text, {
                                                strong: true,
                                                children: "生命周期钩子:"
                                            }),
                                            " 查看已注册的路由生命周期钩子（beforeEach、afterEach）"
                                        ]
                                    }),
                                    /*#__PURE__*/ (0, _jsxruntime.jsxs)(Paragraph, {
                                        children: [
                                            /*#__PURE__*/ (0, _jsxruntime.jsx)(Text, {
                                                strong: true,
                                                children: "中间件:"
                                            }),
                                            " 查看已注册的路由中间件及其优先级和状态"
                                        ]
                                    }),
                                    /*#__PURE__*/ (0, _jsxruntime.jsx)(Paragraph, {
                                        children: /*#__PURE__*/ (0, _jsxruntime.jsx)(Text, {
                                            type: "secondary",
                                            children: "提示: 此工具仅在开发环境下显示，生产环境不会加载"
                                        })
                                    })
                                ]
                            })
                        }, "help")
                    ]
                })
            })
        ]
    });
}

//# sourceMappingURL=RouterDevTools.js.map