import _extends from "@babel/runtime/helpers/extends";
import React from 'react';
import { CodeCrumbedFlowEdge, ExternalEdge } from '../../../component/Edge/CodeCrumbEdge';
import { isCodeCrumbsEqual, getCcPosition } from './helpers';
export default (function (props) {
  var namespace = props.namespace,
      areaHeight = props.areaHeight,
      namespacesList = props.namespacesList,
      shiftToCenterPoint = props.shiftToCenterPoint,
      ccShiftIndexMap = props.ccShiftIndexMap,
      sortedFlowSteps = props.sortedFlowSteps,
      involvedNsData = props.involvedNsData,
      codeCrumbsMinimize = props.codeCrumbsMinimize,
      onFlowEdgeClick = props.onFlowEdgeClick,
      selectedCcFlowEdgeNodes = props.selectedCcFlowEdgeNodes;
  var codecrumbsLayoutMap = involvedNsData[namespace].codecrumbsLayoutMap;
  var ccNamespacesKeys = Object.keys(involvedNsData || {});
  return React.createElement(React.Fragment, null, !codeCrumbsMinimize && sortedFlowSteps.map(function (toItem, i, list) {
    if (!i) return null;
    var fromItem = list[i - 1];

    if (fromItem.namespace !== namespace && toItem.namespace !== namespace || fromItem.step === toItem.step) {
      return null;
    }

    var edgePoints = [fromItem, toItem].map(function (crumb) {
      var _ref = [crumb.y, crumb.x],
          _ = _ref[0],
          cY = _ref[1];
      var ccAlightPoint = involvedNsData[crumb.namespace].ccAlightPoint;
      return shiftToCenterPoint(getCcPosition(ccAlightPoint, ccShiftIndexMap[crumb.id]), cY);
    });
    var edgeBaseProps = {
      key: "cc-external-edge-".concat(fromItem.name, "-").concat(toItem.name, "-").concat(edgePoints[0].y),
      sourcePosition: edgePoints[0],
      targetPosition: edgePoints[1],
      onClick: function onClick() {
        return onFlowEdgeClick(fromItem, toItem, ccNamespacesKeys);
      },
      selected: isFlowEdgeSelected(selectedCcFlowEdgeNodes, fromItem, toItem)
    };
    var namespaceIndex = namespacesList.indexOf(namespace);

    if (fromItem.namespace === namespace && toItem.namespace !== namespace) {
      var _fromFile = codecrumbsLayoutMap[fromItem.filePath];
      var _toFile = involvedNsData[toItem.namespace].codecrumbsLayoutMap[toItem.filePath];
      return React.createElement(ExternalEdge, _extends({}, edgeBaseProps, {
        singleCrumbSource: _fromFile.children.length === 1,
        singleCrumbTarget: _toFile.children.length === 1,
        areaHeight: areaHeight,
        topBottom: namespaceIndex < namespacesList.indexOf(toItem.namespace),
        firstPart: true
      }));
    }

    if (fromItem.namespace !== namespace && toItem.namespace === namespace) {
      var _fromFile2 = involvedNsData[fromItem.namespace].codecrumbsLayoutMap[fromItem.filePath];
      var _toFile2 = codecrumbsLayoutMap[toItem.filePath];
      return React.createElement(ExternalEdge, _extends({}, edgeBaseProps, {
        singleCrumbSource: _fromFile2.children.length === 1,
        singleCrumbTarget: _toFile2.children.length === 1,
        areaHeight: areaHeight,
        topBottom: namespaceIndex < namespacesList.indexOf(fromItem.namespace)
      }));
    }

    var fromFile = codecrumbsLayoutMap[fromItem.filePath];
    var toFile = codecrumbsLayoutMap[toItem.filePath];
    return React.createElement(CodeCrumbedFlowEdge, _extends({}, edgeBaseProps, {
      singleCrumbSource: fromFile.children.length === 1,
      singleCrumbTarget: toFile.children.length === 1,
      sourceName: fromItem.name,
      targetName: toItem.name
    }));
  }));
});

var isFlowEdgeSelected = function isFlowEdgeSelected(selectedCcFlowEdgeNodes, currentSource, currentTarget) {
  if (!selectedCcFlowEdgeNodes) return false;
  var source = selectedCcFlowEdgeNodes.source,
      target = selectedCcFlowEdgeNodes.target;
  return isCodeCrumbsEqual(source, currentSource) && isCodeCrumbsEqual(target, currentTarget);
};