"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (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 = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    PluginManager: ()=>PluginManager
});
const external_Plugin_js_namespaceObject = require("./Plugin.js");
const remotePlugins_js_namespaceObject = require("./utils/remotePlugins.js");
function _define_property(obj, key, value) {
    if (key in obj) Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
    });
    else obj[key] = value;
    return obj;
}
class PluginManager {
    async init(_plugins) {
        await this.initStaticPlugins(_plugins);
        if (this.loadRemotePlugins) await this.initRemotePlugins();
    }
    async initStaticPlugins() {
        let _plugins = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : [];
        for await (const plugin of _plugins){
            const pluginClass = Array.isArray(plugin) ? plugin[0] : plugin;
            const opts = Array.isArray(plugin) ? plugin[1] : void 0;
            await this.add(pluginClass, opts);
        }
    }
    async initRemotePlugins() {
        var _res_data;
        const res = await this.app.apiClient.request({
            url: 'pm:listEnabled'
        });
        const pluginList = (null == res ? void 0 : null == (_res_data = res.data) ? void 0 : _res_data.data) || [];
        const plugins = await (0, remotePlugins_js_namespaceObject.getPlugins)({
            requirejs: this.app.requirejs,
            pluginData: pluginList,
            devDynamicImport: this.app.devDynamicImport
        });
        for await (const [name, pluginClass] of plugins)try {
            const info = pluginList.find((item)=>item.name === name);
            await this.add(pluginClass, info);
        } catch (error) {
            console.error("[PluginManager.initRemotePlugins] ✗ Error adding plugin ".concat(name, ":"), {
                error,
                errorMessage: null == error ? void 0 : error.message,
                errorStack: null == error ? void 0 : error.stack,
                pluginClass
            });
            throw error;
        }
    }
    async add(plugin) {
        let opts = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
        const instance = this.getInstance(plugin, opts);
        this.pluginInstances.set(plugin, instance);
        if (opts.name) this.pluginsAliases[opts.name] = instance;
        if (opts.packageName) this.pluginsAliases[opts.packageName] = instance;
        await instance.afterAdd();
    }
    get(nameOrPluginClass) {
        if ('string' == typeof nameOrPluginClass) return this.pluginsAliases[nameOrPluginClass];
        return this.pluginInstances.get(nameOrPluginClass.default || nameOrPluginClass);
    }
    getInstance(plugin, opts) {
        try {
            return new plugin(opts, this.app);
        } catch  {
            return new external_Plugin_js_namespaceObject.Plugin({}, this.app);
        }
    }
    async load() {
        await this.initPlugins;
        for (const plugin of this.pluginInstances.values())await plugin.beforeLoad();
        for (const plugin of this.pluginInstances.values())await plugin.load();
        for (const plugin of this.pluginInstances.values())await plugin.afterLoad();
    }
    constructor(_plugins, loadRemotePlugins, app){
        _define_property(this, "_plugins", void 0);
        _define_property(this, "loadRemotePlugins", void 0);
        _define_property(this, "app", void 0);
        _define_property(this, "pluginInstances", void 0);
        _define_property(this, "pluginsAliases", void 0);
        _define_property(this, "initPlugins", void 0);
        this._plugins = _plugins;
        this.loadRemotePlugins = loadRemotePlugins;
        this.app = app;
        this.pluginInstances = new Map();
        this.pluginsAliases = {};
        this.app = app;
        this.initPlugins = this.init(_plugins);
    }
}
exports.PluginManager = __webpack_exports__.PluginManager;
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
    "PluginManager"
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
Object.defineProperty(exports, '__esModule', {
    value: true
});
