"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.ReturnFocus = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _events = require("events");
class ReturnFocus {
  constructor() {
    (0, _defineProperty2.default)(this, "prevFocusedElement", null);
    (0, _defineProperty2.default)(this, "modalElement", null);
    (0, _defineProperty2.default)(this, "events", new _events.EventEmitter());
    (0, _mobx.makeObservable)(this, {
      onClose: _mobx.action,
      onOpen: _mobx.action
    });
  }
  onOpen() {
    var _this$modalElement;
    this.prevFocusedElement = document.activeElement;
    (_this$modalElement = this.modalElement) == null || _this$modalElement.focus({
      preventScroll: true
    });
  }
  onClose() {
    const toFocus = this.prevFocusedElement;
    if (toFocus instanceof HTMLElement) {
      try {
        toFocus.focus();
      } catch (e) {
        console.error(e);
      }
      this.prevFocusedElement = null;
    }
  }
  init() {
    return () => {
      this.events.emit('unmount');
    };
  }
}
exports.ReturnFocus = ReturnFocus;
//# sourceMappingURL=ReturnFocus.js.map