'use strict';

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

var _reactRedux = require('react-redux');

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _notification = require('../../actions/notification');

var _prefs = require('../../actions/prefs');

var _record = require('../../actions/record');

var _relation = require('../../actions/relation');

var _reducers = require('../../reducers');

var _RecordEditor = require('../../components/record/RecordEditor');

var _RecordEditor2 = _interopRequireDefault(_RecordEditor);

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

var mapStateToProps = function mapStateToProps(state, ownProps) {
  var csid = ownProps.csid,
      perms = ownProps.perms,
      recordType = ownProps.recordType,
      vocabulary = ownProps.vocabulary;

  // If perms are supplied in ownProps, use them instead of getting perms from state.
  // This allows parent components to supply filtered perms.

  return {
    data: (0, _reducers.getRecordData)(state, csid),
    formName: (0, _reducers.getForm)(state, recordType),
    isModified: (0, _reducers.isRecordModified)(state, csid),
    isReadPending: (0, _reducers.isRecordReadPending)(state, csid),
    isSavePending: (0, _reducers.isRecordSavePending)(state, csid),
    openModalName: (0, _reducers.getOpenModalName)(state),
    perms: perms || (0, _reducers.getUserPerms)(state),
    validationErrors: (0, _reducers.getRecordValidationErrors)(state, csid),
    vocabularyWorkflowState: (0, _reducers.getAuthorityVocabWorkflowState)(state, recordType, vocabulary)
  };
};

var mapDispatchToProps = function mapDispatchToProps(dispatch, ownProps) {
  var config = ownProps.config,
      csid = ownProps.csid,
      relatedSubjectCsid = ownProps.relatedSubjectCsid,
      recordType = ownProps.recordType,
      vocabulary = ownProps.vocabulary;


  var recordTypeConfig = (0, _get2.default)(config, ['recordTypes', recordType]);

  var vocabularyConfig = vocabulary ? (0, _get2.default)(recordTypeConfig, ['vocabularies', vocabulary]) : undefined;

  return {
    closeModal: function closeModal(result) {
      dispatch((0, _notification.closeModal)(result));
    },
    openModal: function openModal(modalName) {
      dispatch((0, _notification.openModal)(modalName));
    },
    createNewRecord: function createNewRecord(cloneCsid) {
      dispatch((0, _record.createNewRecord)(config, recordTypeConfig, vocabularyConfig, cloneCsid));
    },
    deleteRecord: function deleteRecord() {
      return dispatch((0, _record.deleteRecord)(config, recordTypeConfig, vocabularyConfig, csid, relatedSubjectCsid));
    },
    readRecord: function readRecord() {
      dispatch((0, _record.readRecord)(config, recordTypeConfig, vocabularyConfig, csid));
    },
    revert: function revert() {
      dispatch((0, _record.revertRecord)(recordTypeConfig, csid));
    },
    save: function save(onRecordCreated) {
      return dispatch((0, _record.saveRecord)(config, recordTypeConfig, vocabularyConfig, csid, undefined, undefined, relatedSubjectCsid, onRecordCreated)).catch(function () {});
    },
    saveWithTransition: function saveWithTransition(transitionName, onRecordCreated) {
      return dispatch((0, _record.saveRecordWithTransition)(config, recordTypeConfig, vocabularyConfig, csid, undefined, undefined, relatedSubjectCsid, transitionName, onRecordCreated)).catch(function () {});
    },
    setForm: function setForm(formName) {
      dispatch((0, _prefs.setForm)(recordType, formName));
    },
    transitionRecord: function transitionRecord(transitionName) {
      return dispatch((0, _record.transitionRecord)(config, recordTypeConfig, vocabularyConfig, csid, transitionName, relatedSubjectCsid));
    },
    removeNotification: function removeNotification(notificationID) {
      dispatch((0, _notification.removeNotification)(notificationID));
    },
    removeValidationNotification: function removeValidationNotification() {
      dispatch((0, _notification.removeValidationNotification)());
    },
    validateRecordData: function validateRecordData() {
      dispatch((0, _record.validateRecordData)(recordTypeConfig, csid));
    },
    checkForRelations: function checkForRelations(predicate) {
      return dispatch((0, _relation.checkForRelations)(csid, predicate));
    }
  };
};

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