"use strict";

exports.__esModule = true;
exports.updateTrackHelper = updateTrackHelper;
var _arrayUtils = require("../utils/arrayUtils");
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function updateTrackHelper(_ref) {
  var currentPairwiseAlignmentIndex = _ref.currentPairwiseAlignmentIndex,
    pairwiseAlignments = _ref.pairwiseAlignments,
    upsertAlignmentRun = _ref.upsertAlignmentRun,
    hasBeenTrimmed = _ref.hasBeenTrimmed,
    alignmentId = _ref.alignmentId,
    alignmentTracks = _ref.alignmentTracks,
    alignmentTrackIndex = _ref.alignmentTrackIndex,
    update = _ref.update;
  var updateATs = function updateATs(atsToUse, alignmentTrackIndex) {
    var removed = (0, _arrayUtils.removeItem)(atsToUse, alignmentTrackIndex);
    return (0, _arrayUtils.insertItem)(removed, _extends({}, atsToUse[alignmentTrackIndex], {
      alignmentData: _extends({}, atsToUse[alignmentTrackIndex].alignmentData, update),
      sequenceData: _extends({}, atsToUse[alignmentTrackIndex].sequenceData, update)
    }), alignmentTrackIndex);
  };
  upsertAlignmentRun(_extends({
    id: alignmentId,
    hasBeenTrimmed: hasBeenTrimmed
  }, pairwiseAlignments ? {
    pairwiseAlignments: pairwiseAlignments.map(function (ats, i) {
      if (alignmentTrackIndex === 0) {
        return updateATs(ats, 0);
      }
      if (currentPairwiseAlignmentIndex !== undefined ? currentPairwiseAlignmentIndex === i : alignmentTrackIndex - 1 === i) {
        return updateATs(ats, 1);
      }
      return ats;
    })
  } : {
    alignmentTracks: updateATs(alignmentTracks, Number(alignmentTrackIndex))
  }));
}