import { notification } from 'antd';
import CdpFlowTypes from '../../types/flow';
import { FlowIterator } from './util';
var validateEdge = function validateEdge(_ref) {
  var _ref2;
  var edge = _ref.edge;
  var source = edge.source,
    target = edge.target;
  var sourceCell = this.getCellById(source.cell);
  var sourcePort = sourceCell === null || sourceCell === void 0 ? void 0 : sourceCell.ports.items.find(function (port) {
    return port.id === source.port;
  });
  var targetCell = this.getCellById(target.cell);
  var targetPort = targetCell.ports.items.find(function (port) {
    return port.id === target.port;
  });
  // 只能横向从左向右连接
  if (/left/.test(String(sourcePort.group)) || /right/.test(String(targetPort.group))) {
    notification.warn({
      message: '非法操作（流程流向错误）'
    });
    return false;
  }
  // target不能是当前链路上已连接的节点，否则会造成死循环
  var existCount = 0;
  FlowIterator(targetCell, 'down', function (node) {
    // 如果当前节点出现次数超过一次，则代表死循环了，则代表死循环了
    if (node.id === targetCell.id) existCount += 1;
    return existCount > 2;
  });
  if (existCount > 2) {
    notification.warn({
      message: '非法操作（存在死循环风险）'
    });
    return false;
  }
  // sourceCell只能拉取一条路径出去
  if ((this.getOutgoingEdges(sourceCell.id) || []).length > 1 && sourceCell.shape !== CdpFlowTypes.Shape.split) {
    notification.warn({
      message: '非法操作（当前节点连接已存在）'
    });
    return false;
  }
  if (sourceCell.shape === CdpFlowTypes.Shape.split && (this.getOutgoingEdges(sourceCell.id) || []).length > 2) {
    notification.warn({
      message: '非法操作（当前节点连接已存在）'
    });
    return false;
  }
  // targetCell是「连接」时，连接上的每个连接桩只能有一个连接
  if (targetCell.shape === CdpFlowTypes.Shape.join && ((_ref2 = this.getIncomingEdges(targetCell.id) || []) === null || _ref2 === void 0 ? void 0 : _ref2.length) > 2) {
    notification.warn({
      message: '非法操作（连接操作最多支持两个表）'
    });
    return false;
  }
  // 除了合并节点与连接节点可以有多个来源节点，其它都只能有一个
  if (![CdpFlowTypes.Shape.join, CdpFlowTypes.Shape.merge].includes(targetCell.shape) && (this.getIncomingEdges(targetCell) || []).length > 1) {
    notification.warn({
      message: '非法操作（目标节点已有连接）'
    });
    return false;
  }
  // 「合并」节点，只支持三个来源连接
  if (CdpFlowTypes.Shape.merge === targetCell.shape && (this.getIncomingEdges(targetCell.id) || []).length > 3) {
    notification.warn({
      message: '非法操作（最多支持三个集合合并）'
    });
    return false;
  }
  // 如果目标节点是「连接」，且对应连接桩已有连接，则拒绝
  if (targetCell.shape === CdpFlowTypes.Shape.join) {
    var _this$getIncomingEdge;
    var count = 0;
    (_this$getIncomingEdge = this.getIncomingEdges(targetCell)) === null || _this$getIncomingEdge === void 0 ? void 0 : _this$getIncomingEdge.map(function (item) {
      return item.target;
    }).forEach(function (target) {
      if (target.port === (targetPort === null || targetPort === void 0 ? void 0 : targetPort.id)) count++;
    });
    if (count > 1) {
      notification.warn({
        message: '非法操作（目标节点已有连接）'
      });
      return false;
    }
  }
  // 限制链路长度
  var flowLength = 0;
  FlowIterator(targetCell, 'up', function (node, length) {
    flowLength = Math.max(flowLength, length);
  });
  FlowIterator(targetCell, 'down', function (node, length) {
    // 因为向上数，当前节点targetCell已经进行过一次计数，因此需要减1
    flowLength = Math.max(flowLength, length - 1);
  }, flowLength);
  if (flowLength > 10) {
    notification.warn({
      message: '流程链路过长(不能超过10层节点)'
    });
    return false;
  }
  // 如果目标节点是合并，则将目标节点数据清掉
  if (targetCell.shape === CdpFlowTypes.Shape.merge) {
    targetCell.updateData({
      status: 'failed',
      groupInfoList: []
    });
  }
  return true;
};
export default validateEdge;