text: "use strict";
  var __extends = (this && this.__extends) || (function () {
      var extendStatics = Object.setPrototypeOf ||
          ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
          function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
      return function (d, b) {
          extendStatics(d, b);
          function __() { this.constructor = d; }
          d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
      };
  })();
  var __assign = (this && this.__assign) || Object.assign || function(t) {
      for (var s, i = 1, n = arguments.length; i < n; i++) {
          s = arguments[i];
          for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
              t[p] = s[p];
      }
      return t;
  };
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
      var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
      if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
      else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
      return c > 3 && r && Object.defineProperty(target, key, r), r;
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  var React = require("react");
  var Utilities_1 = require("../../Utilities");
  var FocusTrapZone = (function (_super) {
      __extends(FocusTrapZone, _super);
      function FocusTrapZone() {
          var _this = _super !== null && _super.apply(this, arguments) || this;
          _this._isInFocusStack = false;
          _this._isInClickStack = false;
          return _this;
      }
      FocusTrapZone.prototype.componentWillMount = function () {
          var _a = this.props, _b = _a.isClickableOutsideFocusTrap, isClickableOutsideFocusTrap = _b === void 0 ? false : _b, _c = _a.forceFocusInsideTrap, forceFocusInsideTrap = _c === void 0 ? true : _c;
          if (forceFocusInsideTrap) {
              this._isInFocusStack = true;
              FocusTrapZone._focusStack.push(this);
          }
          if (!isClickableOutsideFocusTrap) {
              this._isInClickStack = true;
              FocusTrapZone._clickStack.push(this);
          }
      };
      FocusTrapZone.prototype.componentDidMount = function () {
          var _a = this.props, elementToFocusOnDismiss = _a.elementToFocusOnDismiss, _b = _a.isClickableOutsideFocusTrap, isClickableOutsideFocusTrap = _b === void 0 ? false : _b, _c = _a.forceFocusInsideTrap, forceFocusInsideTrap = _c === void 0 ? true : _c;
          this._previouslyFocusedElement = elementToFocusOnDismiss ? elementToFocusOnDismiss : document.activeElement;
          if (!Utilities_1.elementContains(this.refs.root, this._previouslyFocusedElement)) {
              this.focus();
          }
          if (forceFocusInsideTrap) {
              this._events.on(window, 'focus', this._forceFocusInTrap, true);
          }
          if (!isClickableOutsideFocusTrap) {
              this._events.on(window, 'click', this._forceClickInTrap, true);
          }
      };
      FocusTrapZone.prototype.componentWillUnmount = function () {
          var _this = this;
          var ignoreExternalFocusing = this.props.ignoreExternalFocusing;
          this._events.dispose();
          if (this._isInFocusStack || this._isInClickStack) {
              var filter = function (value) {
                  return _this !== value;
              };
              if (this._isInFocusStack) {
                  FocusTrapZone._focusStack = FocusTrapZone._focusStack.filter(filter);
              }
              if (this._isInClickStack) {
                  FocusTrapZone._clickStack = FocusTrapZone._clickStack.filter(filter);
              }
          }
          if (!ignoreExternalFocusing && this._previouslyFocusedElement) {
              this._previouslyFocusedElement.focus();
          }
      };
      FocusTrapZone.prototype.render = function () {
          var _a = this.props, className = _a.className, ariaLabelledBy = _a.ariaLabelledBy;
          var divProps = Utilities_1.getNativeProps(this.props, Utilities_1.divProperties);
          return (React.createElement("div", __assign({}, divProps, { className: className, ref: 'root', "aria-labelledby": ariaLabelledBy, onKeyDown: this._onKeyboardHandler }), this.props.children));
      };
      /**
       * Need to expose this method in case of popups since focus needs to be set when popup is opened
       */
      FocusTrapZone.prototype.focus = function () {
          var firstFocusableSelector = this.props.firstFocusableSelector;
          var _firstFocusableChild;
          var root = this.refs.root;
          if (firstFocusableSelector) {
              _firstFocusableChild = root.querySelector('.' + firstFocusableSelector);
          }
          else {
              _firstFocusableChild = Utilities_1.getNextElement(root, root.firstChild, true, false, false, true);
          }
          if (_firstFocusableChild) {
              _firstFocusableChild.focus();
          }
      };
      FocusTrapZone.prototype._onKeyboardHandler = function (ev) {
          if (ev.which !== Utilities_1.KeyCodes.tab) {
              return;
          }
          var root = this.refs.root;
          var _firstFocusableChild = Utilities_1.getFirstFocusable(root, root.firstChild, true);
          var _lastFocusableChild = Utilities_1.getLastFocusable(root, root.lastChild, true);
          if (ev.shiftKey && _firstFocusableChild === ev.target) {
              _lastFocusableChild.focus();
              ev.preventDefault();
              ev.stopPropagation();
          }
          else if (!ev.shiftKey && _lastFocusableChild === ev.target) {
              _firstFocusableChild.focus();
              ev.preventDefault();
              ev.stopPropagation();
          }
      };
      FocusTrapZone.prototype._forceFocusInTrap = function (ev) {
          if (FocusTrapZone._focusStack.length && this === FocusTrapZone._focusStack[FocusTrapZone._focusStack.length - 1]) {
              var focusedElement = document.activeElement;
              if (!Utilities_1.elementContains(this.refs.root, focusedElement)) {
                  this.focus();
                  ev.preventDefault();
                  ev.stopPropagation();
              }
          }
      };
      FocusTrapZone.prototype._forceClickInTrap = function (ev) {
          if (FocusTrapZone._clickStack.length && this === FocusTrapZone._clickStack[FocusTrapZone._clickStack.length - 1]) {
              var clickedElement = ev.target;
              if (clickedElement && !Utilities_1.elementContains(this.refs.root, clickedElement)) {
                  this.focus();
                  ev.preventDefault();
                  ev.stopPropagation();
              }
          }
      };
      return FocusTrapZone;
  }(Utilities_1.BaseComponent));
  FocusTrapZone._focusStack = [];
  FocusTrapZone._clickStack = [];
  __decorate([
      Utilities_1.autobind
  ], FocusTrapZone.prototype, "_onKeyboardHandler", null);
  exports.FocusTrapZone = FocusTrapZone;
  
  //# sourceMappingURL=FocusTrapZone.js.map
  