import React from 'react';
import { NO_TRAIL_FLOW } from '../../../../../core/constants';
import { CodeCrumbName } from '../../../../treeDiagram/component/Node/CodeCrumb';
import { PartEdge, CodeCrumbMultiEdge } from '../../../../treeDiagram/component/Edge/CodeCrumbEdge';
import { isCodeCrumbSelected, getCcPosition } from './helpers';

var Tree = function Tree(props) {
  var ccAlightPoint = props.ccAlightPoint,
      ccShiftIndexMap = props.ccShiftIndexMap,
      shiftToCenterPoint = props.shiftToCenterPoint,
      codecrumbsLayoutMap = props.codecrumbsLayoutMap,
      filesMap = props.filesMap,
      currentSelectedCrumbedFlowKey = props.currentSelectedCrumbedFlowKey,
      codeCrumbsMinimize = props.codeCrumbsMinimize,
      codeCrumbsLineNumbers = props.codeCrumbsLineNumbers,
      onCodeCrumbSelect = props.onCodeCrumbSelect,
      selectedCcFlowEdgeNodes = props.selectedCcFlowEdgeNodes;
  return React.createElement(React.Fragment, null, Object.keys(codecrumbsLayoutMap).map(function (key) {
    var node = codecrumbsLayoutMap[key];
    var _ref = [node.y, node.x],
        nX = _ref[0],
        nY = _ref[1];
    var position = shiftToCenterPoint(nX, nY);
    var file = filesMap[node.data.path];
    var singleCrumb = node.children.length === 1;
    var firstCrumbXPoint = !singleCrumb && shiftToCenterPoint(currentSelectedCrumbedFlowKey !== NO_TRAIL_FLOW ? getCcPosition(ccAlightPoint, ccShiftIndexMap[node.children[0].data.id]) : ccAlightPoint, node.children[0].y).x;
    return React.createElement(React.Fragment, {
      key: "code-crumb-".concat(key, "-").concat(nX, "-").concat(nY)
    }, !codeCrumbsMinimize && node.children.map(function (crumb, i) {
      var _ref2 = [crumb.y, crumb.x],
          _ = _ref2[0],
          cY = _ref2[1];
      var crumbData = crumb.data;
      var crumbPosition = shiftToCenterPoint(currentSelectedCrumbedFlowKey !== NO_TRAIL_FLOW ? getCcPosition(ccAlightPoint, ccShiftIndexMap[crumbData.id]) : ccAlightPoint, cY);
      var ccParams = crumbData.params;
      return React.createElement(React.Fragment, {
        key: "code-crumb-edge-".concat(crumbData.name, "-").concat(crumbPosition.x, "-").concat(crumbPosition.y)
      }, !i && React.createElement(PartEdge, {
        sourcePosition: position,
        parentName: file.name,
        ccAlightPoint: crumbPosition.x,
        singleCrumb: singleCrumb
      }), !singleCrumb && React.createElement(CodeCrumbMultiEdge, {
        sourcePosition: position,
        targetPosition: crumbPosition,
        ccAlightPoint: i && firstCrumbXPoint
      }), React.createElement(CodeCrumbName, {
        position: crumbPosition,
        loc: codeCrumbsLineNumbers ? crumbData.displayLoc : '',
        name: crumbData.name,
        singleCrumb: singleCrumb,
        cover: true,
        selected: isCodeCrumbSelected(selectedCcFlowEdgeNodes, crumbData),
        flow: ccParams.flow && ccParams.flow === currentSelectedCrumbedFlowKey && currentSelectedCrumbedFlowKey !== NO_TRAIL_FLOW,
        flowStep: ccParams.flowStep,
        original: ccParams.original,
        onClick: function onClick(e) {
          return onCodeCrumbSelect(e, {
            fileNode: file,
            codeCrumb: crumbData
          });
        }
      }));
    }));
  }));
};

export default Tree;