import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var _jsxFileName = 'src/menu/SubMenu.jsx';
import React, { Component, Children, isValidElement, cloneElement, Fragment } from 'react';
import Tooltip from '../tooltip';
import MenuTitle from './MenuTitle';
import MenuItem from './MenuItem';
import Icon from '../icon';
import Collapse from '../collapse';
import PropTypes from 'prop-types';
import classNames from 'classnames';
/**
 * SubMenu
 */

var SubMenu = function (_Component) {
    _inherits(SubMenu, _Component);

    function SubMenu() {
        var _ref;

        var _temp, _this, _ret;

        _classCallCheck(this, SubMenu);

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

        return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = SubMenu.__proto__ || Object.getPrototypeOf(SubMenu)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
            isOpen: false,
            selected: false
        }, _this.toggle = function () {
            _this.setState({
                isOpen: _this.props.disabled ? false : !_this.state.isOpen
            });
        }, _this.open = function () {
            _this.setState({
                isOpen: _this.props.disabled ? false : true
            });
        }, _this.close = function () {
            _this.setState({
                isOpen: false
            });
        }, _temp), _possibleConstructorReturn(_this, _ret);
    }

    _createClass(SubMenu, [{
        key: 'componentDidMount',
        value: function componentDidMount() {
            var _props = this.props,
                propCurrentValue = _props.currentValue,
                disabled = _props.disabled,
                children = _props.children,
                defaultIsOpen = _props.defaultIsOpen,
                propMode = _props.mode;
            var _context$nuiMenuContr = this.context.nuiMenuControl,
                nuiMenuControl = _context$nuiMenuContr === undefined ? {} : _context$nuiMenuContr;

            var currentValue = propCurrentValue == null ? nuiMenuControl.currentValue : propCurrentValue;
            var mode = propMode == null ? nuiMenuControl.mode : propMode;
            var shouldOpen = disabled ? false : defaultIsOpen || this.findValueRecrusively(children, currentValue);
            this.setState({
                isOpen: shouldOpen && mode !== 'popup',
                selected: shouldOpen
            });
        }
    }, {
        key: 'componentWillReceiveProps',
        value: function componentWillReceiveProps(newProps, newContext) {
            var defaultIsOpen = newProps.defaultIsOpen,
                children = newProps.children,
                disabled = newProps.disabled,
                compact = newProps.compact,
                propsCurrentValue = newProps.currentValue,
                propsAutoFold = newProps.autoFold,
                propMode = newProps.mode;
            var _newContext$nuiMenuCo = newContext.nuiMenuControl,
                nuiMenuControl = _newContext$nuiMenuCo === undefined ? {} : _newContext$nuiMenuCo;
            var _context$oldNuiMenuCo = this.context.oldNuiMenuControl,
                oldNuiMenuControl = _context$oldNuiMenuCo === undefined ? {} : _context$oldNuiMenuCo;

            var contextCurrentValue = nuiMenuControl.currentValue;
            var currentValue = propsCurrentValue == null ? contextCurrentValue : propsCurrentValue;
            var mode = propMode == null ? nuiMenuControl.mode : propMode;
            var autoFold = propsAutoFold == null ? nuiMenuControl.autoFold : propsAutoFold;

            if (compact !== this.props.compact) {
                this.setState({
                    isOpen: false
                });
            } else if (defaultIsOpen !== this.props.defaultIsOpen) {
                var shouldOpen = disabled ? false : defaultIsOpen;
                this.setState({
                    isOpen: shouldOpen
                });
            } else if ((propsCurrentValue !== this.props.currentValue || contextCurrentValue !== oldNuiMenuControl.currentValue) && mode === 'inline' && autoFold) {
                var selected = disabled ? false : this.findValueRecrusively(children, currentValue);
                this.setState({
                    isOpen: selected
                });
            }
            if (propsCurrentValue !== this.props.currentValue || contextCurrentValue !== oldNuiMenuControl.currentValue) {
                var _selected = disabled ? false : this.findValueRecrusively(children, currentValue);
                this.setState({
                    selected: _selected
                });
            }
        }
    }, {
        key: 'getMenuItems',
        value: function getMenuItems(children) {
            var items = [];
            Children.forEach(children, function (item) {
                if (isValidElement(item)) {
                    items.push(item);
                }
            });
            return items;
        }
    }, {
        key: 'findValueRecrusively',
        value: function findValueRecrusively(children, target) {
            var _this2 = this;

            var finded = false;
            Children.forEach(children, function (item) {
                if (isValidElement(item) && !finded) {
                    if (target.includes(item.props.value)) {
                        finded = true;
                    } else if (item.type.displayName === 'NeatUI.SubMenu' || item.type.displayName === 'NeatUI.MenuItemGroup') {
                        finded = _this2.findValueRecrusively(item.props.children, target);
                    }
                }
            });
            return finded;
        }
    }, {
        key: 'render',
        value: function render() {
            var _classNames,
                _this3 = this,
                _classNames2;

            var _state = this.state,
                isOpen = _state.isOpen,
                selected = _state.selected;

            var _props2 = this.props,
                children = _props2.children,
                prefixCls = _props2.prefixCls,
                className = _props2.className,
                title = _props2.title,
                currentValue = _props2.currentValue,
                value = _props2.value,
                disabled = _props2.disabled,
                defaultIsOpen = _props2.defaultIsOpen,
                autoFold = _props2.autoFold,
                propMode = _props2.mode,
                popIcon = _props2.popIcon,
                expandLessIcon = _props2.expandLessIcon,
                expandMoreIcon = _props2.expandMoreIcon,
                level = _props2.level,
                close = _props2.close,
                compact = _props2.compact,
                prefix = _props2.prefix,
                iconRender = _props2.iconRender,
                size = _props2.size,
                propSelectable = _props2.selectable,
                other = _objectWithoutProperties(_props2, ['children', 'prefixCls', 'className', 'title', 'currentValue', 'value', 'disabled', 'defaultIsOpen', 'autoFold', 'mode', 'popIcon', 'expandLessIcon', 'expandMoreIcon', 'level', 'close', 'compact', 'prefix', 'iconRender', 'size', 'selectable']);

            var nuiMenuControl = this.context.nuiMenuControl;

            var selectable = propSelectable;
            var mode = propMode;
            if (nuiMenuControl) {
                if (typeof selectable === 'undefined') {
                    selectable = nuiMenuControl.selectable;
                }
                if (typeof mode === 'undefined') {
                    mode = nuiMenuControl.mode;
                }
            }
            var classes = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, prefixCls + '-inline', mode === 'inline'), _defineProperty(_classNames, prefixCls + '-popup', mode === 'popup'), _defineProperty(_classNames, prefixCls + '-parallel', mode === 'parallel'), _classNames), className);
            var menu = this.getMenuItems(children).map(function (item, key) {
                return cloneElement(item, {
                    level: mode === 'inline' ? level + 1 : undefined,
                    key: item.props.value != null && item.props.value || 'subMenu-' + key,
                    close: mode === 'popup' ? _this3.close : close
                });
            });
            var titleClasses = classNames(prefixCls + '-title', (_classNames2 = {}, _defineProperty(_classNames2, prefixCls + '-disabled', disabled), _defineProperty(_classNames2, prefixCls + '-title-selected', !disabled && selected && selectable), _classNames2));
            var submenuListCls = classNames(prefixCls + '-list', _defineProperty({}, prefixCls + '-list-size-' + size, size));
            return mode === 'parallel' ? React.createElement(
                MenuItem,
                Object.assign({
                    prefix: prefix,
                    disableTooltip: true,
                    compact: compact,
                    value: value,
                    className: classes,
                    selectable: true,
                    suffix: iconRender ? iconRender(isOpen, mode) : popIcon
                }, other, {
                    __source: {
                        fileName: _jsxFileName,
                        lineNumber: 222
                    },
                    __self: this
                }),
                title
            ) : React.createElement(
                'li',
                { className: classes, __source: {
                        fileName: _jsxFileName,
                        lineNumber: 234
                    },
                    __self: this
                },
                mode === 'inline' ? React.createElement(
                    Fragment,
                    {
                        __source: {
                            fileName: _jsxFileName,
                            lineNumber: 236
                        },
                        __self: this
                    },
                    React.createElement(
                        MenuTitle,
                        {
                            prefix: prefix,
                            compact: compact,
                            level: level,
                            'aria-haspopup': true,
                            'aria-expanded': isOpen,
                            role: 'button',
                            suffix: iconRender ? iconRender(isOpen, mode) : isOpen ? expandLessIcon : expandMoreIcon,
                            className: titleClasses,
                            onClick: this.toggle, __source: {
                                fileName: _jsxFileName,
                                lineNumber: 237
                            },
                            __self: this
                        },
                        title
                    ),
                    React.createElement(
                        Collapse,
                        { 'in': isOpen, timeout: 'auto', __source: {
                                fileName: _jsxFileName,
                                lineNumber: 249
                            },
                            __self: this
                        },
                        React.createElement(
                            'ul',
                            Object.assign({ role: 'menu' }, other, { className: submenuListCls, __source: {
                                    fileName: _jsxFileName,
                                    lineNumber: 250
                                },
                                __self: this
                            }),
                            menu
                        )
                    )
                ) : React.createElement(
                    Tooltip,
                    Object.assign({}, other, {
                        isOpen: isOpen,
                        autoAdjustPosition: true,
                        content: React.createElement(
                            'div',
                            {
                                __source: {
                                    fileName: _jsxFileName,
                                    lineNumber: 261
                                },
                                __self: this
                            },
                            React.createElement(
                                MenuTitle,
                                {
                                    prefix: prefix,
                                    disableTooltip: true,
                                    compact: compact,
                                    'aria-haspopup': true,
                                    'aria-expanded': isOpen,
                                    suffix: iconRender ? iconRender(isOpen, mode) : popIcon,
                                    autoIndent: false,
                                    className: titleClasses,
                                    role: 'button', __source: {
                                        fileName: _jsxFileName,
                                        lineNumber: 262
                                    },
                                    __self: this
                                },
                                title
                            )
                        )
                        // trigger="click"
                        , prefixCls: prefixCls + '-popup-container',
                        position: 'rightTop',
                        onOpen: this.open,
                        onClose: this.close, __source: {
                            fileName: _jsxFileName,
                            lineNumber: 256
                        },
                        __self: this
                    }),
                    React.createElement(
                        'ul',
                        { role: 'menu', className: submenuListCls, __source: {
                                fileName: _jsxFileName,
                                lineNumber: 281
                            },
                            __self: this
                        },
                        menu
                    )
                )
            );
        }
    }]);

    return SubMenu;
}(Component);

