"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"), _typeof = require("@babel/runtime/helpers/typeof");

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 = require("dt2react"), _rayEventpool = require("ray-eventpool"), _amosFramework = require("amos-framework"), _amosIcon = _interopRequireDefault(require("amos-icon")), _processor = require("../../../../utils/processor"), _urlConsts = _interopRequireDefault(require("../../../../consts/urlConsts")), _stageConf = require("./../../ruleStatge/modelConf/stageConf");

function _getRequireWildcardCache(e) {
  if ("function" != typeof WeakMap) return null;
  var t = new WeakMap, r = new WeakMap;
  return (_getRequireWildcardCache = function(e) {
    return e ? r : t;
  })(e);
}

function _interopRequireWildcard(e, t) {
  if (!t && e && e.__esModule) return e;
  if (null === e || "object" !== _typeof(e) && "function" != typeof e) return {
    default: e
  };
  var r = _getRequireWildcardCache(t);
  if (r && r.has(e)) return r.get(e);
  var a = {}, n = Object.defineProperty && Object.getOwnPropertyDescriptor;
  for (var o in e) if ("default" !== o && Object.prototype.hasOwnProperty.call(e, o)) {
    var i = n ? Object.getOwnPropertyDescriptor(e, o) : null;
    i && (i.get || i.set) ? Object.defineProperty(a, o, i) : a[o] = e[o];
  }
  return a.default = e, r && r.set(e, a), a;
}

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, i = {
        parentKey: r,
        title: n,
        key: t.node.props.eventKey
      };
      void 0 !== o && (0, _extends2.default)(i, {
        id: o
      }), a.setState({
        selectedKeys: e,
        currentNode: i
      }), a.props.setCurrentSubviewId(o, n, i, 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, i = {
        parentKey: t.parentKey,
        title: r,
        key: o
      };
      void 0 !== n && (0, _extends2.default)(i, {
        id: n
      }), a.setState({
        selectedKeys: e,
        currentNode: i
      }), (0, a.props.setCurrentSubviewId)(n, r, i, e, a.state.expandedKeys);
    })), (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(a), "getOffset", (function() {
      var e = (0, _dt2react.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 i = o[0].orgCode;
            null != a.props.orgCode && (i = a.props.orgCode);
            var s = [];
            s.push(i), loop(o, i, (function(r) {
              var n = r.orgCode, o = r.companyName;
              null != a.props.orgCode && (n = e, o = t);
              var i = {
                title: o,
                sourceId: n,
                key: n
              };
              a.onArtificialSelect(s, i);
            }));
          }
          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), "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;