"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _treeSelect = _interopRequireDefault(require("antd/lib/tree-select"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = _interopRequireWildcard(require("react"));
var _ahooks = require("ahooks");
require("./index.css");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 ApplyTreeSelect = function ApplyTreeSelect(props) {
  var _props$selNode = props.selNode,
    selNode = _props$selNode === void 0 ? function () {
      console.log('empty selNode func');
    } : _props$selNode;
  var _useSetState = (0, _ahooks.useSetState)({
      loadingData: false,
      treeData: [],
      expandedKeys: [],
      loadedKeys: [],
      biz1List: [],
      biz2List: [],
      selectedKeys: ['root'],
      openKeys: ''
    }),
    state = _useSetState[0],
    setState = _useSetState[1];

  // 深度遍历获取默认展开菜单逻辑
  var getDefaultKeys = function getDefaultKeys(data, appId) {
    if (Array.isArray(data)) {
      data.forEach(function (item) {
        getDefaultKeys(item, appId);
      });
    }
    if (data.children) {
      data.children.forEach(function (item) {
        getDefaultKeys(item, appId);
      });
    } else if (data !== null && data !== void 0 && data.appFlagId && (data === null || data === void 0 ? void 0 : data.appFlagId) === appId) {
      setState({
        openKeys: data === null || data === void 0 ? void 0 : data.businessId
      });
    }
    return;
  };
  var getTreeData = function getTreeData() {
    setState({
      loadingData: true
    });
    try {
      fetch('/devops-appcmdb-service/api/v1/business/list-tree').then( /*#__PURE__*/function () {
        var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(response) {
          var res, data;
          return _regenerator["default"].wrap(function _callee$(_context) {
            while (1) switch (_context.prev = _context.next) {
              case 0:
                setState({
                  loadingData: false
                });
                if (!(response.status === 200)) {
                  _context.next = 10;
                  break;
                }
                _context.next = 4;
                return response.json();
              case 4:
                res = _context.sent;
                data = Array.isArray(res.data) && res.data.map(function (item) {
                  return (0, _extends2["default"])({}, item, {
                    children: item.appList
                  });
                });
                getDefaultKeys(data, props.defaultAppId);
                setState({
                  treeData: [{
                    title: '应用树',
                    treeType: 'root',
                    key: 'root',
                    id: 'root',
                    children: data
                  }],
                  expandedKeys: ['root', state.openKeys || props.defaultAppId],
                  loadedKeys: ['root']
                });
                _context.next = 11;
                break;
              case 10:
                alert((response === null || response === void 0 ? void 0 : response.message) || '请求错误');
              case 11:
              case "end":
                return _context.stop();
            }
          }, _callee);
        }));
        return function (_x) {
          return _ref.apply(this, arguments);
        };
      }());
    } catch (err) {
      console.log(err);
    }
  };
  var selectNode = function selectNode(selectedKeys) {
    // props.selNode(selectedKeys);
    selNode(selectedKeys);
  };
  var onExpand = function onExpand(expandedKeys) {
    setState({
      expandedKeys: expandedKeys
    });
  };
  var renderTreeNodes = function renderTreeNodes(data) {
    return data.map(function (item) {
      if (item.children) {
        return /*#__PURE__*/_react["default"].createElement(_treeSelect["default"].TreeNode, (0, _extends2["default"])({
          title: item.name,
          key: item.appFlagId,
          isLeaf: false,
          value: item.id,
          selectable: false,
          dataRef: item
        }, item), renderTreeNodes(item.children));
      }
      return /*#__PURE__*/_react["default"].createElement(_treeSelect["default"].TreeNode, (0, _extends2["default"])({
        title: item.name,
        key: item.appFlagId,
        isLeaf: true,
        value: item.appFlagId,
        selectable: item.isLeaf,
        dataRef: item
      }, item));
    });
  };
  (0, _react.useEffect)(function () {
    getTreeData();
  }, []);
  return /*#__PURE__*/_react["default"].createElement(_treeSelect["default"], {
    style: {
      width: '100%'
    },
    treeLoadedKeys: state.loadedKeys,
    onTreeExpand: onExpand,
    onSelect: selectNode,
    treeLine: true,
    disabled: (props === null || props === void 0 ? void 0 : props.disabled) || false,
    treeDefaultExpandedKeys: state.expandedKeys,
    defaultValue: props.defaultAppId,
    allowClear: true
  }, renderTreeNodes(state.treeData));
};
var _default = ApplyTreeSelect;
exports["default"] = _default;