function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
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),
    _useState10 = _slicedToArray(_useState9, 2),
    failed = _useState10[0],
    setFailed = _useState10[1];
  var _useState11 = useState(Request.CancelToken.source()),
    _useState12 = _slicedToArray(_useState11, 1),
    token = _useState12[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) {
      setFailed(true);
      setLoading(false);
      console.error('获取初始结构失败', err);
    });
  }
  return {
    nodeList: nodeList,
    tableList: tableList,
    columnList: columnList,
    loading: loading,
    failed: failed
  };
}
export default useNode;