"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; 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); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _tslib = require("tslib");

var _consoleComponents = require("@alicloud/console-components");

var _react = _interopRequireWildcard(require("react"));

var _balloon = _interopRequireDefault(require("../balloon"));

var _classnames = _interopRequireDefault(require("classnames"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

;

var IotTreeNode =
/** @class */
function (_super) {
  (0, _tslib.__extends)(IotTreeNode, _super);

  function IotTreeNode(props) {
    var _this = _super.call(this, props) || this;

    _this.onNodeClick = function (event) {
      if (_this.props.disabled) {
        return;
      }

      if (_this.props.checkable) {
        // 如果有checkbox，把label上的触发实际直接转移到对应的checkbox上
        event.currentTarget.parentNode.parentNode.previousSibling.click();

        if (_this.props.node.children && _this.props.node.children.length > 0) {
          // 如果有子节点，需要展开
          event.currentTarget.parentNode.parentNode.previousSibling.previousSibling.click();
        }
      }

      if (_this.props.node.children && _this.props.node.children.length > 0) {
        // 如果有子节点，需要展开
        event.currentTarget.parentNode.parentNode.previousSibling.click();
      }
    };

    _this.onVisibleChange = function (visible) {
      _this.setState({
        menuVisible: visible
      });
    };

    _this.onMenuClick = function (key) {
      _this.setState({
        visible: false
      });

      _this.props.onClickMenuItem && _this.props.onClickMenuItem(key, _this.props.node);
    };

    _this.state = {
      visible: false,
      menuVisible: false
    };
    return _this;
  }

  IotTreeNode.prototype.renderMenu = function (node) {
    if (!this.props.menu || this.props.disabled) {
      return null;
    }

    var _a = this.props,
        menu = _a.menu,
        level = _a.level;
    var menuData = menu && menu(level, node);
    var menuItems = menuData && menuData.length > 0 ? menuData.map(function (item) {
      var menuName = item.name;
      delete item.name;
      delete item.labelCell;
      return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Menu.Item, (0, _tslib.__assign)({
        key: menuName
      }, item), menuName);
    }) : null;

    if (!menuItems) {
      return null;
    }

    return /*#__PURE__*/_react["default"].createElement(_consoleComponents.Dropdown, {
      className: "tree-drop-menu",
      followTrigger: true,
      trigger: /*#__PURE__*/_react["default"].createElement(_consoleComponents.Icon, {
        size: "small",
        type: "set",
        className: "lp-tree-icon"
      }),
      onVisibleChange: this.onVisibleChange
    }, /*#__PURE__*/_react["default"].createElement(_consoleComponents.Menu, {
      onItemClick: this.onMenuClick
    }, menuItems));
  };

  IotTreeNode.prototype.render = function () {
    var _a = this.props,
        node = _a.node,
        disabled = _a.disabled;
    var itemClass = (0, _classnames["default"])('lp-tree-item', {
      'lp-disabled': disabled
    });
    var content = node.labelCell || node.label;
    return /*#__PURE__*/_react["default"].isValidElement(content) ? content : /*#__PURE__*/_react["default"].createElement("div", {
      className: itemClass,
      tabIndex: 0,
      onClick: this.onNodeClick
    }, /*#__PURE__*/_react["default"].createElement(_balloon["default"], {
      maxLine: 1,
      overShowBalloon: true,
      trigger: node.label
    }, node.label), this.renderMenu(node));
  };

  return IotTreeNode;
}(_react.Component);

var _default = IotTreeNode;
exports["default"] = _default;
IotTreeNode.defaultProps = {
  node: {},
  disabled: false,
  checkable: false
};