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

const IDGeneratorInput = _cspaceInput.components.IDGeneratorInput;


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

const mapStateToProps = (state, ownProps) => {
  const idGeneratorNameProp = ownProps.idGeneratorName,
        intl = ownProps.intl;


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

  const patterns = [];

  idGeneratorNames.forEach(idGeneratorName => {
    const 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,
    sampleColumnLabel: intl.formatMessage(columnMessages.sampleColumn),
    typeColumnLabel: intl.formatMessage(columnMessages.typeColumn)
  };
};

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


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

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

const mergeProps = (stateProps, dispatchProps, ownProps) => {
  const 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);
};

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

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

IntlizedConnectedIDGeneratorInput.propTypes = IDGeneratorInput.propTypes;

exports.default = IntlizedConnectedIDGeneratorInput;