// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var widget_1 = require('./widget');
var utils_1 = require('./utils');
var searching_1 = require('phosphor/lib/algorithm/searching');
var panel_1 = require('phosphor/lib/ui/panel');
var widget_2 = require('phosphor/lib/ui/widget');
var _ = require('underscore');
var $ = require('jquery');
var JupyterPhosphorPanelWidget = (function (_super) {
    __extends(JupyterPhosphorPanelWidget, _super);
    function JupyterPhosphorPanelWidget(options) {
        var view = options.view;
        delete options.view;
        _super.call(this, options);
        this._view = view;
    }
    /**
     * Process the phosphor message.
     *
     * Any custom phosphor widget used inside a Jupyter widget should override
     * the processMessage function like this.
     */
    JupyterPhosphorPanelWidget.prototype.processMessage = function (msg) {
        _super.prototype.processMessage.call(this, msg);
        this._view.processPhosphorMessage(msg);
    };
    /**
     * Dispose the widget.
     *
     * This causes the view to be destroyed as well with 'remove'
     */
    JupyterPhosphorPanelWidget.prototype.dispose = function () {
        if (this.isDisposed) {
            return;
        }
        _super.prototype.dispose.call(this);
        if (this._view) {
            this._view.remove();
        }
        this._view = null;
    };
    return JupyterPhosphorPanelWidget;
}(panel_1.Panel));
exports.JupyterPhosphorPanelWidget = JupyterPhosphorPanelWidget;
var BoxModel = (function (_super) {
    __extends(BoxModel, _super);
    function BoxModel() {
        _super.apply(this, arguments);
    }
    BoxModel.prototype.defaults = function () {
        return _.extend(_super.prototype.defaults.call(this), {
            _view_name: 'BoxView',
            _model_name: 'BoxModel',
            children: [],
            box_style: ''
        });
    };
    BoxModel.serializers = _.extend({
        children: { deserialize: widget_1.unpack_models }
    }, widget_1.DOMWidgetModel.serializers);
    return BoxModel;
}(widget_1.DOMWidgetModel));
exports.BoxModel = BoxModel;
var HBoxModel = (function (_super) {
    __extends(HBoxModel, _super);
    function HBoxModel() {
        _super.apply(this, arguments);
    }
    HBoxModel.prototype.defaults = function () {
        return _.extend(_super.prototype.defaults.call(this), {
            _view_name: 'HBoxView',
            _model_name: 'HBoxModel',
        });
    };
    return HBoxModel;
}(BoxModel));
exports.HBoxModel = HBoxModel;
var VBoxModel = (function (_super) {
    __extends(VBoxModel, _super);
    function VBoxModel() {
        _super.apply(this, arguments);
    }
    VBoxModel.prototype.defaults = function () {
        return _.extend(_super.prototype.defaults.call(this), {
            _view_name: 'VBoxView',
            _model_name: 'VBoxModel',
        });
    };
    return VBoxModel;
}(BoxModel));
exports.VBoxModel = VBoxModel;
var ProxyModel = (function (_super) {
    __extends(ProxyModel, _super);
    function ProxyModel() {
        _super.apply(this, arguments);
    }
    ProxyModel.prototype.defaults = function () {
        return _.extend(_super.prototype.defaults.call(this), {
            _view_name: 'ProxyView',
            _model_name: 'ProxyModel',
            child: null
        });
    };
    ProxyModel.serializers = _.extend({
        child: { deserialize: widget_1.unpack_models }
    }, widget_1.DOMWidgetModel.serializers);
    return ProxyModel;
}(widget_1.DOMWidgetModel));
exports.ProxyModel = ProxyModel;
var ProxyView = (function (_super) {
    __extends(ProxyView, _super);
    function ProxyView() {
        _super.apply(this, arguments);
    }
    ProxyView.prototype.initialize = function (parameters) {
        // Public constructor
        _super.prototype.initialize.call(this, parameters);
        this.el.classList.add('jupyter-widgets');
        this.el.classList.add('widget-container');
        this.box = this.el;
        this.child_promise = Promise.resolve();
    };
    ProxyView.prototype.render = function () {
        var that = this;
        var child_view = this.set_child(this.model.get('child'));
        this.listenTo(this.model, 'change:child', function (model, value) {
            this.set_child(value);
        });
        return child_view;
    };
    ProxyView.prototype.remove = function () {
        var _this = this;
        if (this.pWidget.isAttached) {
            widget_2.Widget.detach(this.pWidget);
        }
        this.child_promise.then(function () {
            if (_this.child) {
                _this.child.remove();
            }
        });
        _super.prototype.remove.call(this);
    };
    ProxyView.prototype.set_child = function (value) {
        var _this = this;
        if (this.child) {
            this.child.remove();
        }
        if (value) {
            this.child_promise = this.child_promise.then(function () {
                return _this.create_child_view(value).then(function (view) {
                    if (!_this.box) {
                        console.error('Widget place holder does not exist');
                        return;
                    }
                    while (_this.box.firstChild) {
                        _this.box.removeChild(_this.box.firstChild);
                    }
                    _this.box.appendChild(view.el);
                    // Trigger the displayed event of the child view.
                    _this.displayed.then(function () {
                        view.trigger('displayed', _this);
                    });
                    _this.child = view;
                    _this.trigger('child:created');
                }).catch(utils_1.reject('Could not add child view to proxy', true));
            });
        }
        return this.child_promise;
    };
    return ProxyView;
}(widget_1.DOMWidgetView));
exports.ProxyView = ProxyView;
var PlaceProxyModel = (function (_super) {
    __extends(PlaceProxyModel, _super);
    function PlaceProxyModel() {
        _super.apply(this, arguments);
    }
    PlaceProxyModel.prototype.defaults = function () {
        return _.extend(_super.prototype.defaults.call(this), {
            _view_name: 'PlaceProxyView',
            _model_name: 'PlaceProxyModel',
            selector: ''
        });
    };
    return PlaceProxyModel;
}(ProxyModel));
exports.PlaceProxyModel = PlaceProxyModel;
var PlaceProxyView = (function (_super) {
    __extends(PlaceProxyView, _super);
    function PlaceProxyView() {
        _super.apply(this, arguments);
    }
    PlaceProxyView.prototype.initialize = function (parameters) {
        _super.prototype.initialize.call(this, parameters);
        this.update_selector(this.model, this.model.get('selector'));
        this.listenTo(this.model, 'change:selector', this.update_selector);
    };
    PlaceProxyView.prototype.update_selector = function (model, selector) {
        this.box = selector && document.querySelector(selector) || this.el;
        this.set_child(this.model.get('child'));
    };
    return PlaceProxyView;
}(ProxyView));
exports.PlaceProxyView = PlaceProxyView;
var BoxView = (function (_super) {
    __extends(BoxView, _super);
    function BoxView() {
        _super.apply(this, arguments);
    }
    BoxView.prototype._createElement = function (tagName) {
        this.pWidget = new JupyterPhosphorPanelWidget({ view: this });
        return this.pWidget.node;
    };
    BoxView.prototype._setElement = function (el) {
        if (this.el || el !== this.pWidget.node) {
            // Boxes don't allow setting the element beyond the initial creation.
            throw new Error('Cannot reset the DOM element.');
        }
        this.el = this.pWidget.node;
        this.$el = $(this.pWidget.node);
    };
    /**
     * Public constructor
     */
    BoxView.prototype.initialize = function (parameters) {
        var _this = this;
        _super.prototype.initialize.call(this, parameters);
        this.children_views = new widget_1.ViewList(this.add_child_model, null, this);
        this.listenTo(this.model, 'change:children', function (model, value) {
            _this.children_views.update(value);
        });
        this.listenTo(this.model, 'change:box_style', this.update_box_style);
        this.pWidget.addClass('jupyter-widgets');
        this.pWidget.addClass('widget-container');
        this.pWidget.addClass('widget-box');
    };
    /**
     * Called when view is rendered.
     */
    BoxView.prototype.render = function () {
        _super.prototype.render.call(this);
        this.children_views.update(this.model.get('children'));
        this.set_box_style();
    };
    BoxView.prototype.update_box_style = function () {
        this.update_mapped_classes(BoxView.class_map, 'box_style');
    };
    BoxView.prototype.set_box_style = function () {
        this.set_mapped_classes(BoxView.class_map, 'box_style');
    };
    BoxView.prototype.add_child_model = function (model) {
        var _this = this;
        // we insert a dummy element so the order is preserved when we add
        // the rendered content later.
        var dummy = new widget_2.Widget();
        this.pWidget.addWidget(dummy);
        return this.create_child_view(model).then(function (view) {
            // replace the dummy widget with the new one.
            var i = searching_1.indexOf(_this.pWidget.widgets, dummy);
            _this.pWidget.insertWidget(i, view.pWidget);
            dummy.dispose();
            return view;
        }).catch(utils_1.reject('Could not add child view to box', true));
    };
    BoxView.prototype.remove = function () {
        if (this.pWidget.isAttached) {
            widget_2.Widget.detach(this.pWidget);
        }
        this.children_views.dispose();
        _super.prototype.remove.call(this);
    };
    BoxView.class_map = {
        success: ['alert', 'alert-success'],
        info: ['alert', 'alert-info'],
        warning: ['alert', 'alert-warning'],
        danger: ['alert', 'alert-danger']
    };
    return BoxView;
}(widget_1.DOMWidgetView));
exports.BoxView = BoxView;
var HBoxView = (function (_super) {
    __extends(HBoxView, _super);
    function HBoxView() {
        _super.apply(this, arguments);
    }
    /**
     * Public constructor
     */
    HBoxView.prototype.initialize = function (parameters) {
        _super.prototype.initialize.call(this, parameters);
        this.pWidget.addClass('widget-hbox');
    };
    return HBoxView;
}(BoxView));
exports.HBoxView = HBoxView;
var VBoxView = (function (_super) {
    __extends(VBoxView, _super);
    function VBoxView() {
        _super.apply(this, arguments);
    }
    /**
     * Public constructor
     */
    VBoxView.prototype.initialize = function (parameters) {
        _super.prototype.initialize.call(this, parameters);
        this.pWidget.addClass('widget-vbox');
    };
    return VBoxView;
}(BoxView));
exports.VBoxView = VBoxView;
