"use strict";
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
Object.defineProperty(exports, "__esModule", { value: true });
const coreutils_1 = require("@lumino/coreutils");
const base_1 = require("@jupyter-widgets/base");
const jupyterlab_manager_1 = require("@jupyter-widgets/jupyterlab-manager");
const widget_1 = require("./widget");
const version_1 = require("./version");
const outputarea_1 = require("@jupyterlab/outputarea");
require("../css/floatview.css");
const EXTENSION_ID = '@jupyter-widgets/jupyterlab-floatview';
const floatviewPlugin = {
    id: EXTENSION_ID,
    requires: [base_1.IJupyterWidgetRegistry],
    activate: activateWidgetExtension,
    autoStart: true
};
exports.default = floatviewPlugin;
class FloatViewOutputArea extends outputarea_1.OutputArea {
    processMessage(msg) {
        switch (msg.type) {
            case 'close-request':
                this.container.model.set('uid', String('disposed'));
                this.container.model.save_changes();
                this.dispose();
                break;
        }
        super.processMessage(msg);
    }
    setContainer(container) {
        this.container = container;
    }
}
/**
 * Activate the widget extension.
 */
function activateWidgetExtension(app, registry) {
    let FloatviewView = class extends jupyterlab_manager_1.output.OutputView {
        render() {
            if (!this.model.rendered) {
                super.render();
                let w = new FloatViewOutputArea({
                    rendermime: this.model.widget_manager.rendermime,
                    contentFactory: outputarea_1.OutputArea.defaultContentFactory,
                    model: this.model.outputs
                });
                this._outputView = w;
                w.setContainer(this);
                w.addClass('jupyterlab-floatview');
                w.addClass('jp-LinkedOutputView');
                w.title.label = this.model.get('title');
                w.title.closable = true;
                w.id = coreutils_1.UUID.uuid4();
                this.model.set('uid', String(w.id));
                this.model.save_changes();
                if (Object.keys(this.model.views).length > 1) {
                    w.node.style.display = 'none';
                    let key = Object.keys(this.model.views)[0];
                    this.model.views[key].then((v) => {
                        v._outputView.activate();
                    });
                }
                else {
                    app.shell.add(w, 'main', { mode: this.model.get('mode') });
                }
            }
        }
    };
    registry.registerWidget({
        name: '@jupyter-widgets/jupyterlab-floatview',
        version: version_1.EXTENSION_SPEC_VERSION,
        exports: {
            FloatviewModel: widget_1.FloatviewModel,
            FloatviewView: FloatviewView
        }
    });
}
