import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "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__Plugin_mjs__ from "../Plugin.mjs";
(0, __WEBPACK_EXTERNAL_MODULE_vitest__.describe)('Application', ()=>{
    beforeAll(()=>{
        const mock = new __WEBPACK_EXTERNAL_MODULE_axios_mock_adapter__["default"](__WEBPACK_EXTERNAL_MODULE_axios__["default"]);
        mock.onGet('pm:listEnabled').reply(200, {
            data: []
        });
    });
    const router = {
        type: 'memory',
        initialEntries: [
            '/'
        ]
    };
    const initialProvidersLength = 7;
    it('basic', ()=>{
        const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
            router
        });
        expect(app.i18n).toBeDefined();
        expect(app.apiClient).toBeDefined();
        expect(app.components).toBeDefined();
        expect(app.pm).toBeDefined();
        expect(app.providers).toBeDefined();
        expect(app.router).toBeDefined();
        expect(app.scopes).toBeDefined();
        expect(app.providers.length).toBeGreaterThan(1);
        expect(Object.keys(app.components).length).toBeGreaterThan(1);
    });
    (0, __WEBPACK_EXTERNAL_MODULE_vitest__.describe)('components', ()=>{
        const Hello = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                children: "Hello"
            });
        Hello.displayName = 'Hello';
        it('initial', ()=>{
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router,
                components: {
                    Hello
                }
            });
            expect(app.components.Hello).toBe(Hello);
        });
        it('addComponents', ()=>{
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router
            });
            app.addComponents({
                Hello
            });
            expect(app.components.Hello).toBe(Hello);
        });
        (0, __WEBPACK_EXTERNAL_MODULE_vitest__.describe)('getComponent', ()=>{
            let originalConsoleError;
            beforeEach(()=>{
                originalConsoleError = console.error;
            });
            afterEach(()=>{
                console.error = originalConsoleError;
            });
            it('arg is Class component', ()=>{
                class Foo extends __WEBPACK_EXTERNAL_MODULE_react__.Component {
                    render() {
                        return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {});
                    }
                }
                const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                    router
                });
                expect(app.getComponent(Foo)).toBe(Foo);
            });
            it('arg is Function component', ()=>{
                const Foo = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {});
                const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                    router
                });
                expect(app.getComponent(Foo)).toBe(Foo);
            });
            it('arg is string', ()=>{
                const Foo = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {});
                const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                    router,
                    components: {
                        Foo
                    }
                });
                expect(app.getComponent('Foo')).toBe(Foo);
            });
            it('arg is string, but not found in components', ()=>{
                const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                    router
                });
                const fn = vitest.fn();
                console.error = fn;
                expect(app.getComponent('Foo')).toBeUndefined();
                expect(fn).toBeCalled();
            });
            it('arg is null or undefined', ()=>{
                const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                    router
                });
                const fn = vitest.fn();
                console.error = fn;
                expect(app.getComponent(null)).toBeUndefined();
                expect(fn).toBeCalled();
            });
            it('arg is other types', ()=>{
                const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                    router
                });
                const fn = vitest.fn();
                console.error = fn;
                expect(app.getComponent({})).toBeUndefined();
                expect(fn).toBeCalled();
            });
        });
        it('renderComponent', ()=>{
            const Foo = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    children: props.name
                });
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router,
                components: {
                    Foo
                }
            });
            expect(app.renderComponent('Foo', {
                name: 'bar'
            })).toEqual(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Foo, {
                name: "bar"
            }));
        });
    });
    (0, __WEBPACK_EXTERNAL_MODULE_vitest__.describe)('scopes', ()=>{
        it('initial', ()=>{
            const scopes = {
                foo: 'bar'
            };
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router,
                scopes
            });
            expect(app.scopes).toEqual(scopes);
        });
        it('addScopes', ()=>{
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router
            });
            app.addScopes({
                foo: 'bar'
            });
            expect(app.scopes).toEqual({
                foo: 'bar'
            });
        });
    });
    (0, __WEBPACK_EXTERNAL_MODULE_vitest__.describe)('providers', ()=>{
        const Hello = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                children: [
                    "Hello ",
                    props.children
                ]
            });
        const World = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                children: [
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                        children: "World"
                    }),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                        children: props.name
                    }),
                    /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                        children: props.children
                    })
                ]
            });
        const Foo = (props)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                children: [
                    "Foo ",
                    props.children
                ]
            });
        it('initial', ()=>{
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router,
                providers: [
                    Hello,
                    [
                        World,
                        {
                            name: 'aaa'
                        }
                    ]
                ]
            });
            expect(app.providers.slice(initialProvidersLength)).toEqual([
                [
                    Hello,
                    void 0
                ],
                [
                    World,
                    {
                        name: 'aaa'
                    }
                ]
            ]);
        });
        it('addProviders', ()=>{
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router,
                providers: [
                    Hello
                ]
            });
            app.addProviders([
                [
                    World,
                    {
                        name: 'aaa'
                    }
                ],
                Foo
            ]);
            expect(app.providers.slice(initialProvidersLength)).toEqual([
                [
                    Hello,
                    void 0
                ],
                [
                    World,
                    {
                        name: 'aaa'
                    }
                ],
                [
                    Foo,
                    void 0
                ]
            ]);
        });
        it('addProvider', ()=>{
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router,
                providers: [
                    Hello
                ]
            });
            app.addProvider(World, {
                name: 'aaa'
            });
            expect(app.providers.slice(initialProvidersLength)).toEqual([
                [
                    Hello,
                    void 0
                ],
                [
                    World,
                    {
                        name: 'aaa'
                    }
                ]
            ]);
        });
        it('use', ()=>{
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router,
                providers: [
                    Hello
                ]
            });
            app.use(World, {
                name: 'aaa'
            });
            expect(app.providers.slice(initialProvidersLength)).toEqual([
                [
                    Hello,
                    void 0
                ],
                [
                    World,
                    {
                        name: 'aaa'
                    }
                ]
            ]);
        });
        it('getComposeProviders', async ()=>{
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router,
                providers: [
                    Hello,
                    [
                        World,
                        {
                            name: 'aaa'
                        }
                    ]
                ]
            });
            const Providers = app.getComposeProviders();
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Foo, {}), {
                wrapper: Providers
            });
            await (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.waitFor)(()=>{
                expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('Hello')).toBeInTheDocument();
            });
            await (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.waitFor)(()=>{
                expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('World')).toBeInTheDocument();
            });
            await (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.waitFor)(()=>{
                expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('aaa')).toBeInTheDocument();
            });
            await (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.waitFor)(()=>{
                expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('Foo')).toBeInTheDocument();
            });
        });
    });
    (0, __WEBPACK_EXTERNAL_MODULE_vitest__.describe)('render', ()=>{
        it('getRootComponent', async ()=>{
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router
            });
            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, {})
                    ]
                });
            const Home = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    children: "HomeComponent"
                });
            const About = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    children: "AboutComponent"
                });
            const HelloProvider = (param)=>{
                let { children } = param;
                return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("div", {
                    children: [
                        "Hello ",
                        children
                    ]
                });
            };
            app.router.add('root', {
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Layout, {})
            });
            app.router.add('root.home', {
                path: '/',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Home, {})
            });
            app.router.add('root.about', {
                path: '/about',
                element: /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(About, {})
            });
            app.addProviders([
                HelloProvider
            ]);
            const Root = app.getRootComponent();
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Root, {}));
            await (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.waitFor)(()=>{
                expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('Loading...')).toBeInTheDocument();
            });
            await (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.waitFor)(()=>{
                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('mount', async ()=>{
            const Hello = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    children: "Hello"
                });
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router,
                providers: [
                    Hello
                ]
            });
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                id: "app"
            }));
            const root = app.mount('#app');
            expect(root).toBeDefined();
            await (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.waitFor)(()=>{
                expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('Hello')).toBeInTheDocument();
            });
        });
        it('mount root error', ()=>{
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router
            });
            const originalConsoleWarn = console.warn;
            const fn = vitest.fn();
            console.warn = fn;
            app.mount('#app');
            expect(fn).toBeCalled();
            console.warn = originalConsoleWarn;
        });
        it('plugin load error', async ()=>{
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router
            });
            class DemoPlugin extends __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__.Plugin {
                async load() {
                    throw new Error('error');
                }
            }
            app.pm.add(DemoPlugin, {
                name: 'demo'
            });
            const Root = app.getRootComponent();
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Root, {}));
            await (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.sleep)(10);
            expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('Load Plugin Error')).toBeInTheDocument();
        });
        it('replace Component', async ()=>{
            const AppSpin = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    children: "AppSpin"
                });
            const AppMain = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    children: "AppMain"
                });
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router,
                components: {
                    AppSpin,
                    AppMain
                }
            });
            const Root = app.getRootComponent();
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Root, {}));
            expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('AppSpin')).toBeInTheDocument();
            await (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.sleep)(10);
            expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('AppMain')).toBeInTheDocument();
        });
        it('render component error', async ()=>{
            const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
                router
            });
            const AppError = ()=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
                    children: "AppError"
                });
            const Foo = ()=>{
                throw new Error('error');
            };
            app.use(Foo);
            app.addComponents({
                AppError
            });
            const originalConsoleWarn = console.error;
            const fn = vitest.fn();
            console.error = fn;
            const Root = app.getRootComponent();
            (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.render)(/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(Root, {}));
            await (0, __WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.sleep)(10);
            expect(fn).toBeCalled();
            expect(__WEBPACK_EXTERNAL_MODULE__tachybase_test_client__.screen.getByText('AppError')).toBeInTheDocument();
            console.error = originalConsoleWarn;
        });
    });
});
