'use strict';

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

var _redux = require('redux');

var _partial = require('lodash/partial');

var _partial2 = _interopRequireDefault(_partial);

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

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

var _stimuli = require('../../../../building/api/stimuli');

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

var _tagAssociations = require('../../../../common/actions/tagAssociations');

var _BankEntry = require('../../../../common/records/BankEntry');

var _BankEntry2 = _interopRequireDefault(_BankEntry);

var _Bank = require('../../../../common/records/Bank');

var _Bank2 = _interopRequireDefault(_Bank);

var _bankEntries = require('../../../api/bankEntries');

var _banks = require('../../../api/banks');

var _tagAssociations2 = require('../../../api/tagAssociations');

var tagAssociationApiActions = _interopRequireWildcard(_tagAssociations2);

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

var _presenter2 = _interopRequireDefault(_presenter);

var _featureCheck = require('../../../../common/util/featureCheck');

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

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

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

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) {
  var tempBankEntry = state.getIn(['modifications', 'bankEntries', _constants.TEMPORARY_BANK_ENTRY_ID]);
  var bankEntry = void 0;
  if (tempBankEntry) {
    bankEntry = new _BankEntry2.default(tempBankEntry);
  } else {
    bankEntry = new _BankEntry2.default(state.getIn(['bankEntries', props.bankEntryId]));
  }
  var bank = new _Bank2.default(state.getIn(['banks', props.bankId]));
  var shouldClone = state.getIn(['ui', _constants.SHOULD_CLONE_ITEM]) || false;
  // bankEntry.entry can be either 'TEMPORARY_ITEM' or an entire Map
  var bankEntryId = bankEntry.entry ? bankEntry.entry.get ? bankEntry.entry.get('id') : bankEntry.entry : null;
  return {
    bank: bank,
    bankEntry: bankEntry,
    shouldClone: shouldClone,
    pendingAdditionTags: state.getIn(['tagAssociations', 'pendingAddition', bankEntry.entryType, bankEntryId]),
    pendingDeletionTags: state.getIn(['tagAssociations', 'pendingDeletion', bankEntry.entryType, bankEntryId]),
    tagAssociations: state.getIn(['tagAssociations', bankEntry.entryType, bankEntryId]),
    tagInput: state.getIn(['tagAssociations', 'tagInput', bankEntry.entryType, bankEntryId]),
    useBankTags: (0, _featureCheck.featureOn)('item_bank_tagging')
  };
}

function mapDispatchToProps(dispatch) {
  return {
    setPendingAdditionTagAssociations: (0, _redux.bindActionCreators)(_tagAssociations.setPendingAdditionTagAssociations, dispatch),
    setPendingDeletionTagAssociations: (0, _redux.bindActionCreators)(_tagAssociations.setPendingDeletionTagAssociations, dispatch),
    setTagInput: (0, _redux.bindActionCreators)(_tagAssociations.setTagInput, dispatch),
    createTagAssociations: (0, _redux.bindActionCreators)(tagAssociationApiActions.createTagAssociations, dispatch),
    deleteTagAssociations: (0, _redux.bindActionCreators)(tagAssociationApiActions.deleteTagAssociations, dispatch),
    getBankEntry: (0, _redux.bindActionCreators)(_bankEntries.getBankEntry, dispatch),
    getBank: (0, _redux.bindActionCreators)(_banks.getBank, dispatch),
    getItemTagAssociations: (0, _redux.bindActionCreators)(tagAssociationApiActions.getItemTagAssociations, dispatch),
    getStimulusTagAssociations: (0, _redux.bindActionCreators)(tagAssociationApiActions.getStimulusTagAssociations, dispatch),
    updateItem: (0, _redux.bindActionCreators)(_items.updateItem, dispatch),
    updateStimulus: (0, _redux.bindActionCreators)(_stimuli.updateStimulus, dispatch),
    setShouldClone: (0, _redux.bindActionCreators)((0, _partial2.default)(_ui.set, _constants.SHOULD_CLONE_ITEM), dispatch),
    changeItemTitle: (0, _redux.bindActionCreators)(_modifications.changeItemTitle, dispatch),
    changeQuizEntryPoints: (0, _redux.bindActionCreators)(_modifications.changeQuizEntryPoints, dispatch),
    cloneBankEntry: (0, _redux.bindActionCreators)(_bankEntries.cloneBankEntry, dispatch),
    createEntryAndBankEntry: (0, _redux.bindActionCreators)(_bankEntries.createEntryAndBankEntry, dispatch),
    clearModifications: (0, _redux.bindActionCreators)(_modifications.clearModifications, dispatch),
    clearTemporaryBankItem: (0, _redux.bindActionCreators)(_modifications.clearTemporaryBankItem, dispatch),
    setUi: (0, _redux.bindActionCreators)(_ui.set, dispatch),
    openModal: (0, _redux.bindActionCreators)(_modal.openModal, dispatch)
  };
}

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