import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
/**
 * 路由调试工具（开发环境）
 * 提供路由状态查看、路由匹配可视化、性能分析等功能
 */ import React, { useState, useEffect } from "react";
import { Card, Button, Table, Tag, Space, Drawer, Tabs, Typography, Divider } from "antd";
// 注意：@ant-design/icons 需要在使用框架的项目中安装
// 如果未安装，可以移除图标或使用其他图标库
import { getRouterManager } from "../RouterManager";
import { getRouterLifecycleManager } from "../lifecycle";
import { getRouterMiddlewareManager } from "../middleware";
import { RouterNavigation } from "../navigation";
const { Title, Text, Paragraph } = Typography;
const { TabPane } = Tabs;
/**
 * 路由调试工具组件
 */ export function RouterDevTools() {
    // 只在开发环境显示
    if (process.env.NODE_ENV !== 'development') {
        return null;
    }
    const [visible, setVisible] = useState(false);
    const [currentRoute, setCurrentRoute] = useState(null);
    const [routes, setRoutes] = useState([]);
    const [lifecycleHooks, setLifecycleHooks] = useState([]);
    const [middlewares, setMiddlewares] = useState([]);
    // 更新路由信息
    const updateRouteInfo = ()=>{
        try {
            const route = RouterNavigation.getCurrentRoute();
            setCurrentRoute(route);
            const 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 = getRouterLifecycleManager();
            const hooks = lifecycleManager.getAll();
            setLifecycleHooks(hooks.map((hook, index)=>({
                    key: index,
                    name: `钩子 ${index + 1}`,
                    hasBeforeEach: !!hook.beforeEach,
                    hasAfterEach: !!hook.afterEach
                })));
            // 获取中间件
            const middlewareManager = 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);
        }
    };
    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__*/ _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__*/ _jsx(Tag, {
                    color: "green",
                    children: "已注册"
                }) : /*#__PURE__*/ _jsx(Tag, {
                    children: "未注册"
                })
        },
        {
            title: 'afterEach',
            dataIndex: 'hasAfterEach',
            key: 'hasAfterEach',
            render: (has)=>has ? /*#__PURE__*/ _jsx(Tag, {
                    color: "green",
                    children: "已注册"
                }) : /*#__PURE__*/ _jsx(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__*/ _jsx(Tag, {
                    color: "green",
                    children: "启用"
                }) : /*#__PURE__*/ _jsx(Tag, {
                    color: "red",
                    children: "禁用"
                })
        }
    ];
    return /*#__PURE__*/ _jsxs(_Fragment, {
        children: [
            /*#__PURE__*/ _jsx(Button, {
                type: "primary",
                onClick: ()=>setVisible(true),
                style: {
                    position: 'fixed',
                    bottom: 20,
                    right: 20,
                    zIndex: 9999
                },
                children: "路由调试"
            }),
            /*#__PURE__*/ _jsx(Drawer, {
                title: /*#__PURE__*/ _jsx("span", {
                    children: "路由调试工具"
                }),
                placement: "right",
                width: 800,
                onClose: ()=>setVisible(false),
                visible: visible,
                extra: /*#__PURE__*/ _jsxs(Space, {
                    children: [
                        /*#__PURE__*/ _jsx(Button, {
                            onClick: updateRouteInfo,
                            children: "刷新"
                        }),
                        /*#__PURE__*/ _jsx(Button, {
                            onClick: ()=>setVisible(false),
                            children: "关闭"
                        })
                    ]
                }),
                children: /*#__PURE__*/ _jsxs(Tabs, {
                    defaultActiveKey: "route",
                    children: [
                        /*#__PURE__*/ _jsxs(TabPane, {
                            tab: "路由信息",
                            children: [
                                /*#__PURE__*/ _jsxs(Card, {
                                    children: [
                                        /*#__PURE__*/ _jsx(Title, {
                                            level: 4,
                                            children: "当前路由"
                                        }),
                                        currentRoute ? /*#__PURE__*/ _jsxs(Space, {
                                            direction: "vertical",
                                            style: {
                                                width: '100%'
                                            },
                                            children: [
                                                /*#__PURE__*/ _jsxs("div", {
                                                    children: [
                                                        /*#__PURE__*/ _jsx(Text, {
                                                            strong: true,
                                                            children: "路径:"
                                                        }),
                                                        " ",
                                                        /*#__PURE__*/ _jsx(Text, {
                                                            code: true,
                                                            children: currentRoute.path
                                                        })
                                                    ]
                                                }),
                                                currentRoute.query && Object.keys(currentRoute.query).length > 0 && /*#__PURE__*/ _jsxs("div", {
                                                    children: [
                                                        /*#__PURE__*/ _jsx(Text, {
                                                            strong: true,
                                                            children: "查询参数:"
                                                        }),
                                                        /*#__PURE__*/ _jsx("pre", {
                                                            style: {
                                                                background: '#f5f5f5',
                                                                padding: 8,
                                                                borderRadius: 4
                                                            },
                                                            children: JSON.stringify(currentRoute.query, null, 2)
                                                        })
                                                    ]
                                                })
                                            ]
                                        }) : /*#__PURE__*/ _jsx(Text, {
                                            type: "secondary",
                                            children: "无法获取当前路由信息"
                                        })
                                    ]
                                }),
                                /*#__PURE__*/ _jsx(Divider, {}),
                                /*#__PURE__*/ _jsxs(Card, {
                                    children: [
                                        /*#__PURE__*/ _jsx(Title, {
                                            level: 4,
                                            children: "路由状态"
                                        }),
                                        /*#__PURE__*/ _jsx(Table, {
                                            columns: routeColumns,
                                            dataSource: routes,
                                            pagination: false,
                                            size: "small"
                                        })
                                    ]
                                })
                            ]
                        }, "route"),
                        /*#__PURE__*/ _jsx(TabPane, {
                            tab: "生命周期钩子",
                            children: /*#__PURE__*/ _jsxs(Card, {
                                children: [
                                    /*#__PURE__*/ _jsx(Title, {
                                        level: 4,
                                        children: "已注册的生命周期钩子"
                                    }),
                                    /*#__PURE__*/ _jsx(Table, {
                                        columns: lifecycleColumns,
                                        dataSource: lifecycleHooks,
                                        pagination: false,
                                        size: "small"
                                    }),
                                    lifecycleHooks.length === 0 && /*#__PURE__*/ _jsx(Text, {
                                        type: "secondary",
                                        children: "暂无注册的生命周期钩子"
                                    })
                                ]
                            })
                        }, "lifecycle"),
                        /*#__PURE__*/ _jsx(TabPane, {
                            tab: "中间件",
                            children: /*#__PURE__*/ _jsxs(Card, {
                                children: [
                                    /*#__PURE__*/ _jsx(Title, {
                                        level: 4,
                                        children: "已注册的中间件"
                                    }),
                                    /*#__PURE__*/ _jsx(Table, {
                                        columns: middlewareColumns,
                                        dataSource: middlewares,
                                        pagination: false,
                                        size: "small"
                                    }),
                                    middlewares.length === 0 && /*#__PURE__*/ _jsx(Text, {
                                        type: "secondary",
                                        children: "暂无注册的中间件"
                                    })
                                ]
                            })
                        }, "middleware"),
                        /*#__PURE__*/ _jsx(TabPane, {
                            tab: "使用说明",
                            children: /*#__PURE__*/ _jsxs(Card, {
                                children: [
                                    /*#__PURE__*/ _jsx(Title, {
                                        level: 4,
                                        children: "路由调试工具使用说明"
                                    }),
                                    /*#__PURE__*/ _jsxs(Paragraph, {
                                        children: [
                                            /*#__PURE__*/ _jsx(Text, {
                                                strong: true,
                                                children: "路由信息:"
                                            }),
                                            " 查看当前路由的路径、查询参数、状态等信息"
                                        ]
                                    }),
                                    /*#__PURE__*/ _jsxs(Paragraph, {
                                        children: [
                                            /*#__PURE__*/ _jsx(Text, {
                                                strong: true,
                                                children: "生命周期钩子:"
                                            }),
                                            " 查看已注册的路由生命周期钩子（beforeEach、afterEach）"
                                        ]
                                    }),
                                    /*#__PURE__*/ _jsxs(Paragraph, {
                                        children: [
                                            /*#__PURE__*/ _jsx(Text, {
                                                strong: true,
                                                children: "中间件:"
                                            }),
                                            " 查看已注册的路由中间件及其优先级和状态"
                                        ]
                                    }),
                                    /*#__PURE__*/ _jsx(Paragraph, {
                                        children: /*#__PURE__*/ _jsx(Text, {
                                            type: "secondary",
                                            children: "提示: 此工具仅在开发环境下显示，生产环境不会加载"
                                        })
                                    })
                                ]
                            })
                        }, "help")
                    ]
                })
            })
        ]
    });
}

//# sourceMappingURL=RouterDevTools.js.map