define(["require", "exports", "tslib", "react", "../../Utilities"], function (require, exports, tslib_1, React, Utilities_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    /**
     * This adds accessibility to Dialog and Panel controls
     */
    var Popup = /** @class */ (function (_super) {
        tslib_1.__extends(Popup, _super);
        function Popup(props) {
            var _this = _super.call(this, props) || this;
            _this._root = Utilities_1.createRef();
            _this._onKeyDown = function (ev) {
                switch (ev.which) {
                    case 27 /* escape */:
                        if (_this.props.onDismiss) {
                            _this.props.onDismiss(ev);
                            ev.preventDefault();
                            ev.stopPropagation();
                        }
                        break;
                }
            };
            _this.state = { needsVerticalScrollBar: false };
            return _this;
        }
        Popup.prototype.componentWillMount = function () {
            this._originalFocusedElement = Utilities_1.getDocument().activeElement;
        };
        Popup.prototype.componentDidMount = function () {
            if (!this._root.value) {
                return;
            }
            this._events.on(this._root.value, 'focus', this._onFocus, true);
            this._events.on(this._root.value, 'blur', this._onBlur, true);
            if (Utilities_1.doesElementContainFocus(this._root.value)) {
                this._containsFocus = true;
            }
            this._updateScrollBarAsync();
        };
        Popup.prototype.componentDidUpdate = function () {
            this._updateScrollBarAsync();
        };
        Popup.prototype.componentWillUnmount = function () {
            if (this.props.shouldRestoreFocus &&
                this._originalFocusedElement &&
                this._containsFocus &&
                this._originalFocusedElement !== window) {
                // This slight delay is required so that we can unwind the stack, let react try to mess with focus, and then
                // apply the correct focus. Without the setTimeout, we end up focusing the correct thing, and then React wants
                // to reset the focus back to the thing it thinks should have been focused.
                if (this._originalFocusedElement) {
                    this._originalFocusedElement.focus();
                }
            }
        };
        Popup.prototype.render = function () {
            var _a = this.props, role = _a.role, className = _a.className, ariaLabel = _a.ariaLabel, ariaLabelledBy = _a.ariaLabelledBy, ariaDescribedBy = _a.ariaDescribedBy, style = _a.style;
            return (React.createElement("div", tslib_1.__assign({ ref: this._root }, Utilities_1.getNativeProps(this.props, Utilities_1.divProperties), { className: className, role: role, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, "aria-describedby": ariaDescribedBy, onKeyDown: this._onKeyDown, style: tslib_1.__assign({ overflowY: this.state.needsVerticalScrollBar ? 'scroll' : 'auto' }, style) }), this.props.children));
        };
        Popup.prototype._updateScrollBarAsync = function () {
            var _this = this;
            this._async.requestAnimationFrame(function () {
                _this._getScrollBar();
            });
        };
        Popup.prototype._getScrollBar = function () {
            var needsVerticalScrollBar = false;
            if (this._root && this._root.value && this._root.value.firstElementChild) {
                needsVerticalScrollBar = this._root.value.clientHeight > 0
                    && this._root.value.firstElementChild.clientHeight > this._root.value.clientHeight;
            }
            if (this.state.needsVerticalScrollBar !== needsVerticalScrollBar) {
                this.setState({
                    needsVerticalScrollBar: needsVerticalScrollBar
                });
            }
        };
        Popup.prototype._onFocus = function () {
            this._containsFocus = true;
        };
        Popup.prototype._onBlur = function () {
            this._containsFocus = false;
        };
        Popup.defaultProps = {
            shouldRestoreFocus: true
        };
        return Popup;
    }(Utilities_1.BaseComponent));
    exports.Popup = Popup;
});
//# sourceMappingURL=Popup.js.map