import * as __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__ from "../../Plugin.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_remotePlugins_mjs__ from "../../utils/remotePlugins.mjs";
describe('remotePlugins', ()=>{
    afterEach(()=>{
        window.define = void 0;
    });
    describe('defineDevPlugins()', ()=>{
        it('should define plugins', ()=>{
            class DemoPlugin extends __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__.Plugin {
            }
            const plugins = {
                '@tachybase/demo': DemoPlugin
            };
            const mockDefine = vi.fn();
            window.define = mockDefine;
            (0, __WEBPACK_EXTERNAL_MODULE__utils_remotePlugins_mjs__.defineDevPlugins)(plugins);
            expect(mockDefine).toBeCalledTimes(1);
            expect(mockDefine).toBeCalledWith('@tachybase/demo/client', expect.any(Function));
        });
        it('should return Plugin', ()=>{
            class DemoPlugin extends __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__.Plugin {
            }
            const plugins = {
                '@tachybase/demo': DemoPlugin
            };
            const define = function(packageName, load) {
                expect(packageName).toEqual('@tachybase/demo/client');
                expect(load()).toEqual(DemoPlugin);
            };
            window.define = define;
            (0, __WEBPACK_EXTERNAL_MODULE__utils_remotePlugins_mjs__.defineDevPlugins)(plugins);
        });
    });
    describe('definePluginClient()', ()=>{
        it('should define plugins', ()=>{
            const mockDefine = vi.fn();
            window.define = mockDefine;
            (0, __WEBPACK_EXTERNAL_MODULE__utils_remotePlugins_mjs__.definePluginClient)('@tachybase/demo');
            expect(mockDefine).toBeCalledTimes(1);
            expect(mockDefine).toBeCalledWith('@tachybase/demo/client', [
                'exports',
                '@tachybase/demo'
            ], expect.any(Function));
        });
        it('should proxy', ()=>{
            const mockDefine = vi.fn();
            window.define = mockDefine;
            (0, __WEBPACK_EXTERNAL_MODULE__utils_remotePlugins_mjs__.definePluginClient)('@tachybase/demo');
            const exports = {
                a: 'a'
            };
            const pluginExports = {
                a: 1,
                b: 2
            };
            const proxy = mockDefine.mock.calls[0][2];
            proxy(exports, pluginExports);
            expect(exports.__esModule).toBe(true);
            expect(exports.a).toBe(1);
            expect(exports.b).toBe(2);
        });
    });
    test('should config requirejs', ()=>{
        const requirejs = {
            requirejs: {
                config: vi.fn()
            }
        };
        const pluginData = [
            {
                packageName: '@tachybase/demo',
                url: 'https://demo.com'
            }
        ];
        (0, __WEBPACK_EXTERNAL_MODULE__utils_remotePlugins_mjs__.configRequirejs)(requirejs, pluginData);
        expect(requirejs.requirejs.config).toBeCalledTimes(1);
        expect(requirejs.requirejs.config).toBeCalledWith({
            waitSeconds: 120,
            paths: {
                '@tachybase/demo': 'https://demo.com'
            }
        });
    });
    describe('processRemotePlugins()', ()=>{
        it('should resolve', ()=>{
            const pluginData = [
                {
                    name: '@tachybase/demo',
                    packageName: '@tachybase/demo',
                    url: 'https://demo.com'
                }
            ];
            const resolve = vi.fn();
            const process = (0, __WEBPACK_EXTERNAL_MODULE__utils_remotePlugins_mjs__.processRemotePlugins)(pluginData, resolve);
            const pluginModules = [
                {
                    default: 'default'
                }
            ];
            process(...pluginModules);
            expect(resolve).toBeCalledTimes(1);
            expect(resolve).toBeCalledWith([
                [
                    '@tachybase/demo',
                    'default'
                ]
            ]);
        });
        it('should filter', ()=>{
            const pluginData = [
                {
                    name: '@tachybase/demo',
                    packageName: '@tachybase/demo',
                    url: 'https://demo.com'
                }
            ];
            const resolve = vi.fn();
            const process = (0, __WEBPACK_EXTERNAL_MODULE__utils_remotePlugins_mjs__.processRemotePlugins)(pluginData, resolve);
            const pluginModules = [
                null
            ];
            process(...pluginModules);
            expect(resolve).toBeCalledTimes(1);
            expect(resolve).toBeCalledWith([]);
        });
    });
    describe('getRemotePlugins()', ()=>{
        it('should get remote plugins', async ()=>{
            const mockPluginsModules = (pluginData, resolve)=>{
                resolve({
                    default: 'default'
                });
            };
            const requirejs = {
                requirejs: mockPluginsModules
            };
            requirejs.requirejs.config = vi.fn();
            requirejs.requirejs.requirejs = vi.fn();
            const pluginData = [
                {
                    name: '@tachybase/demo',
                    packageName: '@tachybase/demo',
                    url: 'https://demo.com'
                }
            ];
            const mockDefine = vi.fn();
            window.define = mockDefine;
            const plugins = await (0, __WEBPACK_EXTERNAL_MODULE__utils_remotePlugins_mjs__.getRemotePlugins)(requirejs, pluginData);
            expect(plugins).toEqual([
                [
                    '@tachybase/demo',
                    'default'
                ]
            ]);
        });
    });
    describe('getPlugins()', ()=>{
        it('If there is no devDynamicImport, all plugins are obtained through API requests', async ()=>{
            const remoteFn = vi.fn();
            const mockPluginsModules = (pluginData, resolve)=>{
                remoteFn();
                resolve({
                    default: 'default'
                }, {
                    default: 'default'
                });
            };
            const requirejs = {
                requirejs: mockPluginsModules
            };
            requirejs.requirejs.config = vi.fn();
            requirejs.requirejs.requirejs = vi.fn();
            const pluginData = [
                {
                    name: '@tachybase/demo',
                    packageName: '@tachybase/demo',
                    url: 'https://demo1.com'
                },
                {
                    name: '@tachybase/demo2',
                    packageName: '@tachybase/demo2',
                    url: 'https://demo2.com'
                }
            ];
            const mockDefine = vi.fn();
            window.define = mockDefine;
            const plugins = await (0, __WEBPACK_EXTERNAL_MODULE__utils_remotePlugins_mjs__.getPlugins)({
                requirejs,
                pluginData
            });
            expect(plugins).toEqual([
                [
                    '@tachybase/demo',
                    'default'
                ],
                [
                    '@tachybase/demo2',
                    'default'
                ]
            ]);
            expect(remoteFn).toBeCalledTimes(1);
            expect(mockDefine).toBeCalledTimes(2);
            expect(requirejs.requirejs.config).toBeCalledWith({
                waitSeconds: 120,
                paths: {
                    '@tachybase/demo': 'https://demo1.com',
                    '@tachybase/demo2': 'https://demo2.com'
                }
            });
        });
        it('If there is devDynamicImport and devDynamicImport returns all, remote API will not be requested', async ()=>{
            const remoteFn = vi.fn();
            const mockPluginsModules = (pluginData, resolve)=>{
                remoteFn();
                resolve({
                    default: 'default'
                }, {
                    default: 'default'
                });
            };
            const requirejs = {
                requirejs: mockPluginsModules
            };
            requirejs.requirejs.config = vi.fn();
            requirejs.requirejs.requirejs = vi.fn();
            const pluginData = [
                {
                    name: '@tachybase/demo',
                    packageName: '@tachybase/demo',
                    url: 'https://demo1.com'
                },
                {
                    name: '@tachybase/demo2',
                    packageName: '@tachybase/demo2',
                    url: 'https://demo2.com'
                }
            ];
            const mockDefine = vi.fn();
            window.define = mockDefine;
            const plugins = await (0, __WEBPACK_EXTERNAL_MODULE__utils_remotePlugins_mjs__.getPlugins)({
                requirejs,
                pluginData,
                devDynamicImport: ()=>Promise.resolve({
                        default: 'default'
                    })
            });
            expect(plugins).toEqual([
                [
                    '@tachybase/demo',
                    'default'
                ],
                [
                    '@tachybase/demo2',
                    'default'
                ]
            ]);
            expect(remoteFn).toBeCalledTimes(0);
            expect(mockDefine).toBeCalledTimes(2);
            expect(requirejs.requirejs.config).toBeCalledTimes(0);
        });
        it('If there is devDynamicImport and devDynamicImport returns partial, remote API will be requested', async ()=>{
            const remoteFn = vi.fn();
            const mockPluginsModules = (pluginData, resolve)=>{
                remoteFn();
                resolve({
                    default: 'default'
                });
            };
            const requirejs = {
                requirejs: mockPluginsModules
            };
            requirejs.requirejs.config = vi.fn();
            requirejs.requirejs.requirejs = vi.fn();
            const pluginData = [
                {
                    name: '@tachybase/demo',
                    packageName: '@tachybase/demo',
                    url: 'https://demo1.com'
                },
                {
                    name: '@tachybase/demo2',
                    packageName: '@tachybase/demo2',
                    url: 'https://demo2.com'
                }
            ];
            const mockDefine = vi.fn();
            window.define = mockDefine;
            const plugins = await (0, __WEBPACK_EXTERNAL_MODULE__utils_remotePlugins_mjs__.getPlugins)({
                requirejs,
                pluginData,
                devDynamicImport: (packageName)=>{
                    if ('@tachybase/demo' === packageName) return Promise.resolve({
                        default: 'default'
                    });
                    return Promise.resolve(null);
                }
            });
            expect(plugins).toEqual([
                [
                    '@tachybase/demo',
                    'default'
                ],
                [
                    '@tachybase/demo2',
                    'default'
                ]
            ]);
            expect(remoteFn).toBeCalled();
            expect(mockDefine).toBeCalledTimes(2);
            expect(requirejs.requirejs.config).toBeCalledWith({
                waitSeconds: 120,
                paths: {
                    '@tachybase/demo2': 'https://demo2.com'
                }
            });
        });
    });
});
