"use strict";

exports.__esModule = true;
exports["default"] = void 0;
var _flatMap = _interopRequireDefault(require("lodash/flatMap"));
var _reselect = require("reselect");
var _cutsitesSelector = _interopRequireDefault(require("./cutsitesSelector"));
var _filteredRestrictionEnzymesSelector = _interopRequireDefault(require("./filteredRestrictionEnzymesSelector"));
var _specialCutsiteFilterOptions = _interopRequireDefault(require("../constants/specialCutsiteFilterOptions"));
var _lodash = require("lodash");
var _isEnzymeFilterAndSelector = _interopRequireDefault(require("./isEnzymeFilterAndSelector"));
var _excluded = ["value"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
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 _default = (0, _reselect.createSelector)(_cutsitesSelector["default"], _filteredRestrictionEnzymesSelector["default"], _isEnzymeFilterAndSelector["default"], function (state, addEnzs, enzymeGroupsOverride) {
  return enzymeGroupsOverride;
}, function (_ref, filteredRestrictionEnzymes, isEnzymeFilterAnd, enzymeGroupsOverride) {
  var cutsitesByName = _ref.cutsitesByName;
  var returnVal = {
    cutsitesByName: {}
  };
  // const cutsitesByName = getLowerCaseObj(cutsitesByName);
  var hiddenEnzymesByName = {};
  var filteredEnzymes = [];
  var enzymesFromGroups = [];
  var hasUserGroup;
  var groupCount = 0;
  //handle adding enzymes that are included in user created groups
  filteredRestrictionEnzymes.forEach(function (e) {
    if (e.value.includes("__userCreatedGroup")) {
      hasUserGroup = true;
      var existingGroups = _extends({}, window.getExistingEnzymeGroups(), enzymeGroupsOverride);
      var enzymes = existingGroups[e.value.replace("__userCreatedGroup", "")] || [];
      var zs = (0, _lodash.flatMap)(enzymes, function (e) {
        return e ? {
          value: e
        } : [];
      });
      filteredEnzymes = filteredEnzymes.concat(zs);
      enzymesFromGroups = enzymesFromGroups.concat(zs);
      groupCount += 1;
    } else if (e.isHidden) {
      hiddenEnzymesByName[e.value] = e;
    } else {
      if (!e) return;
      groupCount += 1;
      filteredEnzymes.push(e);
    }
  });
  var cutSiteList = [];
  if (!filteredEnzymes || filteredEnzymes.length === 0 && !hasUserGroup) {
    returnVal.cutsitesByName = cutsitesByName;
  } else {
    //loop through each filter option ('Single Cutters', 'BamHI')
    filteredEnzymes.forEach(function (_ref2) {
      var value = _ref2.value,
        rest = _objectWithoutPropertiesLoose(_ref2, _excluded);
      if (!value) {
        console.error("Missing value for filtered enzyme", rest);
        return;
      }
      var lowerValue = value.toLowerCase();
      var cutsThisManyTimes = _specialCutsiteFilterOptions["default"][value] && _specialCutsiteFilterOptions["default"][value].cutsThisManyTimes;
      if (value === "type2s") {
        Object.keys(cutsitesByName).forEach(function (key) {
          var _cutsitesByName$key$, _cutsitesByName$key$$;
          if (hiddenEnzymesByName[key]) return; //don't show that cutsite
          if (cutsitesByName[key].length && ((_cutsitesByName$key$ = cutsitesByName[key][0]) === null || _cutsitesByName$key$ === void 0 ? void 0 : (_cutsitesByName$key$$ = _cutsitesByName$key$.restrictionEnzyme) === null || _cutsitesByName$key$$ === void 0 ? void 0 : _cutsitesByName$key$$.isType2S)) {
            cutSiteList.push(key);
            returnVal.cutsitesByName[key] = cutsitesByName[key];
          }
        });
      } else if (cutsThisManyTimes > 0) {
        //the cutter type is either 1,2,3 for single, double or triple cutters
        Object.keys(cutsitesByName).forEach(function (key) {
          if (hiddenEnzymesByName[key]) return; //don't show that cutsite
          if (cutsitesByName[key].length === cutsThisManyTimes) {
            cutSiteList.push(key);
            returnVal.cutsitesByName[key] = cutsitesByName[key];
          }
        });
      } else {
        if (hiddenEnzymesByName[lowerValue]) return; //don't show that cutsite
        //normal enzyme ('BamHI')
        if (!cutsitesByName[lowerValue]) return;
        cutSiteList.push(lowerValue);
        returnVal.cutsitesByName[lowerValue] = cutsitesByName[lowerValue];
      }
    });
  }
  var enzymeCounts = {};
  cutSiteList.forEach(function (enzyme) {
    return enzymeCounts[enzyme] = enzymeCounts[enzyme] ? enzymeCounts[enzyme] + 1 : 1;
  });
  var intersectionCutSites = [];
  Object.keys(enzymeCounts).forEach(function (key) {
    if (enzymeCounts[key] === groupCount) intersectionCutSites.push(key);
  });
  returnVal.cutsiteIntersectionCount = intersectionCutSites.length;
  var cutsbyname_AND = {};
  intersectionCutSites.forEach(function (value) {
    cutsbyname_AND[value] = cutsitesByName[value];
  });
  returnVal.cutsiteTotalCount = Object.keys(returnVal.cutsitesByName).length;
  if (isEnzymeFilterAnd && returnVal.cutsiteIntersectionCount > 0) {
    returnVal.cutsitesByName = cutsbyname_AND;
  }
  returnVal.cutsitesArray = (0, _flatMap["default"])(returnVal.cutsitesByName, function (cutsitesByNameArray) {
    return cutsitesByNameArray;
  });
  returnVal.cutsitesById = returnVal.cutsitesArray.reduce(function (obj, item) {
    if (item && item.id) {
      obj[item.id] = item;
    }
    return obj;
  }, {});
  return returnVal;
});
exports["default"] = _default;
module.exports = exports.default;