"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
var __webpack_exports__ = {};
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
require("react");
const client_namespaceObject = require("@tachybase/test/client");
const external_axios_namespaceObject = require("axios");
var external_axios_default = /*#__PURE__*/ __webpack_require__.n(external_axios_namespaceObject);
const external_axios_mock_adapter_namespaceObject = require("axios-mock-adapter");
var external_axios_mock_adapter_default = /*#__PURE__*/ __webpack_require__.n(external_axios_mock_adapter_namespaceObject);
const external_react_router_dom_namespaceObject = require("react-router-dom");
const external_vitest_namespaceObject = require("vitest");
const external_Application_js_namespaceObject = require("../Application.js");
const external_RouterManager_js_namespaceObject = require("../RouterManager.js");
describe('Router', ()=>{
    let app;
    (0, external_vitest_namespaceObject.beforeAll)(()=>{
        const mock = new (external_axios_mock_adapter_default())(external_axios_default());
        mock.onGet('pm:listEnabled').reply(200, {
            data: []
        });
        app = new external_Application_js_namespaceObject.Application();
    });
    describe('add routes', ()=>{
        let router;
        beforeEach(()=>{
            router = new external_RouterManager_js_namespaceObject.RouterManager({
                type: 'memory',
                initialEntries: [
                    '/'
                ]
            }, app);
        });
        it('basic', ()=>{
            const route1 = {
                path: '/',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {})
            };
            router.add('test', route1);
            expect(router.getRoutesTree()).toHaveLength(1);
            expect(router.getRoutesTree()).toEqual([
                route1
            ]);
            const route2 = {
                path: '/test2',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.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, external_RouterManager_js_namespaceObject.createRouterManager)({
                type: 'memory',
                initialEntries: [
                    '/'
                ]
            }, app);
            const route = {
                path: '/',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {})
            };
            router.add('test', route);
            expect(router.has('test')).toBeTruthy();
        });
        it('nested route', ()=>{
            const route1 = {
                path: '/',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {})
            };
            const route2 = {
                path: '/test2',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.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, jsx_runtime_namespaceObject.jsx)("div", {})
            };
            const route2 = {
                path: '/test2',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {})
            };
            const route3 = {
                path: '/test3',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.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, jsx_runtime_namespaceObject.jsx)("div", {});
            const route = {
                path: '/',
                Component: Hello
            };
            router.add('test', route);
            expect(router.getRoutesTree()).toEqual([
                {
                    path: '/',
                    element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Hello, {}),
                    children: void 0
                }
            ]);
        });
        it('Component is string', ()=>{
            const router = new external_RouterManager_js_namespaceObject.RouterManager({
                type: 'memory',
                initialEntries: [
                    '/'
                ]
            }, app);
            const Hello = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {});
            app.addComponents({
                Hello
            });
            const route = {
                path: '/',
                Component: 'Hello'
            };
            router.add('test', route);
            expect(router.getRoutesTree()).toEqual([
                {
                    path: '/',
                    element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Hello, {}),
                    children: void 0
                }
            ]);
        });
    });
    describe('remove', ()=>{
        let router;
        beforeEach(()=>{
            router = new external_RouterManager_js_namespaceObject.RouterManager({
                type: 'memory',
                initialEntries: [
                    '/'
                ]
            }, app);
        });
        it('basic', ()=>{
            const route1 = {
                path: '/',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.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 external_RouterManager_js_namespaceObject.RouterManager({
                type: 'memory',
                initialEntries: [
                    '/'
                ]
            }, app);
            const Layout = ()=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                    children: [
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                            children: [
                                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_router_dom_namespaceObject.Link, {
                                    to: "/",
                                    children: "Home"
                                }),
                                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_router_dom_namespaceObject.Link, {
                                    to: "/about",
                                    children: "About"
                                })
                            ]
                        }),
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_react_router_dom_namespaceObject.Outlet, {})
                    ]
                });
            router.add('root', {
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Layout, {})
            });
            router.add('root.home', {
                path: '/',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    children: "HomeComponent"
                })
            });
            router.add('root.about', {
                path: '/about',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    children: "AboutComponent"
                })
            });
            const RouterComponent = router.getRouterComponent();
            (0, client_namespaceObject.render)(/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(RouterComponent, {}));
            expect(client_namespaceObject.screen.getByText('HomeComponent')).toBeInTheDocument();
            await client_namespaceObject.userEvent.click(client_namespaceObject.screen.getByText('About'));
            expect(client_namespaceObject.screen.getByText('AboutComponent')).toBeInTheDocument();
        });
        it('basename and type', async ()=>{
            const router = new external_RouterManager_js_namespaceObject.RouterManager({
                type: 'browser'
            }, app);
            router.setType('hash');
            router.setBasename('/admin');
            router.add('home', {
                path: '/',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    "data-testid": "content",
                    children: "123"
                })
            });
            const RouterComponent = router.getRouterComponent();
            (0, client_namespaceObject.render)(/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(RouterComponent, {}));
            expect(client_namespaceObject.screen.queryByTestId('content')).not.toBeInTheDocument();
            window.location.hash = '#/admin';
            await (0, client_namespaceObject.waitFor)(()=>{
                expect(client_namespaceObject.screen.getByTestId('content')).toBeInTheDocument();
            });
        });
        it('BaseLayout', ()=>{
            const router = new external_RouterManager_js_namespaceObject.RouterManager({
                type: 'memory',
                initialEntries: [
                    '/'
                ]
            }, app);
            router.add('home', {
                path: '/',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    children: "HomeComponent"
                })
            });
            const RouterComponent = router.getRouterComponent();
            const BaseLayout = (props)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                    children: [
                        "BaseLayout ",
                        props.children
                    ]
                });
            (0, client_namespaceObject.render)(/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(RouterComponent, {
                BaseLayout: BaseLayout
            }));
            expect(client_namespaceObject.screen.getByText('HomeComponent')).toBeInTheDocument();
            expect(client_namespaceObject.screen.getByText('BaseLayout')).toBeInTheDocument();
        });
        it('nested router', ()=>{
            const router = new external_RouterManager_js_namespaceObject.RouterManager({
                type: 'memory',
                initialEntries: [
                    '/'
                ]
            }, app);
            const Test = ()=>{
                const router2 = new external_RouterManager_js_namespaceObject.RouterManager({
                    type: 'memory',
                    initialEntries: [
                        '/'
                    ]
                }, app);
                router2.add('rooter2', {
                    path: '/',
                    element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                        children: "Router2"
                    })
                });
                const RouterComponent = router2.getRouterComponent();
                return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                    children: [
                        "Router1",
                        /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(RouterComponent, {})
                    ]
                });
            };
            router.add('home', {
                path: '/',
                element: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Test, {})
            });
            const RouterComponent = router.getRouterComponent();
            (0, client_namespaceObject.render)(/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(RouterComponent, {}));
            expect(client_namespaceObject.screen.getByText('Router1')).toBeInTheDocument();
            expect(client_namespaceObject.screen.getByText('Router2')).toBeInTheDocument();
        });
    });
});
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
