'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _immutable = require('immutable');

var _redux = require('redux');

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

var _makeEditable = require('../../../../../common/components/shared/functionality/makeEditable');

var _makeEditable2 = _interopRequireDefault(_makeEditable);

var _alerts = require('../../../../../common/actions/alerts');

var alertActions = _interopRequireWildcard(_alerts);

var _errorsShowing = require('../../../../../common/actions/errorsShowing');

var errorsShowingActions = _interopRequireWildcard(_errorsShowing);

var _items = require('../../../../api/items');

var itemApiActions = _interopRequireWildcard(_items);

var _modal = require('../../../../../common/actions/modal');

var modalActions = _interopRequireWildcard(_modal);

var _ui = require('../../../../../common/actions/ui');

var _modifications = require('../../../../../common/actions/modifications');

var modificationActions = _interopRequireWildcard(_modifications);

var _quizEntries = require('../../../../api/quizEntries');

var quizEntryApiActions = _interopRequireWildcard(_quizEntries);

var _constants = require('../../../../../constants');

var _presenter = require('./presenter');

var _presenter2 = _interopRequireDefault(_presenter);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function mapStateToProps(state, props) {
  // Error Related:
  var workingEntry = props.entry.getWorkingInstance();
  var errorsShowing = state.get('errorsShowing', (0, _immutable.Map)());
  var errorsAreShowing = errorsShowing.get(props.guid);
  var addContentPopoverOpen = state.get('editing', (0, _immutable.Map)()).find(function (isEditing, key) {
    return isEditing && key.includes(_constants.ADD_CONTENT_POPOVER_KEY);
  });
  var dndPosition = state.getIn(['dragAndDrop', 'position']);
  var draggingAboveHalf = state.getIn(['dragAndDrop', 'aboveHalf'], false);
  var draggingOver = props.draggingOnBuild && dndPosition === props.quizEntry.position;
  return {
    addContentPopoverOpen: Boolean(addContentPopoverOpen),
    draggingAboveHalf: draggingOver && draggingAboveHalf,
    draggingOver: draggingOver,
    editable: true,
    errors: workingEntry.getErrors().toJS(),
    errorsAreShowing: errorsAreShowing,
    isEditing: true,
    isOccluded: state.getIn(['ui', 'quizEntriesOccluded'], false),
    nextQuizEntry: state.getIn(['modifications', 'nextQuizEntry'], null),
    notifyScreenReader: true,
    stemErrors: workingEntry.errorsFor('itemBody').toJS(),
    workingEntry: workingEntry,
    workingQuizEntry: props.quizEntry.getWorkingInstance(),
    appContainer: state.getIn(['ui', 'appContainer'])
  };
}

function mapDispatchToProps(dispatch, props) {
  var boundErrorActions = (0, _redux.bindActionCreators)(errorsShowingActions, dispatch);
  var boundItemApiActions = (0, _redux.bindActionCreators)(itemApiActions, dispatch);
  var boundModalActions = (0, _redux.bindActionCreators)(modalActions, dispatch);
  var boundModificationActions = (0, _redux.bindActionCreators)(modificationActions, dispatch);
  var boundQuizEntryApiActions = (0, _redux.bindActionCreators)(quizEntryApiActions, dispatch);

  return {
    screenreaderNotification: (0, _redux.bindActionCreators)(alertActions, dispatch).screenreaderNotification,
    setUI: (0, _redux.bindActionCreators)(_ui.set, dispatch),

    changeItemTitle: boundModificationActions.changeItemTitle,
    changeQuizEntryPoints: boundModificationActions.changeQuizEntryPoints,
    clearModifications: boundModificationActions.clearModifications,
    clearTemporaryItem: boundModificationActions.clearTemporaryItem,
    clearNextQuizEntry: boundModificationActions.clearNextQuizEntry,

    hideError: boundErrorActions.hideError,
    showError: boundErrorActions.showError,

    createTempItem: boundModificationActions.createItem,
    createTempStimulus: boundModificationActions.createStimulus,
    createItem: boundItemApiActions.createItem,
    updateItem: boundItemApiActions.updateItem,

    cloneQuizEntry: boundQuizEntryApiActions.cloneQuizEntry,
    createQuizEntry: boundQuizEntryApiActions.createQuizEntry,
    removeQuizEntry: boundQuizEntryApiActions.removeQuizEntry,
    updateQuizEntry: boundQuizEntryApiActions.updateQuizEntry,

    openModal: boundModalActions.openModal,
    withConfirm: boundModalActions.withConfirm
  };
}

exports.default = (0, _makeEditable2.default)((0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(_presenter2.default));