"use strict";

exports.__esModule = true;
exports["default"] = updateEditor;
var _lodash = require("lodash");
var _sequenceUtils = require("@teselagen/sequence-utils");
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 updateEditor(store, editorName, initialValues, extraMeta, _temp) {
  var _currentEditor$sequen;
  if (initialValues === void 0) {
    initialValues = {};
  }
  if (extraMeta === void 0) {
    extraMeta = {};
  }
  var _ref = _temp === void 0 ? {} : _temp,
    convertAnnotationsFromAAIndices = _ref.convertAnnotationsFromAAIndices;
  var _initialValues = initialValues,
    sequenceData = _initialValues.sequenceData,
    annotationVisibility = _initialValues.annotationVisibility,
    annotationsToSupport = _initialValues.annotationsToSupport,
    findTool = _initialValues.findTool,
    justPassingPartialSeqData = _initialValues.justPassingPartialSeqData;
  var currentEditor = store.getState().VectorEditor[editorName] || {};
  var isAlreadyProteinEditor = currentEditor.sequenceData && currentEditor.sequenceData.isProtein;
  var isAlreadyRnaEditor = currentEditor.sequenceData && currentEditor.sequenceData.isRna;
  var isAlreadyOligoEditor = currentEditor.sequenceData && currentEditor.sequenceData.isOligo;
  var reverseSequenceShouldBeUpdate = ((_currentEditor$sequen = currentEditor.sequenceData) === null || _currentEditor$sequen === void 0 ? void 0 : _currentEditor$sequen.isSingleStrandedDNA) !== (sequenceData === null || sequenceData === void 0 ? void 0 : sequenceData.isSingleStrandedDNA);
  var isAlreadySpecialEditor = isAlreadyProteinEditor || isAlreadyRnaEditor || isAlreadyOligoEditor || reverseSequenceShouldBeUpdate;
  var toSpread = {};
  var payload;
  if (justPassingPartialSeqData) {
    payload = {
      sequenceData: (0, _sequenceUtils.tidyUpSequenceData)(_extends({}, currentEditor.sequenceData, sequenceData), {
        convertAnnotationsFromAAIndices: convertAnnotationsFromAAIndices,
        //if we have sequence data coming in make sure to tidy it up for the user :)
        annotationsAsObjects: true
      })
    };
  } else {
    if (sequenceData) {
      var isDna = !sequenceData.isOligo && !sequenceData.isRna && !sequenceData.isProtein;
      if (sequenceData.isProtein && !isAlreadyProteinEditor) {
        //we're editing a protein but haven't initialized the protein editor yet
        toSpread = {
          findTool: _extends({
            dnaOrAA: "AA"
          }, findTool),
          annotationVisibility: _extends({
            caret: true
          }, annotationVisibility, {
            //we spread this here to allow the user to override this .. if they must!
            sequence: false,
            reverseSequence: false,
            cutsites: false,
            translations: false,
            aminoAcidNumbers: false,
            primaryProteinSequence: true
          }),
          annotationsToSupport: _extends({
            features: true,
            translations: false,
            primaryProteinSequence: true,
            parts: true,
            orfs: false,
            cutsites: false,
            primers: false
          }, annotationsToSupport)
        };
      } else if (sequenceData.isOligo && !isAlreadyOligoEditor) {
        toSpread = {
          findTool: _extends({
            dnaOrAA: "DNA"
          }, findTool),
          annotationVisibility: _extends({
            caret: true
          }, annotationVisibility, {
            //we spread this here to allow the user to override this .. if they must!
            sequence: true,
            cutsites: false,
            reverseSequence: false,
            translations: false,
            aminoAcidNumbers: false,
            primaryProteinSequence: false
          }),
          annotationsToSupport: _extends({
            features: true,
            translations: true,
            primaryProteinSequence: false,
            parts: true,
            orfs: false,
            cutsites: true,
            primers: false
          }, annotationsToSupport)
        };
      } else if (sequenceData.isRna && !isAlreadyRnaEditor) {
        toSpread = {
          findTool: _extends({
            dnaOrAA: "DNA"
          }, findTool),
          annotationVisibility: _extends({
            caret: true
          }, annotationVisibility, {
            //we spread this here to allow the user to override this .. if they must!
            sequence: true,
            cutsites: false,
            reverseSequence: false,
            translations: false,
            aminoAcidNumbers: false,
            primaryProteinSequence: false
          }),
          annotationsToSupport: _extends({
            features: true,
            translations: true,
            primaryProteinSequence: false,
            parts: true,
            orfs: true,
            cutsites: true,
            primers: true
          }, annotationsToSupport)
        };
      } else if (isAlreadySpecialEditor && isDna) {
        //we're editing dna but haven't initialized the dna editor yet
        sequenceData.isProtein = false;
        toSpread = {
          findTool: _extends({
            dnaOrAA: "DNA"
          }, findTool),
          annotationVisibility: _extends({
            caret: true
          }, annotationVisibility, {
            //we spread this here to allow the user to override this .. if they must!
            sequence: true,
            reverseSequence: !(sequenceData === null || sequenceData === void 0 ? void 0 : sequenceData.isSingleStrandedDNA),
            translations: false,
            aminoAcidNumbers: false,
            primaryProteinSequence: false
          }),
          annotationsToSupport: _extends({
            features: true,
            translations: true,
            primaryProteinSequence: false,
            parts: true,
            orfs: true,
            cutsites: true,
            primers: true
          }, annotationsToSupport)
        };
      }
    }
    payload = _extends({}, initialValues, toSpread, sequenceData && {
      sequenceData: (0, _sequenceUtils.tidyUpSequenceData)(sequenceData, {
        convertAnnotationsFromAAIndices: convertAnnotationsFromAAIndices,
        //if we have sequence data coming in make sure to tidy it up for the user :)
        annotationsAsObjects: true
      })
    });
  }
  _sequenceUtils.annotationTypes.forEach(function (t) {
    if (Object.keys((sequenceData === null || sequenceData === void 0 ? void 0 : sequenceData[t]) || {}).length > 100) {
      (0, _lodash.set)(payload, "annotationLabelVisibility." + t, false);
    }
  });
  if (sequenceData && sequenceData.size > 20000) {
    (0, _lodash.set)(payload, "annotationVisibility.translations", false);
    (0, _lodash.set)(payload, "annotationVisibility.cutsites", false);
  }
  store.dispatch({
    type: "VECTOR_EDITOR_UPDATE",
    payload: payload,
    meta: _extends({
      mergeStateDeep: true,
      editorName: editorName,
      disregardUndo: true
    }, extraMeta)
  });
}
module.exports = exports.default;