"use strict";

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

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

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

var _get = _interopRequireDefault(require("lodash/get"));

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

var _withConfig = _interopRequireDefault(require("../../enhancers/withConfig"));

var _withCsid = _interopRequireDefault(require("../../enhancers/withCsid"));

var _withRecordType = _interopRequireDefault(require("../../enhancers/withRecordType"));

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

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

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

const {
  IDGeneratorInput
} = _cspaceInput.components;
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 {
    config,
    source,
    intl
  } = ownProps;
  const {
    idGeneratorTransform: transform
  } = config;
  const idGeneratorNames = Array.isArray(source) ? source : source.split(',');
  const patterns = [];
  idGeneratorNames.forEach(idGeneratorName => {
    const idGenerator = (0, _reducers.getIDGenerator)(state, idGeneratorName);

    if (idGenerator) {
      const sample = idGenerator.get('sample');
      patterns.push({
        name: idGeneratorName,
        type: intl.formatMessage(idGenerator.getIn(['messages', 'type']).toJS()),
        sample: transform ? transform(sample) : sample
      });
    }
  });
  return {
    patterns,
    sampleColumnLabel: intl.formatMessage(columnMessages.sampleColumn),
    typeColumnLabel: intl.formatMessage(columnMessages.typeColumn)
  };
};

const mapDispatchToProps = (dispatch, ownProps) => {
  const {
    config,
    csid,
    recordType
  } = ownProps;
  const {
    idGeneratorTransform: transform
  } = config;
  return {
    generateID: (idGeneratorName, path) => {
      const recordTypeConfig = (0, _get.default)(config, ['recordTypes', recordType]);
      dispatch((0, _idGenerator.createID)(recordTypeConfig, idGeneratorName, csid, path, transform));
    },
    onOpen: patterns => {
      patterns.forEach(pattern => {
        dispatch((0, _idGenerator.readIDGenerator)(pattern.name));
      });
    }
  };
};

const mergeProps = (stateProps, dispatchProps, ownProps) => {
  const {
    config,
    csid,
    source,
    intl,
    recordType,
    ...remainingOwnProps
  } = ownProps;
  return { ...remainingOwnProps,
    ...stateProps,
    ...dispatchProps
  };
};

const ConnectedIDGeneratorInput = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps, mergeProps)(IDGeneratorInput);
exports.ConnectedIDGeneratorInput = ConnectedIDGeneratorInput;
const IntlizedConnectedIDGeneratorInput = (0, _reactIntl.injectIntl)((0, _withCsid.default)((0, _withConfig.default)((0, _withRecordType.default)(ConnectedIDGeneratorInput))));
IntlizedConnectedIDGeneratorInput.propTypes = IDGeneratorInput.propTypes;
var _default = IntlizedConnectedIDGeneratorInput;
exports.default = _default;