'use strict';

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

var _tooltip = require('choerodon-hap-ui/lib/tooltip');

var _tooltip2 = _interopRequireDefault(_tooltip);

var _icon = require('choerodon-hap-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 _menu = require('choerodon-hap-ui/lib/menu');

var _menu2 = _interopRequireDefault(_menu);

var _dec, _class;

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

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

require('choerodon-hap-ui/lib/menu/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 SubMenu = _menu2['default'].SubMenu;
var MenuBar = (_dec = (0, _mobxReact.inject)('MenuStore'), (0, _reactRouterDom.withRouter)(_class = _dec(_class = (0, _mobxReact.observer)(_class = function (_Component) {
  (0, _inherits3['default'])(MenuBar, _Component);

  function MenuBar() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3['default'])(this, MenuBar);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3['default'])(this, (_ref = MenuBar.__proto__ || Object.getPrototypeOf(MenuBar)).call.apply(_ref, [this].concat(args))), _this), _this.savedOpenKeys = [], _this.keys = [], _this.onOpenChange = function (openKeys) {
      var MenuStore = _this.props.MenuStore;
      /* const latestOpenKey = openKeys.find(key => MenuStore.openKeys.indexOf(key) === -1);
      if (this.keys.indexOf(latestOpenKey) === -1) {
        MenuStore.setOpenKeys(openKeys);
      } else {
        MenuStore.setOpenKeys(latestOpenKey ? [latestOpenKey] : []);
      } */

      MenuStore.setOpenKeys(openKeys);
    }, _this.onCollapsed = function () {
      var MenuStore = _this.props.MenuStore;
      var collapsed = MenuStore.collapsed,
          openKeys = MenuStore.openKeys;

      if (collapsed) {
        MenuStore.setCollapsed(false);
        MenuStore.setOpenKeys(_this.savedOpenKeys);
      } else {
        _this.savedOpenKeys = openKeys;
        MenuStore.setCollapsed(true);
        MenuStore.setOpenKeys([]);
      }
    }, _this.onClickHome = function () {
      var _this$props = _this.props,
          history = _this$props.history,
          MenuStore = _this$props.MenuStore;

      MenuStore.setActiveMenu({});
      history.push('/');
    }, _temp), (0, _possibleConstructorReturn3['default'])(_this, _ret);
  }

  (0, _createClass3['default'])(MenuBar, [{
    key: 'componentWillMount',
    value: function componentWillMount() {
      var _props = this.props,
          MenuStore = _props.MenuStore,
          history = _props.history;

      this.loadMenu(this.props);
      MenuStore.setHistory(history);
    }
  }, {
    key: 'componentWillReceiveProps',
    value: function componentWillReceiveProps(nextProps) {
      this.loadMenu(nextProps);
    }
  }, {
    key: 'loadMenu',
    value: function loadMenu(props) {
      var _this2 = this;

      var pathname = props.location.pathname,
          MenuStore = props.MenuStore;
      var menus = MenuStore.menus,
          collapsed = MenuStore.collapsed;
      // eslint-disable-next-line no-nested-ternary

      var type = pathname.startsWith('/iframe/') ? 'HTML' : pathname.startsWith('/hap-modeling/metadata/') ? 'PAGE' : 'REACT';
      var key = {
        HTML: pathname.slice(8),
        PAGE: pathname.slice(19),
        REACT: pathname.slice(1)
      }[type];

      MenuStore.loadMenus().then(function (me) {
        // MenuStore.getTreeNodeMenus(me, (treeNodes) => {
        //   MenuStore.setTreeNodeMenus(treeNodes);
        // });
        if (pathname === '/') {
          MenuStore.setSelectedKeys([]);
          return;
        }
        MenuStore.getPathById(key, me, type, function (temppath, targetNode) {
          MenuStore.setOpenKeys(collapsed ? [] : temppath);
          _this2.savedOpenKeys = temppath;
          MenuStore.setSelectedKeys([targetNode.functionCode]);
          MenuStore.setActiveMenu(targetNode);
        }, function () {
          var construct = {
            children: null,
            expand: false,
            functionCode: key,
            icon: '',
            id: -1,
            ischecked: null,
            score: -1,
            shortcutId: null,
            text: {
              SYS_PREFERENCE: '用户信息',
              'hap-core/sys/preferences': '首选项'
            }[key] || key,
            url: {
              SYS_PREFERENCE: 'sys/um/sys_user_info.html',
              'hap-core/sys/preferences': 'hap-core/sys/preferences'
            }[key] || key,
            symbol: type
          };
          MenuStore.setSelectedKeys([construct.functionCode]);
          MenuStore.setActiveMenu(construct);
        });
      });
    }
  }, {
    key: 'renderMenu',
    value: function renderMenu(menu, num) {
      var _this3 = this;

      if (menu.children) {
        var functionCode = menu.functionCode,
            text = menu.text,
            _menu$icon = menu.icon,
            icon = _menu$icon === undefined ? 'face' : _menu$icon;
        // this.keys.push(functionCode);

        return _react2['default'].createElement(
          SubMenu,
          {
            key: functionCode,
            title: [_react2['default'].createElement(_icon2['default'], { type: icon, key: functionCode + '-icon' }), _react2['default'].createElement(
              'span',
              { key: functionCode + '-text' },
              text
            )]
          },
          menu.children.map(function (submenu) {
            return _this3.renderMenu(submenu, parseInt(num, 10) + 1);
          })
        );
      }
      return this.renderMenuItem(menu, num);
    }
  }, {
    key: 'renderMenuItem',
    value: function renderMenuItem(menuItem, num) {
      var functionCode = menuItem.functionCode,
          text = menuItem.text,
          symbol = menuItem.symbol,
          url = menuItem.url,
          _menuItem$icon = menuItem.icon,
          icon = _menuItem$icon === undefined ? 'tag_faces' : _menuItem$icon;

      var LINK_MAP = {
        REACT: '/' + url,
        HTML: '/iframe/' + functionCode,
        PAGE: '/hap-modeling/metadata/' + url
      };
      var link = LINK_MAP[symbol] || '/';
      return _react2['default'].createElement(
        _menu2['default'].Item,
        { key: functionCode },
        _react2['default'].createElement(
          _reactRouterDom.Link,
          {
            to: link
            // style={{ marginLeft: parseInt(num, 10) * 20 }}
            // style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}
          },
          _react2['default'].createElement(_icon2['default'], { type: icon }),
          _react2['default'].createElement(
            'span',
            null,
            text
          )
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      var _this4 = this;

      var MenuStore = this.props.MenuStore;
      var collapsed = MenuStore.collapsed,
          openKeys = MenuStore.openKeys,
          selectedKeys = MenuStore.selectedKeys,
          menus = MenuStore.menus;

      return _react2['default'].createElement(
        'div',
        { className: (0, _classnames2['default'])('menu-content', { 'menu-collapsed': collapsed }) },
        _react2['default'].createElement(
          _tooltip2['default'],
          { placement: 'right', title: collapsed ? '首页' : undefined },
          _react2['default'].createElement(
            'div',
            { className: 'menu-head', role: 'none', onClick: this.onClickHome },
            _react2['default'].createElement(
              'span',
              null,
              _react2['default'].createElement(_icon2['default'], { type: 'home' }),
              _react2['default'].createElement(
                'span',
                { className: (0, _classnames2['default'])({ 'menu-head-collapsed': collapsed }) },
                '\u9996\u9875'
              )
            )
          )
        ),
        _react2['default'].createElement(
          'div',
          { className: 'menu-body' },
          _react2['default'].createElement(
            _menu2['default'],
            {
              theme: 'light',
              mode: 'inline',
              inlineCollapsed: collapsed,
              openKeys: openKeys.slice(),
              selectedKeys: selectedKeys.slice(),
              onOpenChange: this.onOpenChange
            },
            menus.map(function (menu) {
              return _this4.renderMenu(menu, 0);
            })
          )
        ),
        _react2['default'].createElement(
          'div',
          {
            className: 'menu-footer',
            onClick: this.onCollapsed
          },
          _react2['default'].createElement(_icon2['default'], { type: !collapsed ? 'first_page' : 'last_page' })
        )
      );
    }
  }]);
  return MenuBar;
}(_react.Component)) || _class) || _class) || _class);
exports['default'] = MenuBar;