'use strict';

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

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

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

var _get2 = _interopRequireDefault(_get);

var _authority = require('../../actions/authority');

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 }; }

const mapStateToProps = (state, ownProps) => {
  const 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)
  };
};

const mapDispatchToProps = (dispatch, ownProps) => {
  const config = ownProps.config,
        csid = ownProps.csid,
        relatedSubjectCsid = ownProps.relatedSubjectCsid,
        recordType = ownProps.recordType,
        vocabulary = ownProps.vocabulary;


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

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

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

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