'use strict';

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

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

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

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

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

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

var _withCsid2 = _interopRequireDefault(_withCsid);

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 csid = ownProps.csid;


  return {
    generateID: function generateID(idGeneratorName, path) {
      dispatch((0, _idGenerator.createID)(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 csid = ownProps.csid,
      idGeneratorName = ownProps.idGeneratorName,
      intl = ownProps.intl,
      remainingOwnProps = _objectWithoutProperties(ownProps, ['csid', 'idGeneratorName', 'intl']);

  return _extends({}, remainingOwnProps, stateProps, dispatchProps);
};

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

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

IntlizedConnectedIDGeneratorInput.propTypes = IDGeneratorInput.propTypes;

exports.default = IntlizedConnectedIDGeneratorInput;