import React from 'react';
import { connect } from 'react-redux';
import { selectNode, selectNodeToOpenInEditor, toggleFolder } from '../../../../../core/dataBus/actions';
import { getSource, getSourceLayout, getSourceUserChoice, getDependenciesUserChoice, getProjectMetadata } from '../../../../../core/dataBus/selectors';
import { getCheckedState } from '../../../../../core/controlsBus/selectors';
import SourceTree from './Tree';

var mapStateToProps = function mapStateToProps(state, props) {
  var _getCheckedState = getCheckedState(state),
      sourceDiagramOn = _getCheckedState.sourceDiagramOn,
      dependenciesDiagramOn = _getCheckedState.dependenciesDiagramOn,
      sourceDimFolders = _getCheckedState.sourceDimFolders,
      codeCrumbsDiagramOn = _getCheckedState.codeCrumbsDiagramOn,
      codeCrumbsMinimize = _getCheckedState.codeCrumbsMinimize;

  var namespace = props.namespace;
  var namespaceProps = {
    namespace: namespace
  };

  var _getSource = getSource(state, namespaceProps),
      filesMap = _getSource.filesMap;

  var _getSourceLayout = getSourceLayout(state, namespaceProps),
      sourceLayoutTree = _getSourceLayout.sourceLayoutTree;

  var _getProjectMetadata = getProjectMetadata(state, namespaceProps),
      language = _getProjectMetadata.language;

  var _getSourceUserChoice = getSourceUserChoice(state, namespaceProps),
      selectedNode = _getSourceUserChoice.selectedNode,
      openedFolders = _getSourceUserChoice.openedFolders;

  var _getDependenciesUserC = getDependenciesUserChoice(state, namespaceProps),
      dependenciesEntryName = _getDependenciesUserC.dependenciesEntryName,
      selectedDependencyEdgeNodes = _getDependenciesUserC.selectedDependencyEdgeNodes;

  return {
    namespace: namespace,
    language: language,
    sourceDiagramOn: sourceDiagramOn,
    dependenciesDiagramOn: dependenciesDiagramOn,
    sourceDimFolders: sourceDimFolders,
    codeCrumbsDiagramOn: codeCrumbsDiagramOn,
    codeCrumbsMinimize: codeCrumbsMinimize,
    sourceLayoutTree: sourceLayoutTree,
    selectedNode: selectedNode,
    openedFolders: openedFolders,
    filesMap: filesMap,
    dependenciesEntryName: dependenciesEntryName,
    selectedDependencyEdgeNodes: selectedDependencyEdgeNodes
  };
};

var mapDispatchToProps = function mapDispatchToProps(dispatch, props) {
  var namespace = props.namespace;
  return {
    onFileNodeClick: function onFileNodeClick(event, fileNode) {
      event.preventDefault();
      event.stopPropagation();
      return event.metaKey || event.altKey ? dispatch(selectNodeToOpenInEditor(fileNode, namespace)) : dispatch(selectNode(fileNode, namespace));
    },
    onFolderNodeClick: function onFolderNodeClick(folderNode) {
      return dispatch(toggleFolder(folderNode, namespace));
    }
  };
};

export default connect(mapStateToProps, mapDispatchToProps)(SourceTree);