"use strict";
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        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 extendStatics(d, b);
    };
    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 = /** @class */ (function (_super) {
    __extends(HTMLManager, _super);
    function HTMLManager(options) {
        var _this = _super.call(this) || this;
        _this.loader = options && options.loader;
        _this.renderMime = new rendermime_1.RenderMimeRegistry({
            initialFactories: rendermime_1.standardRendererFactories
        });
        _this.renderMime.addFactory({
            safe: false,
            mimeTypes: [output_renderers_1.WIDGET_MIMETYPE],
            createRenderer: function (options) { return new output_renderers_1.WidgetRenderer(options, _this); }
        }, 0);
        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 (comm_target_name, model_id, data, metadata, buffers) {
        return Promise.resolve({
            on_close: function () { return; },
            on_msg: function () { return; },
            close: function () { return; }
        });
    };
    /**
     * Load a class and return a promise to the loaded object.
     */
    HTMLManager.prototype.loadClass = function (className, moduleName, moduleVersion) {
        var _this = this;
        return new Promise(function (resolve, reject) {
            if (moduleName === '@jupyter-widgets/base') {
                resolve(base);
            }
            else if (moduleName === '@jupyter-widgets/controls') {
                resolve(widgets);
            }
            else if (moduleName === '@jupyter-widgets/output') {
                resolve(outputWidgets);
            }
            else if (_this.loader !== undefined) {
                resolve(_this.loader(moduleName, moduleVersion));
            }
            else {
                reject("Could not load module " + moduleName + "@" + moduleVersion);
            }
        }).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;
