import { __awaiter, __generator } from "tslib";
import { asPromise } from '../../lib/as-promise';
import { loadScript } from '../../lib/load-script';
function validate(pluginLike) {
    if (!Array.isArray(pluginLike)) {
        throw new Error('Not a valid list of plugins');
    }
    var required = ['load', 'isLoaded', 'name', 'version', 'type'];
    pluginLike.forEach(function (plugin) {
        required.forEach(function (method) {
            var _a;
            if (plugin[method] === undefined) {
                throw new Error("Plugin: " + ((_a = plugin.name) !== null && _a !== void 0 ? _a : 'unknown') + " missing required function " + method);
            }
        });
    });
    return true;
}
export function remoteLoader(settings) {
    var _a;
    return __awaiter(this, void 0, void 0, function () {
        var allPlugins, pluginPromises;
        var _this = this;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    allPlugins = [];
                    pluginPromises = ((_a = settings.remotePlugins) !== null && _a !== void 0 ? _a : []).map(function (remotePlugin) { return __awaiter(_this, void 0, void 0, function () {
                        var libraryName, pluginFactory, plugin, plugins, error_1;
                        return __generator(this, function (_a) {
                            switch (_a.label) {
                                case 0:
                                    _a.trys.push([0, 4, , 5]);
                                    return [4 /*yield*/, loadScript(remotePlugin.url)];
                                case 1:
                                    _a.sent();
                                    libraryName = remotePlugin.libraryName;
                                    if (!(typeof window[libraryName] === 'function')) return [3 /*break*/, 3];
                                    pluginFactory = window[libraryName];
                                    return [4 /*yield*/, asPromise(pluginFactory(remotePlugin.settings))];
                                case 2:
                                    plugin = _a.sent();
                                    plugins = Array.isArray(plugin) ? plugin : [plugin];
                                    validate(plugins);
                                    allPlugins.push.apply(allPlugins, plugins);
                                    _a.label = 3;
                                case 3: return [3 /*break*/, 5];
                                case 4:
                                    error_1 = _a.sent();
                                    console.warn('Failed to load Remote Plugin', error_1);
                                    return [3 /*break*/, 5];
                                case 5: return [2 /*return*/];
                            }
                        });
                    }); });
                    return [4 /*yield*/, Promise.all(pluginPromises)];
                case 1:
                    _b.sent();
                    return [2 /*return*/, allPlugins.filter(Boolean)];
            }
        });
    });
}
//# sourceMappingURL=index.js.map