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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

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 {
    source,
    intl
  } = ownProps;
  const idGeneratorNames = Array.isArray(source) ? source : source.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,
    csid,
    recordType
  } = ownProps;
  return {
    generateID: (idGeneratorName, path) => {
      const recordTypeConfig = (0, _get.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 {
    /* eslint-disable no-unused-vars */
    config,
    csid,
    source,
    intl,
    recordType
  } = ownProps,
        remainingOwnProps = _objectWithoutProperties(ownProps, ["config", "csid", "source", "intl", "recordType"]);

  return _objectSpread({}, 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;