"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 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_Application_js_namespaceObject = require("../Application.js");
const external_Plugin_js_namespaceObject = require("../Plugin.js");
describe('Plugin', ()=>{
    it('lifecycle', async ()=>{
        const afterAdd = vitest.fn();
        const beforeLoad = vitest.fn();
        const load = vitest.fn();
        class DemoPlugin extends external_Plugin_js_namespaceObject.Plugin {
            async afterAdd() {
                afterAdd();
            }
            async beforeLoad() {
                beforeLoad();
            }
            async load() {
                load();
            }
        }
        const app = new external_Application_js_namespaceObject.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 external_Plugin_js_namespaceObject.Plugin {
            async load() {
                fn1();
            }
        }
        const fn2 = vitest.fn();
        const config = {
            a: 1
        };
        class Demo2Plugin extends external_Plugin_js_namespaceObject.Plugin {
            async load() {
                fn2(this.options.config);
            }
        }
        const app = new external_Application_js_namespaceObject.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 (external_axios_mock_adapter_default())(external_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 external_Plugin_js_namespaceObject.Plugin {
            async load() {
                demo1Mock();
            }
        }
        class Demo2Plugin extends external_Plugin_js_namespaceObject.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 external_Application_js_namespaceObject.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 external_Plugin_js_namespaceObject.Plugin {
            async load() {
                fn2(this.options.config);
            }
        }
        class Demo1Plugin extends external_Plugin_js_namespaceObject.Plugin {
            async afterAdd() {
                await this.app.pm.add(Demo2, {
                    name: 'demo2',
                    config
                });
            }
        }
        const app = new external_Application_js_namespaceObject.Application({
            plugins: [
                [
                    Demo1Plugin,
                    {
                        name: 'demo1'
                    }
                ]
            ]
        });
        await app.load();
        expect(fn2).toBeCalledTimes(1);
        expect(fn2).toBeCalledWith(config);
    });
    it('getter', async ()=>{
        class DemoPlugin extends external_Plugin_js_namespaceObject.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 external_Application_js_namespaceObject.Application({
            plugins: [
                [
                    DemoPlugin,
                    {
                        name: 'demo'
                    }
                ]
            ]
        });
        await app.load();
    });
    it('get', async ()=>{
        class DemoPlugin extends external_Plugin_js_namespaceObject.Plugin {
        }
        const app = new external_Application_js_namespaceObject.Application({
            plugins: [
                [
                    DemoPlugin,
                    {
                        name: 'demo'
                    }
                ]
            ]
        });
        await app.load();
        expect(app.pm.get('demo')).toBeInstanceOf(DemoPlugin);
    });
});
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
});
