function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
import { useEffect, useState } from 'react';
import { getData } from '../../../../components/flow';
import apis from '../../../../constants/api';
import request from '../../../../utils/ajax';
import Request from 'umi-request';
function useNode(node) {
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    tableList = _useState2[0],
    setTableList = _useState2[1];
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    nodeList = _useState4[0],
    setNodeList = _useState4[1];
  var _useState5 = useState([]),
    _useState6 = _slicedToArray(_useState5, 2),
    columnList = _useState6[0],
    setColumnList = _useState6[1];
  var _useState7 = useState(true),
    _useState8 = _slicedToArray(_useState7, 2),
    loading = _useState8[0],
    setLoading = _useState8[1];
  var _useState9 = useState(false),
    _useState0 = _slicedToArray(_useState9, 2),
    failed = _useState0[0],
    setFailed = _useState0[1];
  var _useState1 = useState(Request.CancelToken.source()),
    _useState10 = _slicedToArray(_useState1, 1),
    token = _useState10[0];
  useEffect(function () {
    var _node$model, _node$model$graph;
    /**
     * 查询来源数据结构
     */
    updateSourceData();
    (_node$model = node.model) === null || _node$model === void 0 ? void 0 : (_node$model$graph = _node$model.graph) === null || _node$model$graph === void 0 ? void 0 : _node$model$graph.on('edge:connected', onConnected);
    return function () {
      var _node$model2, _node$model2$graph;
      token.cancel();
      (_node$model2 = node.model) === null || _node$model2 === void 0 ? void 0 : (_node$model2$graph = _node$model2.graph) === null || _node$model2$graph === void 0 ? void 0 : _node$model2$graph.off('edge:connected', onConnected);
    };
  }, []);
  useEffect(function () {
    setColumnList(function () {
      var list = [];
      tableList.forEach(function (table) {
        list = list.concat(table.columns);
      });
      return list;
    });
  }, [tableList]);
  /**
   * 当当前节点的来源节点发生变化时，更新来源数据结构
   */
  function onConnected(_ref) {
    var edge = _ref.edge;
    if (edge.target.cell !== node.id) return;
    updateSourceData();
  }
  /**
   * 更新节点信息,查询来源数据结构
   */
  function updateSourceData() {
    var _node$model3, _node$model5;
    var edges = (_node$model3 = node.model) === null || _node$model3 === void 0 ? void 0 : _node$model3.getIncomingEdges(node);
    // 如果没有来源节点，则直接置为空
    if (!(edges === null || edges === void 0 ? void 0 : edges.length)) {
      setTableList([]);
      setNodeList([]);
      setLoading(false);
      return;
    }
    setNodeList(edges.map(function (edge) {
      var _node$model4, _node$model4$graph, _edge$source;
      return (_node$model4 = node.model) === null || _node$model4 === void 0 ? void 0 : (_node$model4$graph = _node$model4.graph) === null || _node$model4$graph === void 0 ? void 0 : _node$model4$graph.getCellById((_edge$source = edge.source) === null || _edge$source === void 0 ? void 0 : _edge$source.cell);
    }));
    setFailed(false);
    setLoading(true);
    // 有来源节点，则通过后端接口，获取最新的来源结构
    request(apis.queryInputColumns, {
      method: 'post',
      cancelToken: token.token,
      data: _objectSpread({
        nodeId: node.id
      }, getData((_node$model5 = node.model) === null || _node$model5 === void 0 ? void 0 : _node$model5.graph))
    }).then(function (res) {
      if (!res || !res.data) return;
      setTableList(res.data);
      setLoading(false);
    }).catch(function (err) {
      if (err.isCancel) return;
      setFailed(true);
      setLoading(false);
      console.error('获取初始结构失败', err);
    });
  }
  return {
    nodeList: nodeList,
    tableList: tableList,
    columnList: columnList,
    loading: loading,
    failed: failed
  };
}
export default useNode;