"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); // import uniqid from "uniqid";

// import Ladder from "./Ladder";


var _withEditorProps = require("../withEditorProps");

var _withEditorProps2 = _interopRequireDefault(_withEditorProps);

var _recompose = require("recompose");

var _veRangeUtils = require("ve-range-utils");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// import selectionLayer from "../redux/selectionLayer";

exports.default = (0, _recompose.compose)(_withEditorProps2.default, (0, _recompose.withProps)(function (props) {
  var sequenceData = props.sequenceData,
      sequenceLength = props.sequenceLength,
      selectionLayerUpdate = props.selectionLayerUpdate,
      updateSelectedFragment = props.updateSelectedFragment;

  var fragments = [];
  var overlappingEnzymes = [];
  var pairs = [];
  var sortedCutsites = sequenceData.cutsites.sort(function (a, b) {
    return a.topSnipPosition - b.topSnipPosition;
  });

  sortedCutsites.forEach(function (cutsite1, index) {
    pairs.push([cutsite1, sortedCutsites[index + 1] ? sortedCutsites[index + 1] : sortedCutsites[0]]);
  });

  pairs.forEach(function (_ref) {
    var _ref2 = _slicedToArray(_ref, 2),
        cut1 = _ref2[0],
        cut2 = _ref2[1];

    var start = (0, _veRangeUtils.normalizePositionByRangeLength)(cut1.topSnipPosition, sequenceLength);
    var end = (0, _veRangeUtils.normalizePositionByRangeLength)(cut2.topSnipPosition - 1, sequenceLength);
    var size = (0, _veRangeUtils.getRangeLength)({ start: start, end: end }, sequenceLength);

    // const id = uniqid()
    var id = start + "-" + end + "-" + size + "-";
    (0, _veRangeUtils.getRangeLength)({ start: start, end: end }, sequenceLength);
    fragments.push({
      cut1: cut1,
      cut2: cut2,
      start: start,
      end: end,
      size: size,
      id: id,
      onFragmentSelect: function onFragmentSelect() {
        selectionLayerUpdate({
          start: start,
          end: end
        });
        updateSelectedFragment(id);
      }
    });
  });

  fragments.filter(function (fragment) {
    if (!fragment.size) {
      overlappingEnzymes.push(fragment);
      return false;
    }
    return true;
  });
  return {
    lanes: [fragments],
    overlappingEnzymes: overlappingEnzymes
  };
}));