'use strict';

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

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

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

var _get2 = _interopRequireDefault(_get);

var _invocationHelpers = require('../../helpers/invocationHelpers');

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

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

var _InvocationEditor = require('../../components/invocable/InvocationEditor');

var _InvocationEditor2 = _interopRequireDefault(_InvocationEditor);

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

const mapStateToProps = state => ({
  data: (0, _reducers.getRecordData)(state, '')
});

const mapDispatchToProps = (dispatch, ownProps) => {
  const config = ownProps.config,
        invocationItem = ownProps.invocationItem,
        type = ownProps.type;


  return {
    createNewRecord: () => {
      const invocableName = type === 'report' ? (0, _invocationHelpers.getReportName)(invocationItem) : (0, _invocationHelpers.getBatchName)(invocationItem);

      const recordTypeConfig = (0, _get2.default)(config, ['invocables', type, invocableName]);

      if (recordTypeConfig) {
        return dispatch((0, _record.createNewRecord)(config, recordTypeConfig));
      }

      return undefined;
    }
  };
};

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