import _objectSpread from "@babel/runtime/helpers/objectSpread";
import React from 'react';
import { connect } from 'react-redux';
import Collapse from 'antd/es/collapse';
import 'antd/es/collapse/style';
import Alert from 'antd/es/alert';
import 'antd/es/alert/style';
import { NO_TRAIL_FLOW } from '../../../../shared-constants';
import { getCodeCrumbsUserChoice } from '../../../../core/dataBus/selectors';
import { getNamespacesList } from '../../../../core/dataBus/selectors';
import { gatherFlowStepsData } from '../../../treeDiagram/component/Tree/CodeCrumbs/helpers';
import Code from '../Code';
import './index.less';
var Panel = Collapse.Panel;

var CrumbsTab = function CrumbsTab(props) {
  var language = props.language,
      flowStepsFiles = props.flowStepsFiles;
  return React.createElement("div", {
    className: 'CrumbsTab'
  }, flowStepsFiles.length ? React.createElement(Collapse, {
    bordered: false,
    defaultActiveKey: Array(flowStepsFiles.length).fill().map(function (item, index) {
      return "".concat(index);
    })
  }, flowStepsFiles.map(function (stepFile, i) {
    return React.createElement(Panel, {
      header: "[".concat(typeof stepFile.step !== 'undefined' ? stepFile.step : '*', "] ").concat(stepFile.file.path),
      key: stepFile.crumbNodeLines.join(',')
    }, React.createElement(Code, {
      language: language,
      limitedHeight: true,
      code: stepFile.file.fileCode || '',
      crumbedLines: [stepFile.crumbNodeLines]
    }));
  })) : React.createElement(Alert, {
    message: "C-mon, my dear friend, select crumbs trail, what are you waiting for?",
    type: "info",
    showIcon: true
  }));
};

var mapStateToProps = function mapStateToProps(state, props) {
  var namespacesList = getNamespacesList(state);
  var namespace = props.namespace;

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

  var _gatherFlowStepsData = gatherFlowStepsData(state, {
    currentSelectedCrumbedFlowKey: currentSelectedCrumbedFlowKey,
    namespacesList: namespacesList
  }),
      involvedNsData = _gatherFlowStepsData.involvedNsData,
      sortedFlowSteps = _gatherFlowStepsData.sortedFlowSteps;

  if (currentSelectedCrumbedFlowKey === NO_TRAIL_FLOW) {
    return {
      flowStepsFiles: getUnTrailedCodecrumbs(currentSelectedCrumbedFlowKey, codeCrumbedFlowsMap, involvedNsData[namespace].codecrumbsLayoutMap)
    };
  }

  return {
    flowStepsFiles: (selectedCcFlowEdgeNodes ? [selectedCcFlowEdgeNodes.source, selectedCcFlowEdgeNodes.target] : sortedFlowSteps).map(function (item) {
      return _objectSpread({}, item, {
        file: involvedNsData[item.namespace].codecrumbsLayoutMap[item.filePath].data
      });
    })
  };
};

var getUnTrailedCodecrumbs = function getUnTrailedCodecrumbs(selectedCrumbedFlowKey, codeCrumbedFlowsMap, filesLayoutMap) {
  var codecrumbs = [];
  var currentFlow = codeCrumbedFlowsMap[selectedCrumbedFlowKey];

  if (!currentFlow) {
    return codecrumbs;
  }

  Object.keys(currentFlow).forEach(function (filePath) {
    codecrumbs = codecrumbs.concat((filesLayoutMap[filePath] && filesLayoutMap[filePath].children || []).filter(function (_ref) {
      var data = _ref.data;
      return data.params.flow === selectedCrumbedFlowKey;
    }).map(function (_ref2) {
      var data = _ref2.data;
      return {
        crumbNodeLines: data.crumbNodeLines,
        file: filesLayoutMap[filePath].data
      };
    }));
  });
  return codecrumbs;
};

export default connect(mapStateToProps)(CrumbsTab);