import { __assign, __extends } from "tslib";
import { Icon, Dropdown, Menu } from '@alicloud/console-components';
import React, { Component } from 'react';
import LPBalloon from '../balloon';
import classnames from 'classnames';
;
var IotTreeNode = /** @class */ (function (_super) {
    __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 (React.createElement(Menu.Item, __assign({ key: menuName }, item), menuName));
            })
            : null;
        if (!menuItems) {
            return null;
        }
        return (React.createElement(Dropdown, { className: "tree-drop-menu", followTrigger: true, trigger: React.createElement(Icon, { size: "small", type: "set", className: "lp-tree-icon" }), onVisibleChange: this.onVisibleChange },
            React.createElement(Menu, { onItemClick: this.onMenuClick }, menuItems)));
    };
    IotTreeNode.prototype.render = function () {
        var _a = this.props, node = _a.node, disabled = _a.disabled;
        var itemClass = classnames('lp-tree-item', {
            'lp-disabled': disabled
        });
        var content = node.labelCell || node.label;
        return React.isValidElement(content) ? content : (React.createElement("div", { className: itemClass, tabIndex: 0, onClick: this.onNodeClick },
            React.createElement(LPBalloon, { maxLine: 1, overShowBalloon: true, trigger: node.label }, node.label),
            this.renderMenu(node)));
    };
    return IotTreeNode;
}(Component));
export default IotTreeNode;
IotTreeNode.defaultProps = {
    node: {},
    disabled: false,
    checkable: false
};
