"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initPlugins = exports.createPluginStore = void 0;
const logger_1 = require("./logger");
function createPluginStore() {
    let plugins = [];
    const addPlugins = (newPlugins, options) => {
        const { before } = options || {};
        newPlugins.forEach(newPlugin => {
            if (typeof newPlugin !== 'object' || newPlugin === null) {
                throw new Error(`expect plugin instance is object, but got ${typeof newPlugin}.`);
            }
            else if (typeof newPlugin.setup !== 'function') {
                throw new Error(`expect plugin.setup is function, but got ${typeof newPlugin}.`);
            }
            if (plugins.find(item => item.name === newPlugin.name)) {
                logger_1.logger.warn(`Plugin "${newPlugin.name}" already exists.`);
            }
            else if (before) {
                const index = plugins.findIndex(item => item.name === before);
                if (index === -1) {
                    logger_1.logger.warn(`Plugin "${before}" does not exist.`);
                    plugins.push(newPlugin);
                }
                else {
                    plugins.splice(index, 0, newPlugin);
                }
            }
            else {
                plugins.push(newPlugin);
            }
        });
    };
    const removePlugins = (pluginNames) => {
        plugins = plugins.filter(plugin => !pluginNames.includes(plugin.name));
    };
    const isPluginExists = (pluginName) => Boolean(plugins.find(plugin => plugin.name === pluginName));
    return {
        get plugins() {
            return plugins;
        },
        addPlugins,
        removePlugins,
        isPluginExists,
    };
}
exports.createPluginStore = createPluginStore;
async function initPlugins({ pluginAPI, pluginStore, }) {
    (0, logger_1.debug)('init plugins');
    const { pluginDagSort } = await Promise.resolve().then(() => __importStar(require('@modern-js/utils/universal/plugin-dag-sort')));
    const plugins = pluginDagSort(pluginStore.plugins);
    const removedPlugins = plugins.reduce((ret, plugin) => {
        if (plugin.remove) {
            return ret.concat(plugin.remove);
        }
        return ret;
    }, []);
    for (const plugin of plugins) {
        if (removedPlugins.includes(plugin.name)) {
            continue;
        }
        await plugin.setup(pluginAPI);
    }
    (0, logger_1.debug)('init plugins done');
}
exports.initPlugins = initPlugins;
