import React from 'react';
import { connect } from 'react-redux';
import { getSource, getSourceLayout, getCodeCrumbsUserChoice, getNamespacesList } from '../../../../../core/dataBus/selectors';
import { selectCodeCrumb, selectNodeToOpenInEditor, selectCcFlowEdge } from '../../../../../core/dataBus/actions';
import { getCheckedState } from '../../../../../core/controlsBus/selectors';
import Tree from './Tree';
import FlowEdges from './FlowEdge';

var mapStateToProps = function mapStateToProps(state, props) {
  var _getCheckedState = getCheckedState(state),
      codeCrumbsMinimize = _getCheckedState.codeCrumbsMinimize,
      codeCrumbsLineNumbers = _getCheckedState.codeCrumbsLineNumbers;

  var namespacesList = getNamespacesList(state);
  var namespace = props.namespace;
  var namespaceProps = {
    namespace: namespace
  };

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

  var _getSourceLayout = getSourceLayout(state, {
    namespace: namespace
  }),
      codecrumbsLayoutMap = _getSourceLayout.codecrumbsLayoutMap,
      ccAlightPoint = _getSourceLayout.ccAlightPoint;

  var _getCodeCrumbsUserCho = getCodeCrumbsUserChoice(state, {
    namespace: namespace
  }),
      currentSelectedCrumbedFlowKey = _getCodeCrumbsUserCho.selectedCrumbedFlowKey,
      selectedCcFlowEdgeNodes = _getCodeCrumbsUserCho.selectedCcFlowEdgeNodes;

  return {
    codecrumbsLayoutMap: codecrumbsLayoutMap,
    ccAlightPoint: ccAlightPoint,
    filesMap: filesMap,
    currentSelectedCrumbedFlowKey: currentSelectedCrumbedFlowKey,
    codeCrumbsLineNumbers: codeCrumbsLineNumbers,
    namespacesList: namespacesList,
    codeCrumbsMinimize: codeCrumbsMinimize,
    selectedCcFlowEdgeNodes: selectedCcFlowEdgeNodes
  };
};

var mapDispatchToProps = function mapDispatchToProps(dispatch, props) {
  var namespace = props.namespace;
  return {
    onCodeCrumbSelect: function onCodeCrumbSelect(event, options) {
      return event.metaKey || event.altKey ? dispatch(selectNodeToOpenInEditor({
        path: options.fileNode.path,
        line: options.codeCrumb.crumbNodeLines[0]
      }, namespace)) : dispatch(selectCodeCrumb(options, namespace));
    },
    onFlowEdgeClick: function onFlowEdgeClick(source, target, ccNamespacesKeys) {
      dispatch(selectCcFlowEdge({
        source: source,
        target: target
      }, source.namespace)); // case with external edge

      if (source.namespace !== target.namespace) {
        dispatch(selectCcFlowEdge({
          source: source,
          target: target
        }, target.namespace));
      }

      ccNamespacesKeys.forEach(function (ns) {
        if (ns !== source.namespace && ns !== target.namespace) {
          dispatch(selectCcFlowEdge(undefined, ns));
        }
      });
    }
  };
};

export default connect(mapStateToProps, mapDispatchToProps)(function (props) {
  return React.createElement(React.Fragment, null, React.createElement(FlowEdges, props), React.createElement(Tree, props));
});