SubMenu.propTypes = {
    prefixCls: PropTypes.string,
    className: PropTypes.string,
    /** 子菜单标题 */
    title: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
    /**显示模式 */
    mode: PropTypes.oneOf(['inline', 'popup', 'parallel']),
    /**是否自动折叠 */
    autoFold: PropTypes.bool,
    /** pop模式状态icon */
    popIcon: PropTypes.node,
    /** 展开时状态icon（pop模式下无效）*/
    expandMoreIcon: PropTypes.node,
    /** 收缩时状态iocn（pop模式下无效）*/
    expandLessIcon: PropTypes.node,
    /** 组件所处层数（用于确定缩进）*/
    level: PropTypes.number,
    /** 菜单当前值 */
    currentValue: PropTypes.array,
    /** 是否紧凑显示 */
    compact: PropTypes.bool,
    /** 前缀 */
    prefix: PropTypes.node,
    /** 状态icon渲染方法 function(isOpen, mode) => ReactNode */
    iconRender: PropTypes.func,
    /** 子菜单尺寸，继承自最上层 */
    size: PropTypes.string
};
SubMenu.defaultProps = {
    title: '',
    prefixCls: 'ne-submenu',
    defaultIsOpen: false,
    expandMoreIcon: React.createElement(Icon, { type: 'down-arrow', __source: {
            fileName: _jsxFileName,
            lineNumber: 46
        },
        __self: this
    }),
    expandLessIcon: React.createElement(Icon, { type: 'up-arrow', __source: {
            fileName: _jsxFileName,
            lineNumber: 47
        },
        __self: this
    }),
    popIcon: React.createElement(Icon, { type: 'right-arrow', __source: {
            fileName: _jsxFileName,
            lineNumber: 48
        },
        __self: this
    }),
    level: 1,
    compact: false
};
SubMenu.contextTypes = {
    nuiMenuControl: PropTypes.object
};
SubMenu.displayName = 'NeatUI.SubMenu';


export default SubMenu;