import React from 'react';
import { FILE_NODE_TYPE, DIR_NODE_TYPE } from '../../../../../core/constants';
import { FileName } from '../../Node/File';
import { FolderName } from '../../Node/Folder';
import Dot from '../../Icons/Dot';
import { SourceEdge } from '../../Edge/SourceEdge';
import DependenciesTree from '../Dependencies/index';
import CodeCrumbsTree from '../CodeCrumbs/';

var SourceTree = function SourceTree(props) {
  var namespace = props.namespace,
      language = props.language,
      sourceDiagramOn = props.sourceDiagramOn,
      dependenciesDiagramOn = props.dependenciesDiagramOn,
      sourceDimFolders = props.sourceDimFolders,
      codeCrumbsDiagramOn = props.codeCrumbsDiagramOn,
      codeCrumbsMinimize = props.codeCrumbsMinimize,
      sortedFlowSteps = props.sortedFlowSteps,
      involvedNsData = props.involvedNsData,
      ccShiftIndexMap = props.ccShiftIndexMap,
      areaHeight = props.areaHeight,
      sourceLayoutTree = props.sourceLayoutTree,
      openedFolders = props.openedFolders,
      filesMap = props.filesMap,
      selectedNode = props.selectedNode,
      shiftToCenterPoint = props.shiftToCenterPoint,
      onFileNodeClick = props.onFileNodeClick,
      onFolderNodeClick = props.onFolderNodeClick,
      dependenciesEntryName = props.dependenciesEntryName,
      selectedDependencyEdgeNodes = props.selectedDependencyEdgeNodes;
  var sourceEdges = [];
  var selectedSourceEdges = [];
  var sourceNodes = [];
  var sourceDotes = []; // TODO: add normal id generators for keys to not use i
  // TODO: refactor, too long render method

  var i = 0;
  sourceDiagramOn && sourceLayoutTree.each(function (node) {
    i++;
    var _ref = [node.y, node.x],
        nX = _ref[0],
        nY = _ref[1];
    var position = shiftToCenterPoint(nX, nY);
    var _node$data = node.data,
        name = _node$data.name,
        path = _node$data.path,
        type = _node$data.type;
    var parent = node.parent;
    var selected = selectedNode && selectedNode.path.indexOf(path) !== -1;

    if (parent && parent.data.type === DIR_NODE_TYPE) {
      var _ref2 = [parent.y, parent.x],
          pX = _ref2[0],
          pY = _ref2[1];
      var sourcePosition = shiftToCenterPoint(pX, pY);
      var edge = React.createElement(SourceEdge, {
        key: "source-edge-".concat(path, "-").concat(sourcePosition.x, "-").concat(sourcePosition.y),
        targetPosition: position,
        sourcePosition: sourcePosition,
        disabled: sourceDimFolders,
        singleChild: parent.children.length === 1,
        selected: selected
      });
      selected ? selectedSourceEdges.push(edge) : sourceEdges.push(edge);
    }

    if (type === DIR_NODE_TYPE || type === FILE_NODE_TYPE && !(dependenciesDiagramOn && selectedNode.dependencies && selectedNode.dependencies[path])) {
      sourceDotes.push(React.createElement(Dot, {
        key: "dot-".concat(path, "-").concat(position.x, "-").concat(position.y),
        position: position,
        selected: selected
      }));
    }

    var nodeBasedOnType = null;

    if (type === FILE_NODE_TYPE) {
      var selectedNodeDependencies = selectedNode.dependencies;
      var fileNode = filesMap[path];
      nodeBasedOnType = React.createElement(FileName, {
        key: path,
        language: language,
        position: position,
        name: name,
        path: fileNode.path,
        codeCrumbs: codeCrumbsDiagramOn,
        purple: codeCrumbsDiagramOn && fileNode.hasCodecrumbs && codeCrumbsMinimize,
        selected: selectedDependencyEdgeNodes && (selectedDependencyEdgeNodes.target === path || selectedDependencyEdgeNodes.sources.includes(path)),
        depEntryPoint: path === dependenciesEntryName,
        dependency: dependenciesDiagramOn && selectedNodeDependencies && selectedNodeDependencies[path],
        dependencyImportedOnly: dependenciesDiagramOn && selectedNodeDependencies && selectedNodeDependencies[path] && !selectedNodeDependencies[path].importedModuleNames.length,
        onNodeClick: function onNodeClick(e) {
          return onFileNodeClick(e, fileNode);
        }
      });
    } else if (type === DIR_NODE_TYPE) {
      nodeBasedOnType = React.createElement(FolderName, {
        key: path,
        position: position,
        name: name,
        cover: dependenciesDiagramOn || codeCrumbsDiagramOn,
        disabled: sourceDimFolders,
        openedState: openedFolders[node.data.path],
        onNodeClick: function onNodeClick() {
          return onFolderNodeClick(node.data);
        }
      });
    }

    sourceNodes.push(nodeBasedOnType);
  });
  return React.createElement(React.Fragment, null, sourceDiagramOn && sourceEdges || null, sourceDiagramOn && selectedSourceEdges || null, dependenciesDiagramOn && selectedNode.dependencies && React.createElement(DependenciesTree, {
    namespace: namespace,
    language: language,
    shiftToCenterPoint: shiftToCenterPoint
  }), sourceDiagramOn && sourceNodes || null, sourceDiagramOn && sourceDotes || null, codeCrumbsDiagramOn && React.createElement(CodeCrumbsTree, {
    namespace: namespace,
    language: language,
    shiftToCenterPoint: shiftToCenterPoint,
    sortedFlowSteps: sortedFlowSteps,
    involvedNsData: involvedNsData,
    ccShiftIndexMap: ccShiftIndexMap,
    areaHeight: areaHeight
  }) || null);
};

export default SourceTree;