import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/objectSpread";
import { getCodeCrumbsUserChoice, getSourceLayout } from '../../../../../core/dataBus/selectors';
import { NO_TRAIL_FLOW } from '../../../../../core/constants';
export var isCodeCrumbsEqual = function isCodeCrumbsEqual(cc, currentCc) {
  return cc.name === currentCc.name && cc.flowStep === currentCc.flowStep;
};
export var isCodeCrumbSelected = function isCodeCrumbSelected(selectedCcFlowEdgeNodes, cc) {
  if (!selectedCcFlowEdgeNodes) return false;
  return isCodeCrumbsEqual(selectedCcFlowEdgeNodes.source, cc) || isCodeCrumbsEqual(selectedCcFlowEdgeNodes.target, cc);
};

var stepSorter = function stepSorter(a, b) {
  return a.step - b.step;
};

var getFlowSteps = function getFlowSteps(_ref) {
  var namespace = _ref.namespace,
      codeCrumbedFlowsMap = _ref.codeCrumbedFlowsMap,
      selectedCrumbedFlowKey = _ref.selectedCrumbedFlowKey,
      codecrumbsLayoutMap = _ref.codecrumbsLayoutMap;
  var currentFlow = codeCrumbedFlowsMap[selectedCrumbedFlowKey] || {};
  return Object.keys(currentFlow).reduce(function (flowSteps, filePath) {
    var steps = (codecrumbsLayoutMap[filePath] && codecrumbsLayoutMap[filePath].children || []).filter(function (_ref2) {
      var data = _ref2.data;
      return data.params.flow === selectedCrumbedFlowKey;
    }).map(function (_ref3) {
      var data = _ref3.data,
          x = _ref3.x,
          y = _ref3.y;
      return _objectSpread({}, data, {
        namespace: namespace,
        filePath: filePath,
        step: data.params.flowStep,
        flow: selectedCrumbedFlowKey,
        x: x,
        y: y
      });
    });
    return _toConsumableArray(flowSteps).concat(_toConsumableArray(steps));
  }, []);
};

var createCcShiftIndexMap = function createCcShiftIndexMap(sortedFlowSteps) {
  var ccMap = {};
  var shiftOrderIndex = 0;
  sortedFlowSteps.forEach(function (crumb, i, list) {
    if (!i) {
      ccMap[crumb.id] = shiftOrderIndex;
      return;
    }

    if (crumb.x < list[i - 1].x) {
      shiftOrderIndex++;
    }

    ccMap[crumb.id] = shiftOrderIndex;
  });
  return ccMap;
};

export var gatherFlowStepsData = function gatherFlowStepsData(state, _ref4) {
  var namespacesList = _ref4.namespacesList,
      currentSelectedCrumbedFlowKey = _ref4.currentSelectedCrumbedFlowKey;
  var flowStepsData = namespacesList.reduce(function (acc, ns) {
    var namespaceProps = {
      namespace: ns
    };

    var _getCodeCrumbsUserCho = getCodeCrumbsUserChoice(state, namespaceProps),
        selectedCrumbedFlowKey = _getCodeCrumbsUserCho.selectedCrumbedFlowKey,
        codeCrumbedFlowsMap = _getCodeCrumbsUserCho.codeCrumbedFlowsMap;

    if (currentSelectedCrumbedFlowKey !== selectedCrumbedFlowKey) {
      return acc;
    }

    var _getSourceLayout = getSourceLayout(state, namespaceProps),
        codecrumbsLayoutMap = _getSourceLayout.codecrumbsLayoutMap,
        ccAlightPoint = _getSourceLayout.ccAlightPoint;

    var flowSteps = getFlowSteps({
      namespace: ns,
      codeCrumbedFlowsMap: codeCrumbedFlowsMap,
      selectedCrumbedFlowKey: selectedCrumbedFlowKey,
      codecrumbsLayoutMap: codecrumbsLayoutMap
    });
    return {
      flowSteps: flowSteps,
      sortedFlowSteps: selectedCrumbedFlowKey !== NO_TRAIL_FLOW ? _toConsumableArray(acc.sortedFlowSteps).concat(_toConsumableArray(flowSteps)).sort(stepSorter) : [],
      involvedNsData: _objectSpread({}, acc.involvedNsData, _defineProperty({}, ns, {
        codecrumbsLayoutMap: codecrumbsLayoutMap,
        ccAlightPoint: ccAlightPoint
      }))
    };
  }, {
    involvedNsData: {},
    flowSteps: [],
    sortedFlowSteps: []
  });
  return _objectSpread({}, flowStepsData, {
    ccShiftIndexMap: createCcShiftIndexMap(flowStepsData.sortedFlowSteps)
  });
};
export var getCcPosition = function getCcPosition(x) {
  var index = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  return x + 70 * index;
};
export var getMaxWidthForNs = function getMaxWidthForNs(state, _ref5) {
  var namespacesList = _ref5.namespacesList;
  return namespacesList.reduce(function (maxWidth, namespace) {
    var _getSourceLayout2 = getSourceLayout(state, {
      namespace: namespace
    }),
        layoutSize = _getSourceLayout2.layoutSize;

    return layoutSize && layoutSize.width > maxWidth ? layoutSize.width : maxWidth;
  }, 0);
};