// 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 panel_1 = require('phosphor/lib/ui/panel');
var widget_2 = require('phosphor/lib/ui/widget');
var searching_1 = require('phosphor/lib/algorithm/searching');
var _ = require('underscore');
var $ = require('jquery');
var JupyterPhosphorPanelWidget = (function (_super) {
    __extends(JupyterPhosphorPanelWidget, _super);
    function JupyterPhosphorPanelWidget(options) {
        _super.call(this);
        this._view = options.view;
    }
    JupyterPhosphorPanelWidget.prototype.onAfterAttach = function (msg) {
        _super.prototype.onAfterAttach.call(this, msg);
        this._view.trigger('displayed');
    };
    Object.defineProperty(JupyterPhosphorPanelWidget.prototype, "isDisposed", {
        get: function () {
            return this._view === null;
        },
        enumerable: true,
        configurable: true
    });
    JupyterPhosphorPanelWidget.prototype.dispose = function () {
        if (this.isDisposed) {
            return;
        }
        _super.prototype.dispose.call(this);
        this._view = null;
    };
    JupyterPhosphorPanelWidget.prototype.onResize = function (msg) {
        if (this._view.onResize) {
            this._view.onResize(msg);
        }
        _super.prototype.onResize.call(this, msg);
    };
    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: '',
            overflow_x: '',
            overflow_y: ''
        });
    };
    BoxModel.serializers = _.extend({
        children: { deserialize: widget_1.unpack_models }
    }, widget_1.DOMWidgetModel.serializers);
    return BoxModel;
}(widget_1.DOMWidgetModel));
exports.BoxModel = BoxModel;
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;
        _super.prototype.remove.call(this);
        this.child_promise.then(function () {
            if (_this.child) {
                _this.child.remove();
            }
        });
    };
    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) {
        _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:overflow_x', this.update_overflow_x);
        this.listenTo(this.model, 'change:overflow_y', this.update_overflow_y);
        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 () {
        this.children_views.update(this.model.get('children'));
        this.update_overflow_x();
        this.update_overflow_y();
        this.update_box_style();
    };
    /**
     * Called when the x-axis overflow setting is changed.
     */
    BoxView.prototype.update_overflow_x = function () {
        this.pWidget.node.style.overflowX = this.model.get('overflow_x');
    };
    /**
     * Called when the y-axis overflow setting is changed.
     */
    BoxView.prototype.update_overflow_y = function () {
        this.pWidget.node.style.overflowY = this.model.get('overflow_y');
    };
    BoxView.prototype.update_box_style = function () {
        var class_map = {
            success: ['alert', 'alert-success'],
            info: ['alert', 'alert-info'],
            warning: ['alert', 'alert-warning'],
            danger: ['alert', 'alert-danger']
        };
        this.update_mapped_classes(class_map, 'box_style');
    };
    /**
     * Called when a model is added to the children list.
     */
    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();
            // Trigger the displayed event of the child view.
            _this.displayed.then(function () {
                view.trigger('displayed', _this);
            });
            return view;
        }).catch(utils_1.reject('Could not add child view to box', true));
    };
    BoxView.prototype.remove = function () {
        // We remove this widget before removing the children as an optimization
        // we want to remove the entire container from the DOM first before
        // removing each individual child separately.
        _super.prototype.remove.call(this);
        this.children_views.remove();
    };
    return BoxView;
}(widget_1.DOMWidgetView));
exports.BoxView = BoxView;
