"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"), _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")), _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")), _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")), _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")), _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")), _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")), _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")), _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")), _react = _interopRequireWildcard(require("react")), _propTypes = _interopRequireDefault(require("prop-types")), _amosAntd = require("amos-antd"), _dt2react = _interopRequireDefault(require("dt2react")), _processor = require("../../../../utils/processor"), _urlConsts = _interopRequireDefault(require("../../../../consts/urlConsts")), _rayEventpool = require("ray-eventpool"), _amosFramework = require("amos-framework"), _stageConf = require("./../../ruleStatge/modelConf/stageConf"), _amosIcon = _interopRequireDefault(require("amos-icon"));

function _createSuper(e) {
    var t = _isNativeReflectConstruct();
    return function() {
        var r, a = (0, _getPrototypeOf2.default)(e);
        if (t) {
            var n = (0, _getPrototypeOf2.default)(this).constructor;
            r = Reflect.construct(a, arguments, n);
        } else r = a.apply(this, arguments);
        return (0, _possibleConstructorReturn2.default)(this, r);
    };
}

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

var TreeNode = _amosAntd.Tree.TreeNode, loop = function e(t, r, a) {
    t.forEach((function(t, n, o) {
        return t.orgCode === r ? a(t, n, o) : t.children ? e(t.children, r, a) : void 0;
    }));
}, RuleTree = function(e) {
    (0, _inherits2.default)(r, e);
    var t = _createSuper(r);
    function r(e) {
        var a;
        return (0, _classCallCheck2.default)(this, r), a = t.call(this, e), (0, _defineProperty2.default)((0, 
        _assertThisInitialized2.default)(a), "onExpand", (function(e) {
            a.setState({
                expandedKeys: e,
                autoExpandParent: !1
            });
        })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onCheck", (function(e) {
            console.log("onCheck", e), a.setState({
                checkedKeys: e
            });
        })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onSelect", (function(e, t) {
            var r = t.node.props.orgCode ? t.node.props.orgCode : t.node.props.dataRef.orgCode, n = t.node.props.title ? t.node.props.title : t.node.props.dataRef.title, o = t.node.props.eventKey ? t.node.props.eventKey : t.node.props.dataRef.eventKey, s = {
                parentKey: r,
                title: n,
                key: t.node.props.eventKey
            };
            void 0 !== o && (0, _extends2.default)(s, {
                id: o
            }), a.setState({
                selectedKeys: e,
                currentNode: s
            }), a.props.setCurrentSubviewId(o, n, s, e, a.state.expandedKeys), _rayEventpool.PubSub.publish(_stageConf.TREE_FETCH_TOPIC.trigger, {
                id: o
            });
        })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "onArtificialSelect", (function(e, t) {
            var r = t.title, n = t.sourceId, o = t.key, s = {
                parentKey: t.parentKey,
                title: r,
                key: o
            };
            void 0 !== n && (0, _extends2.default)(s, {
                id: n
            }), a.setState({
                selectedKeys: e,
                currentNode: s
            }), (0, a.props.setCurrentSubviewId)(n, r, s, e, a.state.expandedKeys);
        })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "getOffset", (function() {
            var e = _dt2react.default.getOffset(a.treeRoot) || {};
            a.setState({
                treeLocation: e
            });
        })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "getTreeData", (function() {
            var e = a.props.orgCode, t = a.props.orgName, r = a.props.expandedKeys;
            _processor.AmosFetch.get(_urlConsts.default.fetchSubgraphInfos).then((function(e) {
                if (e.ok) return e.json();
                throw new Error("从服务端获取数据失败！");
            })).then((function(n) {
                if ("SUCCESS" === n.result) {
                    var o = n.dataList;
                    if (o.length > 0) {
                        var s = o[0].orgCode;
                        null != a.props.orgCode && (s = a.props.orgCode);
                        var i = [];
                        i.push(s), loop(o, s, (function(r) {
                            var n = r.orgCode, o = r.companyName;
                            null != a.props.orgCode && (n = e, o = t);
                            var s = {
                                title: o,
                                sourceId: n,
                                key: n
                            };
                            a.onArtificialSelect(i, s);
                        }));
                    }
                    r && r.length > 0 ? a.setState({
                        currentTreeData: o,
                        expandedKeys: a.props.expandedKeys,
                        autoExpandParent: !1
                    }) : (a.setState({
                        currentTreeData: o,
                        expandedKeys: o[0].orgCode,
                        autoExpandParent: !1
                    }), _rayEventpool.PubSub.publish(_stageConf.TREE_FETCH_TOPIC.trigger, {
                        id: o[0].orgCode
                    }));
                }
            }));
        })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "nodeRightClick", (function(e) {
            a.refs.contextMenu.showMenu(e.event, e.node.props.eventKey);
        })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "renderTreeNodes", (function(e) {
            return e.map((function(e) {
                return e.children ? _react.default.createElement(TreeNode, {
                    title: e.companyName,
                    key: e.orgCode,
                    dataRef: e
                }, a.renderTreeNodes(e.children)) : _react.default.createElement(TreeNode, (0, _extends2.default)({
                    title: e.companyName,
                    key: e.orgCode
                }, e));
            }));
        })), a.state = {
            expandedKeys: [],
            autoExpandParent: !0,
            checkedKeys: [],
            selectedKeys: [],
            treeLocation: {},
            currentTreeData: [],
            currentNode: {}
        }, a;
    }
    return (0, _createClass2.default)(r, [ {
        key: "componentDidMount",
        value: function() {
            this.getOffset(), this.getTreeData();
        }
    }, {
        key: "render",
        value: function() {
            var e = this, t = this.state, r = (t.treeLocation, t.selectedKeys, t.currentTreeData), a = t.expandedKeys, n = (t.currentNode, 
            t.defaultSelectedKeys);
            return _react.default.createElement("div", {
                className: "rule-tree",
                ref: function(t) {
                    return e.treeRoot = t;
                }
            }, _react.default.createElement("div", {
                className: "rule-tree-content"
            }, _react.default.createElement(_amosFramework.Scrollbars, null, _react.default.createElement(_amosAntd.Tree, {
                showLine: !0,
                onExpand: this.onExpand,
                expandedKeys: a,
                defaultSelectedKeys: n,
                autoExpandParent: this.state.autoExpandParent,
                onCheck: this.onCheck,
                checkedKeys: this.state.checkedKeys,
                onSelect: this.onSelect,
                selectedKeys: this.state.selectedKeys
            }, this.renderTreeNodes(r)))));
        }
    } ]), r;
}(_react.Component);

RuleTree.displayName = "RuleTree", RuleTree.propTypes = {
    setCurrentSubviewId: _propTypes.default.func,
    setCurrentName: _propTypes.default.func,
    orgCode: _propTypes.default.any,
    orgName: _propTypes.default.any,
    expandedKeys: _propTypes.default.any
};

var _default = RuleTree;

exports.default = _default;