function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); 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 _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

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

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

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

import React from "react";
import Menu from "antd/es/menu"; // import "@whalecloud/fdx-theme-fdx/context-menu.less";

var SubMenu = Menu.SubMenu;

var ContextMenu = /*#__PURE__*/function (_React$Component) {
  _inherits(ContextMenu, _React$Component);

  var _super = _createSuper(ContextMenu);

  function ContextMenu() {
    var _this;

    _classCallCheck(this, ContextMenu);

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

    _this = _super.call.apply(_super, [this].concat(args));
    _this.root = undefined;
    _this.state = {
      visible: false
    };

    _this._handleContextMenu = function (event) {
      event.preventDefault();

      _this.setState({
        visible: true
      });

      var clickX = event.clientX;
      var clickY = event.clientY;
      _this.root.style.left = "".concat(clickX + 5, "px");
      _this.root.style.top = "".concat(clickY + 5, "px");
    };

    _this._handleClick = function () {
      var visible = _this.state.visible;
      if (visible) _this.setState({
        visible: false
      });
    };

    _this._handleScroll = function () {
      var visible = _this.state.visible;
      if (visible) _this.setState({
        visible: false
      });
    };

    _this.renderMenu = function (options) {
      // @ts-ignore
      return options.map(function (item) {
        if (Array.isArray(item.children) && item.children.length !== 0) {
          return /*#__PURE__*/React.createElement(SubMenu, {
            disabled: item.disable || false,
            title: item.label,
            key: item.key
          }, _this.renderMenu(item.children));
        }

        return /*#__PURE__*/React.createElement(Menu.Item, {
          disabled: item.disable || false,
          key: item.key
        }, item.label);
      });
    };

    return _this;
  }

  _createClass(ContextMenu, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      setTimeout(function () {
        var getContainer = _this2.props.getContainer;
        var container = getContainer() || document.body;
        container.addEventListener("contextmenu", _this2._handleContextMenu);
        container.addEventListener("scroll", _this2._handleScroll);
        document.addEventListener("click", _this2._handleClick);
      }, 0);
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var getContainer = this.props.getContainer;
      var container = getContainer() || document.body;
      container.removeEventListener("contextmenu", this._handleContextMenu);
      document.removeEventListener("click", this._handleClick);
      container.removeEventListener("scroll", this._handleScroll);
    } // @ts-ignore

  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var visible = this.state.visible;
      var _this$props = this.props,
          onChange = _this$props.onChange,
          style = _this$props.style;
      return (visible || null) && /*#__PURE__*/React.createElement("div", {
        className: "context-menu",
        ref: function ref(_ref) {
          _this3.root = _ref;
        }
      }, /*#__PURE__*/React.createElement(Menu, {
        onClick: onChange,
        style: style ? style : null,
        mode: "vertical"
      }, this.renderMenu(this.props.options)));
    }
  }]);

  return ContextMenu;
}(React.Component);

ContextMenu.Option = function (_ref2) {
  var value = _ref2.value,
      children = _ref2.children;
  return [/*#__PURE__*/React.createElement("span", null, children), value];
};

ContextMenu.Group = function (_ref3) {
  var children = _ref3.children;
  return children;
};

ContextMenu.defaultProps = {
  getContainer: function getContainer() {
    return document.body;
  },
  onChange: function onChange() {}
};
export default ContextMenu;