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 __());
};
define(["require", "exports", 'react', 'react-dom', './LayerHost', './Layer.scss'], function (require, exports, React, ReactDOM, LayerHost_1) {
    "use strict";
    var LAYER_HOST_ELEMENT_ID = 'ms-layer-host';
    var _instance = 0;
    var _layerHost;
    var Layer = (function (_super) {
        __extends(Layer, _super);
        function Layer(props) {
            _super.call(this, props);
            this._layer = {
                id: String(_instance++),
                children: props.children
            };
        }
        Layer.prototype.render = function () {
            var isInLayer = this.context.isInLayer;
            return isInLayer ? this.props.children : null;
        };
        Layer.prototype.componentWillMount = function () {
            if (!_layerHost) {
                var hostElement = document.createElement('div');
                hostElement.setAttribute('id', LAYER_HOST_ELEMENT_ID);
                document.body.appendChild(hostElement);
                var layerHost = ReactDOM.render((React.createElement(LayerHost_1.LayerHost, null)), hostElement);
                _layerHost = layerHost;
            }
        };
        Layer.prototype.componentDidMount = function () {
            if (!this.context.isInLayer) {
                _layerHost.addLayer(this._layer, this.props.onLayerMounted);
            }
            else {
                if (this.props.onLayerMounted) {
                    this.props.onLayerMounted();
                }
            }
        };
        Layer.prototype.componentWillReceiveProps = function (props) {
            if (!this.context.isInLayer) {
                this._layer.children = props.children;
                _layerHost.updateLayer(this._layer);
            }
        };
        Layer.prototype.componentWillUnmount = function () {
            if (!this.context.isInLayer) {
                _layerHost.removeLayer(this._layer);
            }
        };
        Layer.contextTypes = {
            isInLayer: React.PropTypes.bool
        };
        return Layer;
    }(React.Component));
    exports.Layer = Layer;
});
