"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.shouldFocus = shouldFocus;
exports.default = void 0;

var _redux = require("redux");

var _reactRedux = require("../../../../../react-redux");

var _makeEditable = _interopRequireDefault(require("../../../../shared/functionality/makeEditable"));

var _constants = require("@instructure/quiz-common/lib/constants");

var _ui = require("../../../../../actions/ui");

var _modal = require("../../../../../actions/modal");

var _presenter = _interopRequireDefault(require("./presenter"));

// only exported for testing - not intended for external use
function shouldFocus(state, props) {
  var deletedQuizEntry = state.getIn(['ui', _constants.DELETED_QUIZ_ENTRY], {});
  var position = props.position,
      stimulusId = props.stimulusId;
  var positionMatches = deletedQuizEntry.position === position;
  var bothInStimulus = deletedQuizEntry.inStimulus && stimulusId !== void 0;
  var neitherInStimulus = !deletedQuizEntry.inStimulus && stimulusId === void 0;
  var stimulusMatches = bothInStimulus || neitherInStimulus;
  var deletedFirst = deletedQuizEntry.position === 1;
  var isFirst = position === 0;
  return positionMatches && stimulusMatches || deletedFirst && isFirst;
}

var mapStateToProps = function mapStateToProps(state, props) {
  return {
    appContainer: state.getIn(['ui', 'appContainer']),
    shouldFocus: shouldFocus(state, props)
  };
};

var mapDispatchToProps = {
  openAddContentModal: _modal.openAddContentModal,
  clearUi: _ui.clear
};

var _default = (0, _redux.compose)(_makeEditable.default, (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps))(_presenter.default);

exports.default = _default;