"use strict";

exports.__esModule = true;
exports.actions = void 0;
exports["default"] = reducerFactory;
exports.vectorEditorMiddleware = exports.getEditorByName = exports.editorReducer = void 0;
var _lodash = require("lodash");
var createYourOwnEnzyme = _interopRequireWildcard(require("./createYourOwnEnzyme"));
var showGCContent = _interopRequireWildcard(require("./showGCContent"));
var annotationLabelVisibility = _interopRequireWildcard(require("./annotationLabelVisibility"));
var annotationsToSupport = _interopRequireWildcard(require("./annotationsToSupport"));
var annotationVisibility = _interopRequireWildcard(require("./annotationVisibility"));
var caretPosition = _interopRequireWildcard(require("./caretPosition"));
var copyOptions = _interopRequireWildcard(require("./copyOptions"));
var deletionLayers = _interopRequireWildcard(require("./deletionLayers"));
var digestTool = _interopRequireWildcard(require("./digestTool"));
var findTool = _interopRequireWildcard(require("./findTool"));
var toolBar = _interopRequireWildcard(require("./toolBar"));
var frameTranslations = _interopRequireWildcard(require("./frameTranslations"));
var hoveredAnnotation = _interopRequireWildcard(require("./hoveredAnnotation"));
var minimumOrfSize = _interopRequireWildcard(require("./minimumOrfSize"));
var alignments = _interopRequireWildcard(require("./alignments"));
var panelsShown = _interopRequireWildcard(require("./panelsShown"));
var propertiesTool = _interopRequireWildcard(require("./propertiesTool"));
var lastSavedId = _interopRequireWildcard(require("./lastSavedId"));
var readOnly = _interopRequireWildcard(require("./readOnly"));
var versionHistory = _interopRequireWildcard(require("./versionHistory"));
var replacementLayers = _interopRequireWildcard(require("./replacementLayers"));
var restrictionEnzymes = _interopRequireWildcard(require("./restrictionEnzymes"));
var selectedAnnotations = _interopRequireWildcard(require("./selectedAnnotations"));
var selectionLayer = _interopRequireWildcard(require("./selectionLayer"));
var sequenceDataHistory = _interopRequireWildcard(require("./sequenceDataHistory"));
var sequenceData = _interopRequireWildcard(require("./sequenceData"));
var useAdditionalOrfStartCodons = _interopRequireWildcard(require("./useAdditionalOrfStartCodons"));
var uppercaseSequenceMapFont = _interopRequireWildcard(require("./uppercaseSequenceMapFont"));
var charWidth = _interopRequireWildcard(require("./charWidth"));
var labelLineIntensity = _interopRequireWildcard(require("./labelLineIntensity"));
var labelSize = _interopRequireWildcard(require("./labelSize"));
var featureLengthsToHide = _interopRequireWildcard(require("./featureLengthsToHide"));
var primerLengthsToHide = _interopRequireWildcard(require("./primerLengthsToHide"));
var partLengthsToHide = _interopRequireWildcard(require("./partLengthsToHide"));
var selectedPartTags = _interopRequireWildcard(require("./selectedPartTags"));
var _redux = require("redux");
var _createMetaAction = _interopRequireDefault(require("./utils/createMetaAction"));
var _middleware = _interopRequireDefault(require("./middleware"));
exports.vectorEditorMiddleware = _middleware["default"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
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); }
var subReducers = {
  createYourOwnEnzyme: createYourOwnEnzyme,
  annotationLabelVisibility: annotationLabelVisibility,
  annotationsToSupport: annotationsToSupport,
  annotationVisibility: annotationVisibility,
  caretPosition: caretPosition,
  copyOptions: copyOptions,
  deletionLayers: deletionLayers,
  digestTool: digestTool,
  toolBar: toolBar,
  findTool: findTool,
  frameTranslations: frameTranslations,
  hoveredAnnotation: hoveredAnnotation,
  minimumOrfSize: minimumOrfSize,
  panelsShown: panelsShown,
  propertiesTool: propertiesTool,
  lastSavedId: lastSavedId,
  readOnly: readOnly,
  versionHistory: versionHistory,
  replacementLayers: replacementLayers,
  restrictionEnzymes: restrictionEnzymes,
  selectedAnnotations: selectedAnnotations,
  selectionLayer: selectionLayer,
  sequenceDataHistory: sequenceDataHistory,
  sequenceData: sequenceData,
  useAdditionalOrfStartCodons: useAdditionalOrfStartCodons,
  charWidth: charWidth,
  uppercaseSequenceMapFont: uppercaseSequenceMapFont,
  showGCContent: showGCContent,
  labelLineIntensity: labelLineIntensity,
  labelSize: labelSize,
  partLengthsToHide: partLengthsToHide,
  primerLengthsToHide: primerLengthsToHide,
  featureLengthsToHide: featureLengthsToHide,
  selectedPartTags: selectedPartTags
};
var vectorEditorInitialize = (0, _createMetaAction["default"])("VECTOR_EDITOR_UPDATE");
var vectorEditorClear = (0, _createMetaAction["default"])("VECTOR_EDITOR_CLEAR");

