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/dropdownMenu/DropdownMenu.jsx';
import React, { PureComponent, cloneElement } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import Tooltip from '../tooltip';
// import ClickAwayListener from '../clickAwayListener';

var DropdownMenu = function (_PureComponent) {
    _inherits(DropdownMenu, _PureComponent);

    function DropdownMenu(props) {
        _classCallCheck(this, DropdownMenu);

        var _this = _possibleConstructorReturn(this, (DropdownMenu.__proto__ || Object.getPrototypeOf(DropdownMenu)).call(this, props));

        _this.onOpen = function () {
            var onOpenChange = _this.props.onOpenChange;

            if (!_this.isControlled) {
                _this.setState({
                    isOpen: true
                });
            }
            onOpenChange && onOpenChange(true);
        };

        _this.onClose = function () {
            var onOpenChange = _this.props.onOpenChange;

            if (!_this.isControlled) {
                _this.setState({
                    isOpen: false
                });
            }
            onOpenChange && onOpenChange(false);
        };

        _this.close = function () {
            _this.setState({
                isOpen: false
            });
        };

        _this.isControlled = props.isOpen != null;
        if (!_this.isControlled) {
            _this.state = {
                isOpen: props.defaultIsOpen
            };
        }
        return _this;
    }

    _createClass(DropdownMenu, [{
        key: 'componentWillReceiveProps',
        value: function componentWillReceiveProps(newProps) {
            if (!this.isControlled && newProps.defaultIsOpen !== this.props.defaultIsOpen) {
                this.setState({
                    isOpen: newProps.defaultIsOpen
                });
            }
        }
    }, {
        key: 'render',
        value: function render() {
            var _this2 = this;

            var _props = this.props,
                defaultIsOpen = _props.defaultIsOpen,
                prefixCls = _props.prefixCls,
                className = _props.className,
                placeholder = _props.placeholder,
                children = _props.children,
                defaultValue = _props.defaultValue,
                disabled = _props.disabled,
                trigger = _props.trigger,
                anchorEl = _props.anchorEl,
                _onSelect = _props.onSelect,
                onChange = _props.onChange,
                propIsOpen = _props.isOpen,
                position = _props.position,
                noClose = _props.noClose,
                other = _objectWithoutProperties(_props, ['defaultIsOpen', 'prefixCls', 'className', 'placeholder', 'children', 'defaultValue', 'disabled', 'trigger', 'anchorEl', 'onSelect', 'onChange', 'isOpen', 'position', 'noClose']);

            var menu = cloneElement(children, {
                onSelect: function onSelect() {
                    var menuSelect = _onSelect || children.props.onSelect;
                    menuSelect && menuSelect.apply(undefined, arguments);
                    !noClose && _this2.close();
                },
                onChange: onChange || children.props && children.props.onChange,
                defaultValue: defaultValue || children.props && children.props.defaultValue
            });
            var classes = classNames(prefixCls, {}, className);
            var isOpen = this.isControlled ? propIsOpen : this.state.isOpen;
            return React.createElement(
                Tooltip,
                Object.assign({
                    content: anchorEl,
                    isOpen: isOpen,
                    trigger: !disabled ? trigger : 'none',
                    prefixCls: prefixCls,
                    position: position,
                    className: classes,
                    onOpen: this.onOpen,
                    onClose: this.onClose,
                    autoAdjustPosition: true
                }, other, {
                    __source: {
                        fileName: _jsxFileName,
                        lineNumber: 116
                    },
                    __self: this
                }),
                menu
            );
        }
    }]);

    return DropdownMenu;
}(PureComponent);

DropdownMenu.displayName = 'NeatUI.DropdownMenu';
DropdownMenu.propTypes = {
    prefixCls: PropTypes.string,
    className: PropTypes.string,
    /** 默认值 */
    defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool]),
    /** 是否显示菜单 */
    defaultIsOpen: PropTypes.bool,
    /** 显示状态改变时回调 function(isOpen) */
    onOpenChange: PropTypes.func,
    /** 挂载点 */
    anchorEl: PropTypes.element,
    //onChange: PropTypes.func,
    /** 选中时菜单项时回调 function(event, value) */
    onSelect: PropTypes.func,
    /** 弹出位置 */
    position: PropTypes.string,
    /** 选中后是否自动关闭 */
    noClose: PropTypes.bool,
    /** 禁止操作 */
    disabled: PropTypes.bool,
    /** 触发方式*/
    trigger: PropTypes.oneOf(['click', 'hover', 'focus'])
};
DropdownMenu.defaultProps = {
    prefixCls: 'ne-drop-down-menu',
    defaultIsOpen: false,
    trigger: 'click',
    position: 'bottomLeft',
    noClose: false,
    disabled: false
};

export default DropdownMenu;