'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.vocabNames = exports.optionListNames = 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 _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

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

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

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

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 labelable = _cspaceInput.enhancers.labelable,
      repeatable = _cspaceInput.enhancers.repeatable;


const BaseStructuredDateInput = repeatable(labelable(_cspaceInput.baseComponents.StructuredDateInput));

const fieldMessages = (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'
  }
});

const messages = (0, _reactIntl.defineMessages)({
  parseFailed: {
    'id': 'structuredDateInput.parseFailed',
    'defaultMessage': 'Unrecognized display date format. Try a different format, or enter values in the fields below.'
  }
});

const optionListNames = exports.optionListNames = ['dateQualifiers'];
const vocabNames = exports.vocabNames = ['dateera', 'datecertainty', 'datequalifier'];

const propTypes = Object.assign({}, BaseStructuredDateInput.propTypes, {
  intl: _reactIntl.intlShape,
  perms: _propTypes2.default.instanceOf(_immutable2.default.Map),
  readTerms: _propTypes2.default.func
});

class StructuredDateInput extends _react.Component {
  constructor() {
    super();

    this.formatFieldLabel = this.formatFieldLabel.bind(this);
    this.formatOptionLabel = this.formatOptionLabel.bind(this);
    this.formatParseFailedMessage = this.formatParseFailedMessage.bind(this);
  }

  componentDidMount() {
    const readTerms = this.props.readTerms;


    if (readTerms) {
      vocabNames.forEach(vocabName => {
        readTerms(vocabName);
      });
    }
  }

  componentDidUpdate(prevProps) {
    var _props = this.props;
    const perms = _props.perms,
          readTerms = _props.readTerms;
    const prevPerms = prevProps.perms;


    if (readTerms && perms !== prevPerms) {
      vocabNames.forEach(vocabName => {
        readTerms(vocabName);
      });
    }
  }

  formatFieldLabel(name) {
    const intl = this.props.intl;


    return intl.formatMessage(fieldMessages[name]);
  }

  formatOptionLabel(option) {
    const intl = this.props.intl;


    return option.message ? intl.formatMessage(option.message) : option.value;
  }

  formatParseFailedMessage() {
    const intl = this.props.intl;


    return intl.formatMessage(messages.parseFailed);
  }

  render() {
    var _props2 = this.props;

    const intl = _props2.intl,
          perms = _props2.perms,
          readTerms = _props2.readTerms,
          remainingProps = _objectWithoutProperties(_props2, ['intl', 'perms', 'readTerms']);

    return _react2.default.createElement(BaseStructuredDateInput, _extends({
      defaultValue: _immutable2.default.Map(),
      formatFieldLabel: this.formatFieldLabel,
      formatOptionLabel: this.formatOptionLabel,
      formatParseFailedMessage: this.formatParseFailedMessage
    }, remainingProps));
  }
}

StructuredDateInput.propTypes = propTypes;

const IntlAwareStructuredDateInput = (0, _reactIntl.injectIntl)(StructuredDateInput);

IntlAwareStructuredDateInput.propTypes = StructuredDateInput.propTypes;

exports.default = IntlAwareStructuredDateInput;