"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__, {
    configRequirejs: ()=>configRequirejs,
    defineDevPlugins: ()=>defineDevPlugins,
    definePluginClient: ()=>definePluginClient,
    getPlugins: ()=>getPlugins,
    getRemotePlugins: ()=>getRemotePlugins,
    processRemotePlugins: ()=>processRemotePlugins
});
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), (error)=>{
            console.error('[getRemotePlugins] requirejs error:', error);
            reject(error);
        });
    });
}
async function getPlugins(options) {
    const { requirejs, pluginData, devDynamicImport } = options;
    if (0 === pluginData.length) {
        console.warn('[getPlugins] ⚠ Early return: pluginData.length === 0');
        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;
}
exports.configRequirejs = __webpack_exports__.configRequirejs;
exports.defineDevPlugins = __webpack_exports__.defineDevPlugins;
exports.definePluginClient = __webpack_exports__.definePluginClient;
exports.getPlugins = __webpack_exports__.getPlugins;
exports.getRemotePlugins = __webpack_exports__.getRemotePlugins;
exports.processRemotePlugins = __webpack_exports__.processRemotePlugins;
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
    "configRequirejs",
    "defineDevPlugins",
    "definePluginClient",
    "getPlugins",
    "getRemotePlugins",
    "processRemotePlugins"
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
Object.defineProperty(exports, '__esModule', {
    value: true
});
