'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 _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var labelable = _cspaceInput.enhancers.labelable,
    repeatable = _cspaceInput.enhancers.repeatable;


var BaseStructuredDateInput = repeatable(labelable(_cspaceInput.baseComponents.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 = exports.optionListNames = ['dateQualifiers'];
var vocabNames = exports.vocabNames = ['dateera', 'datecertainty', 'datequalifier'];

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

var StructuredDateInput = function (_Component) {
  _inherits(StructuredDateInput, _Component);

  function StructuredDateInput() {
    _classCallCheck(this, StructuredDateInput);

    var _this = _possibleConstructorReturn(this, (StructuredDateInput.__proto__ || Object.getPrototypeOf(StructuredDateInput)).call(this));

    _this.formatFieldLabel = _this.formatFieldLabel.bind(_this);
    _this.formatOptionLabel = _this.formatOptionLabel.bind(_this);
    return _this;
  }

  _createClass(StructuredDateInput, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var readTerms = this.props.readTerms;


      if (readTerms) {
        vocabNames.forEach(function (vocabName) {
          readTerms(vocabName);
        });
      }
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      var _props = this.props,
          perms = _props.perms,
          readTerms = _props.readTerms;
      var prevPerms = prevProps.perms;


      if (readTerms && perms !== prevPerms) {
        vocabNames.forEach(function (vocabName) {
          readTerms(vocabName);
        });
      }
    }
  }, {
    key: 'formatFieldLabel',
    value: function formatFieldLabel(name) {
      var intl = this.props.intl;


      return intl.formatMessage(messages[name]);
    }
  }, {
    key: 'formatOptionLabel',
    value: function formatOptionLabel(option) {
      var intl = this.props.intl;


      return option.message ? intl.formatMessage(option.message) : option.value;
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          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
      }, remainingProps));
    }
  }]);

  return StructuredDateInput;
}(_react.Component);

StructuredDateInput.propTypes = propTypes;

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

IntlAwareStructuredDateInput.propTypes = StructuredDateInput.propTypes;

exports.default = IntlAwareStructuredDateInput;