import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__ from "@tachybase/test/client";
import * as __WEBPACK_EXTERNAL_MODULE_axios__ from "axios";
import * as __WEBPACK_EXTERNAL_MODULE_axios_mock_adapter__ from "axios-mock-adapter";
import * as __WEBPACK_EXTERNAL_MODULE_react_router_dom__ from "react-router-dom";
import * as __WEBPACK_EXTERNAL_MODULE_vitest__ from "vitest";
import * as __WEBPACK_EXTERNAL_MODULE__Application_mjs__ from "../Application.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__RouterManager_mjs__ from "../RouterManager.mjs";
describe('Router', ()=>{
    let app;
    (0, __WEBPACK_EXTERNAL_MODULE_vitest__.beforeAll)(()=>{
        const mock = new __WEBPACK_EXTERNAL_MODULE_axios_mock_adapter__["default"](__WEBPACK_EXTERNAL_MODULE_axios__["default"]);
        mock.onGet('pm:listEnabled').reply(200, {
            data: []
        });
        app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application();
    });
    describe('add routes', ()=>{
        let router;
        beforeEach(()=>{
            router = new __WEBPACK_EXTERNAL_MODULE__RouterManager_mjs__.RouterManager({
                type: 'memory',
                initialEntries: [
                    '/'
                ]
            }, app);
        });
        it('basic', ()=>{
            const route1 = {
                path: '/',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {})
            };
            router.add('test', route1);
            expect(router.getRoutesTree()).toHaveLength(1);
            expect(router.getRoutesTree()).toEqual([
                route1
            ]);
            const route2 = {
                path: '/test2',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {})
            };
            router.add('test2', route2);
            expect(router.getRoutesTree()).toHaveLength(2);
            expect(router.getRoutesTree()).toMatchObject([
                route1,
                route2
            ]);
            expect(router.getRoutes()).toEqual({
                test: route1,
                test2: route2
            });
            expect(router.get('test')).toEqual(route1);
            expect(router.get('test2')).toEqual(route2);
            expect(router.has('test')).toBeTruthy();
        });
        it('createRouterManager', ()=>{
            const router = (0, __WEBPACK_EXTERNAL_MODULE__RouterManager_mjs__.createRouterManager)({
                type: 'memory',
                initialEntries: [
                    '/'
                ]
            }, app);
            const route = {
                path: '/',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {})
            };
            router.add('test', route);
            expect(router.has('test')).toBeTruthy();
        });
        it('nested route', ()=>{
            const route1 = {
                path: '/',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {})
            };
            const route2 = {
                path: '/test2',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {})
            };
            router.add('test', route1);
            router.add('test.test2', route2);
            expect(router.getRoutesTree()).toEqual([
                {
                    ...route1,
                    children: [
                        route2
                    ]
                }
            ]);
        });
        it('nested route with empty parent', ()=>{
            const route1 = {
                path: '/',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {})
            };
            const route2 = {
                path: '/test2',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {})
            };
            const route3 = {
                path: '/test3',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {})
            };
            router.add('test', route1);
            router.add('test.empty.test2', route2);
            router.add('test.empty2.empty3.test3', route3);
            expect(router.getRoutesTree()).toEqual([
                {
                    ...route1,
                    children: [
                        route2,
                        route3
                    ]
                }
            ]);
        });
        it('Component', ()=>{
            const Hello = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {});
            const route = {
                path: '/',
                Component: Hello
            };
            router.add('test', route);
            expect(router.getRoutesTree()).toEqual([
                {
                    path: '/',
                    element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Hello, {}),
                    children: void 0
                }
            ]);
        });
        it('Component is string', ()=>{
            const router = new __WEBPACK_EXTERNAL_MODULE__RouterManager_mjs__.RouterManager({
                type: 'memory',
                initialEntries: [
                    '/'
                ]
            }, app);
            const Hello = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {});
            app.addComponents({
                Hello
            });
            const route = {
                path: '/',
                Component: 'Hello'
            };
            router.add('test', route);
            expect(router.getRoutesTree()).toEqual([
                {
                    path: '/',
                    element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Hello, {}),
                    children: void 0
                }
            ]);
        });
    });
    describe('remove', ()=>{
        let router;
        beforeEach(()=>{
            router = new __WEBPACK_EXTERNAL_MODULE__RouterManager_mjs__.RouterManager({
                type: 'memory',
                initialEntries: [
                    '/'
                ]
            }, app);
        });
        it('basic', ()=>{
            const route1 = {
                path: '/',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {})
            };
            router.add('test', route1);
            expect(router.getRoutesTree()).toEqual([
                route1
            ]);
            router.remove('test');
            expect(router.getRoutesTree()).toEqual([]);
        });
    });
    describe('getRouterComponent', ()=>{
        it('basic', async ()=>{
            const router = new __WEBPACK_EXTERNAL_MODULE__RouterManager_mjs__.RouterManager({
                type: 'memory',
                initialEntries: [
                    '/'
                ]
            }, app);
            const Layout = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                    children: [
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                            children: [
                                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_router_dom__.Link, {
                                    to: "/",
                                    children: "Home"
                                }),
                                /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_router_dom__.Link, {
                                    to: "/about",
                                    children: "About"
                                })
                            ]
                        }),
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_router_dom__.Outlet, {})
                    ]
                });
            router.add('root', {
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Layout, {})
            });
            router.add('root.home', {
                path: '/',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    children: "HomeComponent"
                })
            });
            router.add('root.about', {
                path: '/about',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    children: "AboutComponent"
                })
            });
            const RouterComponent = router.getRouterComponent();
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(RouterComponent, {}));
            expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('HomeComponent')).toBeInTheDocument();
            await __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.userEvent.click(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('About'));
            expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('AboutComponent')).toBeInTheDocument();
        });
        it('basename and type', async ()=>{
            const router = new __WEBPACK_EXTERNAL_MODULE__RouterManager_mjs__.RouterManager({
                type: 'browser'
            }, app);
            router.setType('hash');
            router.setBasename('/admin');
            router.add('home', {
                path: '/',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    "data-testid": "content",
                    children: "123"
                })
            });
            const RouterComponent = router.getRouterComponent();
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(RouterComponent, {}));
            expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.queryByTestId('content')).not.toBeInTheDocument();
            window.location.hash = '#/admin';
            await (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.waitFor)(()=>{
                expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByTestId('content')).toBeInTheDocument();
            });
        });
        it('BaseLayout', ()=>{
            const router = new __WEBPACK_EXTERNAL_MODULE__RouterManager_mjs__.RouterManager({
                type: 'memory',
                initialEntries: [
                    '/'
                ]
            }, app);
            router.add('home', {
                path: '/',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    children: "HomeComponent"
                })
            });
            const RouterComponent = router.getRouterComponent();
            const BaseLayout = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                    children: [
                        "BaseLayout ",
                        props.children
                    ]
                });
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(RouterComponent, {
                BaseLayout: BaseLayout
            }));
            expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('HomeComponent')).toBeInTheDocument();
            expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('BaseLayout')).toBeInTheDocument();
        });
        it('nested router', ()=>{
            const router = new __WEBPACK_EXTERNAL_MODULE__RouterManager_mjs__.RouterManager({
                type: 'memory',
                initialEntries: [
                    '/'
                ]
            }, app);
            const Test = ()=>{
                const router2 = new __WEBPACK_EXTERNAL_MODULE__RouterManager_mjs__.RouterManager({
                    type: 'memory',
                    initialEntries: [
                        '/'
                    ]
                }, app);
                router2.add('rooter2', {
                    path: '/',
                    element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                        children: "Router2"
                    })
                });
                const RouterComponent = router2.getRouterComponent();
                return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                    children: [
                        "Router1",
                        /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(RouterComponent, {})
                    ]
                });
            };
            router.add('home', {
                path: '/',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Test, {})
            });
            const RouterComponent = router.getRouterComponent();
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(RouterComponent, {}));
            expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('Router1')).toBeInTheDocument();
            expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('Router2')).toBeInTheDocument();
        });
    });
});
