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/MenuItem.jsx';
import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Tooltip from '../tooltip';

/**
 *
 * MenuItem
 */

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

    function MenuItem() {
        var _ref;

        var _temp, _this, _ret;

        _classCallCheck(this, MenuItem);

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

        return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = MenuItem.__proto__ || Object.getPrototypeOf(MenuItem)).call.apply(_ref, [this].concat(args))), _this), _this.keyDown = false, _this.onKeyDown = function (event) {
            var onKeyDown = _this.props.onKeyDown;

            var key = event.key;
            if (!_this.keyDown && key === ' ') {
                _this.keyDown = true;
                event.persist();
            }

            if (onKeyDown) {
                onKeyDown(event);
            }
            if (event.target === _this.el && (key === ' ' || key === 'Enter')) {
                event.preventDefault();
                _this.onClick(event);
            }
        }, _this.onKeyUp = function (event) {
            if (event === ' ') {
                _this.keyDown = false;
                event.persist();
            }
            if (_this.props.onKeyUp) {
                _this.props.onKeyUp(event);
            }
        }, _this.onClick = function (e) {
            var _this$props = _this.props,
                disabled = _this$props.disabled,
                value = _this$props.value,
                propOnSelect = _this$props.onSelect,
                onClick = _this$props.onClick,
                close = _this$props.close;

            var onSelect = propOnSelect;
            if (_this.context.nuiMenuControl) {
                if (typeof onSelect === 'undefined') {
                    onSelect = _this.context.nuiMenuControl.onSelect;
                }
            }
            onClick && onClick(e);
            if (!disabled) {
                onSelect && onSelect(e, value);
            }
            close && close();
        }, _temp), _possibleConstructorReturn(_this, _ret);
    }

    _createClass(MenuItem, [{
        key: 'render',
        value: function render() {
            var _classNames;

            var _props = this.props,
                className = _props.className,
                index = _props.index,
                value = _props.value,
                selected = _props.selected,
                disabled = _props.disabled,
                prefixCls = _props.prefixCls,
                onSelect = _props.onSelect,
                close = _props.close,
                children = _props.children,
                prefix = _props.prefix,
                suffix = _props.suffix,
                tabIndex = _props.tabIndex,
                isLastNode = _props.isLastNode,
                level = _props.level,
                compact = _props.compact,
                searchText = _props.searchText,
                disableTooltip = _props.disableTooltip,
                Title = _props.tag,
                propIndent = _props.indent,
                propAutoIndent = _props.autoIndent,
                propCurrentValue = _props.currentValue,
                propSelectable = _props.selectable,
                other = _objectWithoutProperties(_props, ['className', 'index', 'value', 'selected', 'disabled', 'prefixCls', 'onSelect', 'close', 'children', 'prefix', 'suffix', 'tabIndex', 'isLastNode', 'level', 'compact', 'searchText', 'disableTooltip', 'tag', 'indent', 'autoIndent', 'currentValue', 'selectable']);

            var nuiMenuControl = this.context.nuiMenuControl;

            var selectable = propSelectable;
            var currentValue = propCurrentValue;
            var autoIndent = propAutoIndent;
            var indent = propIndent;
            if (nuiMenuControl) {
                if (typeof selectable === 'undefined') {
                    selectable = nuiMenuControl.selectable;
                }

                if (typeof currentValue === 'undefined') {
                    currentValue = nuiMenuControl.currentValue;
                }

                if (typeof autoIndent === 'undefined') {
                    autoIndent = nuiMenuControl.autoIndent;
                }
                if (typeof autoIndent === 'undefined') {
                    autoIndent = nuiMenuControl.autoIndent;
                }
                if (typeof indent === 'undefined') {
                    indent = nuiMenuControl.indent;
                }
            }
            var itemSelected = false;
            if (selected != null) {
                itemSelected = selected;
            } else {
                itemSelected = value != null && currentValue.indexOf(value) !== -1 && selectable;
            }
            var indentLevel = autoIndent ? level : 1;

            var isSelected = !disabled && selectable && itemSelected;
            var classes = classNames(prefixCls, (_classNames = {}, _defineProperty(_classNames, prefixCls + '-selected', isSelected), _defineProperty(_classNames, prefixCls + '-disabled', disabled), _defineProperty(_classNames, prefixCls + '-has-suffix', !compact && suffix), _classNames), className);
            var content = compact ? React.createElement(
                Fragment,
                {
                    __source: {
                        fileName: _jsxFileName,
                        lineNumber: 173
                    },
                    __self: this
                },
                !prefix ? React.createElement(
                    'span',
                    { className: classNames(prefixCls + '-prefix', prefixCls + '-prefix-compact'), __source: {
                            fileName: _jsxFileName,
                            lineNumber: 175
                        },
                        __self: this
                    },
                    this.props.children && this.props.children[0]
                ) : React.cloneElement(prefix, {
                    className: classNames(prefix.className, prefixCls + '-prefix', prefixCls + '-prefix-compact')
                })
            ) : React.createElement(
                Fragment,
                {
                    __source: {
                        fileName: _jsxFileName,
                        lineNumber: 185
                    },
                    __self: this
                },
                !prefix ? null : React.cloneElement(prefix, {
                    className: classNames(prefix.className, prefixCls + '-prefix')
                }),
                React.isValidElement(children) ? React.cloneElement(children, {
                    className: classNames(children.props.className, prefixCls + '-content')
                }) : React.createElement(
                    'span',
                    { className: prefixCls + '-content', __source: {
                            fileName: _jsxFileName,
                            lineNumber: 196
                        },
                        __self: this
                    },
                    children
                ),
                !suffix ? null : React.cloneElement(suffix, {
                    className: classNames(suffix.className, prefixCls + '-suffix')
                })
            );

            var title = React.createElement(
                Title,
                Object.assign({}, other, {
                    style: {
                        paddingLeft: indentLevel * indent + 'px'
                    },
                    tabIndex: disabled || !itemSelected ? -1 : 0
                    // aria-selected={selectable && selected}
                    , className: classes,
                    onKeyDown: this.onKeyDown,
                    onKeyUp: this.onKeyUp,
                    onClick: this.onClick, __source: {
                        fileName: _jsxFileName,
                        lineNumber: 207
                    },
                    __self: this
                }),
                content
            );
            return compact && !disableTooltip ? React.createElement(
                Tooltip,
                { content: title, autoAdjustPosition: true, position: 'right', __source: {
                        fileName: _jsxFileName,
                        lineNumber: 222
                    },
                    __self: this
                },
                this.props.children
            ) : title;
        }
    }]);

    return MenuItem;
}(Component);

