"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 React = require('react');
var BaseComponent_1 = require('../../common/BaseComponent');
var RESIZE_DELAY = 500;
function withViewport(ComposedComponent) {
    return (function (_super) {
        __extends(WithViewportComponent, _super);
        function WithViewportComponent() {
            _super.call(this);
            this.state = {
                viewport: {
                    width: 0,
                    height: 0
                }
            };
        }
        WithViewportComponent.prototype.componentDidMount = function () {
            this._onAsyncResize = this._async.debounce(this._onAsyncResize, RESIZE_DELAY, {
                leading: false
            });
            this._events.on(window, 'resize', this._onAsyncResize);
            this._updateViewport();
        };
        WithViewportComponent.prototype.componentWillUnmount = function () {
            this._events.dispose();
        };
        WithViewportComponent.prototype.render = function () {
            var viewport = this.state.viewport;
            return (React.createElement("div", {className: 'ms-Viewport', ref: 'root', style: { minWidth: 1, minHeight: 1 }}, this.state.viewport.height > 0 && (React.createElement(ComposedComponent, React.__spread({ref: 'component', viewport: viewport}, this.props)))));
        };
        WithViewportComponent.prototype.forceUpdate = function () {
            this.refs.component.forceUpdate();
        };
        WithViewportComponent.prototype._onAsyncResize = function () {
            this._updateViewport();
        };
        WithViewportComponent.prototype._updateViewport = function () {
            var viewportElement = this.refs.root;
            var scrollElement = this._findScrollableElement(viewportElement);
            var clientRect = viewportElement.getBoundingClientRect();
            var scrollRect = scrollElement.getBoundingClientRect();
            this.setState({
                viewport: {
                    width: clientRect.width,
                    height: scrollRect.height
                }
            });
        };
        WithViewportComponent.prototype._findScrollableElement = function (rootElement) {
            var computedOverflow = getComputedStyle(rootElement)['overflow-y'];
            while ((rootElement !== document.body) &&
                (computedOverflow !== 'auto') &&
                (computedOverflow !== 'scroll')) {
                rootElement = rootElement.parentElement;
                computedOverflow = getComputedStyle(rootElement)['overflow-y'];
            }
            return rootElement;
        };
        return WithViewportComponent;
    }(BaseComponent_1.BaseComponent));
}
exports.withViewport = withViewport;

//# sourceMappingURL=withViewport.js.map
