"use strict";
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
Object.defineProperty(exports, "__esModule", { value: true });
var libembed = require("./libembed");
/**
 * Load a package using requirejs and return a promise
 *
 * @param pkg Package name or names to load
 */
var requirePromise = function (pkg) {
    return new Promise(function (resolve, reject) {
        var require = window.require;
        if (require === undefined) {
            reject("Requirejs is needed, please ensure it is loaded on the page.");
        }
        else {
            require(pkg, resolve, reject);
        }
    });
};
function requireLoader(moduleName, moduleVersion) {
    return requirePromise(["" + moduleName]).catch(function (err) {
        var failedId = err.requireModules && err.requireModules[0];
        if (failedId) {
            console.log("Falling back to unpkg.com for " + moduleName + "@" + moduleVersion);
            return requirePromise(["https://unpkg.com/" + moduleName + "@" + moduleVersion + "/dist/index.js"]);
        }
    });
}
/**
 * Render widgets in a given element.
 *
 * @param element (default document.documentElement) The element containing widget state and views.
 */
function renderWidgets(element) {
    if (element === void 0) { element = document.documentElement; }
    requirePromise(['@jupyter-widgets/html-manager']).then(function (htmlmanager) {
        var managerFactory = function () {
            return new htmlmanager.HTMLManager({ loader: requireLoader });
        };
        libembed.renderWidgets(managerFactory, element);
    });
}
exports.renderWidgets = renderWidgets;
