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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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 PropTypes from 'prop-types';
import cx from 'classnames';
import Menu from '../menu';
import Icon from '../icon';
import { func, obj, KEYCODE } from '../util';
var bindCtx = func.bindCtx;
var pickOthers = obj.pickOthers;

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

  function CascaderMenuItem(props) {
    var _this;

    _classCallCheck(this, CascaderMenuItem);

    _this = _possibleConstructorReturn(this, _getPrototypeOf(CascaderMenuItem).call(this, props));
    _this.state = {
      loading: false
    };
    bindCtx(_assertThisInitialized(_this), ['handleExpand', 'handleClick', 'handleMouseEnter', 'handleKeyDown', 'removeLoading']);
    return _this;
  }

  _createClass(CascaderMenuItem, [{
    key: "addLoading",
    value: function addLoading() {
      this.setState({
        loading: true
      });
    }
  }, {
    key: "removeLoading",
    value: function removeLoading() {
      this.setState({
        loading: false
      });
    }
  }, {
    key: "setLoadingIfNeed",
    value: function setLoadingIfNeed(p) {
      if (p && typeof p.then === 'function') {
        this.addLoading();
        p.then(this.removeLoading)["catch"](this.removeLoading);
      }
    }
  }, {
    key: "handleExpand",
    value: function handleExpand(focusedFirstChild) {
      this.setLoadingIfNeed(this.props.onExpand(focusedFirstChild));
    }
  }, {
    key: "handleClick",
    value: function handleClick() {
      this.handleExpand(false);
    }
  }, {
    key: "handleMouseEnter",
    value: function handleMouseEnter() {
      this.handleExpand(false);
    }
  }, {
    key: "handleKeyDown",
    value: function handleKeyDown(e) {
      if (!this.props.disabled) {
        if (e.keyCode === KEYCODE.RIGHT || e.keyCode === KEYCODE.ENTER) {
          if (this.props.canExpand) {
            this.handleExpand(true);
          }
        } else if (e.keyCode === KEYCODE.LEFT || e.keyCode === KEYCODE.ESC) {
          this.props.onFold();
        } else if (e.keyCode === KEYCODE.SPACE) {
          this.handleExpand(false);
        }
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _cx;

      var _this$props = this.props,
          prefix = _this$props.prefix,
          className = _this$props.className,
          menu = _this$props.menu,
          disabled = _this$props.disabled,
          selected = _this$props.selected,
          onSelect = _this$props.onSelect,
          expanded = _this$props.expanded,
          canExpand = _this$props.canExpand,
          expandTriggerType = _this$props.expandTriggerType,
          checkable = _this$props.checkable,
          checked = _this$props.checked,
          indeterminate = _this$props.indeterminate,
          checkboxDisabled = _this$props.checkboxDisabled,
          onCheck = _this$props.onCheck,
          children = _this$props.children;
      var others = pickOthers(Object.keys(CascaderMenuItem.propTypes), this.props);
      var loading = this.state.loading;

      var itemProps = _objectSpread({
        className: cx((_cx = {}, _defineProperty(_cx, "".concat(prefix, "cascader-menu-item"), true), _defineProperty(_cx, "".concat(prefix, "expanded"), expanded), _defineProperty(_cx, className, !!className), _cx)),
        disabled: disabled,
        menu: menu,
        onKeyDown: this.handleKeyDown,
        role: 'option'
      }, others);

      if (!disabled) {
        if (expandTriggerType === 'hover') {
          itemProps.onMouseEnter = this.handleMouseEnter;
        } else {
          itemProps.onClick = this.handleClick;
        }
      }

      var Item;

      if (checkable) {
        Item = Menu.CheckboxItem;
        itemProps.checked = checked;
        itemProps.indeterminate = indeterminate;
        itemProps.checkboxDisabled = checkboxDisabled;
        itemProps.onChange = onCheck;
      } else {
        Item = Menu.Item;
        itemProps.selected = selected;
        itemProps.onSelect = onSelect;
      }

      return React.createElement(Item, itemProps, children, canExpand ? loading ? React.createElement(Icon, {
        className: "".concat(prefix, "cascader-menu-icon-right ").concat(prefix, "cascader-menu-icon-loading"),
        type: "loading"
      }) : React.createElement(Icon, {
        className: "".concat(prefix, "cascader-menu-icon-right ").concat(prefix, "cascader-menu-icon-expand"),
        type: "chevron-right"
      }) : null);
    }
  }]);

  return CascaderMenuItem;
}(Component);

_defineProperty(CascaderMenuItem, "menuChildType", 'item');

_defineProperty(CascaderMenuItem, "propTypes", {
  prefix: PropTypes.string,
  className: PropTypes.string,
  disabled: PropTypes.bool,
  selected: PropTypes.bool,
  onSelect: PropTypes.func,
  expanded: PropTypes.bool,
  canExpand: PropTypes.bool,
  menu: PropTypes.any,
  expandTriggerType: PropTypes.oneOf(['click', 'hover']),
  onExpand: PropTypes.func,
  onFold: PropTypes.func,
  checkable: PropTypes.bool,
  checked: PropTypes.bool,
  indeterminate: PropTypes.bool,
  checkboxDisabled: PropTypes.bool,
  onCheck: PropTypes.func,
  children: PropTypes.node
});

export { CascaderMenuItem as default };