"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _lodash = require("lodash");
var _shortid = _interopRequireDefault(require("shortid"));
var _sequenceSelector = _interopRequireDefault(require("./sequenceSelector"));
var _orfsSelector = _interopRequireDefault(require("./orfsSelector"));
var _reselect = require("reselect");
var _sequenceUtils = require("@teselagen/sequence-utils");
var _each = _interopRequireDefault(require("lodash/each"));
var _translationsRawSelector = _interopRequireDefault(require("./translationsRawSelector"));
var _translationSearchMatchesSelector = _interopRequireDefault(require("./translationSearchMatchesSelector"));
var _rangeUtils = require("@teselagen/range-utils");
var _cdsFeaturesSelector = _interopRequireDefault(require("./cdsFeaturesSelector"));
var _circularSelector = _interopRequireDefault(require("./circularSelector"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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 translationsSelector(isCircular, translationSearchMatches, sequence, orfs, showOrfTranslations, showOrfs, cdsFeatures, showCdsFeatureTranslations, showFeatures, translations, frameTranslations) {
  var translationsToPass = _extends({}, translationSearchMatches.reduce(function (acc, match) {
    if (!match) return acc;
    var id = match.id || (0, _shortid["default"])();
    acc[id] = _extends({}, match, {
      id: id,
      translationType: "AA Search Match",
      isOrf: true,
      //pass isOrf = true here in order to not have it show up in the properties window
      forward: !match.bottomStrand
    });
    return acc;
  }, {}), (0, _lodash.reduce)(translations, function (acc, translation) {
    if (!translation.isOrf) {
      acc[translation.id] = _extends({}, translation, {
        translationType: "User Created"
      });
    }
    return acc;
  }, {}), showOrfTranslations && showOrfs ? (0, _lodash.reduce)(orfs, function (acc, orf) {
    acc[orf.id] = _extends({}, orf, {
      translationType: "ORF"
    });
    return acc;
  }, {}) : {}, showCdsFeatureTranslations && showFeatures && (0, _lodash.reduce)(cdsFeatures, function (acc, cdsFeature) {
    acc[cdsFeature.id] = _extends({}, cdsFeature, {
      translationType: "CDS Feature"
    });
    return acc;
  }, {}), (0, _lodash.reduce)(frameTranslations, function (acc, isActive, frameName) {
    var frameOffset = Number(frameName);
    if (isActive) {
      var id = (0, _shortid["default"])();
      acc[id] = {
        id: id,
        start: isCircular || frameOffset > 0 ? (0, _rangeUtils.normalizePositionByRangeLength)(0 + frameOffset + (frameOffset > 0 ? -1 : 1), sequence.length) : 0,
        end: isCircular || frameOffset < 0 ? (0, _rangeUtils.normalizePositionByRangeLength)(sequence.length - 1 + frameOffset + (frameOffset > 0 ? -1 : 1), sequence.length) : sequence.length - 1,
        translationType: "Frame",
        forward: frameOffset > 0,
        isOrf: true //pass isOrf = true here in order to not have it show up in the properties window
      };
    }

    return acc;
  }, {}));
  (0, _each["default"])(translationsToPass, function (translation) {
    translation.aminoAcids = (0, _sequenceUtils.getAminoAcidDataForEachBaseOfDna)(sequence, translation.forward, translation);
  });
  return translationsToPass;
}
var _default = (0, _reselect.createSelector)(_circularSelector["default"], _translationSearchMatchesSelector["default"], _sequenceSelector["default"], _orfsSelector["default"], function (state) {
  return state.annotationVisibility.orfTranslations;
}, function (state) {
  return state.annotationVisibility.orfs;
}, _cdsFeaturesSelector["default"], function (state) {
  return state.annotationVisibility.cdsFeatureTranslations;
}, function (state) {
  return state.annotationVisibility.features;
}, _translationsRawSelector["default"], function (state) {
  return state.frameTranslations;
}, function (state) {
  return state.sequenceData.isProtein;
}, function (state) {
  return state.sequenceData.proteinSequence;
}, translationsSelector);
exports["default"] = _default;
module.exports = exports.default;