"use strict";
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var widgets = require("@jupyter-widgets/controls");
var base = require("@jupyter-widgets/base");
var outputWidgets = require("./output");
var PhosphorWidget = require("@phosphor/widgets");
var rendermime_1 = require("@jupyterlab/rendermime");
var output_renderers_1 = require("./output_renderers");
var HTMLManager = (function (_super) {
    __extends(HTMLManager, _super);
    function HTMLManager() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        /**
         * Renderers for contents of the output widgets
         *
         * Defines how outputs in the output widget should be rendered.
         */
        _this.renderMime = new rendermime_1.RenderMime({
            items: [
                { mimeType: output_renderers_1.WIDGET_MIMETYPE, renderer: new output_renderers_1.WidgetRenderer(_this) }
            ].concat(rendermime_1.RenderMime.getDefaultItems())
        });
        return _this;
    }
    /**
     * Display the specified view. Element where the view is displayed
     * is specified in the `options.el` argument.
     */
    HTMLManager.prototype.display_view = function (msg, view, options) {
        return Promise.resolve(view).then(function (view) {
            PhosphorWidget.Widget.attach(view.pWidget, options.el);
            view.on('remove', function () {
                console.log('View removed', view);
            });
            return view;
        });
    };
    ;
    /**
     * Placeholder implementation for _get_comm_info.
     */
    HTMLManager.prototype._get_comm_info = function () {
        return Promise.resolve({});
    };
    ;
    /**
     * Placeholder implementation for _create_comm.
     */
    HTMLManager.prototype._create_comm = function () {
        return Promise.resolve({
            on_close: function () { },
            on_msg: function () { },
            close: function () { }
        });
    };
    ;
    /**
     * Load a class and return a promise to the loaded object.
     */
    HTMLManager.prototype.loadClass = function (className, moduleName, moduleVersion) {
        return new Promise(function (resolve, reject) {
            // Shortcuts resolving the standard widgets so we don't load two
            // copies on the page. If we ever separate the widgets from the
            // base manager, we should get rid of this special case.
            if (moduleName === '@jupyter-widgets/controls') {
                resolve(widgets);
            }
            else if (moduleName === '@jupyter-widgets/base') {
                resolve(base);
            }
            else if (moduleName === '@jupyter-widgets/output') {
                resolve(outputWidgets);
            }
            else {
                var fallback = function (err) {
                    var failedId = err.requireModules && err.requireModules[0];
                    if (failedId) {
                        console.log("Falling back to unpkg.com for " + moduleName + "@" + moduleVersion);
                        window.require(["https://unpkg.com/" + moduleName + "@" + moduleVersion + "/dist/index.js"], resolve, reject);
                    }
                    else {
                        throw err;
                    }
                };
                window.require([moduleName + ".js"], resolve, fallback);
            }
        }).then(function (module) {
            if (module[className]) {
                return module[className];
            }
            else {
                return Promise.reject("Class " + className + " not found in module " + moduleName + "@" + moduleVersion);
            }
        });
    };
    return HTMLManager;
}(base.ManagerBase));
exports.HTMLManager = HTMLManager;
;
