'use strict';

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

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

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

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

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

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

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

var _inherits3 = _interopRequireDefault(_inherits2);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _rcMenu = require('rc-menu');

var _rcMenu2 = _interopRequireDefault(_rcMenu);

var _openAnimation = require('../_util/openAnimation');

var _openAnimation2 = _interopRequireDefault(_openAnimation);

var _warning = require('../_util/warning');

var _warning2 = _interopRequireDefault(_warning);

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

var Menu = function (_React$Component) {
    (0, _inherits3["default"])(Menu, _React$Component);

    function Menu(props) {
        (0, _classCallCheck3["default"])(this, Menu);

        var _this = (0, _possibleConstructorReturn3["default"])(this, _React$Component.call(this, props));

        _this.handleClick = function (e) {
            _this.setOpenKeys([]);
            var onClick = _this.props.onClick;

            if (onClick) {
                onClick(e);
            }
        };
        _this.handleOpenChange = function (openKeys) {
            _this.setOpenKeys(openKeys);
            var onOpenChange = _this.props.onOpenChange;

            if (onOpenChange) {
                onOpenChange(openKeys);
            }
        };
        (0, _warning2["default"])(!('onOpen' in props || 'onClose' in props), '`onOpen` and `onClose` are removed, please use `onOpenChange` instead, ' + 'see: http://u.ant.design/menu-on-open-change.');
        var openKeys = void 0;
        if ('defaultOpenKeys' in props) {
            openKeys = props.defaultOpenKeys;
        } else if ('openKeys' in props) {
            openKeys = props.openKeys;
        }
        _this.state = {
            openKeys: openKeys || []
        };
        return _this;
    }

    Menu.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
        if (this.props.mode === 'inline' && nextProps.mode !== 'inline') {
            this.switchModeFromInline = true;
        }
        if ('openKeys' in nextProps) {
            this.setState({ openKeys: nextProps.openKeys });
        }
    };

    Menu.prototype.setOpenKeys = function setOpenKeys(openKeys) {
        if (!('openKeys' in this.props)) {
            this.setState({ openKeys: openKeys });
        }
    };

    Menu.prototype.render = function render() {
        var openAnimation = this.props.openAnimation || this.props.openTransitionName;
        if (this.props.openAnimation === undefined && this.props.openTransitionName === undefined) {
            switch (this.props.mode) {
                case 'horizontal':
                    openAnimation = 'slide-up';
                    break;
                case 'vertical':
                    // When mode switch from inline
                    // submenu should hide without animation
                    if (this.switchModeFromInline) {
                        openAnimation = '';
                        this.switchModeFromInline = false;
                    } else {
                        openAnimation = 'zoom-big';
                    }
                    break;
                case 'inline':
                    openAnimation = _openAnimation2["default"];
                    break;
                default:
            }
        }
        var props = {};
        var className = this.props.className + ' ' + this.props.prefixCls + '-' + this.props.theme;
        if (this.props.mode !== 'inline') {
            // There is this.state.openKeys for
            // closing vertical popup submenu after click it
            props = {
                openKeys: this.state.openKeys,
                onClick: this.handleClick,
                onOpenChange: this.handleOpenChange,
                openTransitionName: openAnimation,
                className: className
            };
        } else {
            props = {
                openAnimation: openAnimation,
                className: className
            };
        }
        return _react2["default"].createElement(_rcMenu2["default"], (0, _extends3["default"])({}, this.props, props));
    };

    return Menu;
}(_react2["default"].Component);

exports["default"] = Menu;

Menu.Divider = _rcMenu.Divider;
Menu.Item = _rcMenu.Item;
Menu.SubMenu = _rcMenu.SubMenu;
Menu.ItemGroup = _rcMenu.ItemGroup;
Menu.defaultProps = {
    prefixCls: 'ant-menu',
    className: '',
    theme: 'light'
};
module.exports = exports['default'];