"use strict";
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
Object.defineProperty(exports, "__esModule", { value: true });
var widgets_1 = require("@phosphor/widgets");
exports.WIDGET_MIMETYPE = 'application/vnd.jupyter.widget-view+json';
// Renderer to allow the output widget to render sub-widgets
var WidgetRenderer = (function () {
    function WidgetRenderer(widget_manager) {
        this.mimeTypes = [exports.WIDGET_MIMETYPE];
        this._manager = widget_manager;
    }
    WidgetRenderer.prototype.canRender = function (options) {
        var source = options.model.data.get(options.mimeType);
        var model = this._manager.get_model(source.model_id);
        return model !== void 0;
    };
    WidgetRenderer.prototype.wouldSanitize = function (options) {
        return false;
    };
    WidgetRenderer.prototype.render = function (options) {
        var _this = this;
        var widget = new widgets_1.Panel();
        var source = options.model.data.get(options.mimeType);
        var model = this._manager.get_model(source.model_id);
        if (model) {
            model.then(function (model) {
                return _this._manager.display_model(null, model, {
                    el: widget.node
                });
            }).catch(function (error) {
                console.log('Error creating widget.');
                console.log(error);
                var node = document.createElement('p');
                node.textContent = 'Error creating widget.';
                widget.addWidget(new widgets_1.Widget({ node: node }));
            });
        }
        else {
            var node = document.createElement('p');
            node.textContent = 'Error creating widget.';
            widget.addWidget(new widgets_1.Widget({ node: node }));
        }
        return widget;
    };
    return WidgetRenderer;
}());
exports.WidgetRenderer = WidgetRenderer;
