'use strict';

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

var _modalContainer = require('choerodon-hap-ui/lib/modal-container');

var _modalContainer2 = _interopRequireDefault(_modalContainer);

var _localeContext2 = require('choerodon-hap-ui/lib/locale-context');

var _localeContext3 = _interopRequireDefault(_localeContext2);

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;

require('choerodon-hap-ui/lib/modal-container/style');

require('choerodon-hap-ui/lib/locale-context/style');

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

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

var _reactHelmet = require('react-helmet');

require('moment/locale/zh-cn');

var _sockjsClient = require('sockjs-client');

var _sockjsClient2 = _interopRequireDefault(_sockjsClient);

var _menu = require('../menu');

var _menu2 = _interopRequireDefault(_menu);

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

var _axios2 = _interopRequireDefault(_axios);

var _tabbar = require('../tabbar');

var _tabbar2 = _interopRequireDefault(_tabbar);

var _header = require('../header');

var _header2 = _interopRequireDefault(_header);

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

var _HotkeyManager2 = _interopRequireDefault(_HotkeyManager);

var _IntlManager = require('./IntlManager');

var _IntlManager2 = _interopRequireDefault(_IntlManager);

var _repeatLogin = require('../../common/repeatLogin');

var _repeatLogin2 = _interopRequireDefault(_repeatLogin);

var _constants = require('../../common/constants');

require('./style');

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

var Index = (_dec = (0, _mobxReact.inject)('AppState'), _dec(_class = (0, _mobxReact.observer)(_class = function (_React$Component) {
  (0, _inherits3['default'])(Index, _React$Component);

  function Index() {
    (0, _classCallCheck3['default'])(this, Index);
    return (0, _possibleConstructorReturn3['default'])(this, (Index.__proto__ || Object.getPrototypeOf(Index)).apply(this, arguments));
  }

  (0, _createClass3['default'])(Index, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.handleLocaleContext();
      this.initHotkeyManager();
      this.initIntl();
      this.initSocket();
    }
  }, {
    key: 'initSocket',
    value: function initSocket() {
      var socket = new _sockjsClient2['default'](_constants.WEBSOCKET_SERVER + '/websocket');
      socket.onmessage = function (e) {
        if (e.data && e.data.action === 'SYS_REPEAT_LOGIN') {
          socket.close();
          (0, _repeatLogin2['default'])();
        }
      };
    }

    /**
     * query hotkey dictionary
     * and set in manager
     */

  }, {
    key: 'initHotkeyManager',
    value: function initHotkeyManager() {
      var hotkeyManager = (0, _HotkeyManager2['default'])();
      _axios2['default'].post('/dataset/Hotkey/queries?page=1&pagesize=10', {}).then(function (res) {
        if (res.success) {
          hotkeyManager.init(res.rows);
        } else {
          hotkeyManager.init([]);
        }
      })['catch'](function (error) {
        hotkeyManager.init([]);
      });
    }
  }, {
    key: 'initIntl',
    value: function initIntl() {
      var intlManager = (0, _IntlManager2['default'])();
      _axios2['default'].post('/common/prompt/hap-common', {}).then(function (res) {
        intlManager.add(res || {});
      })['catch'](function (error) {
        intlManager.add({});
      });
    }
  }, {
    key: 'handleLocaleContext',
    value: function handleLocaleContext() {
      var AppState = this.props.AppState;
      var locales = AppState.locales,
          currentLang = AppState.currentLang;

      if (Object.keys(locales).length) {
        _localeContext3['default'].setSupports(locales);
      }

      if (currentLang) {
        import('choerodon-hap-ui/lib/locale-context/' + currentLang + '.js').then(function (o) {
          _localeContext3['default'].setLocale(o);
        });
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _props = this.props,
          AutoRouter = _props.AutoRouter,
          AppState = _props.AppState;

      var title = AppState.title ? _react2['default'].createElement(
        'title',
        null,
        AppState.title
      ) : null;

      var menu = !AppState.hideMenu ? _react2['default'].createElement(_menu2['default'], null) : null;

      return _react2['default'].createElement(
        'div',
        { className: 'master-wrapper' },
        _react2['default'].createElement(_header2['default'], null),
        _react2['default'].createElement(
          'div',
          { className: 'master-body' },
          _react2['default'].createElement(
            'div',
            { className: 'master-content-wrapper' },
            menu,
            _react2['default'].createElement(
              'div',
              { className: 'master-content-container' },
              _react2['default'].createElement(
                'div',
                { className: 'master-container' },
                _react2['default'].createElement(_tabbar2['default'], null),
                _react2['default'].createElement(
                  _reactHelmet.Helmet,
                  { preserved: true },
                  title
                ),
                _react2['default'].createElement(
                  'div',
                  { className: 'master-content' },
                  _react2['default'].createElement(AutoRouter, null)
                )
              )
            )
          )
        ),
        _react2['default'].createElement(_modalContainer2['default'], null)
      );
    }
  }]);
  return Index;
}(_react2['default'].Component)) || _class) || _class);
exports['default'] = Index;