import * as __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__ from "./Plugin.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__utils_remotePlugins_mjs__ from "./utils/remotePlugins.mjs";
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 ? void 0 : _res_data.data) || [];
        const plugins = await (0, __WEBPACK_EXTERNAL_MODULE__utils_remotePlugins_mjs__.getPlugins)({
            requirejs: this.app.requirejs,
            pluginData: pluginList,
            devDynamicImport: this.app.devDynamicImport
        });
        for await (const [name, pluginClass] of plugins){
            const info = pluginList.find((item)=>item.name === name);
            await this.add(pluginClass, info);
        }
    }
    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 __WEBPACK_EXTERNAL_MODULE__Plugin_mjs__.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);
    }
}
export { PluginManager };
