function defineDevPlugins(plugins) {
    Object.entries(plugins).forEach((param)=>{
        let [packageName, plugin] = param;
        if ('@tachybase/module-hera' === packageName) window.define("@hera/plugin-core/client", ()=>plugin);
        window.define("".concat(packageName, "/client"), ()=>plugin);
    });
}
function definePluginClient(packageName) {
    if ('@tachybase/module-hera' === packageName) window.define("@hera/plugin-core/client", [
        'exports',
        "@hera/plugin-core/client"
    ], function(_exports, _pluginExports) {
        Object.defineProperty(_exports, '__esModule', {
            value: true
        });
        Object.keys(_pluginExports).forEach(function(key) {
            if ('__esModule' === key) return;
            if (key in _exports && _exports[key] === _pluginExports[key]) return;
            Object.defineProperty(_exports, key, {
                enumerable: true,
                get: function() {
                    return _pluginExports[key];
                }
            });
        });
    });
    window.define("".concat(packageName, "/client"), [
        'exports',
        packageName
    ], function(_exports, _pluginExports) {
        Object.defineProperty(_exports, '__esModule', {
            value: true
        });
        Object.keys(_pluginExports).forEach(function(key) {
            if ('__esModule' === key) return;
            if (key in _exports && _exports[key] === _pluginExports[key]) return;
            Object.defineProperty(_exports, key, {
                enumerable: true,
                get: function() {
                    return _pluginExports[key];
                }
            });
        });
    });
}
function configRequirejs(requirejs, pluginData) {
    requirejs.requirejs.config({
        waitSeconds: 120,
        paths: pluginData.reduce((acc, cur)=>{
            acc[cur.packageName] = cur.url;
            return acc;
        }, {})
    });
}
function processRemotePlugins(pluginData, resolve) {
    return function() {
        for(var _len = arguments.length, pluginModules = new Array(_len), _key = 0; _key < _len; _key++)pluginModules[_key] = arguments[_key];
        const res = pluginModules.map((item, index)=>[
                pluginData[index].name,
                (null == item ? void 0 : item.default) || item
            ]).filter((item)=>item[1]);
        resolve(res);
        const emptyPlugins = pluginModules.map((item, index)=>item ? null : index).filter((i)=>null !== i).map((i)=>pluginData[i].packageName);
        if (emptyPlugins.length > 0) console.error('[tachybase load plugin error]: These plugins do not have an `export.default` exported content or there is an error in the plugins. error plugins: \r\n%s', emptyPlugins.join(', \r\n'));
    };
}
function getRemotePlugins(requirejs) {
    let pluginData = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : [];
    configRequirejs(requirejs, pluginData);
    const packageNames = pluginData.map((item)=>item.packageName);
    packageNames.forEach((packageName)=>{
        definePluginClient(packageName);
    });
    return new Promise((resolve, reject)=>{
        requirejs.requirejs(packageNames, processRemotePlugins(pluginData, resolve), reject);
    });
}
async function getPlugins(options) {
    const { requirejs, pluginData, devDynamicImport } = options;
    if (0 === pluginData.length) return [];
    const res = [];
    const resolveDevPlugins = {};
    if (devDynamicImport) {
        for await (const plugin of pluginData){
            const pluginModule = await devDynamicImport(plugin.packageName);
            if (pluginModule) {
                res.push([
                    plugin.name,
                    pluginModule.default
                ]);
                resolveDevPlugins[plugin.packageName] = pluginModule;
            }
        }
        defineDevPlugins(resolveDevPlugins);
    }
    const remotePlugins = pluginData.filter((item)=>!resolveDevPlugins[item.packageName]);
    if (0 === remotePlugins.length) return res;
    const remotePluginList = await getRemotePlugins(requirejs, remotePlugins);
    res.push(...remotePluginList);
    return res;
}
export { configRequirejs, defineDevPlugins, definePluginClient, getPlugins, getRemotePlugins, processRemotePlugins };
