'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _desc, _value, _class;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _redux = require('redux');

var _reactRedux = require('react-redux');

var _reactRouterRedux = require('react-router-redux');

var _coreDecorators = require('core-decorators');

require('../styles/global/index.less');

var _global = require('../actions/global');

var _longRunOpt = require('../system/longRunOpt');

var _MapUtils = require('../utils/MapUtils');

var _layoutFrameWork = require('../layoutFrameWork');

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) {
  var desc = {};
  Object['ke' + 'ys'](descriptor).forEach(function (key) {
    desc[key] = descriptor[key];
  });
  desc.enumerable = !!desc.enumerable;
  desc.configurable = !!desc.configurable;

  if ('value' in desc || desc.initializer) {
    desc.writable = true;
  }

  desc = decorators.slice().reverse().reduce(function (desc, decorator) {
    return decorator(target, property, desc) || desc;
  }, desc);

  if (context && desc.initializer !== void 0) {
    desc.value = desc.initializer ? desc.initializer.call(context) : void 0;
    desc.initializer = undefined;
  }

  if (desc.initializer === void 0) {
    Object['define' + 'Property'](target, property, desc);
    desc = null;
  }

  return desc;
}

var App = (_class = function (_React$Component) {
  _inherits(App, _React$Component);

  function App(props) {
    _classCallCheck(this, App);

    var _this = _possibleConstructorReturn(this, (App.__proto__ || Object.getPrototypeOf(App)).call(this, props));

    var defaultNavBarStatus = (0, _MapUtils.getValueByKey)(_this.props.routing, null, 'locationBeforeTransitions', 'pathname') === '/';
    _this.state = {
      navBarStatus: defaultNavBarStatus
    };
    return _this;
  }

  _createClass(App, [{
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      var newPathName = (0, _MapUtils.getValueByKey)(nextProps.routing, null, 'locationBeforeTransitions', 'pathname');
      var oldPathName = (0, _MapUtils.getValueByKey)(this.props.routing, null, 'locationBeforeTransitions', 'pathname');
      if (newPathName !== oldPathName && newPathName !== '/') {
        this.setState({
          navBarStatus: false
        });
      }
    }
  }, {
    key: 'componentWillMount',
    value: function componentWillMount() {
      if (!this.props.isInit) {
        if (!window.__showLogin) {
          // 不需要登陆 则
          this._initData();
        } else if (window.__loginCallBack) {
          this._initData().then(function () {
            return window.__initPage && window.__initPage();
          });
        } else {
          window.__initApp = this._initData;
        }
      }
    }
  }, {
    key: '_initData',
    value: function _initData() {
      var _this2 = this;

      return (0, _longRunOpt.longRunExec)(function () {
        if (!_this2.props.query || _this2.props.query.s !== '1') {
          return _this2.props.actions.initDataFromServer();
        }
        return _this2.props.actions.initDataFromServerNoMenu();
      });
    }
  }, {
    key: '_switchNavBar',
    value: function _switchNavBar() {
      var flag = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;

      var navBarStatus = this.state.navBarStatus;
      if (flag !== null) {
        navBarStatus = !flag;
        if (!navBarStatus === this.state.navBarStatus) return;
      }
      this.setState({
        navBarStatus: !navBarStatus
      }, function () {
        setTimeout(function () {
          return window.dispatchEvent(new Event('resize', { bubbles: true, cancelable: false }));
        }, 500);
      });
    }
  }, {
    key: 'render',
    value: function render() {
      if (this.props.isInit) {
        /* eslint-disable */
        var isInfoModal = this.props.query && this.props.query.s === "1";
        if (isInfoModal) {
          return _react2.default.createElement(
            'div',
            { className: 'simple-main-content' },
            _react2.default.createElement(
              'div',
              { className: 'main-content-row' },
              _react2.default.createElement(
                'div',
                { className: 'wrapper-panel' },
                _react2.default.cloneElement(this.props.children, { query: this.props.query, goBack: this._goBack, switchNavBar: this._switchNavBar })
              )
            )
          );
        }
        return _react2.default.createElement(
          _layoutFrameWork.CoreLayout,
          this.props,
          this.props.children
        );
      }
      return null;
    }
  }]);

  return App;
}(_react2.default.Component), (_applyDecoratedDescriptor(_class.prototype, '_initData', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_initData'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, '_switchNavBar', [_coreDecorators.autobind], Object.getOwnPropertyDescriptor(_class.prototype, '_switchNavBar'), _class.prototype)), _class);

// eslint-disable-next-line arrow-body-style

var mapStateToProps = function mapStateToProps(state) {
  return {
    routing: state.routing,
    session: state.session,
    isInit: state.global.isInit,
    query: state.global.query,
    menu: state.menu,
    subMenu: state.subMenu,
    quickMenu: state.global.quickMenu,
    userName: state.userName
  };
};

// eslint-disable-next-line arrow-body-style
var mapDispatchToProps = function mapDispatchToProps() {
  return function (dispatch) {
    return {
      actions: _extends({}, (0, _redux.bindActionCreators)({ push: _reactRouterRedux.push, replace: _reactRouterRedux.replace, goBack: _reactRouterRedux.goBack, initDataFromServer: _global.initDataFromServer, initDataFromServerNoMenu: _global.initDataFromServerNoMenu, updateEnterMenuId: _global.updateEnterMenuId, updateQuickMenu: _global.updateQuickMenu }, dispatch))
    };
  };
};
exports.default = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(App);