'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports['default'] = undefined;

var _defineProperty2 = require('babel-runtime/helpers/defineProperty');

var _defineProperty3 = _interopRequireDefault(_defineProperty2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _dec, _class, _class2, _temp, _initialiseProps;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

var _reactRouterDom = require('react-router-dom');

var _mobxReact = require('mobx-react');

var _mobx = require('mobx');

var _axios = require('../axios');

var _axios2 = _interopRequireDefault(_axios);

var _HotkeyManager = require('../master/HotkeyManager');

var _HotkeyManager2 = _interopRequireDefault(_HotkeyManager);

var _transformHotkey = require('../util/transformHotkey');

var _transformHotkey2 = _interopRequireDefault(_transformHotkey);

require('./style');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var PageContent = (_dec = (0, _mobxReact.inject)('AppState', 'MenuStore'), (0, _reactRouterDom.withRouter)(_class = _dec(_class = (0, _mobxReact.observer)(_class = (_temp = _class2 = function (_Component) {
  (0, _inherits3['default'])(PageContent, _Component);

  function PageContent(props) {
    (0, _classCallCheck3['default'])(this, PageContent);

    var _this = (0, _possibleConstructorReturn3['default'])(this, (PageContent.__proto__ || Object.getPrototypeOf(PageContent)).call(this, props));

    _initialiseProps.call(_this);

    _this.urlKey = undefined;
    _this.ref = _react2['default'].createRef();
    return _this;
  }

  (0, _createClass3['default'])(PageContent, [{
    key: 'componentWillMount',
    value: function componentWillMount() {
      this.checkIfFocus(this.props);
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      this.checkIfFocus(nextProps);
    }
  }, {
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _props = this.props,
          MenuStore = _props.MenuStore,
          history = _props.history;

      var urlKey = history.location.pathname;
      this.registryHotkey();
      // MenuStore.setContentKey(urlKey, Date.now());
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      this.removeHotkey();
    }
  }, {
    key: 'registryHotkey',
    value: function registryHotkey() {
      var props = this.props;
      var hotkeys = props.hotkeys,
          history = props.history;

      if (hotkeys) {
        // registry hotkeys
        var hotkeyManager = (0, _HotkeyManager2['default'])();
        var urlKey = history.location.pathname;
        this.urlKey = urlKey;
        hotkeyManager.addHandlers((0, _defineProperty3['default'])({}, urlKey, hotkeys));
        this.checkIfFocus(this.props, true);
      }
    }
  }, {
    key: 'removeHotkey',
    value: function removeHotkey() {
      // remove hotkeys
      var props = this.props;
      var hotkeys = props.hotkeys,
          history = props.history;

      if (hotkeys && this.urlKey) {
        // registry hotkeys
        var hotkeyManager = (0, _HotkeyManager2['default'])();
        hotkeyManager.deleteHandlers([this.urlKey]);
      }
    }

    /**
     * get string of keydown, return `Shift+G` or such like this
     * @param {*} event 
     */

  }, {
    key: 'handleEventToGetHotkeyString',
    value: function handleEventToGetHotkeyString(event) {
      return (0, _transformHotkey2['default'])(event);
    }
  }, {
    key: 'render',
    value: function render() {
      var props = this.props;
      var className = props.className,
          children = props.children,
          style = props.style,
          history = props.history;

      var classString = (0, _classnames2['default'])('page-content', className);

      // const urlKey = history.location.pathname;
      // const key = get(props.MenuStore.contentKeys, [urlKey]);

      if (!props.hotkeys) {
        return _react2['default'].createElement(
          'div',
          { className: classString, style: style },
          children
        );
      }
      return _react2['default'].createElement(
        'div',
        { className: classString, style: style, tabIndex: '-1', onKeyDown: this.handleOnKeyDown, ref: this.ref },
        children
      );
    }
  }]);
  return PageContent;
}(_react.Component), _initialiseProps = function _initialiseProps() {
  var _this2 = this;

  this.checkIfFocus = function (props) {
    var isFocus = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
    var hotkeys = props.hotkeys,
        history = props.history;

    if (hotkeys) {
      var urlKey = history.location.pathname;
      if (_this2.urlKey === urlKey || isFocus) {
        _this2.ref.current.focus();
      }
    }
  };

  this.handleOnKeyDown = function (event) {
    var hotkeyManager = (0, _HotkeyManager2['default'])();
    var props = _this2.props;
    var hotkeys = props.hotkeys,
        history = props.history;

    var urlKey = history.location.pathname;
    hotkeyManager.emit(urlKey, _this2.handleEventToGetHotkeyString(event), event);
  };
}, _temp)) || _class) || _class) || _class);
exports['default'] = PageContent;