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 { Addon } from '@antv/x6';
import { BizGlobalDataContext, IconFont } from '@zgfe/business-lib';
import { Button, Input, Popconfirm, Result, Spin, Tooltip } from 'antd';
import React, { useContext, useEffect, useState } from 'react';
import { classPrefix } from '.';
import apis from '../../../constants/api';
import CdpFlowTypes from '../../../types/flow';
import request from '../../../utils/ajax';
import actionList from './actions';
import _ from 'lodash';
import VisualStore from './store';
var TableList = function TableList(props) {
  var graph = props.graph;
  var _useState = useState(),
    _useState2 = _slicedToArray(_useState, 2),
    dnd = _useState2[0],
    setDnd = _useState2[1];
  var _useContext = useContext(BizGlobalDataContext),
    currentApp = _useContext.currentApp;
  var _useState3 = useState(''),
    _useState4 = _slicedToArray(_useState3, 2),
    search = _useState4[0],
    setSearch = _useState4[1];
  var _useState5 = useState(true),
    _useState6 = _slicedToArray(_useState5, 2),
    loading = _useState6[0],
    setLoading = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    failed = _useState8[0],
    setFailed = _useState8[1];
  var _useState9 = useState(!Boolean(localStorage.getItem('flowGuide'))),
    _useState10 = _slicedToArray(_useState9, 2),
    showGuide = _useState10[0],
    setShowGuide = _useState10[1];
  var _useState11 = useState([]),
    _useState12 = _slicedToArray(_useState11, 2),
    datasetList = _useState12[0],
    setDatasetList = _useState12[1];
  var _useState13 = useState([]),
    _useState14 = _slicedToArray(_useState13, 2),
    addedList = _useState14[0],
    setAddedList = _useState14[1];
  var _useState15 = useState(false),
    _useState16 = _slicedToArray(_useState15, 2),
    outputExist = _useState16[0],
    setOutputExist = _useState16[1];
  useEffect(function () {
    queryTableList();
    setDnd(new Addon.Dnd({
      target: graph,
      scaled: false,
      animation: true
    }));
    var tableArray = [];
    var outputExist_ = false;
    props.nodeList.forEach(function (node) {
      if (node.shape === CdpFlowTypes.Shape.table) tableArray.push(node.data.tableName);
      if (node.shape === CdpFlowTypes.Shape.output) outputExist_ = true;
    });
    setAddedList(tableArray);
    setOutputExist(outputExist_);
    graph.on('node:added', function (_ref) {
      var node = _ref.node;
      if (node.getData().shape === CdpFlowTypes.Shape.output) setOutputExist(true);
      if (node.shape !== CdpFlowTypes.Shape.table) return;
      setAddedList(function (list) {
        var res = _.cloneDeep(list);
        res.push(node.data.tableName);
        return res;
      });
    });
    graph.on('node:removed', function (_ref2) {
      var node = _ref2.node;
      if (node.getData().shape === CdpFlowTypes.Shape.output) setOutputExist(false);
      setAddedList(function (list) {
        var res = _.cloneDeep(list);
        return res.filter(function (name) {
          return name !== node.data.tableName;
        });
      });
    });
    return function () {
      graph.off();
    };
  }, []);
  useEffect(function () {
    queryTableList();
  }, [search]);
  function _onMouseDown(e, data) {
    var node = graph.createNode({
      shape: data.type,
      data: {
        label: data.name,
        shape: data.type,
        icon: data.icon
      }
    });
    dnd === null || dnd === void 0 ? void 0 : dnd.start(node, e.nativeEvent);
  }
  function onMouseDownTable(e, data) {
    var nodeData = {
      datasetId: data.id,
      shape: CdpFlowTypes.Shape.table,
      icon: 'shujujiyewu',
      label: "".concat(data.name),
      tableName: data.tableName
    };
    var node = graph.createNode({
      shape: 'table',
      data: nodeData
    });
    dnd === null || dnd === void 0 ? void 0 : dnd.start(node, e.nativeEvent);
  }
  function onHideTip() {
    setShowGuide(false);
    localStorage.setItem('flowGuide', 'true');
  }
  function queryTableList() {
    setLoading(true);
    setFailed(false);
    request(apis.querySimpleDataset, {
      method: 'post',
      data: {
        appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId,
        name: search
      }
    }).then(function (res) {
      if (!res) return;
      VisualStore.datasetList = res.data;
      setDatasetList(res.data);
    }).catch(function (err) {
      setFailed(true);
      console.error('获取数据集列表失败', err);
    }).finally(function () {
      setLoading(false);
    });
  }
  if (failed) {
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(classPrefix, "-dataset")
    }, /*#__PURE__*/React.createElement(Result, {
      className: "".concat(classPrefix, "-error"),
      status: "error",
      subTitle: "\u6570\u636E\u83B7\u53D6\u5931\u8D25",
      extra: /*#__PURE__*/React.createElement(Button, {
        onClick: queryTableList,
        type: "primary"
      }, "\u91CD\u65B0\u52A0\u8F7D")
    }));
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-dataset")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-top")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-dataset-head")
  }, /*#__PURE__*/React.createElement("h3", null, "\u6570\u636E\u96C6(\u540D\u79F0/\u8868\u540D)"), /*#__PURE__*/React.createElement(Input, {
    placeholder: "\u641C\u7D22\u6570\u636E\u96C6",
    value: search,
    onChange: function onChange(e) {
      setSearch(e.target.value);
    },
    prefix: /*#__PURE__*/React.createElement(IconFont, {
      type: "sousuo"
    }),
    className: "".concat(classPrefix, "-search")
  })), loading ? /*#__PURE__*/React.createElement(Spin, {
    className: "".concat(classPrefix, "-loading")
  }) : /*#__PURE__*/React.createElement(Popconfirm, {
    title: "\u60A8\u53EF\u4EE5\u5C1D\u8BD5\u62D6\u62FD\u6570\u636E\u96C6\u5230\u753B\u5E03\u4E2D",
    okText: "\u4E0D\u518D\u63D0\u9192",
    showCancel: false,
    placement: "rightTop",
    onConfirm: onHideTip,
    open: showGuide
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-dataset-list")
  }, datasetList.map(function (dataset) {
    return /*#__PURE__*/React.createElement(Tooltip, {
      title: "".concat(dataset.name, "/").concat(dataset.tableName),
      key: dataset.id,
      placement: "right"
    }, /*#__PURE__*/React.createElement("div", {
      className: "".concat(classPrefix, "-dataset-item ").concat(addedList.includes(dataset.tableName) || addedList.length >= 3 ? 'disabled' : ''),
      onMouseDown: function onMouseDown(e) {
        if (addedList.includes(dataset.tableName) || addedList.length >= 3) return;
        onMouseDownTable(e, dataset);
      }
    }, /*#__PURE__*/React.createElement(IconFont, {
      type: "shujujiyewu"
    }), /*#__PURE__*/React.createElement("span", null, dataset.name, "/", dataset.tableName)));
  })))), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-bottom")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-dataset-head")
  }, /*#__PURE__*/React.createElement("h3", null, "\u52A8\u4F5C")), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-dataset-list")
  }, actionList.map(function (action) {
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(classPrefix, "-dataset-item ").concat(outputExist && action.type === CdpFlowTypes.Shape.output ? 'disabled' : ''),
      onMouseDown: function onMouseDown(e) {
        if (outputExist && action.type === CdpFlowTypes.Shape.output) return;
        _onMouseDown(e, action);
      },
      key: action.type
    }, /*#__PURE__*/React.createElement(IconFont, {
      type: action.icon
    }), /*#__PURE__*/React.createElement("span", null, action.name));
  }))));
};
export default TableList;