"use strict";
var __webpack_exports__ = {};
const external_Plugin_js_namespaceObject = require("../../Plugin.js");
const remotePlugins_js_namespaceObject = require("../../utils/remotePlugins.js");
describe('remotePlugins', ()=>{
    afterEach(()=>{
        window.define = void 0;
    });
    describe('defineDevPlugins()', ()=>{
        it('should define plugins', ()=>{
            class DemoPlugin extends external_Plugin_js_namespaceObject.Plugin {
            }
            const plugins = {
                '@tachybase/demo': DemoPlugin
            };
            const mockDefine = vi.fn();
            window.define = mockDefine;
            (0, remotePlugins_js_namespaceObject.defineDevPlugins)(plugins);
            expect(mockDefine).toBeCalledTimes(1);
            expect(mockDefine).toBeCalledWith('@tachybase/demo/client', expect.any(Function));
        });
        it('should return Plugin', ()=>{
            class DemoPlugin extends external_Plugin_js_namespaceObject.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, remotePlugins_js_namespaceObject.defineDevPlugins)(plugins);
        });
    });
    describe('definePluginClient()', ()=>{
        it('should define plugins', ()=>{
            const mockDefine = vi.fn();
            window.define = mockDefine;
            (0, remotePlugins_js_namespaceObject.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, remotePlugins_js_namespaceObject.definePluginClient)('@tachybase/demo');
            const exports1 = {
                a: 'a'
            };
            const pluginExports = {
                a: 1,
                b: 2
            };
            const proxy = mockDefine.mock.calls[0][2];
            proxy(exports1, pluginExports);
            expect(exports1.__esModule).toBe(true);
            expect(exports1.a).toBe(1);
            expect(exports1.b).toBe(2);
        });
    });
    test('should config requirejs', ()=>{
        const requirejs = {
            requirejs: {
                config: vi.fn()
            }
        };
        const pluginData = [
            {
                packageName: '@tachybase/demo',
                url: 'https://demo.com'
            }
        ];
        (0, remotePlugins_js_namespaceObject.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, remotePlugins_js_namespaceObject.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, remotePlugins_js_namespaceObject.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, remotePlugins_js_namespaceObject.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, remotePlugins_js_namespaceObject.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, remotePlugins_js_namespaceObject.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, remotePlugins_js_namespaceObject.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'
                }
            });
        });
    });
});
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
});
