'use strict';

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

var _dropdown = require('choerodon-ui/lib/dropdown');

var _dropdown2 = _interopRequireDefault(_dropdown);

var _icon = require('choerodon-ui/lib/icon');

var _icon2 = _interopRequireDefault(_icon);

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-ui/lib/dropdown/style');

require('choerodon-ui/lib/icon/style');

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

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

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

require('./style');

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

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

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

  (0, _createClass3['default'])(Nav, [{
    key: 'handleLink',
    value: function handleLink(tab) {
      var _props = this.props,
          MenuStore = _props.MenuStore,
          isTabMode = _props.AppState.isTabMode;
      var selectedKeys = MenuStore.selectedKeys;
      // const isReact = tab.symbol === 'REACT';

      var type = tab.symbol;

      if (selectedKeys.length && selectedKeys[0] === tab.functionCode && isTabMode) return;

      var LINK_MAP = {
        REACT: '/' + tab.url,
        HTML: '/iframe/' + tab.functionCode
      };
      var link = LINK_MAP[type] || '/';
      // const link = isReact
      //   ? `/${tab.url}`
      //   : `/iframe/${tab.functionCode}`;
      this.props.history.push(link);
      if (link === '/') {
        MenuStore.setActiveMenu({});
      }
    }
  }, {
    key: 'handleCloseTab',
    value: function handleCloseTab(tab, event) {
      var MenuStore = this.props.MenuStore;
      var selectedKeys = MenuStore.selectedKeys;

      if (event) event.stopPropagation();
      if (selectedKeys.length && selectedKeys[0] === tab.functionCode) {
        var desTab = MenuStore.getNextTab(tab);
        var desUrl = void 0;
        if (desTab.functionCode !== 'HOME_PAGE') {
          var LINK_MAP = {
            REACT: '/' + desTab.url,
            HTML: '/iframe/' + desTab.functionCode
          };
          // if (desTab.symbol === '1') {
          //   desUrl = `/${desTab.url}`;
          // } else {
          //   desUrl = `/iframe/${desTab.functionCode}`;
          // }
          desUrl = LINK_MAP[desTab.symbol];
        } else {
          desUrl = '/';
          MenuStore.setActiveMenu({});
        }
        this.props.history.push(desUrl);
      }
      MenuStore.closeTabAndClearCacheByCacheKey(tab);
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props2 = this.props,
          pathname = _props2.location.pathname,
          MenuStore = _props2.MenuStore,
          isTabMode = _props2.AppState.isTabMode;
      var tabs = MenuStore.tabs,
          activeMenu = MenuStore.activeMenu,
          collapsed = MenuStore.collapsed;


      var isHome = pathname === '/';
      var activeIndex = tabs.findIndex(function (tab) {
        return pathname === '/' + tab.url || pathname === '/iframe/' + tab.functionCode;
      });

      return isTabMode ? null : _react2['default'].createElement(
        _dropdown2['default'],
        {
          trigger: ['click'],
          overlay: _react2['default'].createElement(
            'ul',
            { className: 'nav-wrapper' },
            tabs.filter(function (v) {
              return !!v;
            }).map(function (tab, i) {
              return _react2['default'].createElement(
                'li',
                {
                  key: tab.functionCode,
                  className: (0, _classnames2['default'])({
                    tab: true,
                    'tab-active': pathname === '/' + tab.url || pathname === '/iframe/' + tab.functionCode,
                    'tab-hover': pathname !== '/' + tab.url && pathname !== '/iframe/' + tab.functionCode,
                    'tab-active-before': activeIndex >= 1 && i === activeIndex - 1,
                    'tab-active-after': activeIndex >= 0 && i === activeIndex + 1
                  }),
                  onClick: _this2.handleLink.bind(_this2, tab)
                },
                _react2['default'].createElement(
                  'div',
                  { className: 'li-wrapper', style: { positon: 'relative' } },
                  _react2['default'].createElement(
                    'div',
                    {
                      style: { overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }
                    },
                    tab.text
                  ),
                  tab.functionCode === 'HOME_PAGE' ? null : _react2['default'].createElement(_icon2['default'], {
                    type: 'close',
                    style: { fontSize: 14, marginLeft: 20 },
                    onClick: _this2.handleCloseTab.bind(_this2, tab)
                  })
                )
              );
            })
          )
        },
        _react2['default'].createElement(
          'span',
          { className: 'header-nav-action' },
          _react2['default'].createElement(
            'span',
            null,
            '\u9875\u9762\u5BFC\u822A'
          ),
          _react2['default'].createElement(_icon2['default'], { type: 'baseline-arrow_drop_down' })
        )
      );
    }
  }]);
  return Nav;
}(_react.Component)) || _class) || _class) || _class);
exports['default'] = Nav;