'use strict';

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

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

var _reactIntl = require('react-intl');

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

var _get2 = _interopRequireDefault(_get);

var _cspaceInput = require('cspace-input');

var _withConfig = require('../../enhancers/withConfig');

var _withConfig2 = _interopRequireDefault(_withConfig);

var _withCsid = require('../../enhancers/withCsid');

var _withCsid2 = _interopRequireDefault(_withCsid);

var _withRecordType = require('../../enhancers/withRecordType');

var _withRecordType2 = _interopRequireDefault(_withRecordType);

var _idGenerator = require('../../actions/idGenerator');

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

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

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

var IDGeneratorInput = _cspaceInput.components.IDGeneratorInput;


var columnMessages = (0, _reactIntl.defineMessages)({
  sampleColumn: {
    'id': 'idGeneratorInput.column.sample',
    'defaultMessage': 'Last Generated Value'
  },
  typeColumn: {
    'id': 'idGeneratorInput.column.type',
    'defaultMessage': 'Type'
  }
});

var mapStateToProps = function mapStateToProps(state, ownProps) {
  var idGeneratorNameProp = ownProps.idGeneratorName,
      intl = ownProps.intl;


  var idGeneratorNames = Array.isArray(idGeneratorNameProp) ? idGeneratorNameProp : idGeneratorNameProp.split(',');

  var patterns = [];

  idGeneratorNames.forEach(function (idGeneratorName) {
    var idGenerator = (0, _reducers.getIDGenerator)(state, idGeneratorName);

    if (idGenerator) {
      patterns.push({
        name: idGeneratorName,
        type: intl.formatMessage(idGenerator.getIn(['messages', 'type']).toJS()),
        sample: idGenerator.get('sample')
      });
    }
  });

  return {
    patterns: patterns,
    sampleColumnLabel: intl.formatMessage(columnMessages.sampleColumn),
    typeColumnLabel: intl.formatMessage(columnMessages.typeColumn)
  };
};

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


  return {
    generateID: function generateID(idGeneratorName, path) {
      var recordTypeConfig = (0, _get2.default)(config, ['recordTypes', recordType]);

      dispatch((0, _idGenerator.createID)(recordTypeConfig, idGeneratorName, csid, path));
    },
    onOpen: function onOpen(patterns) {
      patterns.forEach(function (pattern) {
        dispatch((0, _idGenerator.readIDGenerator)(pattern.name));
      });
    }
  };
};

var mergeProps = function mergeProps(stateProps, dispatchProps, ownProps) {
  var config = ownProps.config,
      csid = ownProps.csid,
      idGeneratorName = ownProps.idGeneratorName,
      intl = ownProps.intl,
      recordType = ownProps.recordType,
      remainingOwnProps = _objectWithoutProperties(ownProps, ['config', 'csid', 'idGeneratorName', 'intl', 'recordType']);

  return Object.assign({}, remainingOwnProps, stateProps, dispatchProps);
};

var ConnectedIDGeneratorInput = exports.ConnectedIDGeneratorInput = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps, mergeProps)(IDGeneratorInput);

var IntlizedConnectedIDGeneratorInput = (0, _reactIntl.injectIntl)((0, _withCsid2.default)((0, _withConfig2.default)((0, _withRecordType2.default)(ConnectedIDGeneratorInput))));

IntlizedConnectedIDGeneratorInput.propTypes = IDGeneratorInput.propTypes;

exports.default = IntlizedConnectedIDGeneratorInput;