function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import React, { Component } from 'react';
import { findDOMNode } from 'react-dom';
import PropTypes from 'prop-types';
import cx from 'classnames';
import Icon from '../../icon';
import Overlay from '../../overlay';
import { func, obj, dom } from '../../util';
import Item from './item';
import SelectableItem from './selectable-item';
var bindCtx = func.bindCtx;
var setStyle = dom.setStyle;
var Popup = Overlay.Popup;
/**
 * Menu.PopupItem
 * @order 2
 */

var PopupItem =
/*#__PURE__*/
function (_Component) {
  _inherits(PopupItem, _Component);

  function PopupItem(props) {
    var _this;

    _classCallCheck(this, PopupItem);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(PopupItem).call(this, props));
    bindCtx(_assertThisInitialized(_this), ['handleOpen', 'handlePopupOpen', 'handlePopupClose', 'getPopup']);
    return _this;
  }

  _createClass(PopupItem, [{
    key: "getPopup",
    value: function getPopup(ref) {
      this.popup = ref;
    }
  }, {
    key: "getOpen",
    value: function getOpen() {
      var _this$props = this.props,
          _key = _this$props._key,
          root = _this$props.root;
      var openKeys = root.state.openKeys;
      return openKeys.indexOf(_key) > -1;
    }
  }, {
    key: "getPopupProps",
    value: function getPopupProps() {
      var popupProps = this.props.root.props.popupProps;

      if (typeof popupProps === 'function') {
        popupProps = popupProps(this.props);
      }

      return popupProps;
    }
  }, {
    key: "handleOpen",
    value: function handleOpen(open, triggerType, e) {
      var _this$props2 = this.props,
          _key = _this$props2._key,
          root = _this$props2.root;
      root.handleOpen(_key, open, triggerType, e);
      var popupProps = this.popupProps;
      popupProps.onVisibleChange && popupProps.onVisibleChange(open, triggerType, e);
    }
  }, {
    key: "handlePopupOpen",
    value: function handlePopupOpen() {
      var _this$props3 = this.props,
          root = _this$props3.root,
          level = _this$props3.level,
          align = _this$props3.align,
          autoWidth = _this$props3.autoWidth;
      var _root$props = root.props,
          rootPopupAutoWidth = _root$props.popupAutoWidth,
          rootPopupAlign = _root$props.popupAlign,
          direction = _root$props.direction;
      var popupAlign = align || rootPopupAlign;
      var popupAutoWidth = 'autoWidth' in this.props ? autoWidth : rootPopupAutoWidth;
      var itemNode = findDOMNode(this);
      var menuNode = itemNode.parentNode;
      this.popupNode = this.popup.getInstance().overlay.getInstance().getContentNode();
      root.popupNodes.push(this.popupNode);

      if (popupAutoWidth) {
        var targetNode = direction === 'hoz' && level === 1 ? itemNode : menuNode;

        if (targetNode.offsetWidth > this.popupNode.offsetWidth) {
          setStyle(this.popupNode, 'width', "".concat(targetNode.offsetWidth, "px"));
        }
      }

      if (popupAlign === 'outside' && !(direction === 'hoz' && level === 1)) {
        setStyle(this.popupNode, 'height', "".concat(menuNode.offsetHeight, "px"));
        setStyle(this.popupNode, 'overflow-y', 'scroll');
      } // removeClass(this.popupNode, `${prefix}hide`);


      var popupProps = this.popupProps;
      popupProps.onOpen && popupProps.onOpen();
    }
  }, {
    key: "handlePopupClose",
    value: function handlePopupClose() {
      var root = this.props.root;
      var popupNodes = root.popupNodes;
      var index = popupNodes.indexOf(this.popupNode);
      index > -1 && popupNodes.splice(index, 1);
      var popupProps = this.popupProps;
      popupProps.onClose && popupProps.onClose();
    }
  }, {
    key: "renderItem",
    value: function renderItem(selectable, children, others) {
      var _this$props4 = this.props,
          _key = _this$props4._key,
          root = _this$props4.root,
          level = _this$props4.level,
          label = _this$props4.label,
          className = _this$props4.className;
      var prefix = root.props.prefix;
      var NewItem = selectable ? SelectableItem : Item;
      var open = this.getOpen();
      var itemProps = {
        'aria-haspopup': true,
        'aria-expanded': open,
        _key: _key,
        root: root,
        level: level,
        type: 'submenu'
      };

      if (open) {
        var _cx;

        itemProps.className = cx((_cx = {}, _defineProperty(_cx, "".concat(prefix, "opened"), true), _defineProperty(_cx, className, !!className), _cx));
      } else {
        itemProps.className = className;
      }

      return React.createElement(NewItem, _extends({}, itemProps, others), React.createElement("span", {
        className: "".concat(prefix, "menu-item-text")
      }, label), children);
    }
  }, {
    key: "renderPopup",
    value: function renderPopup(trigger, triggerType, positionProps, children) {
      var _this2 = this;

      var _this$props5 = this.props,
          root = _this$props5.root,
          level = _this$props5.level,
          selectable = _this$props5.selectable;
      var direction = root.props.direction;
      this.popupProps = this.getPopupProps();
      var open = this.getOpen();

      if (direction === 'hoz' && level === 1 && selectable) {
        positionProps.target = function () {
          return findDOMNode(_this2);
        };
      }

      return React.createElement(Popup, _extends({
        ref: this.getPopup
      }, positionProps, this.popupProps, {
        canCloseByEsc: false,
        trigger: trigger,
        triggerType: triggerType,
        visible: open,
        onVisibleChange: this.handleOpen,
        onOpen: this.handlePopupOpen,
        onClose: this.handlePopupClose
      }), children);
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var _this$props6 = this.props,
          root = _this$props6.root,
          level = _this$props6.level,
          hasSubMenu = _this$props6.hasSubMenu,
          selectableFromProps = _this$props6.selectable,
          children = _this$props6.children,
          triggerType = _this$props6.triggerType,
          align = _this$props6.align,
          noIcon = _this$props6.noIcon,
          rtl = _this$props6.rtl;
      var others = obj.pickOthers(Object.keys(PopupItem.propTypes), this.props);
      var _root$props2 = root.props,
          prefix = _root$props2.prefix,
          selectMode = _root$props2.selectMode,
          direction = _root$props2.direction,
          rootPopupAlign = _root$props2.popupAlign,
          rootTriggerType = _root$props2.triggerType;
      var popupAlign = align || rootPopupAlign;
      var newTriggerType = triggerType || (hasSubMenu ? rootTriggerType : 'hover');
      var newChildren = Array.isArray(children) ? children[0] : children; // let newChildren = Array.isArray(children) ? children[0] : children;
      // newChildren = cloneElement(newChildren, {
      //     className: cx({
      //         [`${prefix}menu-popup-content`]: true,
      //         [newChildren.props.className]: !!newChildren.props.className,
      //         [`${prefix}hide`]: popupAutoWidth || popupAlign === 'outside'
      //     })
      // });

      var selectable = selectMode && selectableFromProps;
      var triggerIsIcon = selectable && newTriggerType === 'click';
      var open = this.getOpen();
      var positionProps = {};
      var arrowProps;

      if (direction === 'hoz' && level === 1) {
        var _cx2;

        positionProps.align = 'tl bl';
        positionProps.offset = [0, 0];
        arrowProps = {
          type: 'chevron-down-s',
          className: cx((_cx2 = {}, _defineProperty(_cx2, "".concat(prefix, "menu-hoz-icon-arrow"), true), _defineProperty(_cx2, "".concat(prefix, "open"), open), _cx2))
        };
      } else {
        if (popupAlign === 'outside') {
          positionProps.target = function () {
            return findDOMNode(root);
          };

          positionProps.align = 'tl tr';
          rtl ? positionProps.offset = [-2, 0] : positionProps.offset = [2, 0];
        } else {
          if (triggerIsIcon) {
            positionProps.target = function () {
              return findDOMNode(_this3);
            };
          }

          positionProps.align = 'tl tr';
          rtl ? positionProps.offset = [0, -4] : positionProps.offset = [0, -4];
        }

        arrowProps = {
          type: 'chevron-right-s',
          className: "".concat(prefix, "menu-icon-arrow")
        };
      }

      var arrow = React.createElement(Icon, arrowProps);
      var trigger = triggerIsIcon ? arrow : this.renderItem(selectable, noIcon ? null : arrow, others);
      var popup = this.renderPopup(trigger, newTriggerType, positionProps, newChildren);
      return triggerIsIcon ? this.renderItem(selectable, popup, others) : popup;
    }
  }]);

  return PopupItem;
}(Component);

_defineProperty(PopupItem, "menuChildType", 'submenu');

_defineProperty(PopupItem, "propTypes", {
  _key: PropTypes.string,
  root: PropTypes.object,
  level: PropTypes.number,
  hasSubMenu: PropTypes.bool,
  noIcon: PropTypes.bool,
  rtl: PropTypes.bool,
  selectable: PropTypes.bool,

  /**
   * 标签内容
   */
  label: PropTypes.node,

  /**
   * 自定义弹层内容
   */
  children: PropTypes.node,
  className: PropTypes.string,
  triggerType: PropTypes.oneOf(['click', 'hover']),
  align: PropTypes.oneOf(['outside', 'follow']),
  autoWidth: PropTypes.bool
});

_defineProperty(PopupItem, "defaultProps", {
  selectable: false,
  noIcon: false
});

export { PopupItem as default };