"use strict";

exports.__esModule = true;
exports.getStructuredBases = getStructuredBases;
var _lodash = require("lodash");
var _rangeUtils = require("@teselagen/range-utils");
var _sequenceUtils = require("@teselagen/sequence-utils");
var ambiguous_dna_values = _sequenceUtils.bioData.ambiguous_dna_values;
function getStructuredBases(_ref) {
  var annotationRange = _ref.annotationRange,
    forward = _ref.forward,
    _ref$bases = _ref.bases,
    bases = _ref$bases === void 0 ? "" : _ref$bases,
    start = _ref.start,
    end = _ref.end,
    fullSequence = _ref.fullSequence,
    primerBindsOn = _ref.primerBindsOn,
    sequenceLength = _ref.sequenceLength;
  var annLen = (0, _rangeUtils.getRangeLength)({
    start: start,
    end: end
  }, sequenceLength);
  var basesToUse = bases;
  if (bases.length < annLen) {
    if (forward && primerBindsOn === "3prime") {
      var toAddLen = annLen - bases.length;
      for (var index = 0; index < toAddLen; index++) {
        basesToUse = "&" + basesToUse;
      }
    } else if (!forward && primerBindsOn === "5prime") {
      var _toAddLen = annLen - bases.length;
      for (var _index = 0; _index < _toAddLen; _index++) {
        basesToUse = basesToUse + "&";
      }
    }
  }
  var aRange = {
    //tnr: this probably needs to be changed in case annotation wraps origin
    start: annotationRange.start - start,
    end: annotationRange.end - start
  };
  var r = {
    aRange: aRange,
    basesNoInserts: basesToUse,
    inserts: []
  };
  var baseLen = basesToUse.length;
  var diffLen = baseLen - annLen;
  if (diffLen > 0) {
    r.basesNoInserts = basesToUse.slice(primerBindsOn === "5prime" ? 0 : diffLen, primerBindsOn === "5prime" ? annLen : baseLen);
    var insertBases = basesToUse.slice(primerBindsOn === "5prime" ? annLen : 0, primerBindsOn === "5prime" ? baseLen : diffLen);
    r.inserts = [{
      bases: insertBases,
      index: primerBindsOn === "5prime" ? annLen : 0
    }];
  }
  var basesForRange = (0, _rangeUtils.getSequenceWithinRange)(aRange, forward ? r.basesNoInserts : r.basesNoInserts.split("").reverse().join(""));
  r.basesNoInsertsWithMetaData = basesForRange.split("").map(function (b, i) {
    var indexOfBase = i + annotationRange.start;
    var seqForBase = fullSequence && fullSequence[indexOfBase] || "";
    if (!forward) {
      seqForBase = (0, _sequenceUtils.getComplementSequenceString)(seqForBase);
    }
    var isMatch = seqForBase.toLowerCase() === b.toLowerCase();
    var isAmbiguousMatch = !isMatch && ambiguous_dna_values[b.toUpperCase()].length > 1 && ambiguous_dna_values[b.toUpperCase()].includes(seqForBase.toUpperCase());
    return {
      b: b,
      isMatch: isMatch,
      isAmbiguousMatch: isAmbiguousMatch
    };
  });
  r.allBasesWithMetaData = (0, _lodash.clone)(r.basesNoInsertsWithMetaData);
  if (!forward) {
    r.allBasesWithMetaData = r.allBasesWithMetaData.reverse();
  }
  r.inserts.sort(function (a, b) {
    return a.index - b.index;
  }).forEach(function (_ref2) {
    var _r$allBasesWithMetaDa;
    var bases = _ref2.bases,
      index = _ref2.index;
    (_r$allBasesWithMetaDa = r.allBasesWithMetaData).splice.apply(_r$allBasesWithMetaDa, [index, 0].concat(bases.split("").map(function (b) {
      return {
        b: b,
        isMatch: false
      };
    })));
  });
  return r;
}