'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ConnectedStructuredDateInput = 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 _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _vocabulary = require('../../actions/vocabulary');

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 StructuredDateInput = _cspaceInput.components.StructuredDateInput;


var messages = (0, _reactIntl.defineMessages)({
  earliestSingle: {
    'id': 'field.structuredDate.earliestSingle',
    'defaultMessage': 'Earliest/Single'
  },
  latest: {
    'id': 'field.structuredDate.latest',
    'defaultMessage': 'Latest'
  },
  datePeriod: {
    'id': 'field.structuredDate.datePeriod',
    'defaultMessage': 'Period'
  },
  dateAssociation: {
    'id': 'field.structuredDate.dateAssociation',
    'defaultMessage': 'Association'
  },
  dateNote: {
    'id': 'field.structuredDate.dateNote',
    'defaultMessage': 'Note'
  },
  dateYear: {
    'id': 'field.structuredDate.dateYear',
    'defaultMessage': 'Year'
  },
  dateMonth: {
    'id': 'field.structuredDate.dateMonth',
    'defaultMessage': 'Month'
  },
  dateDay: {
    'id': 'field.structuredDate.dateDay',
    'defaultMessage': 'Day'
  },
  dateEra: {
    'id': 'field.structuredDate.dateEra',
    'defaultMessage': 'Era'
  },
  dateCertainty: {
    'id': 'field.structuredDate.dateCertainty',
    'defaultMessage': 'Certainty'
  },
  dateQualifier: {
    'id': 'field.structuredDate.dateQualifier',
    'defaultMessage': 'Qualifier'
  },
  dateQualifierValue: {
    'id': 'field.structuredDate.dateQualifierValue',
    'defaultMessage': 'Value'
  },
  dateQualifierUnit: {
    'id': 'field.structuredDate.dateQualifierUnit',
    'defaultMessage': 'Unit'
  }
});

var optionListNames = ['dateQualifiers'];
var vocabNames = ['dateera', 'datecertainty', 'datequalifier'];

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


  var optionLists = {};
  var terms = {};

  optionListNames.forEach(function (optionListName) {
    optionLists[optionListName] = (0, _reducers.getOptionList)(state, optionListName);
  });

  vocabNames.forEach(function (vocabName) {
    var vocab = (0, _reducers.getVocabulary)(state, vocabName);

    terms[vocabName] = vocab ? vocab.items : null;
  });

  return {
    defaultValue: _immutable2.default.Map(),
    optionLists: optionLists,
    terms: terms,
    formatFieldLabel: function formatFieldLabel(name) {
      return intl.formatMessage(messages[name]);
    },
    formatOptionLabel: function formatOptionLabel(option) {
      return option.message ? intl.formatMessage(option.message) : option.value;
    }
  };
};

var mapDispatchToProps = function mapDispatchToProps(dispatch) {
  return {
    onMount: function onMount() {
      vocabNames.forEach(function (vocabName) {
        dispatch((0, _vocabulary.readVocabularyItems)(vocabName));
      });
    }
  };
};

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

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

var ConnectedStructuredDateInput = exports.ConnectedStructuredDateInput = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps, mergeProps)(StructuredDateInput);

var IntlizedConnectedStructuredDateInput = (0, _reactIntl.injectIntl)(ConnectedStructuredDateInput);

IntlizedConnectedStructuredDateInput.propTypes = StructuredDateInput.propTypes;

exports.default = IntlizedConnectedStructuredDateInput;