//export the actions for use elsewhere
var actions = _extends({}, Object.keys(subReducers).reduce(function (acc, k) {
  return _extends({}, acc, subReducers[k]);
}, {}), alignments, {
  vectorEditorInitialize: vectorEditorInitialize,
  vectorEditorClear: vectorEditorClear
});
exports.actions = actions;
var mergeDeepKeys = [];
//define the reducer
var reducers = _extends({}, Object.keys(subReducers).reduce(function (acc, k) {
  var _extends2;
  if (subReducers[k]["default"] && subReducers[k]["default"].__shouldUseMergedState) {
    mergeDeepKeys.push(k);
  }
  return _extends({}, acc, (_extends2 = {}, _extends2[k] = subReducers[k]["default"], _extends2));
}, {}), {
  instantiated: function instantiated() {
    return true;
  }
});
var editorReducer = (0, _redux.combineReducers)(reducers);
exports.editorReducer = editorReducer;
var customDeepMerge = function customDeepMerge(state, newState) {
  if (state === void 0) {
    state = {};
  }
  if (newState === void 0) {
    newState = {};
  }
  return _extends({}, state, newState, mergeDeepKeys.reduce(function (acc, key) {
    acc[key] = (0, _lodash.merge)(state[key], newState[key]);
    return acc;
  }, {}));
};
function reducerFactory(initialState) {
  if (initialState === void 0) {
    initialState = {};
  }
  // if (!initialState || !Object.keys(initialState).length) {
  //   throw new Error(
  //     "Please pass an initial state to the vector editor reducer like: {DemoEditor: {}}!"
  //   );
  // }
  return function (state, action) {
    if (state === void 0) {
      state = initialState;
    }
    var editorNames;
    var newState = {};
    if (action.meta && action.meta.editorName) {
      editorNames = Array.isArray(action.meta.editorName) ? action.meta.editorName : [action.meta.editorName];
    }
    var stateToReturn;
    if (editorNames) {
      //we're dealing with an action specific to a given editor
      editorNames.forEach(function (editorName) {
        var currentState = state[editorName];
        if (action.type === "VECTOR_EDITOR_UPDATE") {
          //deep merge certain parts of the exisiting state with the new payload of props
          //(if you want to do a clean wipe, use VECTOR_EDITOR_CLEAR)
          currentState = customDeepMerge(state[editorName], action.payload);
        }
        if (action.type === "VECTOR_EDITOR_CLEAR") {
          currentState = undefined;
        }
        newState[editorName] = editorReducer(currentState, action);
      });
      stateToReturn = _extends({}, state, newState);
    } else {
      //just a normal action
      Object.keys(state).forEach(function (editorName) {
        if (editorName === "__allEditorsOptions") return; //we deal with __allEditorsOptions below so don't pass it here
        newState[editorName] = editorReducer(state[editorName], action);
      });
      stateToReturn = newState;
    }
    return _extends({}, stateToReturn, {
      //these are reducers that are not editor specific (aka shared across editor instances)
      __allEditorsOptions: [["createYourOwnEnzyme", createYourOwnEnzyme], ["uppercaseSequenceMapFont", uppercaseSequenceMapFont], ["showGCContent", showGCContent], ["labelLineIntensity", labelLineIntensity], ["labelSize", labelSize], ["alignments", alignments]].reduce(function (acc, _ref) {
        var key = _ref[0],
          val = _ref[1];
        acc[key] = val["default"](!state.__allEditorsOptions ? undefined : state.__allEditorsOptions[key], action);
        return acc;
      }, {})
    });
  };
}

// export const getBlankEditor = (state) => (state.blankEditor)
var getEditorByName = function getEditorByName(state, editorName) {
  return state[editorName];
};

// export default connect((state, ownProps) => {
//   return {
//     toggled: state.VectorEditor[ownProps.editorName].annotationVisibility.features
//   }
// })()
exports.getEditorByName = getEditorByName;