MenuItem.propTypes = {
    prefixCls: PropTypes.string,
    className: PropTypes.string,
    /** MenuItem的值 */
    value: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]),
    /** 被选中时回调  */
    onSelect: PropTypes.func,
    /** @ignore 关闭父组件的回调*/
    close: PropTypes.func,
    /** 是否被选中 */
    selected: PropTypes.bool,
    /** 是否禁用 */
    disabled: PropTypes.bool,
    /** 是否自动缩进 */
    autoIndent: PropTypes.bool,
    /** 缩进距离（单位px） */
    indent: PropTypes.number,
    /** 当前嵌套层数 */
    level: PropTypes.number,
    /** 前缀 */
    suffix: PropTypes.element,
    /** 后缀 */
    prefix: PropTypes.element,
    /** 同原生role */
    role: PropTypes.string,
    /** 提示文本 */
    tag: PropTypes.string,
    /** 是否可选中 */
    selectable: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
    /** 是否紧凑显示 */
    compact: PropTypes.bool,
    /** 是否显示提示框 */
    disableTooltip: PropTypes.bool
};
MenuItem.defaultProps = {
    prefixCls: 'ne-menu-item',
    disabled: false,
    level: 1,
    role: 'menuitem',
    compact: false,
    tag: 'li',
    disableTooltip: false
};
MenuItem.contextTypes = {
    nuiMenuControl: PropTypes.object
};
MenuItem.displayName = 'NeatUI.MenuItem';
export default MenuItem;