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__Application_mjs__ from "../Application.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__ from "../Plugin.mjs";
describe('Plugin', ()=>{
    it('lifecycle', async ()=>{
        const afterAdd = vitest.fn();
        const beforeLoad = vitest.fn();
        const load = vitest.fn();
        class DemoPlugin extends __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__.Plugin {
            async afterAdd() {
                afterAdd();
            }
            async beforeLoad() {
                beforeLoad();
            }
            async load() {
                load();
            }
        }
        const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
            plugins: [
                [
                    DemoPlugin,
                    {
                        name: 'demo1'
                    }
                ]
            ]
        });
        await app.load();
        expect(afterAdd).toBeCalledTimes(1);
        expect(beforeLoad).toBeCalledTimes(1);
        expect(load).toBeCalledTimes(1);
    });
});
describe('PluginManager', ()=>{
    it('static Plugins', async ()=>{
        const fn1 = vitest.fn();
        class Demo1Plugin extends __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__.Plugin {
            async load() {
                fn1();
            }
        }
        const fn2 = vitest.fn();
        const config = {
            a: 1
        };
        class Demo2Plugin extends __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__.Plugin {
            async load() {
                fn2(this.options.config);
            }
        }
        const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
            plugins: [
                [
                    Demo1Plugin,
                    {
                        name: 'demo1'
                    }
                ],
                [
                    Demo2Plugin,
                    {
                        name: 'demo2',
                        config
                    }
                ]
            ]
        });
        await app.load();
        expect(fn1).toBeCalledTimes(1);
        expect(fn2).toBeCalledTimes(1);
        expect(fn2).toBeCalledWith(config);
    });
    it('remote plugins', async ()=>{
        const mock = new __WEBPACK_EXTERNAL_MODULE_axios_mock_adapter__["default"](__WEBPACK_EXTERNAL_MODULE_axios__["default"]);
        mock.onGet('pm:listEnabled').reply(200, {
            data: [
                {
                    name: '@tachybase/demo',
                    packageName: '@tachybase/demo',
                    url: 'https://demo1.com'
                },
                {
                    name: '@tachybase/demo2',
                    packageName: '@tachybase/demo2',
                    url: 'https://demo2.com'
                }
            ]
        });
        const remoteFn = vi.fn();
        const demo1Mock = vi.fn();
        const demo2Mock = vi.fn();
        class Demo1Plugin extends __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__.Plugin {
            async load() {
                demo1Mock();
            }
        }
        class Demo2Plugin extends __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__.Plugin {
            async load() {
                demo2Mock();
            }
        }
        const mockPluginsModules = (pluginData, resolve)=>{
            remoteFn();
            resolve({
                default: Demo1Plugin
            }, {
                default: Demo2Plugin
            });
        };
        const requirejs = {
            requirejs: mockPluginsModules
        };
        requirejs.requirejs.config = vi.fn();
        requirejs.requirejs.requirejs = vi.fn();
        const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
            loadRemotePlugins: true
        });
        app.requirejs = requirejs;
        await app.load();
        expect(remoteFn).toBeCalledTimes(1);
        expect(demo1Mock).toBeCalledTimes(1);
    });
    it('Load other plugins through plugins', async ()=>{
        const fn2 = vitest.fn();
        const config = {
            a: 1
        };
        class Demo2 extends __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__.Plugin {
            async load() {
                fn2(this.options.config);
            }
        }
        class Demo1Plugin extends __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__.Plugin {
            async afterAdd() {
                await this.app.pm.add(Demo2, {
                    name: 'demo2',
                    config
                });
            }
        }
        const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
            plugins: [
                [
                    Demo1Plugin,
                    {
                        name: 'demo1'
                    }
                ]
            ]
        });
        await app.load();
        expect(fn2).toBeCalledTimes(1);
        expect(fn2).toBeCalledWith(config);
    });
    it('getter', async ()=>{
        class DemoPlugin extends __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__.Plugin {
            async afterAdd() {
                expect(this.pm).toBe(this.app.pm);
                expect(this.router).toBe(this.app.router);
                expect(this.pluginManager).toBe(this.app.pluginManager);
                expect(this.systemSettingsManager).toBe(this.app.systemSettingsManager);
                expect(this.schemaInitializerManager).toBe(this.app.schemaInitializerManager);
                expect(this.schemaSettingsManager).toBe(this.app.schemaSettingsManager);
            }
        }
        const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
            plugins: [
                [
                    DemoPlugin,
                    {
                        name: 'demo'
                    }
                ]
            ]
        });
        await app.load();
    });
    it('get', async ()=>{
        class DemoPlugin extends __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__.Plugin {
        }
        const app = new __WEBPACK_EXTERNAL_MODULE__Application_mjs__.Application({
            plugins: [
                [
                    DemoPlugin,
                    {
                        name: 'demo'
                    }
                ]
            ]
        });
        await app.load();
        expect(app.pm.get('demo')).toBeInstanceOf(DemoPlugin);
    });
});
